Skip to content

Commit fbb9f3c

Browse files
committed
Merge pull request brianc#292 from brianc/cleanup
Cleanup
2 parents e8f7649 + c57eee8 commit fbb9f3c

File tree

14 files changed

+252
-194
lines changed

14 files changed

+252
-194
lines changed

lib/client.js

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,23 @@ 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

3737
util.inherits(Client, EventEmitter);
3838

39-
var p = Client.prototype;
40-
41-
p.connect = function(callback) {
39+
Client.prototype.connect = function(callback) {
4240
var self = this;
4341
var con = this.connection;
4442
if(this.host && this.host.indexOf('/') === 0) {
@@ -50,7 +48,7 @@ p.connect = function(callback) {
5048

5149
//once connection is established send startup message
5250
con.on('connect', function() {
53-
if (self.ssl) {
51+
if(self.ssl) {
5452
con.requestSsl();
5553
} else {
5654
con.startup({
@@ -59,6 +57,7 @@ p.connect = function(callback) {
5957
});
6058
}
6159
});
60+
6261
con.on('sslconnect', function() {
6362
con.startup({
6463
user: self.user,
@@ -91,10 +90,12 @@ p.connect = function(callback) {
9190
con.on('rowDescription', function(msg) {
9291
self.activeQuery.handleRowDescription(msg);
9392
});
93+
9494
//delegate datarow to active query
9595
con.on('dataRow', function(msg) {
9696
self.activeQuery.handleDataRow(msg);
9797
});
98+
9899
//TODO should query gain access to connection?
99100
con.on('portalSuspended', function(msg) {
100101
self.activeQuery.getRows(con);
@@ -108,11 +109,13 @@ p.connect = function(callback) {
108109
con.sync();
109110
}
110111
});
112+
111113
con.on('copyInResponse', function(msg) {
112114
self.activeQuery.streamData(self.connection);
113115
});
116+
114117
con.on('copyOutResponse', function(msg) {
115-
if (self.activeQuery.stream === undefined) {
118+
if(self.activeQuery.stream === undefined) {
116119
self.activeQuery._canceledDueToError =
117120
new Error('No destination stream defined');
118121
//canceling query requires creation of new connection
@@ -121,9 +124,11 @@ p.connect = function(callback) {
121124
.cancel(self, self.activeQuery);
122125
}
123126
});
127+
124128
con.on('copyData', function (msg) {
125129
self.activeQuery.handleCopyFromChunk(msg.chunk);
126130
});
131+
127132
if (!callback) {
128133
self.emit('connect');
129134
} else {
@@ -170,8 +175,8 @@ p.connect = function(callback) {
170175

171176
};
172177

173-
p.cancel = function(client, query) {
174-
if (client.activeQuery == query) {
178+
Client.prototype.cancel = function(client, query) {
179+
if(client.activeQuery == query) {
175180
var con = this.connection;
176181

177182
if(this.host && this.host.indexOf('/') === 0) {
@@ -184,13 +189,12 @@ p.cancel = function(client, query) {
184189
con.on('connect', function() {
185190
con.cancel(client.processID, client.secretKey);
186191
});
187-
}
188-
else if (client.queryQueue.indexOf(query) != -1) {
192+
} else if(client.queryQueue.indexOf(query) != -1) {
189193
client.queryQueue.splice(client.queryQueue.indexOf(query), 1);
190194
}
191195
};
192196

193-
p._pulseQueryQueue = function() {
197+
Client.prototype._pulseQueryQueue = function() {
194198
if(this.readyForQuery===true) {
195199
this.activeQuery = this.queryQueue.shift();
196200
if(this.activeQuery) {
@@ -199,40 +203,48 @@ p._pulseQueryQueue = function() {
199203
this.activeQuery.submit(this.connection);
200204
} else if(this.hasExecuted) {
201205
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+
}
204213
}
205214
}
206215
};
207-
p._copy = function (text, stream) {
208-
var config = {},
209-
query;
216+
217+
Client.prototype._copy = function (text, stream) {
218+
var config = {};
210219
config.text = text;
211220
config.stream = stream;
212221
config.callback = function (error) {
213-
if (error) {
222+
if(error) {
214223
config.stream.error(error);
215224
} else {
216225
config.stream.close();
217226
}
218227
};
219-
query = new Query(config);
228+
var query = new Query(config);
220229
this.queryQueue.push(query);
221230
this._pulseQueryQueue();
222231
return config.stream;
223232

224233
};
225-
p.copyFrom = function (text) {
234+
235+
Client.prototype.copyFrom = function (text) {
226236
return this._copy(text, new CopyFromStream());
227237
};
228-
p.copyTo = function (text) {
238+
239+
Client.prototype.copyTo = function (text) {
229240
return this._copy(text, new CopyToStream());
230241
};
231-
p.query = function(config, values, callback) {
242+
243+
Client.prototype.query = function(config, values, callback) {
232244
//can take in strings, config object or query object
233245
var query = (config instanceof Query) ? config :
234246
new Query(config, values, callback);
235-
if (this.binary && !query.binary) {
247+
if(this.binary && !query.binary) {
236248
query.binary = true;
237249
}
238250

@@ -243,19 +255,19 @@ p.query = function(config, values, callback) {
243255

244256
//prevents client from otherwise emitting 'drain' event until 'resumeDrain' is
245257
//called
246-
p.pauseDrain = function() {
258+
Client.prototype.pauseDrain = function() {
247259
this._drainPaused = 1;
248260
};
249261

250262
//resume raising 'drain' event
251-
p.resumeDrain = function() {
263+
Client.prototype.resumeDrain = function() {
252264
if(this._drainPaused > 1) {
253265
this.emit('drain');
254266
}
255267
this._drainPaused = 0;
256268
};
257269

258-
p.end = function() {
270+
Client.prototype.end = function() {
259271
this.connection.end();
260272
};
261273

0 commit comments

Comments
 (0)