@@ -42,7 +42,10 @@ export class MysqlDriver implements Driver {
42
42
let connection = this . #connections. get ( rawConnection )
43
43
44
44
if ( ! connection ) {
45
- connection = new MysqlConnection ( rawConnection )
45
+ connection = new MysqlConnection (
46
+ rawConnection ,
47
+ this . #config. createConnection ,
48
+ )
46
49
this . #connections. set ( rawConnection , connection )
47
50
48
51
// The driver must take care of calling `onCreateConnection` when a new
@@ -165,9 +168,14 @@ function isOkPacket(obj: unknown): obj is MysqlOkPacket {
165
168
}
166
169
167
170
class MysqlConnection implements DatabaseConnection {
171
+ readonly #createConnection: MysqlDialectConfig [ 'createConnection' ]
168
172
readonly #rawConnection: MysqlPoolConnection
169
173
170
- constructor ( rawConnection : MysqlPoolConnection ) {
174
+ constructor (
175
+ rawConnection : MysqlPoolConnection ,
176
+ createConnection : MysqlDialectConfig [ 'createConnection' ] ,
177
+ ) {
178
+ this . #createConnection = createConnection
171
179
this . #rawConnection = rawConnection
172
180
}
173
181
@@ -183,14 +191,38 @@ class MysqlConnection implements DatabaseConnection {
183
191
// noop
184
192
}
185
193
194
+ const { config, threadId } = this . #rawConnection
195
+
196
+ // this kills the query and the connection database-side.
197
+ // we're not using `kill query <connection_id>` here because it doesn't
198
+ // guarantee that the query is killed immediately. we saw that in tests,
199
+ // the query can still run for a while after - including registering writes.
200
+ const cancelQuery = `kill connection ${ threadId } `
201
+
202
+ if ( this . #createConnection && config ) {
203
+ const controlConnection = await this . #createConnection( { ...config } )
204
+
205
+ return await new Promise ( ( resolve , reject ) => {
206
+ controlConnection . connect ( ( connectError ) => {
207
+ if ( connectError ) {
208
+ return reject ( connectError )
209
+ }
210
+
211
+ controlConnection . query ( cancelQuery , [ ] , ( error ) => {
212
+ controlConnection . destroy ( )
213
+
214
+ if ( error ) {
215
+ return reject ( error )
216
+ }
217
+
218
+ resolve ( )
219
+ } )
220
+ } )
221
+ } )
222
+ }
223
+
186
224
await controlConnectionProvider ( async ( controlConnection ) => {
187
- // this kills the query and the connection database-side.
188
- // we're not using `kill query <connection_id>` here because it doesn't
189
- // guarantee that the query is killed immediately. we saw that in tests,
190
- // the query can still run for a while after - including registering writes.
191
- await controlConnection . executeQuery (
192
- CompiledQuery . raw ( 'kill connection ?' , [ this . #rawConnection. threadId ] ) ,
193
- )
225
+ await controlConnection . executeQuery ( CompiledQuery . raw ( cancelQuery , [ ] ) )
194
226
} )
195
227
}
196
228
@@ -231,7 +263,6 @@ class MysqlConnection implements DatabaseConnection {
231
263
async * streamQuery < O > (
232
264
compiledQuery : CompiledQuery ,
233
265
_chunkSize : number ,
234
- options ?: QueryOptions ,
235
266
) : AsyncIterableIterator < QueryResult < O > > {
236
267
const stream = this . #rawConnection
237
268
. query ( compiledQuery . sql , compiledQuery . parameters )
0 commit comments