Skip to content

Commit

Permalink
revert "fix(query-interface): incorrect regex escape with json queryi…
Browse files Browse the repository at this point in the history
…ng (#10615)" (#10623)

This reverts commit 674db19.
  • Loading branch information
sushantdhiman committed Mar 26, 2019
1 parent ef18710 commit d81ea5e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
9 changes: 4 additions & 5 deletions lib/dialects/abstract/query-generator.js
Expand Up @@ -19,8 +19,6 @@ const sequelizeError = require('../../errors');

const QuoteHelper = require('./query-generator/helpers/quote');

const nonEscapeOperators = new Set([Op.like, Op.iLike, Op.regexp, Op.iRegexp, Op.notRegexp, Op.notIRegexp]);

/**
* Abstract Query Generator
*
Expand Down Expand Up @@ -952,7 +950,7 @@ class QueryGenerator {
// Users shouldn't have to worry about these args - just give them a function that takes a single arg
const simpleEscape = escVal => SqlString.escape(escVal, this.options.timezone, this.dialect);

value = field.type.stringify(value, { escape: simpleEscape, field, timezone: this.options.timezone, acceptStrings: options.acceptStrings });
value = field.type.stringify(value, { escape: simpleEscape, field, timezone: this.options.timezone, operation: options.operation });

if (field.type.escape === false) {
// The data-type already did the required escaping
Expand Down Expand Up @@ -987,7 +985,7 @@ class QueryGenerator {
this.validate(value, field, options);

if (field.type.bindParam) {
return field.type.bindParam(value, { escape: _.identity, field, timezone: this.options.timezone, bindParam });
return field.type.bindParam(value, { escape: _.identity, field, timezone: this.options.timezone, operation: options.operation, bindParam });
}
}
}
Expand Down Expand Up @@ -2393,8 +2391,9 @@ class QueryGenerator {
comparator = this.OperatorMap[Op.like];
return this._joinKeyValue(key, this.escape(`%${value}%`), comparator, options.prefix);
}

const escapeOptions = {
acceptStrings: nonEscapeOperators.has(prop)
acceptStrings: comparator.includes(this.OperatorMap[Op.like])
};

if (_.isPlainObject(value)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/mariadb/data-types.js
Expand Up @@ -105,7 +105,7 @@ module.exports = BaseTypes => {

class JSONTYPE extends BaseTypes.JSON {
_stringify(value, options) {
return options.acceptsString && typeof value === 'string' ? value
return options.operation === 'where' && typeof value === 'string' ? value
: JSON.stringify(value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/mysql/data-types.js
Expand Up @@ -124,7 +124,7 @@ module.exports = BaseTypes => {

class JSONTYPE extends BaseTypes.JSON {
_stringify(value, options) {
return options.acceptStrings && typeof value === 'string' ? value : JSON.stringify(value);
return options.operation === 'where' && typeof value === 'string' ? value : JSON.stringify(value);
}
}

Expand Down
37 changes: 8 additions & 29 deletions test/integration/operators.test.js
@@ -1,13 +1,13 @@
'use strict';

const { stub } = require('sinon');
const { expect } = require('chai');
const Sequelize = require('../../index');
const Op = Sequelize.Op;
const Promise = Sequelize.Promise;
const Support = require('../support');
const DataTypes = require('../../lib/data-types');
const dialect = Support.getTestDialect();
const chai = require('chai'),
Sequelize = require('../../index'),
Op = Sequelize.Op,
Promise = Sequelize.Promise,
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../lib/data-types'),
dialect = Support.getTestDialect();

describe(Support.getTestDialectTeaser('Operators'), () => {
describe('REGEXP', () => {
Expand All @@ -23,9 +23,6 @@ describe(Support.getTestDialectTeaser('Operators'), () => {
name: {
type: DataTypes.STRING,
field: 'full_name'
},
json: {
type: DataTypes.JSON
}
}, {
tableName: 'users',
Expand All @@ -42,9 +39,6 @@ describe(Support.getTestDialectTeaser('Operators'), () => {
},
full_name: {
type: DataTypes.STRING
},
json: {
type: DataTypes.JSON
}
})
]);
Expand Down Expand Up @@ -84,21 +78,6 @@ describe(Support.getTestDialectTeaser('Operators'), () => {
});
});

it('should work with json', function() {
const logging = stub();
return this.User.findOne({
logging,
where: {
json: {
[Op.regexp]: 'test'
}
}
})
.then(() => {
expect(logging.firstCall.args[0]).to.not.include('\\"test\\"');
});
});

it('should properly escape regular expressions', function() {
return this.User.bulkCreate([{
name: 'John'
Expand Down

0 comments on commit d81ea5e

Please sign in to comment.