mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
pass result object to native query 'row' event - closes #183
This commit is contained in:
parent
6640271f53
commit
886926a777
@ -133,11 +133,9 @@ var clientBuilder = function(config) {
|
||||
});
|
||||
|
||||
connection.on('_cmdStatus', function(status) {
|
||||
var meta = {
|
||||
};
|
||||
meta.command = status.command.split(' ')[0];
|
||||
meta.rowCount = parseInt(status.value);
|
||||
connection._lastMeta = meta;
|
||||
//set this here so we can pass it to the query
|
||||
//when the query completes
|
||||
connection._lastMeta = status;
|
||||
});
|
||||
|
||||
//TODO: emit more native error properties (make it match js error)
|
||||
|
||||
@ -3,10 +3,18 @@ var util = require('util');
|
||||
|
||||
var types = require(__dirname + '/../types');
|
||||
var utils = require(__dirname + '/../utils');
|
||||
var Result = require(__dirname + '/../result');
|
||||
|
||||
//event emitter proxy
|
||||
var NativeQuery = function(text, values, callback) {
|
||||
//TODO there are better ways to detect overloads
|
||||
EventEmitter.call(this);
|
||||
|
||||
this.text = null;
|
||||
this.values = null;
|
||||
this.callback = null;
|
||||
this.name = null;
|
||||
|
||||
//allow 'config object' as first parameter
|
||||
if(typeof text == 'object') {
|
||||
this.text = text.text;
|
||||
this.values = text.values;
|
||||
@ -26,17 +34,13 @@ var NativeQuery = function(text, values, callback) {
|
||||
this.callback = values;
|
||||
}
|
||||
}
|
||||
if(this.callback) {
|
||||
this.rows = [];
|
||||
}
|
||||
this.result = new Result();
|
||||
//normalize values
|
||||
if(this.values) {
|
||||
for(var i = 0, len = this.values.length; i < len; i++) {
|
||||
this.values[i] = utils.prepareValue(this.values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
EventEmitter.call(this);
|
||||
};
|
||||
|
||||
util.inherits(NativeQuery, EventEmitter);
|
||||
@ -55,9 +59,9 @@ var mapRowData = function(row) {
|
||||
p.handleRow = function(rowData) {
|
||||
var row = mapRowData(rowData);
|
||||
if(this.callback) {
|
||||
this.rows.push(row);
|
||||
this.result.addRow(row);
|
||||
}
|
||||
this.emit('row', row);
|
||||
this.emit('row', row, this.result);
|
||||
};
|
||||
|
||||
p.handleError = function(error) {
|
||||
@ -71,8 +75,9 @@ p.handleError = function(error) {
|
||||
|
||||
p.handleReadyForQuery = function(meta) {
|
||||
if(this.callback) {
|
||||
(meta || {}).rows = this.rows;
|
||||
this.callback(null, meta);
|
||||
this.result.command = meta.command.split(' ')[0];
|
||||
this.result.rowCount = parseInt(meta.value);
|
||||
this.callback(null, this.result);
|
||||
}
|
||||
this.emit('end');
|
||||
};
|
||||
|
||||
@ -2,12 +2,14 @@
|
||||
//in the 'end' event and also
|
||||
//passed as second argument to provided callback
|
||||
var Result = function() {
|
||||
this.command = null;
|
||||
this.rowCount = null;
|
||||
this.oid = null;
|
||||
this.rows = [];
|
||||
};
|
||||
|
||||
var p = Result.prototype;
|
||||
|
||||
|
||||
var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/
|
||||
|
||||
//adds a command complete message
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user