Skip to content

Commit

Permalink
Support non-enumerable properties in object argument to connection.query
Browse files Browse the repository at this point in the history
closes #2253
closes #2254
  • Loading branch information
blakeembrey authored and dougwilson committed Jan 21, 2020
1 parent ca84d0a commit dbb07ed
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 5 additions & 7 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/unit/query/test-args-query-like-object-values.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
29 changes: 29 additions & 0 deletions test/unit/query/test-args-query-like-object.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit dbb07ed

Please sign in to comment.