diff --git a/lib/query.js b/lib/query.js index c92b7b22..5f4f35f2 100644 --- a/lib/query.js +++ b/lib/query.js @@ -85,7 +85,7 @@ p.submit = function(connection) { connection.removeListener('error', onError); connection.removeListener('commandComplete', onCommandComplete); if(self.callback) { - self.callback(null, {rows: rows}); + self.callback(null, result); rows = []; } self.emit('end', result); diff --git a/lib/result.js b/lib/result.js index c8d9fe1d..f46ed418 100644 --- a/lib/result.js +++ b/lib/result.js @@ -7,14 +7,21 @@ var Result = function() { var p = Result.prototype; + +var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/ + //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]; + var match = matchRegexp.exec(msg.text); + if(match) { + this.command = match[1]; + //match 3 will only be existing on insert commands + if(match[3]) { + this.rowCount = parseInt(match[3]); + this.oid = parseInt(match[2]); + } else { + this.rowCount = parseInt(match[2]); + } } }; diff --git a/test/integration/client/result-metadata-tests.js b/test/integration/client/result-metadata-tests.js new file mode 100644 index 00000000..2f466f6f --- /dev/null +++ b/test/integration/client/result-metadata-tests.js @@ -0,0 +1,25 @@ +var helper = require(__dirname + "/test-helper"); +var pg = helper.pg; +var conString = helper.connectionString(); + +pg.connect(conString, assert.calls(function(err, client) { + assert.isNull(err); + client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { + assert.isNull(err); + //let's list this as ignored for now + // process.nextTick(function() { + // test('should identify "CREATE TABLE" message', function() { + // return false; + // assert.equal(result.command, "CREATE TABLE"); + // assert.equal(result.rowCount, 0); + // }) + // }) + assert.equal(result.oid, null); + client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { + assert.equal(result.command, "INSERT"); + assert.equal(result.rowCount, 1); + process.nextTick(client.end.bind(client)); + return false; + })) + })) +})) diff --git a/test/unit/client/result-metadata-tests.js b/test/unit/client/result-metadata-tests.js index 8399ce28..4a04df7f 100644 --- a/test/unit/client/result-metadata-tests.js +++ b/test/unit/client/result-metadata-tests.js @@ -22,11 +22,13 @@ var testForTag = function(tagText, callback) { }) } -var check = function(oid, rowCount, commandType) { +var check = function(oid, rowCount, command) { return function(result) { - assert.equal(result.oid, oid); + if(oid != null) { + assert.equal(result.oid, oid); + } assert.equal(result.rowCount, rowCount); - assert.equal(result.commandType, commandType); + assert.equal(result.command, command); } }