Skip to content

Commit

Permalink
have native bindings emit proper result object on 'end' event - closes
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Dec 11, 2012
1 parent 99f1717 commit 102a069
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
15 changes: 8 additions & 7 deletions lib/native/query.js
Expand Up @@ -34,7 +34,7 @@ var NativeQuery = function(text, values, callback) {
this.callback = values;
}
}
this.result = new Result();
this._result = new Result();
//normalize values
if(this.values) {
for(var i = 0, len = this.values.length; i < len; i++) {
Expand All @@ -59,9 +59,9 @@ var mapRowData = function(row) {
p.handleRow = function(rowData) {
var row = mapRowData(rowData);
if(this.callback) {
this.result.addRow(row);
this._result.addRow(row);
}
this.emit('row', row, this.result);
this.emit('row', row, this._result);
};

p.handleError = function(error) {
Expand All @@ -74,12 +74,13 @@ p.handleError = function(error) {
}

p.handleReadyForQuery = function(meta) {
if(meta) {
this._result.addCommandComplete(meta);
}
if(this.callback) {
this.result.command = meta.command.split(' ')[0];
this.result.rowCount = parseInt(meta.value);
this.callback(null, this.result);
this.callback(null, this._result);
}
this.emit('end');
this.emit('end', this._result);
};

module.exports = NativeQuery;
11 changes: 9 additions & 2 deletions lib/result.js
Expand Up @@ -14,12 +14,19 @@ var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/

//adds a command complete message
p.addCommandComplete = function(msg) {
var match = matchRegexp.exec(msg.text);
if(msg.text) {
//pure javascript
var match = matchRegexp.exec(msg.text);
} else {
//native bindings
var match = matchRegexp.exec(msg.command);
}
if(match) {
this.command = match[1];
//match 3 will only be existing on insert commands
if(match[3]) {
this.rowCount = parseInt(match[3]);
//msg.value is from native bindings
this.rowCount = parseInt(match[3] || msg.value);
this.oid = parseInt(match[2]);
} else {
this.rowCount = parseInt(match[2]);
Expand Down
21 changes: 15 additions & 6 deletions test/integration/client/result-metadata-tests.js
Expand Up @@ -4,20 +4,29 @@ var pg = helper.pg;
test('should return insert metadata', function() {
pg.connect(helper.config, 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);
assert.equal(result.oid, null);
assert.equal(result.command, 'CREATE');
client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) {

var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) {
assert.equal(result.command, "INSERT");
assert.equal(result.rowCount, 1);

client.query('SELECT * FROM zugzug', assert.calls(function(err, result) {
assert.isNull(err);
assert.equal(result.rowCount, 1);
assert.equal(result.command, 'SELECT');
process.nextTick(pg.end.bind(pg));
}))
}))
}))
}))
})
}));
}));

assert.emits(q, 'end', function(result) {
assert.equal(result.command, "INSERT");
assert.equal(result.rowCount, 1);
});

}));
}));
});

0 comments on commit 102a069

Please sign in to comment.