Skip to content

Commit fc36340

Browse files
committed
Move attach listeners into its own function
Just for readability
1 parent a4b42ac commit fc36340

File tree

1 file changed

+74
-70
lines changed

1 file changed

+74
-70
lines changed

lib/client.js

+74-70
Original file line numberDiff line numberDiff line change
@@ -112,52 +112,8 @@ Client.prototype.connect = function(callback) {
112112
//after the connection initially becomes ready for queries
113113
con.once('readyForQuery', function() {
114114
self._connecting = false;
115+
self._attachListeners(con);
115116

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-
});
161117

162118
//process possible callback argument to Client#connect
163119
if (callback) {
@@ -241,10 +197,58 @@ Client.prototype.connect = function(callback) {
241197
})
242198
})
243199
}
244-
245200
};
246201

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 () {
248252
var params = this.connectionParameters;
249253

250254
var data = {
@@ -263,41 +267,41 @@ Client.prototype.getStartupConf = function() {
263267
return data;
264268
};
265269

266-
Client.prototype.cancel = function(client, query) {
267-
if(client.activeQuery == query) {
270+
Client.prototype.cancel = function (client, query) {
271+
if (client.activeQuery == query) {
268272
var con = this.connection;
269273

270-
if(this.host && this.host.indexOf('/') === 0) {
274+
if (this.host && this.host.indexOf('/') === 0) {
271275
con.connect(this.host + '/.s.PGSQL.' + this.port);
272276
} else {
273277
con.connect(this.port, this.host);
274278
}
275279

276280
//once connection is established send cancel message
277-
con.on('connect', function() {
281+
con.on('connect', function () {
278282
con.cancel(client.processID, client.secretKey);
279283
});
280-
} else if(client.queryQueue.indexOf(query) != -1) {
284+
} else if (client.queryQueue.indexOf(query) != -1) {
281285
client.queryQueue.splice(client.queryQueue.indexOf(query), 1);
282286
}
283287
};
284288

285-
Client.prototype.setTypeParser = function(oid, format, parseFn) {
289+
Client.prototype.setTypeParser = function (oid, format, parseFn) {
286290
return this._types.setTypeParser(oid, format, parseFn);
287291
};
288292

289-
Client.prototype.getTypeParser = function(oid, format) {
293+
Client.prototype.getTypeParser = function (oid, format) {
290294
return this._types.getTypeParser(oid, format);
291295
};
292296

293297
// 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) {
295299

296300
var escaped = '"';
297301

298-
for(var i = 0; i < str.length; i++) {
302+
for (var i = 0; i < str.length; i++) {
299303
var c = str[i];
300-
if(c === '"') {
304+
if (c === '"') {
301305
escaped += c + c;
302306
} else {
303307
escaped += c;
@@ -310,14 +314,14 @@ Client.prototype.escapeIdentifier = function(str) {
310314
};
311315

312316
// 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) {
314318

315319
var hasBackslash = false;
316320
var escaped = '\'';
317321

318-
for(var i = 0; i < str.length; i++) {
322+
for (var i = 0; i < str.length; i++) {
319323
var c = str[i];
320-
if(c === '\'') {
324+
if (c === '\'') {
321325
escaped += c + c;
322326
} else if (c === '\\') {
323327
escaped += c + c;
@@ -329,21 +333,21 @@ Client.prototype.escapeLiteral = function(str) {
329333

330334
escaped += '\'';
331335

332-
if(hasBackslash === true) {
336+
if (hasBackslash === true) {
333337
escaped = ' E' + escaped;
334338
}
335339

336340
return escaped;
337341
};
338342

339-
Client.prototype._pulseQueryQueue = function() {
340-
if(this.readyForQuery===true) {
343+
Client.prototype._pulseQueryQueue = function () {
344+
if (this.readyForQuery === true) {
341345
this.activeQuery = this.queryQueue.shift();
342-
if(this.activeQuery) {
346+
if (this.activeQuery) {
343347
this.readyForQuery = false;
344348
this.hasExecuted = true;
345349
this.activeQuery.submit(this.connection);
346-
} else if(this.hasExecuted) {
350+
} else if (this.hasExecuted) {
347351
this.activeQuery = null;
348352
this.emit('drain');
349353
}
@@ -358,7 +362,7 @@ Client.prototype.copyTo = function (text) {
358362
throw new Error("For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams");
359363
};
360364

361-
Client.prototype.query = function(config, values, callback) {
365+
Client.prototype.query = function (config, values, callback) {
362366
//can take in strings, config object or query object
363367
var query;
364368
var result;
@@ -376,10 +380,10 @@ Client.prototype.query = function(config, values, callback) {
376380
})
377381
}
378382

379-
if(this.binary && !query.binary) {
383+
if (this.binary && !query.binary) {
380384
query.binary = true;
381385
}
382-
if(query._result) {
386+
if (query._result) {
383387
query._result._getTypeParser = this._types.getTypeParser.bind(this._types);
384388
}
385389

@@ -388,7 +392,7 @@ Client.prototype.query = function(config, values, callback) {
388392
return result
389393
};
390394

391-
Client.prototype.end = function(cb) {
395+
Client.prototype.end = function (cb) {
392396
this._ending = true;
393397
if (this.activeQuery) {
394398
// if we have an active query we need to force a disconnect
@@ -407,7 +411,7 @@ Client.prototype.end = function(cb) {
407411
}
408412
};
409413

410-
Client.md5 = function(string) {
414+
Client.md5 = function (string) {
411415
return crypto.createHash('md5').update(string, 'utf-8').digest('hex');
412416
};
413417

0 commit comments

Comments
 (0)