Skip to content

Commit

Permalink
fix(query-generator): convert numbers to date types (#10569)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchick authored and sushantdhiman committed Mar 26, 2019
1 parent b0da59b commit b6f04ac
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/dialects/abstract/query-generator.js
Expand Up @@ -2365,7 +2365,7 @@ class QueryGenerator {
return this._joinKeyValue(key, `(${this.escape(value, field)})`, comparator, options.prefix);
case Op.between:
case Op.notBetween:
return this._joinKeyValue(key, `${this.escape(value[0])} AND ${this.escape(value[1])}`, comparator, options.prefix);
return this._joinKeyValue(key, `${this.escape(value[0], field)} AND ${this.escape(value[1], field)}`, comparator, options.prefix);
case Op.raw:
throw new Error('The `$raw` where property is no longer supported. Use `sequelize.literal` instead.');
case Op.col:
Expand Down
86 changes: 55 additions & 31 deletions test/unit/sql/where.test.js
Expand Up @@ -118,7 +118,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
mssql: '[deleted] IS NULL'
});

describe('$in', () => {
describe('Op.in', () => {
testsql('equipment', {
[Op.in]: [1, 3]
}, {
Expand Down Expand Up @@ -156,7 +156,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$not', () => {
describe('Op.not', () => {
testsql('deleted', {
[Op.not]: true
}, {
Expand All @@ -178,7 +178,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$notIn', () => {
describe('Op.notIn', () => {
testsql('equipment', {
[Op.notIn]: []
}, {
Expand All @@ -200,7 +200,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$ne', () => {
describe('Op.ne', () => {
testsql('email', {
[Op.ne]: 'jack.bauer@gmail.com'
}, {
Expand All @@ -209,8 +209,8 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$and/$or/$not', () => {
describe('$or', () => {
describe('Op.and/Op.or/Op.not', () => {
describe('Op.or', () => {
testsql('email', {
[Op.or]: ['maker@mhansen.io', 'janzeh@gmail.com']
}, {
Expand Down Expand Up @@ -292,7 +292,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$and', () => {
describe('Op.and', () => {
testsql(Op.and, {
[Op.or]: {
group_id: 1,
Expand Down Expand Up @@ -345,7 +345,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$not', () => {
describe('Op.not', () => {
testsql(Op.not, {
[Op.or]: {
group_id: 1,
Expand All @@ -366,7 +366,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$col', () => {
describe('Op.col', () => {
testsql('userId', {
[Op.col]: 'user.id'
}, {
Expand Down Expand Up @@ -409,7 +409,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$gt', () => {
describe('Op.gt', () => {
testsql('rank', {
[Op.gt]: 2
}, {
Expand All @@ -425,7 +425,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$like', () => {
describe('Op.like', () => {
testsql('username', {
[Op.like]: '%swagger'
}, {
Expand All @@ -434,7 +434,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$startsWith', () => {
describe('Op.startsWith', () => {
testsql('username', {
[Op.startsWith]: 'swagger'
}, {
Expand All @@ -443,7 +443,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$endsWith', () => {
describe('Op.endsWith', () => {
testsql('username', {
[Op.endsWith]: 'swagger'
}, {
Expand All @@ -452,7 +452,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$substring', () => {
describe('Op.substring', () => {
testsql('username', {
[Op.substring]: 'swagger'
}, {
Expand All @@ -461,14 +461,38 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$between', () => {
describe('Op.between', () => {
testsql('date', {
[Op.between]: ['2013-01-01', '2013-01-11']
}, {
default: "[date] BETWEEN '2013-01-01' AND '2013-01-11'",
mssql: "[date] BETWEEN N'2013-01-01' AND N'2013-01-11'"
});

testsql('date', {
[Op.between]: [new Date('2013-01-01'), new Date('2013-01-11')]
}, {
default: "[date] BETWEEN '2013-01-01 00:00:00.000 +00:00' AND '2013-01-11 00:00:00.000 +00:00'",
mysql: "`date` BETWEEN '2013-01-01 00:00:00' AND '2013-01-11 00:00:00'",
mariadb: "`date` BETWEEN '2013-01-01 00:00:00.000' AND '2013-01-11 00:00:00.000'"
});

testsql('date', {
[Op.between]: [1356998400000, 1357862400000]
}, {
model: {
rawAttributes: {
date: {
type: new DataTypes.DATE()
}
}
}
},
{
default: "[date] BETWEEN '2013-01-01 00:00:00.000 +00:00' AND '2013-01-11 00:00:00.000 +00:00'",
mssql: "[date] BETWEEN N'2013-01-01 00:00:00.000 +00:00' AND N'2013-01-11 00:00:00.000 +00:00'"
});

testsql('date', {
[Op.between]: ['2012-12-10', '2013-01-02'],
[Op.notBetween]: ['2013-01-04', '2013-01-20']
Expand All @@ -478,7 +502,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$notBetween', () => {
describe('Op.notBetween', () => {
testsql('date', {
[Op.notBetween]: ['2013-01-01', '2013-01-11']
}, {
Expand All @@ -489,7 +513,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {

if (current.dialect.supports.ARRAY) {
describe('ARRAY', () => {
describe('$contains', () => {
describe('Op.contains', () => {
testsql('muscles', {
[Op.contains]: [2, 3]
}, {
Expand All @@ -513,15 +537,15 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$overlap', () => {
describe('Op.overlap', () => {
testsql('muscles', {
[Op.overlap]: [3, 11]
}, {
postgres: '"muscles" && ARRAY[3,11]'
});
});

describe('$any', () => {
describe('Op.any', () => {
testsql('userId', {
[Op.any]: [4, 5, 6]
}, {
Expand All @@ -538,7 +562,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
postgres: '"userId" = ANY (ARRAY[2,5]::INTEGER[])'
});

describe('$values', () => {
describe('Op.values', () => {
testsql('userId', {
[Op.any]: {
[Op.values]: [4, 5, 6]
Expand All @@ -561,7 +585,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$all', () => {
describe('Op.all', () => {
testsql('userId', {
[Op.all]: [4, 5, 6]
}, {
Expand All @@ -578,7 +602,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
postgres: '"userId" = ALL (ARRAY[2,5]::INTEGER[])'
});

describe('$values', () => {
describe('Op.values', () => {
testsql('userId', {
[Op.all]: {
[Op.values]: [4, 5, 6]
Expand All @@ -601,7 +625,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$like', () => {
describe('Op.like', () => {
testsql('userId', {
[Op.like]: {
[Op.any]: ['foo', 'bar', 'baz']
Expand Down Expand Up @@ -1064,7 +1088,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
}

if (current.dialect.supports.REGEXP) {
describe('$regexp', () => {
describe('Op.regexp', () => {
testsql('username', {
[Op.regexp]: '^sw.*r$'
}, {
Expand All @@ -1074,7 +1098,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$regexp', () => {
describe('Op.regexp', () => {
testsql('newline', {
[Op.regexp]: '^new\nline$'
}, {
Expand All @@ -1084,7 +1108,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$notRegexp', () => {
describe('Op.notRegexp', () => {
testsql('username', {
[Op.notRegexp]: '^sw.*r$'
}, {
Expand All @@ -1094,7 +1118,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});
});

describe('$notRegexp', () => {
describe('Op.notRegexp', () => {
testsql('newline', {
[Op.notRegexp]: '^new\nline$'
}, {
Expand All @@ -1105,31 +1129,31 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});

if (current.dialect.name === 'postgres') {
describe('$iRegexp', () => {
describe('Op.iRegexp', () => {
testsql('username', {
[Op.iRegexp]: '^sw.*r$'
}, {
postgres: '"username" ~* \'^sw.*r$\''
});
});

describe('$iRegexp', () => {
describe('Op.iRegexp', () => {
testsql('newline', {
[Op.iRegexp]: '^new\nline$'
}, {
postgres: '"newline" ~* \'^new\nline$\''
});
});

describe('$notIRegexp', () => {
describe('Op.notIRegexp', () => {
testsql('username', {
[Op.notIRegexp]: '^sw.*r$'
}, {
postgres: '"username" !~* \'^sw.*r$\''
});
});

describe('$notIRegexp', () => {
describe('Op.notIRegexp', () => {
testsql('newline', {
[Op.notIRegexp]: '^new\nline$'
}, {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/utils.test.js
Expand Up @@ -144,7 +144,7 @@ describe(Support.getTestDialectTeaser('Utils'), () => {
});
});

it('$or where', () => {
it('Op.or where', () => {
expect(Utils.mapOptionFieldNames({
where: {
[Op.or]: {
Expand All @@ -171,7 +171,7 @@ describe(Support.getTestDialectTeaser('Utils'), () => {
});
});

it('$or[] where', () => {
it('Op.or[] where', () => {
expect(Utils.mapOptionFieldNames({
where: {
[Op.or]: [
Expand Down

0 comments on commit b6f04ac

Please sign in to comment.