diff --git a/packages/pg-native/lib/build-result.js b/packages/pg-native/lib/build-result.js index 9117a11ef..8c5d02010 100644 --- a/packages/pg-native/lib/build-result.js +++ b/packages/pg-native/lib/build-result.js @@ -14,8 +14,11 @@ class Result { } consumeCommand(pq) { - this.command = pq.cmdStatus().split(' ')[0] - this.rowCount = parseInt(pq.cmdTuples(), 10) + // null when there is none, as pg reports it: BEGIN has no row count, an empty query no command + const status = pq.cmdStatus() + this.command = status ? status.split(' ')[0] : null + const tuples = pq.cmdTuples() + this.rowCount = tuples ? parseInt(tuples, 10) : null } consumeFields(pq) { diff --git a/packages/pg-native/test/empty-query.js b/packages/pg-native/test/empty-query.js index aa3f05a0d..9fd73c64d 100644 --- a/packages/pg-native/test/empty-query.js +++ b/packages/pg-native/test/empty-query.js @@ -13,4 +13,22 @@ describe('empty query', () => { client.end(done) }) }) + + // what pg reports too: a command without a row count and an empty query without a command + // are null, not NaN and an empty string + it('reports no row count and no command as null', (done) => { + const client = new Client() + client.connectSync() + client.query('BEGIN', (err, rows, res) => { + assert(!err) + assert.strictEqual(res.command, 'BEGIN') + assert.strictEqual(res.rowCount, null) + client.query('', (err, rows, res) => { + assert(!err) + assert.strictEqual(res.command, null) + assert.strictEqual(res.rowCount, null) + client.end(done) + }) + }) + }) })