Skip to content

Commit 47591d6

Browse files
author
bmc
committed
callback api working
1 parent ded6c05 commit 47591d6

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

lib/native.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ var NativeQuery = function(text, values, callback) {
119119
this.values = text.values;
120120
} else {
121121
this.text = text;
122-
if(typeof values == 'function') {
123-
this.callback = values;
124-
} else {
125-
this.callback = callback;
126-
this.values = values;
127-
}
122+
this.callback = callback;
123+
this.values = values;
124+
}
125+
if(typeof values == 'function') {
126+
this.values = null;
127+
this.callback = values;
128+
}
129+
if(this.callback) {
130+
this.rows = [];
128131
}
129-
this.rows = [];
130132
EventEmitter.call(this);
131133
this._translateValues();
132134
};
@@ -135,7 +137,6 @@ sys.inherits(NativeQuery, EventEmitter);
135137
var p = NativeQuery.prototype;
136138

137139
p.handleRow = function(row) {
138-
console.log('handling row');
139140
if(this.callback) {
140141
this.rows.push(row);
141142
}
@@ -144,7 +145,7 @@ p.handleRow = function(row) {
144145

145146
p.handleReadyForQuery = function() {
146147
if(this.callback) {
147-
this.callback(null, this.rows);
148+
this.callback(null, { rows: this.rows });
148149
}
149150
this.emit('end');
150151
};

test/native/callback-api-tests.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ var conString = helper.connectionString();
44

55
test('fires callback with results', function() {
66
var client = new Client(conString);
7-
var q = client.query('SELECT 1', assert.calls(function(err, result) {
8-
7+
client.connect();
8+
client.query('SELECT 1 as num', assert.calls(function(err, result) {
9+
assert.isNull(err);
10+
assert.equal(result.rows[0].num, 1);
11+
client.query('SELECT * FROM person WHERE name = $1', ['Brian'], assert.calls(function(err, result) {
12+
assert.isNull(err);
13+
assert.equal(result.rows[0].name, 'Brian');
14+
client.end();
15+
}))
916
}));
10-
q.on('row', function(row) {
11-
console.log(row);
12-
})
1317
})

0 commit comments

Comments
 (0)