initial implementation of providing command execution results

This commit is contained in:
Brian Carlson 2011-01-18 23:03:24 -06:00
parent 1aee4786a0
commit 31b5f82ad0
3 changed files with 30 additions and 10 deletions

View File

@ -36,7 +36,8 @@ p.submit = function(connection) {
} }
var converters = []; var converters = [];
var names = []; var names = [];
var rows = [];
var result = new Result();
var handleRowDescription = function(msg) { var handleRowDescription = function(msg) {
for(var i = 0; i < msg.fields.length; i++) { for(var i = 0; i < msg.fields.length; i++) {
converters[i] = dataTypeParsers[msg.fields[i].dataTypeID] || noParse; converters[i] = dataTypeParsers[msg.fields[i].dataTypeID] || noParse;
@ -44,25 +45,30 @@ p.submit = function(connection) {
}; };
}; };
var handleDatarow = function(msg) { var handleDatarow = function(msg) {
var result = {}; var row = {};
for(var i = 0; i < msg.fields.length; i++) { for(var i = 0; i < msg.fields.length; i++) {
var rawValue = msg.fields[i]; var rawValue = msg.fields[i];
result[names[i]] = rawValue === null ? null : converters[i](rawValue); row[names[i]] = rawValue === null ? null : converters[i](rawValue);
} }
self.emit('row', result); self.emit('row', row);
//if no reciever, buffer rows //if there is a callback collect rows
if(self.callback) { if(self.callback) {
rows.push(result); result.addRow(row);
} }
}; };
var onCommandComplete = function(msg) {
result.addCommandComplete(msg);
};
var onError = function(err) { var onError = function(err) {
//remove all listeners //remove all listeners
connection.removeListener('rowDescription', handleDatarow); connection.removeListener('rowDescription', handleDatarow);
connection.removeListener('dataRow', handleDatarow); connection.removeListener('dataRow', handleDatarow);
connection.removeListener('error', onError); connection.removeListener('error', onError);
connection.removeListener('readyForQuery', onReadyForQuery); connection.removeListener('readyForQuery', onReadyForQuery);
connection.removeListener('commandComplete', onCommandComplete);
if(self.callback) { if(self.callback) {
self.callback(err); self.callback(err);
} else { } else {
@ -77,17 +83,18 @@ p.submit = function(connection) {
connection.removeListener('dataRow', handleDatarow); connection.removeListener('dataRow', handleDatarow);
connection.removeListener('readyForQuery', onReadyForQuery); connection.removeListener('readyForQuery', onReadyForQuery);
connection.removeListener('error', onError); connection.removeListener('error', onError);
connection.removeListener('commandComplete', onCommandComplete);
if(self.callback) { if(self.callback) {
self.callback(null, {rows: rows}); self.callback(null, {rows: rows});
rows = []; rows = [];
} }
self.emit('end'); self.emit('end', result);
}; };
connection.on('rowDescription', handleRowDescription); connection.on('rowDescription', handleRowDescription);
connection.on('dataRow', handleDatarow); connection.on('dataRow', handleDatarow);
connection.on('readyForQuery', onReadyForQuery); connection.on('readyForQuery', onReadyForQuery);
connection.on('commandComplete', onCommandComplete);
connection.on('error', onError); connection.on('error', onError);
}; };

View File

@ -2,11 +2,24 @@
//in the 'end' event and also //in the 'end' event and also
//passed as second argument to provided callback //passed as second argument to provided callback
var Result = function() { var Result = function() {
this.rows = [];
}; };
var p = Result.prototype; var p = Result.prototype;
//adds a command complete message //adds a command complete message
p.addCommandComplete = function(msg) { p.addCommandComplete = function(msg) {
var splitMsg = msg.text.split(' ');
this.commandType = splitMsg.shift();
this.rowCount = splitMsg.pop();
//with INSERT we have oid in the middle
if(splitMsg.length) {
this.oid = splitMsg[0];
}
}; };
p.addRow = function(row) {
this.rows.push(row);
};
module.exports = Result;

View File

@ -23,7 +23,7 @@ con.execute = function(arg) {
executeArg = arg; executeArg = arg;
process.nextTick(function() { process.nextTick(function() {
con.emit('rowData',{ fields: [] }); con.emit('rowData',{ fields: [] });
con.emit('commandComplete'); con.emit('commandComplete', { text: "" });
}); });
}; };