Move attach listeners into its own function

Just for readability
This commit is contained in:
Brian Carlson 2017-06-15 13:22:58 -05:00 committed by Brian C
parent a4b42ac36b
commit fc3634045b

View File

@ -112,52 +112,8 @@ Client.prototype.connect = function(callback) {
//after the connection initially becomes ready for queries
con.once('readyForQuery', function() {
self._connecting = false;
self._attachListeners(con);
//delegate rowDescription to active query
con.on('rowDescription', function(msg) {
self.activeQuery.handleRowDescription(msg);
});
//delegate dataRow to active query
con.on('dataRow', function(msg) {
self.activeQuery.handleDataRow(msg);
});
//delegate portalSuspended to active query
con.on('portalSuspended', function(msg) {
self.activeQuery.handlePortalSuspended(con);
});
//deletagate emptyQuery to active query
con.on('emptyQuery', function(msg) {
self.activeQuery.handleEmptyQuery(con);
});
//delegate commandComplete to active query
con.on('commandComplete', function(msg) {
self.activeQuery.handleCommandComplete(msg, con);
});
//if a prepared statement has a name and properly parses
//we track that its already been executed so we don't parse
//it again on the same client
con.on('parseComplete', function(msg) {
if(self.activeQuery.name) {
con.parsedStatements[self.activeQuery.name] = true;
}
});
con.on('copyInResponse', function(msg) {
self.activeQuery.handleCopyInResponse(self.connection);
});
con.on('copyData', function (msg) {
self.activeQuery.handleCopyData(msg, self.connection);
});
con.on('notification', function(msg) {
self.emit('notification', msg);
});
//process possible callback argument to Client#connect
if (callback) {
@ -241,10 +197,58 @@ Client.prototype.connect = function(callback) {
})
})
}
};
Client.prototype.getStartupConf = function() {
Client.prototype._attachListeners = function(con) {
const self = this
//delegate rowDescription to active query
con.on('rowDescription', function (msg) {
self.activeQuery.handleRowDescription(msg);
});
//delegate dataRow to active query
con.on('dataRow', function (msg) {
self.activeQuery.handleDataRow(msg);
});
//delegate portalSuspended to active query
con.on('portalSuspended', function (msg) {
self.activeQuery.handlePortalSuspended(con);
});
//deletagate emptyQuery to active query
con.on('emptyQuery', function (msg) {
self.activeQuery.handleEmptyQuery(con);
});
//delegate commandComplete to active query
con.on('commandComplete', function (msg) {
self.activeQuery.handleCommandComplete(msg, con);
});
//if a prepared statement has a name and properly parses
//we track that its already been executed so we don't parse
//it again on the same client
con.on('parseComplete', function (msg) {
if (self.activeQuery.name) {
con.parsedStatements[self.activeQuery.name] = true;
}
});
con.on('copyInResponse', function (msg) {
self.activeQuery.handleCopyInResponse(self.connection);
});
con.on('copyData', function (msg) {
self.activeQuery.handleCopyData(msg, self.connection);
});
con.on('notification', function (msg) {
self.emit('notification', msg);
});
}
Client.prototype.getStartupConf = function () {
var params = this.connectionParameters;
var data = {
@ -263,41 +267,41 @@ Client.prototype.getStartupConf = function() {
return data;
};
Client.prototype.cancel = function(client, query) {
if(client.activeQuery == query) {
Client.prototype.cancel = function (client, query) {
if (client.activeQuery == query) {
var con = this.connection;
if(this.host && this.host.indexOf('/') === 0) {
if (this.host && this.host.indexOf('/') === 0) {
con.connect(this.host + '/.s.PGSQL.' + this.port);
} else {
con.connect(this.port, this.host);
}
//once connection is established send cancel message
con.on('connect', function() {
con.on('connect', function () {
con.cancel(client.processID, client.secretKey);
});
} else if(client.queryQueue.indexOf(query) != -1) {
} else if (client.queryQueue.indexOf(query) != -1) {
client.queryQueue.splice(client.queryQueue.indexOf(query), 1);
}
};
Client.prototype.setTypeParser = function(oid, format, parseFn) {
Client.prototype.setTypeParser = function (oid, format, parseFn) {
return this._types.setTypeParser(oid, format, parseFn);
};
Client.prototype.getTypeParser = function(oid, format) {
Client.prototype.getTypeParser = function (oid, format) {
return this._types.getTypeParser(oid, format);
};
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
Client.prototype.escapeIdentifier = function(str) {
Client.prototype.escapeIdentifier = function (str) {
var escaped = '"';
for(var i = 0; i < str.length; i++) {
for (var i = 0; i < str.length; i++) {
var c = str[i];
if(c === '"') {
if (c === '"') {
escaped += c + c;
} else {
escaped += c;
@ -310,14 +314,14 @@ Client.prototype.escapeIdentifier = function(str) {
};
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
Client.prototype.escapeLiteral = function(str) {
Client.prototype.escapeLiteral = function (str) {
var hasBackslash = false;
var escaped = '\'';
for(var i = 0; i < str.length; i++) {
for (var i = 0; i < str.length; i++) {
var c = str[i];
if(c === '\'') {
if (c === '\'') {
escaped += c + c;
} else if (c === '\\') {
escaped += c + c;
@ -329,21 +333,21 @@ Client.prototype.escapeLiteral = function(str) {
escaped += '\'';
if(hasBackslash === true) {
if (hasBackslash === true) {
escaped = ' E' + escaped;
}
return escaped;
};
Client.prototype._pulseQueryQueue = function() {
if(this.readyForQuery===true) {
Client.prototype._pulseQueryQueue = function () {
if (this.readyForQuery === true) {
this.activeQuery = this.queryQueue.shift();
if(this.activeQuery) {
if (this.activeQuery) {
this.readyForQuery = false;
this.hasExecuted = true;
this.activeQuery.submit(this.connection);
} else if(this.hasExecuted) {
} else if (this.hasExecuted) {
this.activeQuery = null;
this.emit('drain');
}
@ -358,7 +362,7 @@ Client.prototype.copyTo = function (text) {
throw new Error("For PostgreSQL COPY TO/COPY FROM support npm install pg-copy-streams");
};
Client.prototype.query = function(config, values, callback) {
Client.prototype.query = function (config, values, callback) {
//can take in strings, config object or query object
var query;
var result;
@ -376,10 +380,10 @@ Client.prototype.query = function(config, values, callback) {
})
}
if(this.binary && !query.binary) {
if (this.binary && !query.binary) {
query.binary = true;
}
if(query._result) {
if (query._result) {
query._result._getTypeParser = this._types.getTypeParser.bind(this._types);
}
@ -388,7 +392,7 @@ Client.prototype.query = function(config, values, callback) {
return result
};
Client.prototype.end = function(cb) {
Client.prototype.end = function (cb) {
this._ending = true;
if (this.activeQuery) {
// if we have an active query we need to force a disconnect
@ -407,7 +411,7 @@ Client.prototype.end = function(cb) {
}
};
Client.md5 = function(string) {
Client.md5 = function (string) {
return crypto.createHash('md5').update(string, 'utf-8').digest('hex');
};