@@ -78,6 +78,9 @@ class SQLiteCasette {
78
78
useNullAsDefault : true
79
79
} ) ;
80
80
}
81
+ rows ( result ) {
82
+ return result ;
83
+ }
81
84
explainSql ( s ) {
82
85
return `EXPLAIN QUERY PLAN ${ s } ` ;
83
86
}
@@ -223,6 +226,9 @@ class PostgreSQLCasette {
223
226
}
224
227
throw Error ( "Failed to start PostgreSQL server" ) ;
225
228
}
229
+ rows ( result ) {
230
+ return result . rows ;
231
+ }
226
232
explainSql ( s ) {
227
233
return `EXPLAIN ${ s } ` ;
228
234
}
@@ -264,49 +270,9 @@ class PostgreSQLCasette {
264
270
265
271
class MySQLCassette {
266
272
async newConnection ( options ) {
267
- async function startServer ( ) {
268
- return new Promise ( ( _res , _rej ) => {
269
- let done = false ;
270
- const res = ( ...args ) => {
271
- if ( ! done ) {
272
- done = true ;
273
- _res ( ...args ) ;
274
- }
275
- }
276
- const rej = ( ...args ) => {
277
- if ( ! done ) {
278
- done = true ;
279
- _rej ( ...args ) ;
280
- }
281
- }
282
- sleep ( 10000 ) . then ( ( ) => rej ( new Error ( 'Timeout database connection' ) ) ) ;
283
- const p = cp . spawn ( 'service' , [ 'mysql' , 'start' ] , {
284
- detached : true ,
285
- } ) ;
286
- p . stdout . on ( 'data' , data => {
287
- const message = data . toString ( ) ;
288
- if ( options . verbose ) {
289
- console . log ( message ) ;
290
- }
291
- if ( message . includes ( 'database system is ready to accept connections' ) ) {
292
- res ( ) ;
293
- }
294
- } ) ;
295
- p . stderr . on ( 'data' , data => {
296
- const message = data . toString ( ) ;
297
- if ( options . verbose ) {
298
- console . error ( message ) ;
299
- }
300
- if ( message . includes ( 'database system is ready to accept connections' ) ) {
301
- res ( ) ;
302
- }
303
- } ) ;
304
- } ) ;
305
- }
306
-
307
273
for ( let retries = 10 ; retries > 0 ; retries -- ) {
308
274
try {
309
- await startServer ( ) ;
275
+ exec ( 'service mysql start' ) ;
310
276
const conn = knex ( {
311
277
client : 'mysql' ,
312
278
connection : {
@@ -321,7 +287,7 @@ class MySQLCassette {
321
287
if ( options && options . clean ) {
322
288
try {
323
289
let tables = await conn . raw ( "SHOW TABLES" ) ;
324
- for ( let table of tables . rows ) {
290
+ for ( let table of tables [ 0 ] ) {
325
291
await conn . raw ( `DROP TABLE IF EXISTS ${ table . Tables_in_track } CASCADE` ) ;
326
292
}
327
293
} catch ( e2 ) { console . error ( e2 ) ; }
@@ -333,6 +299,9 @@ class MySQLCassette {
333
299
}
334
300
throw Error ( "Failed to start MySQL server" ) ;
335
301
}
302
+ rows ( result ) {
303
+ return result [ 0 ] ;
304
+ }
336
305
explainSql ( s ) {
337
306
return `EXPLAIN ${ s } ` ;
338
307
}
@@ -534,24 +503,19 @@ class Connection {
534
503
* @returns {Promise<Array<object>?> }
535
504
*/
536
505
async query ( sql , opt_args ) {
537
-
538
- function rows ( records ) {
539
- return ! ! records . rows ? records . rows /* postgres */ : records /* sqlite */
540
- }
541
-
542
506
if ( sql . trim ( ) . length === 0 ) {
543
507
throw 'Empty query' ;
544
508
}
545
509
546
510
const isSelectStatement = / ^ \s * S E L E C T / i. test ( sql ) ;
547
511
if ( isSelectStatement ) {
548
- const count = rows ( await this . _conn . raw ( `SELECT count(1) AS count FROM (${ sql . replace ( / ; \s * $ / , "" ) } ) AS x` , opt_args ) ) . length ;
512
+ const count = this . _cassette . rows ( await this . _conn . raw ( `SELECT count(1) AS count FROM (${ sql . replace ( / ; \s * $ / , "" ) } ) AS x` , opt_args ) ) . length ;
549
513
if ( count > ( this . _options . maxRows || 10000 ) ) {
550
514
throw 'Too many records' ;
551
515
}
552
516
}
553
517
554
- return rows ( await this . _conn . raw ( sql , opt_args ) ) ;
518
+ return this . _cassette . rows ( await this . _conn . raw ( sql , opt_args ) ) ;
555
519
}
556
520
557
521
/**
0 commit comments