@@ -42,6 +42,7 @@ class Client extends EventEmitter {
42
42
this . _connected = false
43
43
this . _connectionError = false
44
44
this . _queryable = true
45
+ this . _activeQuery = null
45
46
46
47
this . enableChannelBinding = Boolean ( c . enableChannelBinding ) // set true to use SCRAM-SHA-256-PLUS when offered
47
48
this . connection =
@@ -70,16 +71,31 @@ class Client extends EventEmitter {
70
71
this . _connectionTimeoutMillis = c . connectionTimeoutMillis || 0
71
72
}
72
73
74
+ get activeQuery ( ) {
75
+ console . warn ( 'Warning: Client.activeQuery is deprecated and will be removed in a future version.' )
76
+ return this . _activeQuery
77
+ }
78
+
79
+ set activeQuery ( val ) {
80
+ console . warn ( 'Warning: Client.activeQuery is deprecated and will be removed in a future version.' )
81
+ this . _activeQuery = val
82
+ }
83
+
84
+ _getActiveQuery ( ) {
85
+ return this . _activeQuery
86
+ }
87
+
73
88
_errorAllQueries ( err ) {
74
89
const enqueueError = ( query ) => {
75
90
process . nextTick ( ( ) => {
76
91
query . handleError ( err , this . connection )
77
92
} )
78
93
}
79
94
80
- if ( this . activeQuery ) {
81
- enqueueError ( this . activeQuery )
82
- this . activeQuery = null
95
+ const activeQuery = this . _getActiveQuery ( )
96
+ if ( activeQuery ) {
97
+ enqueueError ( activeQuery )
98
+ this . _activeQuery = null
83
99
}
84
100
85
101
this . queryQueue . forEach ( enqueueError )
@@ -314,8 +330,8 @@ class Client extends EventEmitter {
314
330
}
315
331
this . emit ( 'connect' )
316
332
}
317
- const { activeQuery } = this
318
- this . activeQuery = null
333
+ const activeQuery = this . _getActiveQuery ( )
334
+ this . _activeQuery = null
319
335
this . readyForQuery = true
320
336
if ( activeQuery ) {
321
337
activeQuery . handleReadyForQuery ( this . connection )
@@ -355,67 +371,69 @@ class Client extends EventEmitter {
355
371
if ( this . _connecting ) {
356
372
return this . _handleErrorWhileConnecting ( msg )
357
373
}
358
- const activeQuery = this . activeQuery
374
+ const activeQuery = this . _getActiveQuery ( )
359
375
360
376
if ( ! activeQuery ) {
361
377
this . _handleErrorEvent ( msg )
362
378
return
363
379
}
364
380
365
- this . activeQuery = null
381
+ this . _activeQuery = null
366
382
activeQuery . handleError ( msg , this . connection )
367
383
}
368
384
369
385
_handleRowDescription ( msg ) {
370
386
// delegate rowDescription to active query
371
- this . activeQuery . handleRowDescription ( msg )
387
+ this . _getActiveQuery ( ) . handleRowDescription ( msg )
372
388
}
373
389
374
390
_handleDataRow ( msg ) {
375
391
// delegate dataRow to active query
376
- this . activeQuery . handleDataRow ( msg )
392
+ this . _getActiveQuery ( ) . handleDataRow ( msg )
377
393
}
378
394
379
395
_handlePortalSuspended ( msg ) {
380
396
// delegate portalSuspended to active query
381
- this . activeQuery . handlePortalSuspended ( this . connection )
397
+ this . _getActiveQuery ( ) . handlePortalSuspended ( this . connection )
382
398
}
383
399
384
400
_handleEmptyQuery ( msg ) {
385
401
// delegate emptyQuery to active query
386
- this . activeQuery . handleEmptyQuery ( this . connection )
402
+ this . _getActiveQuery ( ) . handleEmptyQuery ( this . connection )
387
403
}
388
404
389
405
_handleCommandComplete ( msg ) {
390
- if ( this . activeQuery == null ) {
406
+ const activeQuery = this . _getActiveQuery ( )
407
+ if ( activeQuery == null ) {
391
408
const error = new Error ( 'Received unexpected commandComplete message from backend.' )
392
409
this . _handleErrorEvent ( error )
393
410
return
394
411
}
395
412
// delegate commandComplete to active query
396
- this . activeQuery . handleCommandComplete ( msg , this . connection )
413
+ activeQuery . handleCommandComplete ( msg , this . connection )
397
414
}
398
415
399
416
_handleParseComplete ( ) {
400
- if ( this . activeQuery == null ) {
417
+ const activeQuery = this . _getActiveQuery ( )
418
+ if ( activeQuery == null ) {
401
419
const error = new Error ( 'Received unexpected parseComplete message from backend.' )
402
420
this . _handleErrorEvent ( error )
403
421
return
404
422
}
405
423
// if a prepared statement has a name and properly parses
406
424
// we track that its already been executed so we don't parse
407
425
// it again on the same client
408
- if ( this . activeQuery . name ) {
409
- this . connection . parsedStatements [ this . activeQuery . name ] = this . activeQuery . text
426
+ if ( activeQuery . name ) {
427
+ this . connection . parsedStatements [ activeQuery . name ] = activeQuery . text
410
428
}
411
429
}
412
430
413
431
_handleCopyInResponse ( msg ) {
414
- this . activeQuery . handleCopyInResponse ( this . connection )
432
+ this . _getActiveQuery ( ) . handleCopyInResponse ( this . connection )
415
433
}
416
434
417
435
_handleCopyData ( msg ) {
418
- this . activeQuery . handleCopyData ( msg , this . connection )
436
+ this . _getActiveQuery ( ) . handleCopyData ( msg , this . connection )
419
437
}
420
438
421
439
_handleNotification ( msg ) {
@@ -497,21 +515,22 @@ class Client extends EventEmitter {
497
515
498
516
_pulseQueryQueue ( ) {
499
517
if ( this . readyForQuery === true ) {
500
- this . activeQuery = this . queryQueue . shift ( )
501
- if ( this . activeQuery ) {
518
+ this . _activeQuery = this . queryQueue . shift ( )
519
+ const activeQuery = this . _getActiveQuery ( )
520
+ if ( activeQuery ) {
502
521
this . readyForQuery = false
503
522
this . hasExecuted = true
504
523
505
- const queryError = this . activeQuery . submit ( this . connection )
524
+ const queryError = activeQuery . submit ( this . connection )
506
525
if ( queryError ) {
507
526
process . nextTick ( ( ) => {
508
- this . activeQuery . handleError ( queryError , this . connection )
527
+ activeQuery . handleError ( queryError , this . connection )
509
528
this . readyForQuery = true
510
529
this . _pulseQueryQueue ( )
511
530
} )
512
531
}
513
532
} else if ( this . hasExecuted ) {
514
- this . activeQuery = null
533
+ this . _activeQuery = null
515
534
this . emit ( 'drain' )
516
535
}
517
536
}
@@ -540,7 +559,7 @@ class Client extends EventEmitter {
540
559
result = new this . _Promise ( ( resolve , reject ) => {
541
560
query . callback = ( err , res ) => ( err ? reject ( err ) : resolve ( res ) )
542
561
} ) . catch ( ( err ) => {
543
- // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
562
+ // replace the stack trace that leads to \ `TCP.onStreamRead\ ` with one that leads back to the
544
563
// application that created the query
545
564
Error . captureStackTrace ( err )
546
565
throw err
@@ -626,7 +645,7 @@ class Client extends EventEmitter {
626
645
}
627
646
}
628
647
629
- if ( this . activeQuery || ! this . _queryable ) {
648
+ if ( this . _getActiveQuery ( ) || ! this . _queryable ) {
630
649
// if we have an active query we need to force a disconnect
631
650
// on the socket - otherwise a hung query could block end forever
632
651
this . connection . stream . destroy ( )
0 commit comments