From dbb07ed9495abbb770b9fb97cb72d060a153a261 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 31 Jul 2019 17:13:53 -0700 Subject: [PATCH] Support non-enumerable properties in object argument to connection.query closes #2253 closes #2254 --- Changes.md | 1 + lib/Connection.js | 12 ++++---- .../test-args-query-like-object-values.js | 29 +++++++++++++++++++ .../unit/query/test-args-query-like-object.js | 29 +++++++++++++++++++ 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 test/unit/query/test-args-query-like-object-values.js create mode 100644 test/unit/query/test-args-query-like-object.js diff --git a/Changes.md b/Changes.md index b36fe34a7..8ffdffbfc 100644 --- a/Changes.md +++ b/Changes.md @@ -12,6 +12,7 @@ you spot any mistakes. * Fix early detection of bad callback to `connection.query` * Support Node.js 12.x #2211 * Support Node.js 13.x +* Support non-enumerable properties in object argument to `connection.query` #2253 * Update `bignumber.js` to 9.0.0 * Update `readable-stream` to 2.3.7 diff --git a/lib/Connection.js b/lib/Connection.js index b3a612389..6802255dd 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -33,22 +33,20 @@ Connection.createQuery = function createQuery(sql, values, callback) { if (typeof sql === 'function') { cb = sql; } else if (typeof sql === 'object') { - for (var prop in sql) { - options[prop] = sql[prop]; - } + options = Object.create(sql); if (typeof values === 'function') { cb = values; } else if (values !== undefined) { - options.values = values; + Object.defineProperty(options, 'values', { value: values }); } } else { - options.sql = sql; - options.values = values; + options.sql = sql; if (typeof values === 'function') { cb = values; - options.values = undefined; + } else if (values !== undefined) { + options.values = values; } } diff --git a/test/unit/query/test-args-query-like-object-values.js b/test/unit/query/test-args-query-like-object-values.js new file mode 100644 index 000000000..28a7368b0 --- /dev/null +++ b/test/unit/query/test-args-query-like-object-values.js @@ -0,0 +1,29 @@ +var assert = require('assert'); +var common = require('../../common'); +var connection = common.createConnection({port: common.fakeServerPort}); + +var server = common.createFakeServer(); +var sqlQuery = Object.create(null, { + sql: { + enumerable : false, + value : 'SELECT ?', + writable : false + }, + values: { + enumerable : false, + value : [42], + writable : false + } +}); + +server.listen(common.fakeServerPort, function (err) { + assert.ifError(err); + + connection.query(sqlQuery, [99], function (err, rows) { + assert.ifError(err); + assert.strictEqual(rows.length, 1); + assert.strictEqual(rows[0]['99'], 99); + connection.destroy(); + server.destroy(); + }); +}); diff --git a/test/unit/query/test-args-query-like-object.js b/test/unit/query/test-args-query-like-object.js new file mode 100644 index 000000000..9b98025bf --- /dev/null +++ b/test/unit/query/test-args-query-like-object.js @@ -0,0 +1,29 @@ +var assert = require('assert'); +var common = require('../../common'); +var connection = common.createConnection({port: common.fakeServerPort}); + +var server = common.createFakeServer(); +var sqlQuery = Object.create(null, { + sql: { + enumerable : false, + value : 'SELECT ?', + writable : false + }, + values: { + enumerable : false, + value : [42], + writable : false + } +}); + +server.listen(common.fakeServerPort, function (err) { + assert.ifError(err); + + connection.query(sqlQuery, function (err, rows) { + assert.ifError(err); + assert.strictEqual(rows.length, 1); + assert.strictEqual(rows[0]['42'], 42); + connection.destroy(); + server.destroy(); + }); +});