Skip to content

Commit 4f8cce4

Browse files
committed
Merge pull request #209 from /issues/209
Feature request: access field names and types even when NO rows are returned
2 parents b630871 + 337d49d commit 4f8cce4

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Query.prototype.handleRowDescription = function(msg) {
6363
var format = field.format;
6464
this._fieldNames[i] = field.name;
6565
this._fieldConverters[i] = Types.getTypeParser(field.dataTypeID, format);
66+
this._result.addField(field);
6667
}
6768
};
6869

lib/result.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Result = function() {
66
this.rowCount = null;
77
this.oid = null;
88
this.rows = [];
9+
this.fields = [];
910
};
1011

1112
var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/;
@@ -37,4 +38,9 @@ Result.prototype.addRow = function(row) {
3738
this.rows.push(row);
3839
};
3940

41+
//Add a field definition to the result
42+
Result.prototype.addField = function(field) {
43+
this.fields.push(field);
44+
};
45+
4046
module.exports = Result;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var helper = require(__dirname + '/test-helper');
2+
var pg = helper.pg;
3+
var config = helper.config;
4+
5+
test('can access results when no rows are returned', function() {
6+
if(config.native) return false;
7+
var checkResult = function(result) {
8+
assert(result.fields, 'should have fields definition');
9+
assert.equal(result.fields.length, 1);
10+
assert.equal(result.fields[0].name, 'val');
11+
assert.equal(result.fields[0].dataTypeID, 25);
12+
pg.end();
13+
};
14+
15+
pg.connect(config, assert.success(function(client, done) {
16+
var query = client.query('select $1::text as val limit 0', ['hi'], assert.success(function(result) {
17+
checkResult(result);
18+
done();
19+
}));
20+
21+
assert.emits(query, 'end', checkResult);
22+
}));
23+
});

0 commit comments

Comments
 (0)