diff --git a/lib/query.js b/lib/query.js index 52229642..c92b7b22 100644 --- a/lib/query.js +++ b/lib/query.js @@ -36,7 +36,8 @@ p.submit = function(connection) { } var converters = []; var names = []; - var rows = []; + + var result = new Result(); var handleRowDescription = function(msg) { for(var i = 0; i < msg.fields.length; i++) { converters[i] = dataTypeParsers[msg.fields[i].dataTypeID] || noParse; @@ -44,25 +45,30 @@ p.submit = function(connection) { }; }; var handleDatarow = function(msg) { - var result = {}; + var row = {}; for(var i = 0; i < msg.fields.length; 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) { - rows.push(result); + result.addRow(row); } }; + var onCommandComplete = function(msg) { + result.addCommandComplete(msg); + }; + var onError = function(err) { //remove all listeners connection.removeListener('rowDescription', handleDatarow); connection.removeListener('dataRow', handleDatarow); connection.removeListener('error', onError); connection.removeListener('readyForQuery', onReadyForQuery); + connection.removeListener('commandComplete', onCommandComplete); if(self.callback) { self.callback(err); } else { @@ -77,17 +83,18 @@ p.submit = function(connection) { connection.removeListener('dataRow', handleDatarow); connection.removeListener('readyForQuery', onReadyForQuery); connection.removeListener('error', onError); + connection.removeListener('commandComplete', onCommandComplete); if(self.callback) { self.callback(null, {rows: rows}); rows = []; } - self.emit('end'); + self.emit('end', result); }; connection.on('rowDescription', handleRowDescription); connection.on('dataRow', handleDatarow); connection.on('readyForQuery', onReadyForQuery); - + connection.on('commandComplete', onCommandComplete); connection.on('error', onError); }; diff --git a/lib/result.js b/lib/result.js index 6f397adc..c8d9fe1d 100644 --- a/lib/result.js +++ b/lib/result.js @@ -2,11 +2,24 @@ //in the 'end' event and also //passed as second argument to provided callback var Result = function() { + this.rows = []; }; var p = Result.prototype; //adds a command complete message 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; diff --git a/test/unit/client/prepared-statement-tests.js b/test/unit/client/prepared-statement-tests.js index 209e3464..2d3b77b9 100644 --- a/test/unit/client/prepared-statement-tests.js +++ b/test/unit/client/prepared-statement-tests.js @@ -23,7 +23,7 @@ con.execute = function(arg) { executeArg = arg; process.nextTick(function() { con.emit('rowData',{ fields: [] }); - con.emit('commandComplete'); + con.emit('commandComplete', { text: "" }); }); };