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

Commit 6fb74c8a by Sushant Committed by GitHub

fix(query-generator): handle literal for substring based operators (#12210)

1 parent 8d9e58b4
...@@ -425,7 +425,7 @@ class QueryGenerator { ...@@ -425,7 +425,7 @@ class QueryGenerator {
* @param {object} incrementAmountsByField A plain-object with attribute-value-pairs * @param {object} incrementAmountsByField A plain-object with attribute-value-pairs
* @param {object} extraAttributesToBeUpdated A plain-object with attribute-value-pairs * @param {object} extraAttributesToBeUpdated A plain-object with attribute-value-pairs
* @param {object} options * @param {object} options
* *
* @private * @private
*/ */
arithmeticQuery(operator, tableName, where, incrementAmountsByField, extraAttributesToBeUpdated, options) { arithmeticQuery(operator, tableName, where, incrementAmountsByField, extraAttributesToBeUpdated, options) {
...@@ -1724,7 +1724,7 @@ class QueryGenerator { ...@@ -1724,7 +1724,7 @@ class QueryGenerator {
if (this.options.minifyAliases && asRight.length > 63) { if (this.options.minifyAliases && asRight.length > 63) {
const alias = `%${topLevelInfo.options.includeAliases.size}`; const alias = `%${topLevelInfo.options.includeAliases.size}`;
topLevelInfo.options.includeAliases.set(alias, asRight); topLevelInfo.options.includeAliases.set(alias, asRight);
} }
...@@ -2593,14 +2593,20 @@ class QueryGenerator { ...@@ -2593,14 +2593,20 @@ 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: case Op.startsWith:
comparator = this.OperatorMap[Op.like];
return this._joinKeyValue(key, this.escape(`${value}%`), comparator, options.prefix);
case Op.endsWith: case Op.endsWith:
comparator = this.OperatorMap[Op.like];
return this._joinKeyValue(key, this.escape(`%${value}`), comparator, options.prefix);
case Op.substring: case Op.substring:
comparator = this.OperatorMap[Op.like]; comparator = this.OperatorMap[Op.like];
return this._joinKeyValue(key, this.escape(`%${value}%`), comparator, options.prefix);
if (value instanceof Utils.Literal) {
value = value.val;
}
let pattern = `${value}%`;
if (prop === Op.endsWith) pattern = `%${value}`;
if (prop === Op.substring) pattern = `%${value}%`;
return this._joinKeyValue(key, this.escape(pattern), comparator, options.prefix);
} }
const escapeOptions = { const escapeOptions = {
......
...@@ -441,6 +441,13 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -441,6 +441,13 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
default: "[username] LIKE 'swagger%'", default: "[username] LIKE 'swagger%'",
mssql: "[username] LIKE N'swagger%'" mssql: "[username] LIKE N'swagger%'"
}); });
testsql('username', {
[Op.startsWith]: current.literal('swagger')
}, {
default: "[username] LIKE 'swagger%'",
mssql: "[username] LIKE N'swagger%'"
});
}); });
describe('Op.endsWith', () => { describe('Op.endsWith', () => {
...@@ -450,6 +457,13 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -450,6 +457,13 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
default: "[username] LIKE '%swagger'", default: "[username] LIKE '%swagger'",
mssql: "[username] LIKE N'%swagger'" mssql: "[username] LIKE N'%swagger'"
}); });
testsql('username', {
[Op.endsWith]: current.literal('swagger')
}, {
default: "[username] LIKE '%swagger'",
mssql: "[username] LIKE N'%swagger'"
});
}); });
describe('Op.substring', () => { describe('Op.substring', () => {
...@@ -459,6 +473,13 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -459,6 +473,13 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
default: "[username] LIKE '%swagger%'", default: "[username] LIKE '%swagger%'",
mssql: "[username] LIKE N'%swagger%'" mssql: "[username] LIKE N'%swagger%'"
}); });
testsql('username', {
[Op.substring]: current.literal('swagger')
}, {
default: "[username] LIKE '%swagger%'",
mssql: "[username] LIKE N'%swagger%'"
});
}); });
describe('Op.between', () => { describe('Op.between', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!