@@ -46,9 +46,13 @@ export class DuckDBConnection {
46
46
types ?: DuckDBType [ ] | Record < string , DuckDBType | undefined >
47
47
) : Promise < DuckDBMaterializedResult > {
48
48
if ( values ) {
49
- const prepared = await this . prepare ( sql ) ;
50
- prepared . bind ( values , types ) ;
51
- return prepared . run ( ) ;
49
+ const prepared = await this . createPrepared ( sql ) ;
50
+ try {
51
+ prepared . bind ( values , types ) ;
52
+ return prepared . run ( ) ;
53
+ } finally {
54
+ prepared . destroySync ( ) ;
55
+ }
52
56
} else {
53
57
return new DuckDBMaterializedResult (
54
58
await duckdb . query ( this . connection , sql )
@@ -86,11 +90,15 @@ export class DuckDBConnection {
86
90
values ?: DuckDBValue [ ] | Record < string , DuckDBValue > ,
87
91
types ?: DuckDBType [ ] | Record < string , DuckDBType | undefined >
88
92
) : Promise < DuckDBResult > {
89
- const prepared = await this . prepare ( sql ) ;
90
- if ( values ) {
91
- prepared . bind ( values , types ) ;
93
+ const prepared = await this . createPrepared ( sql ) ;
94
+ try {
95
+ if ( values ) {
96
+ prepared . bind ( values , types ) ;
97
+ }
98
+ return prepared . stream ( ) ;
99
+ } finally {
100
+ prepared . destroySync ( ) ;
92
101
}
93
- return prepared . stream ( ) ;
94
102
}
95
103
public async streamAndRead (
96
104
sql : string ,
@@ -127,30 +135,41 @@ export class DuckDBConnection {
127
135
values ?: DuckDBValue [ ] | Record < string , DuckDBValue > ,
128
136
types ?: DuckDBType [ ] | Record < string , DuckDBType | undefined >
129
137
) : Promise < DuckDBPendingResult > {
130
- const prepared = await this . prepare ( sql ) ;
131
- if ( values ) {
132
- prepared . bind ( values , types ) ;
138
+ const prepared = await this . createPrepared ( sql ) ;
139
+ try {
140
+ if ( values ) {
141
+ prepared . bind ( values , types ) ;
142
+ }
143
+ return prepared . start ( ) ;
144
+ } finally {
145
+ prepared . destroySync ( ) ;
133
146
}
134
- return prepared . start ( ) ;
135
147
}
136
148
public async startStream (
137
149
sql : string ,
138
150
values ?: DuckDBValue [ ] | Record < string , DuckDBValue > ,
139
151
types ?: DuckDBType [ ] | Record < string , DuckDBType | undefined >
140
152
) : Promise < DuckDBPendingResult > {
141
- const prepared = await this . prepare ( sql ) ;
142
- if ( values ) {
143
- prepared . bind ( values , types ) ;
153
+ const prepared = await this . createPrepared ( sql ) ;
154
+ try {
155
+ if ( values ) {
156
+ prepared . bind ( values , types ) ;
157
+ }
158
+ return prepared . startStream ( ) ;
159
+ } finally {
160
+ prepared . destroySync ( ) ;
144
161
}
145
- return prepared . startStream ( ) ;
146
162
}
147
163
public async prepare ( sql : string ) : Promise < DuckDBPreparedStatement > {
148
- const prepared = new DuckDBPreparedStatement (
149
- await duckdb . prepare ( this . connection , sql )
150
- ) ;
164
+ const prepared = await this . createPrepared ( sql ) ;
151
165
this . preparedStatements . add ( prepared ) ;
152
166
return prepared ;
153
167
}
168
+ private async createPrepared ( sql : string ) : Promise < DuckDBPreparedStatement > {
169
+ return new DuckDBPreparedStatement (
170
+ await duckdb . prepare ( this . connection , sql )
171
+ ) ;
172
+ }
154
173
public async extractStatements (
155
174
sql : string
156
175
) : Promise < DuckDBExtractedStatements > {
0 commit comments