Skip to content

Commit 8bfd3a5

Browse files
committed
Merge pull request #5 from grncdr/no-row-description
Work with queries that don't have row descriptions
2 parents 37de20e + 8283fd9 commit 8bfd3a5

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

index.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,30 @@ Cursor.prototype.submit = function(connection) {
3232
}, true)
3333

3434
con.flush()
35+
36+
con.once('noData', ifNoData)
37+
con.once('rowDescription', function () {
38+
con.removeListener('noData', ifNoData);
39+
});
40+
41+
function ifNoData () {
42+
self.state = 'idle'
43+
self._shiftQueue();
44+
}
3545
}
3646

37-
Cursor.prototype.handleRowDescription = function(msg) {
38-
this._result.addFields(msg.fields)
39-
this.state = 'idle'
47+
Cursor.prototype._shiftQueue = function () {
4048
if(this._queue.length) {
4149
this._getRows.apply(this, this._queue.shift())
4250
}
4351
}
4452

53+
Cursor.prototype.handleRowDescription = function(msg) {
54+
this._result.addFields(msg.fields)
55+
this.state = 'idle'
56+
this._shiftQueue();
57+
}
58+
4559
Cursor.prototype.handleDataRow = function(msg) {
4660
var row = this._result.parseRow(msg.fields)
4761
this._rows.push(row)
@@ -103,7 +117,7 @@ Cursor.prototype._getRows = function(rows, cb) {
103117
}
104118

105119
Cursor.prototype.end = function(cb) {
106-
if(this.statue != 'initialized') {
120+
if(this.state != 'initialized') {
107121
this.connection.sync()
108122
}
109123
this.connection.end()

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "test"
88
},
99
"scripts": {
10-
"test": "node test/"
10+
"test": "mocha test/"
1111
},
1212
"author": "Brian M. Carlson",
1313
"license": "MIT",

test/no-data-handling.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var assert = require('assert')
2+
var pg = require('pg.js');
3+
var Cursor = require('../');
4+
5+
describe('queries with no data', function () {
6+
beforeEach(function(done) {
7+
var client = this.client = new pg.Client()
8+
client.connect(done)
9+
})
10+
11+
12+
afterEach(function() {
13+
this.client.end()
14+
})
15+
16+
it('handles queries that return no data', function (done) {
17+
var cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
18+
this.client.query(cursor)
19+
cursor.read(100, function (err, rows) {
20+
assert.ifError(err)
21+
assert.equal(rows.length, 0)
22+
done()
23+
})
24+
});
25+
});

0 commit comments

Comments
 (0)