@@ -67,12 +67,12 @@ Client.prototype.connect = function(callback) {
67
67
if ( self . ssl ) {
68
68
con . requestSsl ( ) ;
69
69
} else {
70
- con . startup ( self . _getStartupConfiguration ( ) ) ;
70
+ con . startup ( self . getStartupConf ( ) ) ;
71
71
}
72
72
} ) ;
73
73
74
74
con . on ( 'sslconnect' , function ( ) {
75
- con . startup ( self . _getStartupConfiguration ( ) ) ;
75
+ con . startup ( self . getStartupConf ( ) ) ;
76
76
} ) ;
77
77
78
78
function checkPgPass ( cb ) {
@@ -108,21 +108,56 @@ Client.prototype.connect = function(callback) {
108
108
self . secretKey = msg . secretKey ;
109
109
} ) ;
110
110
111
-
112
- con . on ( 'readyForQuery' , function ( ) {
113
- var activeQuery = self . activeQuery ;
114
- self . activeQuery = null ;
115
- self . readyForQuery = true ;
116
- self . _pulseQueryQueue ( ) ;
117
- if ( activeQuery ) {
118
- activeQuery . handleReadyForQuery ( con ) ;
119
- }
120
- } ) ;
121
-
122
111
//hook up query handling events to connection
123
112
//after the connection initially becomes ready for queries
124
113
con . once ( 'readyForQuery' , function ( ) {
125
- self . _attachEventListeners ( con ) ;
114
+ self . _connecting = false ;
115
+
116
+ //delegate rowDescription to active query
117
+ con . on ( 'rowDescription' , function ( msg ) {
118
+ self . activeQuery . handleRowDescription ( msg ) ;
119
+ } ) ;
120
+
121
+ //delegate dataRow to active query
122
+ con . on ( 'dataRow' , function ( msg ) {
123
+ self . activeQuery . handleDataRow ( msg ) ;
124
+ } ) ;
125
+
126
+ //delegate portalSuspended to active query
127
+ con . on ( 'portalSuspended' , function ( msg ) {
128
+ self . activeQuery . handlePortalSuspended ( con ) ;
129
+ } ) ;
130
+
131
+ //deletagate emptyQuery to active query
132
+ con . on ( 'emptyQuery' , function ( msg ) {
133
+ self . activeQuery . handleEmptyQuery ( con ) ;
134
+ } ) ;
135
+
136
+ //delegate commandComplete to active query
137
+ con . on ( 'commandComplete' , function ( msg ) {
138
+ self . activeQuery . handleCommandComplete ( msg , con ) ;
139
+ } ) ;
140
+
141
+ //if a prepared statement has a name and properly parses
142
+ //we track that its already been executed so we don't parse
143
+ //it again on the same client
144
+ con . on ( 'parseComplete' , function ( msg ) {
145
+ if ( self . activeQuery . name ) {
146
+ con . parsedStatements [ self . activeQuery . name ] = true ;
147
+ }
148
+ } ) ;
149
+
150
+ con . on ( 'copyInResponse' , function ( msg ) {
151
+ self . activeQuery . handleCopyInResponse ( self . connection ) ;
152
+ } ) ;
153
+
154
+ con . on ( 'copyData' , function ( msg ) {
155
+ self . activeQuery . handleCopyData ( msg , self . connection ) ;
156
+ } ) ;
157
+
158
+ con . on ( 'notification' , function ( msg ) {
159
+ self . emit ( 'notification' , msg ) ;
160
+ } ) ;
126
161
127
162
//process possible callback argument to Client#connect
128
163
if ( callback ) {
@@ -134,6 +169,16 @@ Client.prototype.connect = function(callback) {
134
169
self . emit ( 'connect' ) ;
135
170
} ) ;
136
171
172
+ con . on ( 'readyForQuery' , function ( ) {
173
+ var activeQuery = self . activeQuery ;
174
+ self . activeQuery = null ;
175
+ self . readyForQuery = true ;
176
+ self . _pulseQueryQueue ( ) ;
177
+ if ( activeQuery ) {
178
+ activeQuery . handleReadyForQuery ( con ) ;
179
+ }
180
+ } ) ;
181
+
137
182
con . on ( 'error' , function ( error ) {
138
183
if ( this . activeQuery ) {
139
184
var activeQuery = self . activeQuery ;
@@ -187,75 +232,19 @@ Client.prototype.connect = function(callback) {
187
232
self . emit ( 'notice' , msg ) ;
188
233
} ) ;
189
234
190
- var result ;
191
-
192
235
if ( ! callback ) {
193
- result = new global . Promise ( function ( resolve , reject ) {
194
- con . once ( 'connect' , function ( ) {
195
- con . removeListener ( 'error' , reject )
236
+ return new global . Promise ( ( resolve , reject ) => {
237
+ this . once ( 'error' , reject )
238
+ this . once ( 'connect' , ( ) => {
239
+ this . removeListener ( 'error' , reject )
196
240
resolve ( )
197
241
} )
198
- this . once ( 'error' , reject )
199
- } . bind ( this ) )
242
+ } )
200
243
}
201
244
202
- return result ;
203
245
} ;
204
246
205
-
206
- // once a connection is established connect listeners
207
- Client . prototype . _attachEventListeners = function ( con ) {
208
- var self = this ;
209
- self . _connecting = false ;
210
-
211
- //delegate rowDescription to active query
212
- con . on ( 'rowDescription' , function ( msg ) {
213
- self . activeQuery . handleRowDescription ( msg ) ;
214
- } ) ;
215
-
216
- //delegate dataRow to active query
217
- con . on ( 'dataRow' , function ( msg ) {
218
- self . activeQuery . handleDataRow ( msg ) ;
219
- } ) ;
220
-
221
- //delegate portalSuspended to active query
222
- con . on ( 'portalSuspended' , function ( msg ) {
223
- self . activeQuery . handlePortalSuspended ( con ) ;
224
- } ) ;
225
-
226
- //deletagate emptyQuery to active query
227
- con . on ( 'emptyQuery' , function ( msg ) {
228
- self . activeQuery . handleEmptyQuery ( con ) ;
229
- } ) ;
230
-
231
- //delegate commandComplete to active query
232
- con . on ( 'commandComplete' , function ( msg ) {
233
- self . activeQuery . handleCommandComplete ( msg , con ) ;
234
- } ) ;
235
-
236
- //if a prepared statement has a name and properly parses
237
- //we track that its already been executed so we don't parse
238
- //it again on the same client
239
- con . on ( 'parseComplete' , function ( msg ) {
240
- if ( self . activeQuery . name ) {
241
- con . parsedStatements [ self . activeQuery . name ] = true ;
242
- }
243
- } ) ;
244
-
245
- con . on ( 'copyInResponse' , function ( msg ) {
246
- self . activeQuery . handleCopyInResponse ( self . connection ) ;
247
- } ) ;
248
-
249
- con . on ( 'copyData' , function ( msg ) {
250
- self . activeQuery . handleCopyData ( msg , self . connection ) ;
251
- } ) ;
252
-
253
- con . on ( 'notification' , function ( msg ) {
254
- self . emit ( 'notification' , msg ) ;
255
- } ) ;
256
- }
257
-
258
- Client . prototype . _getStartupConfiguration = function ( ) {
247
+ Client . prototype . getStartupConf = function ( ) {
259
248
var params = this . connectionParameters ;
260
249
261
250
var data = {
@@ -370,54 +359,46 @@ Client.prototype.copyTo = function (text) {
370
359
} ;
371
360
372
361
Client . prototype . query = function ( config , values , callback ) {
373
- var promise ;
374
- var isQueryable = typeof config . submit == 'function' ;
362
+ //can take in strings, config object or query object
375
363
var query ;
376
- // if we receive an object with a 'submit' function we delegate
377
- // processing to the passed object - this is how pg.Query, QueryStream, and Cursor work
378
- if ( isQueryable ) {
379
- query = config ;
380
- // accept client.query(new Query('select *'), (err, res) => { }) call signature
364
+ var result ;
365
+ if ( typeof config . submit == 'function' ) {
366
+ query = config
367
+ result = query
381
368
if ( typeof values == 'function' ) {
382
- query . callback = query . callback || values ;
369
+ query . callback = query . callback || values
383
370
}
384
371
} else {
385
- query = new Query ( config , values , callback ) ;
386
- if ( ! query . callback ) {
387
- promise = new global . Promise ( function ( resolve , reject ) {
388
- query . on ( 'error' , reject ) ;
389
- query . on ( 'end' , resolve ) ;
390
- } ) ;
391
- }
372
+ query = new Query ( config , values , callback )
373
+ result = query . callback ? undefined : new global . Promise ( ( resolve , reject ) => {
374
+ query . once ( 'end' , resolve )
375
+ query . once ( 'error' , reject )
376
+ } )
392
377
}
378
+
393
379
if ( this . binary && ! query . binary ) {
394
380
query . binary = true ;
395
381
}
396
-
397
- // TODO - this is a smell
398
382
if ( query . _result ) {
399
383
query . _result . _getTypeParser = this . _types . getTypeParser . bind ( this . _types ) ;
400
384
}
401
385
402
386
this . queryQueue . push ( query ) ;
403
387
this . _pulseQueryQueue ( ) ;
404
-
405
- // if we were passed a queryable, return it
406
- // otherwise return callback/promise result
407
- return isQueryable ? query : promise ;
388
+ return result
408
389
} ;
409
390
410
391
Client . prototype . end = function ( cb ) {
411
392
this . _ending = true ;
412
393
if ( cb ) {
413
- this . connection . once ( 'end' , cb ) ;
414
394
this . connection . end ( ) ;
415
- return ;
395
+ this . connection . once ( 'end' , cb ) ;
396
+ } else {
397
+ return new global . Promise ( ( resolve , reject ) => {
398
+ this . connection . end ( )
399
+ this . connection . once ( 'end' , resolve )
400
+ } )
416
401
}
417
- return new global . Promise ( ( resolve ) => {
418
- this . connection . end ( ) ;
419
- this . connection . once ( 'end' , resolve ) ;
420
- } ) ;
421
402
} ;
422
403
423
404
Client . md5 = function ( string ) {
0 commit comments