have native bindings emit proper result object on 'end' event - closes #219

This commit is contained in:
brianc 2012-12-10 23:25:26 -06:00
parent 99f1717ba8
commit 102a069bd2
3 changed files with 33 additions and 16 deletions

View File

@ -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++) {
@ -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) {
@ -74,12 +74,13 @@ p.handleError = function(error) {
}
p.handleReadyForQuery = function(meta) {
if(this.callback) {
this.result.command = meta.command.split(' ')[0];
this.result.rowCount = parseInt(meta.value);
this.callback(null, this.result);
if(meta) {
this._result.addCommandComplete(meta);
}
this.emit('end');
if(this.callback) {
this.callback(null, this._result);
}
this.emit('end', this._result);
};
module.exports = NativeQuery;

View File

@ -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]);

View File

@ -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);
});
}));
}));
});