normalize whitespace, add comments, and do a little house cleaning

This commit is contained in:
brianc 2013-03-06 10:26:40 -06:00
parent 37bb13fc0c
commit c57eee8661
10 changed files with 105 additions and 80 deletions

View File

@ -20,18 +20,18 @@ var Client = function(config) {
this.host = this.connectionParameters.host; this.host = this.connectionParameters.host;
this.password = this.connectionParameters.password; this.password = this.connectionParameters.password;
config = config || {}; var c = config || {};
this.connection = config.connection || new Connection({ this.connection = c.connection || new Connection({
stream: config.stream, stream: c.stream,
ssl: config.ssl ssl: c.ssl
}); });
this.queryQueue = []; this.queryQueue = [];
this.binary = config.binary || defaults.binary; this.binary = c.binary || defaults.binary;
this.encoding = 'utf8'; this.encoding = 'utf8';
this.processID = null; this.processID = null;
this.secretKey = null; this.secretKey = null;
this.ssl = config.ssl || false; this.ssl = c.ssl || false;
}; };
util.inherits(Client, EventEmitter); util.inherits(Client, EventEmitter);
@ -57,6 +57,7 @@ Client.prototype.connect = function(callback) {
}); });
} }
}); });
con.on('sslconnect', function() { con.on('sslconnect', function() {
con.startup({ con.startup({
user: self.user, user: self.user,
@ -89,10 +90,12 @@ Client.prototype.connect = function(callback) {
con.on('rowDescription', function(msg) { con.on('rowDescription', function(msg) {
self.activeQuery.handleRowDescription(msg); self.activeQuery.handleRowDescription(msg);
}); });
//delegate datarow to active query //delegate datarow to active query
con.on('dataRow', function(msg) { con.on('dataRow', function(msg) {
self.activeQuery.handleDataRow(msg); self.activeQuery.handleDataRow(msg);
}); });
//TODO should query gain access to connection? //TODO should query gain access to connection?
con.on('portalSuspended', function(msg) { con.on('portalSuspended', function(msg) {
self.activeQuery.getRows(con); self.activeQuery.getRows(con);
@ -106,9 +109,11 @@ Client.prototype.connect = function(callback) {
con.sync(); con.sync();
} }
}); });
con.on('copyInResponse', function(msg) { con.on('copyInResponse', function(msg) {
self.activeQuery.streamData(self.connection); self.activeQuery.streamData(self.connection);
}); });
con.on('copyOutResponse', function(msg) { con.on('copyOutResponse', function(msg) {
if(self.activeQuery.stream === undefined) { if(self.activeQuery.stream === undefined) {
self.activeQuery._canceledDueToError = self.activeQuery._canceledDueToError =
@ -119,9 +124,11 @@ Client.prototype.connect = function(callback) {
.cancel(self, self.activeQuery); .cancel(self, self.activeQuery);
} }
}); });
con.on('copyData', function (msg) { con.on('copyData', function (msg) {
self.activeQuery.handleCopyFromChunk(msg.chunk); self.activeQuery.handleCopyFromChunk(msg.chunk);
}); });
if (!callback) { if (!callback) {
self.emit('connect'); self.emit('connect');
} else { } else {
@ -182,8 +189,7 @@ Client.prototype.cancel = function(client, query) {
con.on('connect', function() { con.on('connect', function() {
con.cancel(client.processID, client.secretKey); 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.queryQueue.splice(client.queryQueue.indexOf(query), 1);
} }
}; };
@ -197,15 +203,19 @@ Client.prototype._pulseQueryQueue = function() {
this.activeQuery.submit(this.connection); this.activeQuery.submit(this.connection);
} else if(this.hasExecuted) { } else if(this.hasExecuted) {
this.activeQuery = null; this.activeQuery = null;
if(this._drainPaused > 0) { this._drainPaused++; } //TODO remove pauseDrain for v1.0
else { this.emit('drain'); } if(this._drainPaused > 0) {
this._drainPaused++;
}
else {
this.emit('drain');
}
} }
} }
}; };
Client.prototype._copy = function (text, stream) { Client.prototype._copy = function (text, stream) {
var config = {}, var config = {};
query;
config.text = text; config.text = text;
config.stream = stream; config.stream = stream;
config.callback = function (error) { config.callback = function (error) {
@ -215,7 +225,7 @@ Client.prototype._copy = function (text, stream) {
config.stream.close(); config.stream.close();
} }
}; };
query = new Query(config); var query = new Query(config);
this.queryQueue.push(query); this.queryQueue.push(query);
this._pulseQueryQueue(); this._pulseQueryQueue();
return config.stream; return config.stream;

View File

@ -301,7 +301,10 @@ Connection.prototype.readSslResponse = function() {
this.lastOffset = this.offset; this.lastOffset = this.offset;
return false; return false;
} }
return { name: 'sslresponse', text: this.buffer[this.offset++] }; return {
name: 'sslresponse',
text: this.buffer[this.offset++]
};
}; };
Connection.prototype.parseMessage = function() { Connection.prototype.parseMessage = function() {

View File

@ -2,7 +2,6 @@
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var ConnectionParameters = require(__dirname + '/../connection-parameters'); var ConnectionParameters = require(__dirname + '/../connection-parameters');
var utils = require(__dirname + "/../utils");
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream; var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
var CopyToStream = require(__dirname + '/../copystream').CopyToStream; var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
@ -92,10 +91,11 @@ Connection.prototype.query = function(config, values, callback) {
var nativeCancel = Connection.prototype.cancel; var nativeCancel = Connection.prototype.cancel;
Connection.prototype.cancel = function(client, query) { Connection.prototype.cancel = function(client, query) {
if (client._activeQuery == query) if (client._activeQuery == query) {
this.connect(nativeCancel.bind(client)); this.connect(nativeCancel.bind(client));
else if (client._queryQueue.indexOf(query) != -1) } else if (client._queryQueue.indexOf(query) != -1) {
client._queryQueue.splice(client._queryQueue.indexOf(query), 1); client._queryQueue.splice(client._queryQueue.indexOf(query), 1);
}
}; };
Connection.prototype._pulseQueryQueue = function(initialConnection) { Connection.prototype._pulseQueryQueue = function(initialConnection) {
@ -108,6 +108,7 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) {
var query = this._queryQueue.shift(); var query = this._queryQueue.shift();
if(!query) { if(!query) {
if(!initialConnection) { if(!initialConnection) {
//TODO remove all the pause-drain stuff for v1.0
if(this._drainPaused) { if(this._drainPaused) {
this._drainPaused++; this._drainPaused++;
} else { } else {
@ -125,8 +126,7 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) {
this._namedQueries[query.name] = true; this._namedQueries[query.name] = true;
this._sendPrepare(query.name, query.text, (query.values||[]).length); this._sendPrepare(query.name, query.text, (query.values||[]).length);
} }
} } else if(query.values) {
else if(query.values) {
//call native function //call native function
this._sendQueryWithParams(query.text, query.values); this._sendQueryWithParams(query.text, query.values);
} else { } else {
@ -135,10 +135,12 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) {
} }
}; };
//TODO remove all the pause-drain stuff for v1.0
Connection.prototype.pauseDrain = function() { Connection.prototype.pauseDrain = function() {
this._drainPaused = 1; this._drainPaused = 1;
}; };
//TODO remove all the pause-drain stuff for v1.0
Connection.prototype.resumeDrain = function() { Connection.prototype.resumeDrain = function() {
if(this._drainPaused > 1) { if(this._drainPaused > 1) {
this.emit('drain'); this.emit('drain');
@ -215,10 +217,8 @@ var clientBuilder = function(config) {
}); });
connection.on('copyOutResponse', function(msg) { connection.on('copyOutResponse', function(msg) {
if (connection._activeQuery.stream === undefined) { if (connection._activeQuery.stream === undefined) {
connection._activeQuery._canceledDueToError = connection._activeQuery._canceledDueToError = new Error('No destination stream defined');
new Error('No destination stream defined'); (new clientBuilder({port: connection.port, host: connection.host})).cancel(connection, connection._activeQuery);
(new clientBuilder({port: connection.port, host: connection.host}))
.cancel(connection, connection._activeQuery);
} }
}); });
connection.on('copyData', function (chunk) { connection.on('copyData', function (chunk) {

View File

@ -14,12 +14,12 @@ var NativeQuery = function(config, values, callback) {
EventEmitter.call(this); EventEmitter.call(this);
config = utils.normalizeQueryConfig(config, values, callback); var c = utils.normalizeQueryConfig(config, values, callback);
this.name = config.name; this.name = c.name;
this.text = config.text; this.text = c.text;
this.values = config.values; this.values = c.values;
this.callback = config.callback; this.callback = c.callback;
this._result = new Result(); this._result = new Result();
//normalize values //normalize values
@ -79,8 +79,12 @@ NativeQuery.prototype.handleReadyForQuery = function(meta) {
}; };
NativeQuery.prototype.streamData = function (connection) { NativeQuery.prototype.streamData = function (connection) {
if ( this.stream ) this.stream.startStreamingToConnection(connection); if(this.stream) {
else connection.sendCopyFail('No source stream defined'); this.stream.startStreamingToConnection(connection);
}
else {
connection.sendCopyFail('No source stream defined');
}
}; };
NativeQuery.prototype.handleCopyFromChunk = function (chunk) { NativeQuery.prototype.handleCopyFromChunk = function (chunk) {

View File

@ -11,9 +11,11 @@ function ArrayParser(source, converter) {
}; };
} }
} }
ArrayParser.prototype.eof = function() { ArrayParser.prototype.eof = function() {
return this.pos >= this.source.length; return this.pos >= this.source.length;
}; };
ArrayParser.prototype.nextChar = function() { ArrayParser.prototype.nextChar = function() {
var c; var c;
if ((c = this.source[this.pos++]) === "\\") { if ((c = this.source[this.pos++]) === "\\") {
@ -28,9 +30,11 @@ ArrayParser.prototype.nextChar = function() {
}; };
} }
}; };
ArrayParser.prototype.record = function(c) { ArrayParser.prototype.record = function(c) {
return this.recorded.push(c); return this.recorded.push(c);
}; };
ArrayParser.prototype.newEntry = function(includeEmpty) { ArrayParser.prototype.newEntry = function(includeEmpty) {
var entry; var entry;
if (this.recorded.length > 0 || includeEmpty) { if (this.recorded.length > 0 || includeEmpty) {
@ -45,6 +49,7 @@ ArrayParser.prototype.newEntry = function(includeEmpty) {
this.recorded = []; this.recorded = [];
} }
}; };
ArrayParser.prototype.parse = function(nested) { ArrayParser.prototype.parse = function(nested) {
var c, p, quote; var c, p, quote;
if (nested === null) { if (nested === null) {

View File

@ -1,5 +1,5 @@
var textParsers = require(__dirname + '/textParsers'), var textParsers = require(__dirname + '/textParsers');
binaryParsers = require(__dirname + '/binaryParsers'); var binaryParsers = require(__dirname + '/binaryParsers');
var typeParsers = { var typeParsers = {
text: {}, text: {},

View File

@ -167,6 +167,7 @@ var init = function(register) {
register(21, parseInteger); register(21, parseInteger);
register(23, parseInteger); register(23, parseInteger);
register(26, parseInteger); register(26, parseInteger);
//TODO remove for v1.0
register(1700, function(val){ register(1700, function(val){
if(val.length > maxLen) { if(val.length > maxLen) {
console.warn( console.warn(
@ -175,7 +176,9 @@ var init = function(register) {
} }
return parseFloat(val); return parseFloat(val);
}); });
//TODO remove for v1.0
register(700, parseFloat); register(700, parseFloat);
//TODO remove for v1.0
register(701, parseFloat); register(701, parseFloat);
register(16, parseBool); register(16, parseBool);
register(1082, parseDate); // date register(1082, parseDate); // date