@@ -20,25 +20,23 @@ var Client = function(config) {
20
20
this . host = this . connectionParameters . host ;
21
21
this . password = this . connectionParameters . password ;
22
22
23
- config = config || { } ;
23
+ var c = config || { } ;
24
24
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
28
28
} ) ;
29
29
this . queryQueue = [ ] ;
30
- this . binary = config . binary || defaults . binary ;
30
+ this . binary = c . binary || defaults . binary ;
31
31
this . encoding = 'utf8' ;
32
32
this . processID = null ;
33
33
this . secretKey = null ;
34
- this . ssl = config . ssl || false ;
34
+ this . ssl = c . ssl || false ;
35
35
} ;
36
36
37
37
util . inherits ( Client , EventEmitter ) ;
38
38
39
- var p = Client . prototype ;
40
-
41
- p . connect = function ( callback ) {
39
+ Client . prototype . connect = function ( callback ) {
42
40
var self = this ;
43
41
var con = this . connection ;
44
42
if ( this . host && this . host . indexOf ( '/' ) === 0 ) {
@@ -50,7 +48,7 @@ p.connect = function(callback) {
50
48
51
49
//once connection is established send startup message
52
50
con . on ( 'connect' , function ( ) {
53
- if ( self . ssl ) {
51
+ if ( self . ssl ) {
54
52
con . requestSsl ( ) ;
55
53
} else {
56
54
con . startup ( {
@@ -59,6 +57,7 @@ p.connect = function(callback) {
59
57
} ) ;
60
58
}
61
59
} ) ;
60
+
62
61
con . on ( 'sslconnect' , function ( ) {
63
62
con . startup ( {
64
63
user : self . user ,
@@ -91,10 +90,12 @@ p.connect = function(callback) {
91
90
con . on ( 'rowDescription' , function ( msg ) {
92
91
self . activeQuery . handleRowDescription ( msg ) ;
93
92
} ) ;
93
+
94
94
//delegate datarow to active query
95
95
con . on ( 'dataRow' , function ( msg ) {
96
96
self . activeQuery . handleDataRow ( msg ) ;
97
97
} ) ;
98
+
98
99
//TODO should query gain access to connection?
99
100
con . on ( 'portalSuspended' , function ( msg ) {
100
101
self . activeQuery . getRows ( con ) ;
@@ -108,11 +109,13 @@ p.connect = function(callback) {
108
109
con . sync ( ) ;
109
110
}
110
111
} ) ;
112
+
111
113
con . on ( 'copyInResponse' , function ( msg ) {
112
114
self . activeQuery . streamData ( self . connection ) ;
113
115
} ) ;
116
+
114
117
con . on ( 'copyOutResponse' , function ( msg ) {
115
- if ( self . activeQuery . stream === undefined ) {
118
+ if ( self . activeQuery . stream === undefined ) {
116
119
self . activeQuery . _canceledDueToError =
117
120
new Error ( 'No destination stream defined' ) ;
118
121
//canceling query requires creation of new connection
@@ -121,9 +124,11 @@ p.connect = function(callback) {
121
124
. cancel ( self , self . activeQuery ) ;
122
125
}
123
126
} ) ;
127
+
124
128
con . on ( 'copyData' , function ( msg ) {
125
129
self . activeQuery . handleCopyFromChunk ( msg . chunk ) ;
126
130
} ) ;
131
+
127
132
if ( ! callback ) {
128
133
self . emit ( 'connect' ) ;
129
134
} else {
@@ -170,8 +175,8 @@ p.connect = function(callback) {
170
175
171
176
} ;
172
177
173
- p . cancel = function ( client , query ) {
174
- if ( client . activeQuery == query ) {
178
+ Client . prototype . cancel = function ( client , query ) {
179
+ if ( client . activeQuery == query ) {
175
180
var con = this . connection ;
176
181
177
182
if ( this . host && this . host . indexOf ( '/' ) === 0 ) {
@@ -184,13 +189,12 @@ p.cancel = function(client, query) {
184
189
con . on ( 'connect' , function ( ) {
185
190
con . cancel ( client . processID , client . secretKey ) ;
186
191
} ) ;
187
- }
188
- else if ( client . queryQueue . indexOf ( query ) != - 1 ) {
192
+ } else if ( client . queryQueue . indexOf ( query ) != - 1 ) {
189
193
client . queryQueue . splice ( client . queryQueue . indexOf ( query ) , 1 ) ;
190
194
}
191
195
} ;
192
196
193
- p . _pulseQueryQueue = function ( ) {
197
+ Client . prototype . _pulseQueryQueue = function ( ) {
194
198
if ( this . readyForQuery === true ) {
195
199
this . activeQuery = this . queryQueue . shift ( ) ;
196
200
if ( this . activeQuery ) {
@@ -199,40 +203,48 @@ p._pulseQueryQueue = function() {
199
203
this . activeQuery . submit ( this . connection ) ;
200
204
} else if ( this . hasExecuted ) {
201
205
this . activeQuery = null ;
202
- if ( this . _drainPaused > 0 ) { this . _drainPaused ++ ; }
203
- 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
+ }
204
213
}
205
214
}
206
215
} ;
207
- p . _copy = function ( text , stream ) {
208
- var config = { } ,
209
- query ;
216
+
217
+ Client . prototype . _copy = function ( text , stream ) {
218
+ var config = { } ;
210
219
config . text = text ;
211
220
config . stream = stream ;
212
221
config . callback = function ( error ) {
213
- if ( error ) {
222
+ if ( error ) {
214
223
config . stream . error ( error ) ;
215
224
} else {
216
225
config . stream . close ( ) ;
217
226
}
218
227
} ;
219
- query = new Query ( config ) ;
228
+ var query = new Query ( config ) ;
220
229
this . queryQueue . push ( query ) ;
221
230
this . _pulseQueryQueue ( ) ;
222
231
return config . stream ;
223
232
224
233
} ;
225
- p . copyFrom = function ( text ) {
234
+
235
+ Client . prototype . copyFrom = function ( text ) {
226
236
return this . _copy ( text , new CopyFromStream ( ) ) ;
227
237
} ;
228
- p . copyTo = function ( text ) {
238
+
239
+ Client . prototype . copyTo = function ( text ) {
229
240
return this . _copy ( text , new CopyToStream ( ) ) ;
230
241
} ;
231
- p . query = function ( config , values , callback ) {
242
+
243
+ Client . prototype . query = function ( config , values , callback ) {
232
244
//can take in strings, config object or query object
233
245
var query = ( config instanceof Query ) ? config :
234
246
new Query ( config , values , callback ) ;
235
- if ( this . binary && ! query . binary ) {
247
+ if ( this . binary && ! query . binary ) {
236
248
query . binary = true ;
237
249
}
238
250
@@ -243,19 +255,19 @@ p.query = function(config, values, callback) {
243
255
244
256
//prevents client from otherwise emitting 'drain' event until 'resumeDrain' is
245
257
//called
246
- p . pauseDrain = function ( ) {
258
+ Client . prototype . pauseDrain = function ( ) {
247
259
this . _drainPaused = 1 ;
248
260
} ;
249
261
250
262
//resume raising 'drain' event
251
- p . resumeDrain = function ( ) {
263
+ Client . prototype . resumeDrain = function ( ) {
252
264
if ( this . _drainPaused > 1 ) {
253
265
this . emit ( 'drain' ) ;
254
266
}
255
267
this . _drainPaused = 0 ;
256
268
} ;
257
269
258
- p . end = function ( ) {
270
+ Client . prototype . end = function ( ) {
259
271
this . connection . end ( ) ;
260
272
} ;
261
273
0 commit comments