Skip to content

Commit 60c04d9

Browse files
Kirill89sushantdhiman
authored andcommitted
feat(query): add ability to pass arrays as replacements (#9050) (#9054)
1 parent 222b13f commit 60c04d9

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/sql-string.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ const dataTypes = require('./data-types');
44
const util = require('util');
55
const _ = require('lodash');
66

7+
function arrayToList(array, timeZone, dialect, format) {
8+
return array.reduce((sql, val, i) => {
9+
if (i !== 0) {
10+
sql += ', ';
11+
}
12+
if (Array.isArray(val)) {
13+
sql += `(${arrayToList(val, timeZone, dialect, format)})`;
14+
} else {
15+
sql += escape(val, timeZone, dialect, format);
16+
}
17+
return sql;
18+
}, '');
19+
}
20+
exports.arrayToList = arrayToList;
21+
722
function escape(val, timeZone, dialect, format) {
823
let prependN = false;
924
if (val === undefined || val === null) {
@@ -44,7 +59,7 @@ function escape(val, timeZone, dialect, format) {
4459
if (dialect === 'postgres' && !format) {
4560
return dataTypes.ARRAY.prototype.stringify(val, {escape: partialEscape});
4661
}
47-
return val.map(partialEscape);
62+
return arrayToList(val, timeZone, dialect, format);
4863
}
4964

5065
if (!val.replace) {

test/integration/sequelize.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
273273
return this.sequelize.query(this.insertQuery);
274274
});
275275

276+
it('executes a query if a placeholder value is an array', function() {
277+
return this.sequelize.query(`INSERT INTO ${qq(this.User.tableName)} (username, email_address, ` +
278+
`${qq('createdAt')}, ${qq('updatedAt')}) VALUES ?;`, {
279+
replacements: [[
280+
['john', 'john@gmail.com', '2012-01-01 10:10:10', '2012-01-01 10:10:10'],
281+
['michael', 'michael@gmail.com', '2012-01-01 10:10:10', '2012-01-01 10:10:10']
282+
]]
283+
})
284+
.then(() => this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
285+
type: this.sequelize.QueryTypes.SELECT
286+
}))
287+
.then(rows => {
288+
expect(rows).to.be.lengthOf(2);
289+
expect(rows[0].username).to.be.equal('john');
290+
expect(rows[1].username).to.be.equal('michael');
291+
});
292+
});
276293

277294
describe('logging', () => {
278295
it('executes a query with global benchmarking option and default logger', () => {

test/unit/dialects/abstract/query-generator.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ const chai = require('chai'),
66
getAbstractQueryGenerator = require(__dirname + '/../../support').getAbstractQueryGenerator;
77

88
describe('QueryGenerator', () => {
9+
describe('selectQuery', () => {
10+
it('should generate correct query using array placeholder', function() {
11+
const QG = getAbstractQueryGenerator(this.sequelize);
12+
13+
QG.selectQuery('foo', {where: {bar: {[Op.like]: {[Op.any]: ['a', 'b']}}}})
14+
.should.be.equal('SELECT * FROM foo WHERE foo.bar LIKE ANY (\'a\', \'b\');');
15+
});
16+
});
17+
918
describe('whereItemQuery', () => {
1019
it('should generate correct query for Symbol operators', function() {
1120
const QG = getAbstractQueryGenerator(this.sequelize);

0 commit comments

Comments
 (0)