@@ -112,52 +112,8 @@ Client.prototype.connect = function(callback) {
112
112
//after the connection initially becomes ready for queries
113
113
con . once ( 'readyForQuery' , function ( ) {
114
114
self . _connecting = false ;
115
+ self . _attachListeners ( con ) ;
115
116
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
- } ) ;
161
117
162
118
//process possible callback argument to Client#connect
163
119
if ( callback ) {
@@ -241,10 +197,58 @@ Client.prototype.connect = function(callback) {
241
197
} )
242
198
} )
243
199
}
244
-
245
200
} ;
246
201
247
- Client . prototype . getStartupConf = function ( ) {
202
+ Client . prototype . _attachListeners = function ( con ) {
203
+ const self = this
204
+ //delegate rowDescription to active query
205
+ con . on ( 'rowDescription' , function ( msg ) {
206
+ self . activeQuery . handleRowDescription ( msg ) ;
207
+ } ) ;
208
+
209
+ //delegate dataRow to active query
210
+ con . on ( 'dataRow' , function ( msg ) {
211
+ self . activeQuery . handleDataRow ( msg ) ;
212
+ } ) ;
213
+
214
+ //delegate portalSuspended to active query
215
+ con . on ( 'portalSuspended' , function ( msg ) {
216
+ self . activeQuery . handlePortalSuspended ( con ) ;
217
+ } ) ;
218
+
219
+ //deletagate emptyQuery to active query
220
+ con . on ( 'emptyQuery' , function ( msg ) {
221
+ self . activeQuery . handleEmptyQuery ( con ) ;
222
+ } ) ;
223
+
224
+ //delegate commandComplete to active query
225
+ con . on ( 'commandComplete' , function ( msg ) {
226
+ self . activeQuery . handleCommandComplete ( msg , con ) ;
227
+ } ) ;
228
+
229
+ //if a prepared statement has a name and properly parses
230
+ //we track that its already been executed so we don't parse
231
+ //it again on the same client
232
+ con . on ( 'parseComplete' , function ( msg ) {
233
+ if ( self . activeQuery . name ) {
234
+ con . parsedStatements [ self . activeQuery . name ] = true ;
235
+ }
236
+ } ) ;
237
+
238
+ con . on ( 'copyInResponse' , function ( msg ) {
239
+ self . activeQuery . handleCopyInResponse ( self . connection ) ;
240
+ } ) ;
241
+
242
+ con . on ( 'copyData' , function ( msg ) {
243
+ self . activeQuery . handleCopyData ( msg , self . connection ) ;
244
+ } ) ;
245
+
246
+ con . on ( 'notification' , function ( msg ) {
247
+ self . emit ( 'notification' , msg ) ;
248
+ } ) ;
249
+ }
250
+
251
+ Client . prototype . getStartupConf = function ( ) {
248
252
var params = this . connectionParameters ;
249
253
250
254
var data = {
@@ -263,41 +267,41 @@ Client.prototype.getStartupConf = function() {
263
267
return data ;
264
268
} ;
265
269
266
- Client . prototype . cancel = function ( client , query ) {
267
- if ( client . activeQuery == query ) {
270
+ Client . prototype . cancel = function ( client , query ) {
271
+ if ( client . activeQuery == query ) {
268
272
var con = this . connection ;
269
273
270
- if ( this . host && this . host . indexOf ( '/' ) === 0 ) {
274
+ if ( this . host && this . host . indexOf ( '/' ) === 0 ) {
271
275
con . connect ( this . host + '/.s.PGSQL.' + this . port ) ;
272
276
} else {
273
277
con . connect ( this . port , this . host ) ;
274
278
}
275
279
276
280
//once connection is established send cancel message
277
- con . on ( 'connect' , function ( ) {
281
+ con . on ( 'connect' , function ( ) {
278
282
con . cancel ( client . processID , client . secretKey ) ;
279
283
} ) ;
280
- } else if ( client . queryQueue . indexOf ( query ) != - 1 ) {
284
+ } else if ( client . queryQueue . indexOf ( query ) != - 1 ) {
281
285
client . queryQueue . splice ( client . queryQueue . indexOf ( query ) , 1 ) ;
282
286
}
283
287
} ;
284
288
285
- Client . prototype . setTypeParser = function ( oid , format , parseFn ) {
289
+ Client . prototype . setTypeParser = function ( oid , format , parseFn ) {
286
290
return this . _types . setTypeParser ( oid , format , parseFn ) ;
287
291
} ;
288
292
289
- Client . prototype . getTypeParser = function ( oid , format ) {
293
+ Client . prototype . getTypeParser = function ( oid , format ) {
290
294
return this . _types . getTypeParser ( oid , format ) ;
291
295
} ;
292
296
293
297
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
294
- Client . prototype . escapeIdentifier = function ( str ) {
298
+ Client . prototype . escapeIdentifier = function ( str ) {
295
299
296
300
var escaped = '"' ;
297
301
298
- for ( var i = 0 ; i < str . length ; i ++ ) {
302
+ for ( var i = 0 ; i < str . length ; i ++ ) {
299
303
var c = str [ i ] ;
300
- if ( c === '"' ) {
304
+ if ( c === '"' ) {
301
305
escaped += c + c ;
302
306
} else {
303
307
escaped += c ;
@@ -310,14 +314,14 @@ Client.prototype.escapeIdentifier = function(str) {
310
314
} ;
311
315
312
316
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
313
- Client . prototype . escapeLiteral = function ( str ) {
317
+ Client . prototype . escapeLiteral = function ( str ) {
314
318
315
319
var hasBackslash = false ;
316
320
var escaped = '\'' ;
317
321
318
- for ( var i = 0 ; i < str . length ; i ++ ) {
322
+ for ( var i = 0 ; i < str . length ; i ++ ) {
319
323
var c = str [ i ] ;
320
- if ( c === '\'' ) {
324
+ if ( c === '\'' ) {
321
325
escaped += c + c ;
322
326
} else if ( c === '\\' ) {
323
327
escaped += c + c ;
@@ -329,21 +333,21 @@ Client.prototype.escapeLiteral = function(str) {
329
333
330
334
escaped += '\'' ;
331
335
332
- if ( hasBackslash === true ) {
336
+ if ( hasBackslash === true ) {
333
337
escaped = ' E' + escaped ;
334
338
}
335
339
336
340
return escaped ;
337
341
} ;
338
342
339
- Client . prototype . _pulseQueryQueue = function ( ) {
340
- if ( this . readyForQuery === true ) {
343
+ Client . prototype . _pulseQueryQueue = function ( ) {
344
+ if ( this . readyForQuery === true ) {
341
345
this . activeQuery = this . queryQueue . shift ( ) ;
342
- if ( this . activeQuery ) {
346
+ if ( this . activeQuery ) {
343
347
this . readyForQuery = false ;
344
348
this . hasExecuted = true ;
345
349
this . activeQuery . submit ( this . connection ) ;
346
- } else if ( this . hasExecuted ) {
350
+ } else if ( this . hasExecuted ) {
347
351
this . activeQuery = null ;
348
352
this . emit ( 'drain' ) ;
349
353
}
@@ -358,7 +362,7 @@ Client.prototype.copyTo = function (text) {
358
362
throw new Error ( "For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams" ) ;
359
363
} ;
360
364
361
- Client . prototype . query = function ( config , values , callback ) {
365
+ Client . prototype . query = function ( config , values , callback ) {
362
366
//can take in strings, config object or query object
363
367
var query ;
364
368
var result ;
@@ -376,10 +380,10 @@ Client.prototype.query = function(config, values, callback) {
376
380
} )
377
381
}
378
382
379
- if ( this . binary && ! query . binary ) {
383
+ if ( this . binary && ! query . binary ) {
380
384
query . binary = true ;
381
385
}
382
- if ( query . _result ) {
386
+ if ( query . _result ) {
383
387
query . _result . _getTypeParser = this . _types . getTypeParser . bind ( this . _types ) ;
384
388
}
385
389
@@ -388,7 +392,7 @@ Client.prototype.query = function(config, values, callback) {
388
392
return result
389
393
} ;
390
394
391
- Client . prototype . end = function ( cb ) {
395
+ Client . prototype . end = function ( cb ) {
392
396
this . _ending = true ;
393
397
if ( this . activeQuery ) {
394
398
// if we have an active query we need to force a disconnect
@@ -407,7 +411,7 @@ Client.prototype.end = function(cb) {
407
411
}
408
412
} ;
409
413
410
- Client . md5 = function ( string ) {
414
+ Client . md5 = function ( string ) {
411
415
return crypto . createHash ( 'md5' ) . update ( string , 'utf-8' ) . digest ( 'hex' ) ;
412
416
} ;
413
417
0 commit comments