@@ -20,18 +20,18 @@ var Client = function(config) {
2020 this . host = this . connectionParameters . host ;
2121 this . password = this . connectionParameters . password ;
2222
23- config = config || { } ;
23+ var c = config || { } ;
2424
25- this . connection = config . connection || new Connection ( {
26- stream : config . stream ,
27- ssl : config . ssl
25+ this . connection = c . connection || new Connection ( {
26+ stream : c . stream ,
27+ ssl : c . ssl
2828 } ) ;
2929 this . queryQueue = [ ] ;
30- this . binary = config . binary || defaults . binary ;
30+ this . binary = c . binary || defaults . binary ;
3131 this . encoding = 'utf8' ;
3232 this . processID = null ;
3333 this . secretKey = null ;
34- this . ssl = config . ssl || false ;
34+ this . ssl = c . ssl || false ;
3535} ;
3636
3737util . inherits ( Client , EventEmitter ) ;
@@ -48,7 +48,7 @@ Client.prototype.connect = function(callback) {
4848
4949 //once connection is established send startup message
5050 con . on ( 'connect' , function ( ) {
51- if ( self . ssl ) {
51+ if ( self . ssl ) {
5252 con . requestSsl ( ) ;
5353 } else {
5454 con . startup ( {
@@ -57,6 +57,7 @@ Client.prototype.connect = function(callback) {
5757 } ) ;
5858 }
5959 } ) ;
60+
6061 con . on ( 'sslconnect' , function ( ) {
6162 con . startup ( {
6263 user : self . user ,
@@ -89,10 +90,12 @@ Client.prototype.connect = function(callback) {
8990 con . on ( 'rowDescription' , function ( msg ) {
9091 self . activeQuery . handleRowDescription ( msg ) ;
9192 } ) ;
93+
9294 //delegate datarow to active query
9395 con . on ( 'dataRow' , function ( msg ) {
9496 self . activeQuery . handleDataRow ( msg ) ;
9597 } ) ;
98+
9699 //TODO should query gain access to connection?
97100 con . on ( 'portalSuspended' , function ( msg ) {
98101 self . activeQuery . getRows ( con ) ;
@@ -106,11 +109,13 @@ Client.prototype.connect = function(callback) {
106109 con . sync ( ) ;
107110 }
108111 } ) ;
112+
109113 con . on ( 'copyInResponse' , function ( msg ) {
110114 self . activeQuery . streamData ( self . connection ) ;
111115 } ) ;
116+
112117 con . on ( 'copyOutResponse' , function ( msg ) {
113- if ( self . activeQuery . stream === undefined ) {
118+ if ( self . activeQuery . stream === undefined ) {
114119 self . activeQuery . _canceledDueToError =
115120 new Error ( 'No destination stream defined' ) ;
116121 //canceling query requires creation of new connection
@@ -119,9 +124,11 @@ Client.prototype.connect = function(callback) {
119124 . cancel ( self , self . activeQuery ) ;
120125 }
121126 } ) ;
127+
122128 con . on ( 'copyData' , function ( msg ) {
123129 self . activeQuery . handleCopyFromChunk ( msg . chunk ) ;
124130 } ) ;
131+
125132 if ( ! callback ) {
126133 self . emit ( 'connect' ) ;
127134 } else {
@@ -169,7 +176,7 @@ Client.prototype.connect = function(callback) {
169176} ;
170177
171178Client . prototype . cancel = function ( client , query ) {
172- if ( client . activeQuery == query ) {
179+ if ( client . activeQuery == query ) {
173180 var con = this . connection ;
174181
175182 if ( this . host && this . host . indexOf ( '/' ) === 0 ) {
@@ -182,8 +189,7 @@ Client.prototype.cancel = function(client, query) {
182189 con . on ( 'connect' , function ( ) {
183190 con . cancel ( client . processID , client . secretKey ) ;
184191 } ) ;
185- }
186- else if ( client . queryQueue . indexOf ( query ) != - 1 ) {
192+ } else if ( client . queryQueue . indexOf ( query ) != - 1 ) {
187193 client . queryQueue . splice ( client . queryQueue . indexOf ( query ) , 1 ) ;
188194 }
189195} ;
@@ -197,25 +203,29 @@ Client.prototype._pulseQueryQueue = function() {
197203 this . activeQuery . submit ( this . connection ) ;
198204 } else if ( this . hasExecuted ) {
199205 this . activeQuery = null ;
200- if ( this . _drainPaused > 0 ) { this . _drainPaused ++ ; }
201- else { this . emit ( 'drain' ) ; }
206+ //TODO remove pauseDrain for v1.0
207+ if ( this . _drainPaused > 0 ) {
208+ this . _drainPaused ++ ;
209+ }
210+ else {
211+ this . emit ( 'drain' ) ;
212+ }
202213 }
203214 }
204215} ;
205216
206217Client . prototype . _copy = function ( text , stream ) {
207- var config = { } ,
208- query ;
218+ var config = { } ;
209219 config . text = text ;
210220 config . stream = stream ;
211221 config . callback = function ( error ) {
212- if ( error ) {
222+ if ( error ) {
213223 config . stream . error ( error ) ;
214224 } else {
215225 config . stream . close ( ) ;
216226 }
217227 } ;
218- query = new Query ( config ) ;
228+ var query = new Query ( config ) ;
219229 this . queryQueue . push ( query ) ;
220230 this . _pulseQueryQueue ( ) ;
221231 return config . stream ;
@@ -234,7 +244,7 @@ Client.prototype.query = function(config, values, callback) {
234244 //can take in strings, config object or query object
235245 var query = ( config instanceof Query ) ? config :
236246 new Query ( config , values , callback ) ;
237- if ( this . binary && ! query . binary ) {
247+ if ( this . binary && ! query . binary ) {
238248 query . binary = true ;
239249 }
240250
0 commit comments