callback api failures

This commit is contained in:
brianc 2011-02-28 23:09:09 -06:00
parent 128dbcb84c
commit ded6c05ed6
2 changed files with 31 additions and 7 deletions

View File

@ -46,8 +46,8 @@ p.connect = function() {
}) })
} }
p.query = function(config, values) { p.query = function(config, values, callback) {
var q = new NativeQuery(config, values); var q = new NativeQuery(config, values, callback);
this._queryQueue.push(q); this._queryQueue.push(q);
this._pulseQueryQueue(); this._pulseQueryQueue();
return q; return q;
@ -87,7 +87,7 @@ var ctor = function(config) {
//proxy some events to active query //proxy some events to active query
connection.on('_row', function(row) { connection.on('_row', function(row) {
connection._activeQuery.emit('row', row); connection._activeQuery.handleRow(row);
}) })
connection.on('_error', function(err) { connection.on('_error', function(err) {
if(connection._activeQuery) { if(connection._activeQuery) {
@ -97,7 +97,7 @@ var ctor = function(config) {
} }
}) })
connection.on('_readyForQuery', function() { connection.on('_readyForQuery', function() {
connection._activeQuery.emit('end'); connection._activeQuery.handleReadyForQuery();
connection._activeQuery = null; connection._activeQuery = null;
connection._pulseQueryQueue(); connection._pulseQueryQueue();
}); });
@ -113,14 +113,20 @@ var connect = function(config, callback) {
}; };
//event emitter proxy //event emitter proxy
var NativeQuery = function(text, values) { var NativeQuery = function(text, values, callback) {
if(typeof text == 'object') { if(typeof text == 'object') {
this.text = text.text; this.text = text.text;
this.values = text.values; this.values = text.values;
} else { } else {
this.text = text; this.text = text;
this.values = values; if(typeof values == 'function') {
this.callback = values;
} else {
this.callback = callback;
this.values = values;
}
} }
this.rows = [];
EventEmitter.call(this); EventEmitter.call(this);
this._translateValues(); this._translateValues();
}; };
@ -128,6 +134,21 @@ var NativeQuery = function(text, values) {
sys.inherits(NativeQuery, EventEmitter); sys.inherits(NativeQuery, EventEmitter);
var p = NativeQuery.prototype; var p = NativeQuery.prototype;
p.handleRow = function(row) {
console.log('handling row');
if(this.callback) {
this.rows.push(row);
}
this.emit('row', row);
};
p.handleReadyForQuery = function() {
if(this.callback) {
this.callback(null, this.rows);
}
this.emit('end');
};
//translates values into strings //translates values into strings
p._translateValues = function() { p._translateValues = function() {
if(this.values) { if(this.values) {

View File

@ -4,7 +4,10 @@ var conString = helper.connectionString();
test('fires callback with results', function() { test('fires callback with results', function() {
var client = new Client(conString); var client = new Client(conString);
client.query('SELECT 1', assert.calls(function(result) { var q = client.query('SELECT 1', assert.calls(function(err, result) {
})); }));
q.on('row', function(row) {
console.log(row);
})
}) })