diff --git a/packages/pg/lib/native/query.js b/packages/pg/lib/native/query.js index 39dfcc20b..014112685 100644 --- a/packages/pg/lib/native/query.js +++ b/packages/pg/lib/native/query.js @@ -6,12 +6,20 @@ const utils = require('../utils') const NativeQuery = (module.exports = function (config, values, callback) { EventEmitter.call(this) - config = utils.normalizeQueryConfig(config, values, callback) + // Read the config as given instead of normalizing a copy of it. Values or a + // callback passed as arguments win, as in utils.normalizeQueryConfig. + if (typeof config === 'string') { + config = { text: config } + } + if (typeof values === 'function') { + callback = callback || values + values = undefined + } this.text = config.text - this.values = config.values + this.values = values || config.values this.name = config.name this.queryMode = config.queryMode - this.callback = config.callback + this.callback = callback || config.callback this.state = 'new' this._arrayMode = config.rowMode === 'array' diff --git a/packages/pg/lib/query.js b/packages/pg/lib/query.js index 3b93cb5ba..a3d73e8ee 100644 --- a/packages/pg/lib/query.js +++ b/packages/pg/lib/query.js @@ -9,10 +9,18 @@ class Query extends EventEmitter { constructor(config, values, callback) { super() - config = utils.normalizeQueryConfig(config, values, callback) + // Read the config as given instead of normalizing a copy of it. Values or a + // callback passed as arguments win, as in utils.normalizeQueryConfig. + if (typeof config === 'string') { + config = { text: config } + } + if (typeof values === 'function') { + callback = callback || values + values = undefined + } this.text = config.text - this.values = config.values + this.values = values || config.values this.rows = config.rows this.types = config.types this.name = config.name @@ -20,10 +28,10 @@ class Query extends EventEmitter { this.binary = config.binary // use unique portal name each time this.portal = config.portal || '' - this.callback = config.callback + this.callback = callback || config.callback this._rowMode = config.rowMode - if (process.domain && config.callback) { - this.callback = process.domain.bind(config.callback) + if (process.domain && this.callback) { + this.callback = process.domain.bind(this.callback) } this._result = new Result(this._rowMode, this.types) diff --git a/packages/pg/lib/result.js b/packages/pg/lib/result.js index 329fbf9fc..20428f351 100644 --- a/packages/pg/lib/result.js +++ b/packages/pg/lib/result.js @@ -89,11 +89,14 @@ class Result { this._parsers = new Array(fieldDescriptions.length) } - const row = Object.create(null) + // only object rows start from the prebuilt object; array rows don't need it + const row = this.rowAsArray ? null : Object.create(null) for (let i = 0; i < fieldDescriptions.length; i++) { const desc = fieldDescriptions[i] - row[desc.name] = null + if (row !== null) { + row[desc.name] = null + } if (this._types) { this._parsers[i] = this._types.getTypeParser(desc.dataTypeID, desc.format || 'text') @@ -102,7 +105,7 @@ class Result { } } - this._prebuiltEmptyResultObject = { ...row } + this._prebuiltEmptyResultObject = row === null ? null : { ...row } } } diff --git a/packages/pg/test/unit/query-config-tests.js b/packages/pg/test/unit/query-config-tests.js new file mode 100644 index 000000000..1b0fed8eb --- /dev/null +++ b/packages/pg/test/unit/query-config-tests.js @@ -0,0 +1,76 @@ +'use strict' +const helper = require('./test-helper') +const assert = require('assert') +const suite = new helper.Suite() +const test = suite.test.bind(suite) + +const Query = require('../../lib/query') +const NativeQuery = require('../../lib/native/query') + +for (const [label, QueryClass] of [ + ['Query', Query], + ['NativeQuery', NativeQuery], +]) { + test(`${label} reads text, values and callback like normalizeQueryConfig`, function () { + const callback = function () {} + const other = function () {} + let query + + query = new QueryClass('TEXT') + assert.equal(query.text, 'TEXT') + assert.equal(query.values, undefined) + assert.equal(query.callback, undefined) + + query = new QueryClass('TEXT', [10], callback) + assert.equal(query.text, 'TEXT') + assert.deepEqual(query.values, [10]) + assert.equal(query.callback, callback) + + query = new QueryClass({ text: 'TEXT', values: [10] }) + assert.deepEqual(query.values, [10]) + + query = new QueryClass({ text: 'TEXT', values: [10] }, [20]) + assert.deepEqual(query.values, [20]) + + query = new QueryClass({ text: 'TEXT', values: [10] }, callback) + assert.deepEqual(query.values, [10]) + assert.equal(query.callback, callback) + + query = new QueryClass({ text: 'TEXT', callback: other }) + assert.equal(query.callback, other) + + query = new QueryClass({ text: 'TEXT', callback: other }, callback) + assert.equal(query.callback, callback) + + query = new QueryClass({ text: 'TEXT' }, other, callback) + assert.equal(query.values, undefined) + assert.equal(query.callback, callback) + }) + + test(`${label} does not copy or change the caller's config`, function () { + const callback = function () {} + const config = Object.freeze({ text: 'TEXT', rowMode: 'array' }) + + const query = new QueryClass(config, [10], callback) + + assert.deepEqual(config, { text: 'TEXT', rowMode: 'array' }) + assert.deepEqual(query.values, [10]) + assert.equal(query.callback, callback) + }) + + test(`${label} reads config properties through getters and prototypes`, function () { + class QueryConfig { + constructor() { + this._text = 'TEXT' + } + + get text() { + return this._text + } + } + + const query = new QueryClass(new QueryConfig(), [10]) + assert.equal(query.text, 'TEXT') + assert.deepEqual(query.values, [10]) + }) +} diff --git a/packages/pg/test/unit/result-tests.js b/packages/pg/test/unit/result-tests.js index 5135723ed..440cbe950 100644 --- a/packages/pg/test/unit/result-tests.js +++ b/packages/pg/test/unit/result-tests.js @@ -109,3 +109,31 @@ test('multiple dangerous column names handled safely', function () { assert.strictEqual(row['__defineGetter__'], 'd') assert.strictEqual(row.id, 1) }) + +test('array rows do not build the prebuilt row object', function () { + const result = new Result('array') + result.addFields([ + { name: 'id', dataTypeID: 23, format: 'text' }, + { name: 'name', dataTypeID: 25, format: 'text' }, + ]) + + assert.strictEqual(result._prebuiltEmptyResultObject, null) + assert.deepStrictEqual(result.parseRow(['1', 'a']), [1, 'a']) + assert.deepStrictEqual(result.parseRow([null, 'b']), [null, 'b']) + + // a later result set replaces the parsers + result.addFields([{ name: 'ok', dataTypeID: 16, format: 'text' }]) + assert.strictEqual(result._prebuiltEmptyResultObject, null) + assert.deepStrictEqual(result.parseRow(['t']), [true]) +}) + +test('object rows still start from the prebuilt row object', function () { + const result = new Result() + result.addFields([ + { name: 'id', dataTypeID: 23, format: 'text' }, + { name: 'name', dataTypeID: 25, format: 'text' }, + ]) + + assert.deepStrictEqual(result._prebuiltEmptyResultObject, { id: null, name: null }) + assert.deepStrictEqual(result.parseRow(['1', null]), { id: 1, name: null }) +})