@@ -9,7 +9,7 @@ import * as logger from './logging';
9
9
10
10
import { postgresqlErrorCodes } from './errors' ;
11
11
import { Queue } from './queue' ;
12
- import { Query , QueryParameter } from './query' ;
12
+ import { Query } from './query' ;
13
13
14
14
import { ConnectionOptions , TLSSocket , connect as tls , createSecureContext } from 'tls' ;
15
15
@@ -515,7 +515,7 @@ export class Client {
515
515
}
516
516
517
517
prepare < T = ResultRecord > (
518
- text : QueryParameter | string ,
518
+ text : Query | string ,
519
519
name ?: string ,
520
520
types ?: DataType [ ] ) : Promise < PreparedStatement < T > > {
521
521
@@ -592,10 +592,10 @@ export class Client {
592
592
/**
593
593
* Send a query to the database.
594
594
*
595
- * The query string is given as the first argument, or pass a {@link QueryParameter }
595
+ * The query string is given as the first argument, or pass a {@link Query }
596
596
* object which provides more control.
597
597
*
598
- * @param text - The query string, or pass a {@link QueryParameter }
598
+ * @param text - The query string, or pass a {@link Query }
599
599
* object which provides more control (including streaming values into a socket).
600
600
* @param values - The query parameters, corresponding to $1, $2, etc.
601
601
* @param types - Allows making the database native type explicit for some or all
@@ -605,70 +605,23 @@ export class Client {
605
605
* @returns A promise for the query results.
606
606
*/
607
607
query < T = ResultRecord > (
608
- text : QueryParameter | string ,
608
+ text : Query | string ,
609
609
values ?: any [ ] ,
610
610
types ?: DataType [ ] ,
611
611
format ?: DataFormat | DataFormat [ ] ,
612
612
streams ?: Record < string , Writable > ) :
613
613
ResultIterator < T > {
614
- const query = new Query (
615
- text ,
616
- values , {
617
- types : types ,
618
- format : format ,
619
- streams : streams ,
620
- } ) ;
621
- return this . execute < T > ( query ) ;
622
- }
623
-
624
- private bindAndExecute (
625
- info : RowDataHandlerInfo ,
626
- bind : Bind ,
627
- types : DataType [ ] ) {
628
- try {
629
- this . writer . bind (
630
- bind . name ,
631
- bind . portal ,
632
- bind . format ,
633
- bind . values ,
634
- types
635
- ) ;
636
- } catch ( error ) {
637
- info . handler . callback ( error as Error ) ;
638
- return ;
639
- }
640
-
641
- this . bindQueue . push ( info ) ;
642
- this . writer . execute ( bind . portal ) ;
643
- this . cleanupQueue . push ( Cleanup . Bind ) ;
644
-
645
- if ( bind . close ) {
646
- this . writer . close ( bind . name , 'S' ) ;
647
- this . closeHandlerQueue . push ( null ) ;
648
- this . cleanupQueue . push ( Cleanup . Close ) ;
649
- }
650
-
651
- this . writer . sync ( ) ;
652
- this . errorHandlerQueue . push (
653
- ( error ) => { info . handler . callback ( error ) ; }
654
- ) ;
655
- this . cleanupQueue . push ( Cleanup . ErrorHandler ) ;
656
- this . send ( ) ;
657
- }
614
+ const query = typeof text === 'string' ? { text} : text ;
658
615
659
- execute < T = ResultRecord > ( query : Query ) : ResultIterator < T > {
660
616
if ( this . closed && ! this . connecting ) {
661
617
throw new Error ( 'Connection is closed.' ) ;
662
618
}
663
619
664
- const text = query . text ;
665
- const values = query . values || [ ] ;
666
- const options = query . options ;
667
- const format = options ?. format ;
668
- const types = options ?. types ;
669
- const streams = options ?. streams ;
670
- const portal = options ?. portal || '' ;
671
- const result = makeResult < T > ( options ?. transform ) ;
620
+ format = format || query ?. format ;
621
+ types = types || query ?. types ;
622
+ streams = streams || query ?. streams ;
623
+ const portal = query ?. portal || '' ;
624
+ const result = makeResult < T > ( query ?. transform ) ;
672
625
673
626
const descriptionHandler = ( description : RowDescription ) => {
674
627
result . nameHandler ( description . names ) ;
@@ -680,13 +633,13 @@ export class Client {
680
633
} ;
681
634
682
635
if ( values && values . length ) {
683
- const name = ( options ?. name ) || (
636
+ const name = ( query ?. name ) || (
684
637
( this . config . preparedStatementPrefix ||
685
638
defaults . preparedStatementPrefix ) + (
686
639
this . nextPreparedStatementId ++
687
640
) ) ;
688
641
689
- this . writer . parse ( name , text , types || [ ] ) ;
642
+ this . writer . parse ( name , query . text , types || [ ] ) ;
690
643
this . writer . describe ( name , 'S' ) ;
691
644
this . preFlightQueue . push ( {
692
645
descriptionHandler : descriptionHandler ,
@@ -701,8 +654,8 @@ export class Client {
701
654
} ) ;
702
655
this . cleanupQueue . push ( Cleanup . PreFlight ) ;
703
656
} else {
704
- const name = ( options ? options . name : undefined ) || '' ;
705
- this . writer . parse ( name , text ) ;
657
+ const name = query . name || '' ;
658
+ this . writer . parse ( name , query . text ) ;
706
659
this . writer . bind ( name , portal ) ;
707
660
this . bindQueue . push ( null ) ;
708
661
this . writer . describe ( portal , 'P' ) ;
@@ -738,6 +691,41 @@ export class Client {
738
691
return result . iterator ;
739
692
}
740
693
694
+ private bindAndExecute (
695
+ info : RowDataHandlerInfo ,
696
+ bind : Bind ,
697
+ types : DataType [ ] ) {
698
+ try {
699
+ this . writer . bind (
700
+ bind . name ,
701
+ bind . portal ,
702
+ bind . format ,
703
+ bind . values ,
704
+ types
705
+ ) ;
706
+ } catch ( error ) {
707
+ info . handler . callback ( error as Error ) ;
708
+ return ;
709
+ }
710
+
711
+ this . bindQueue . push ( info ) ;
712
+ this . writer . execute ( bind . portal ) ;
713
+ this . cleanupQueue . push ( Cleanup . Bind ) ;
714
+
715
+ if ( bind . close ) {
716
+ this . writer . close ( bind . name , 'S' ) ;
717
+ this . closeHandlerQueue . push ( null ) ;
718
+ this . cleanupQueue . push ( Cleanup . Close ) ;
719
+ }
720
+
721
+ this . writer . sync ( ) ;
722
+ this . errorHandlerQueue . push (
723
+ ( error ) => { info . handler . callback ( error ) ; }
724
+ ) ;
725
+ this . cleanupQueue . push ( Cleanup . ErrorHandler ) ;
726
+ this . send ( ) ;
727
+ }
728
+
741
729
private handleError ( error : Error ) : boolean {
742
730
while ( true ) {
743
731
switch ( this . cleanupQueue . shiftMaybe ( ) ) {
0 commit comments