不要怂,就是干,撸起袖子干!

Commit 8a8366b4 by Fernando Fabricio dos Santos Committed by Sushant

feat(query-generator): add startsWith, endsWith and substring operators (#9935) (#9999)

1 parent 8ac97efa
...@@ -156,6 +156,9 @@ const Op = Sequelize.Op ...@@ -156,6 +156,9 @@ const Op = Sequelize.Op
[Op.notLike]: '%hat' // NOT LIKE '%hat' [Op.notLike]: '%hat' // NOT LIKE '%hat'
[Op.iLike]: '%hat' // ILIKE '%hat' (case insensitive) (PG only) [Op.iLike]: '%hat' // ILIKE '%hat' (case insensitive) (PG only)
[Op.notILike]: '%hat' // NOT ILIKE '%hat' (PG only) [Op.notILike]: '%hat' // NOT ILIKE '%hat' (PG only)
[Op.startsWith]: 'hat' // LIKE '%hat'
[Op.endsWith]: 'hat' // LIKE 'hat%'
[Op.substring]: 'hat' // LIKE '%hat%'
[Op.regexp]: '^[h|a|t]' // REGEXP/~ '^[h|a|t]' (MySQL/PG only) [Op.regexp]: '^[h|a|t]' // REGEXP/~ '^[h|a|t]' (MySQL/PG only)
[Op.notRegexp]: '^[h|a|t]' // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only) [Op.notRegexp]: '^[h|a|t]' // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)
[Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (PG only) [Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (PG only)
......
...@@ -2428,6 +2428,15 @@ class QueryGenerator { ...@@ -2428,6 +2428,15 @@ class QueryGenerator {
} }
return this._joinKeyValue(key, value.map(identifier => this.quoteIdentifier(identifier)).join('.'), comparator, options.prefix); return this._joinKeyValue(key, value.map(identifier => this.quoteIdentifier(identifier)).join('.'), comparator, options.prefix);
case Op.startsWith:
comparator = this.OperatorMap[Op.like];
return this._joinKeyValue(key, this.escape(`%${value}`), comparator, options.prefix);
case Op.endsWith:
comparator = this.OperatorMap[Op.like];
return this._joinKeyValue(key, this.escape(`${value}%`), comparator, options.prefix);
case Op.substring:
comparator = this.OperatorMap[Op.like];
return this._joinKeyValue(key, this.escape(`%${value}%`), comparator, options.prefix);
} }
const escapeOptions = { const escapeOptions = {
......
...@@ -20,6 +20,9 @@ const OperatorHelpers = { ...@@ -20,6 +20,9 @@ const OperatorHelpers = {
[Op.notLike]: 'NOT LIKE', [Op.notLike]: 'NOT LIKE',
[Op.iLike]: 'ILIKE', [Op.iLike]: 'ILIKE',
[Op.notILike]: 'NOT ILIKE', [Op.notILike]: 'NOT ILIKE',
[Op.startsWith]: 'LIKE',
[Op.endsWith]: 'LIKE',
[Op.substring]: 'LIKE',
[Op.regexp]: '~', [Op.regexp]: '~',
[Op.notRegexp]: '!~', [Op.notRegexp]: '!~',
[Op.iRegexp]: '~*', [Op.iRegexp]: '~*',
...@@ -78,4 +81,4 @@ const OperatorHelpers = { ...@@ -78,4 +81,4 @@ const OperatorHelpers = {
} }
}; };
module.exports = OperatorHelpers; module.exports = OperatorHelpers;
\ No newline at end of file
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
* @property notLike * @property notLike
* @property iLike * @property iLike
* @property notILike * @property notILike
* @property startsWith
* @property endsWith
* @property substring
* @property regexp * @property regexp
* @property notRegexp * @property notRegexp
* @property iRegexp * @property iRegexp
...@@ -57,6 +60,9 @@ const Op = { ...@@ -57,6 +60,9 @@ const Op = {
notLike: Symbol.for('notLike'), notLike: Symbol.for('notLike'),
iLike: Symbol.for('iLike'), iLike: Symbol.for('iLike'),
notILike: Symbol.for('notILike'), notILike: Symbol.for('notILike'),
startsWith: Symbol.for('startsWith'),
endsWith: Symbol.for('endsWith'),
substring: Symbol.for('substring'),
regexp: Symbol.for('regexp'), regexp: Symbol.for('regexp'),
notRegexp: Symbol.for('notRegexp'), notRegexp: Symbol.for('notRegexp'),
iRegexp: Symbol.for('iRegexp'), iRegexp: Symbol.for('iRegexp'),
...@@ -81,4 +87,4 @@ const Op = { ...@@ -81,4 +87,4 @@ const Op = {
join: Symbol.for('join') join: Symbol.for('join')
}; };
module.exports = Op; module.exports = Op;
\ No newline at end of file
...@@ -432,6 +432,33 @@ suite(Support.getTestDialectTeaser('SQL'), () => { ...@@ -432,6 +432,33 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
}); });
}); });
suite('$startsWith', () => {
testsql('username', {
[Op.startsWith]: 'swagger'
}, {
default: "[username] LIKE '%swagger'",
mssql: "[username] LIKE N'%swagger'"
});
});
suite('$endsWith', () => {
testsql('username', {
[Op.endsWith]: 'swagger'
}, {
default: "[username] LIKE 'swagger%'",
mssql: "[username] LIKE N'swagger%'"
});
});
suite('$substring', () => {
testsql('username', {
[Op.substring]: 'swagger'
}, {
default: "[username] LIKE '%swagger%'",
mssql: "[username] LIKE N'%swagger%'"
});
});
suite('$between', () => { suite('$between', () => {
testsql('date', { testsql('date', {
[Op.between]: ['2013-01-01', '2013-01-11'] [Op.between]: ['2013-01-01', '2013-01-11']
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!