Allow case sensitive regex operation
This commit is contained in:
parent
4077ae8864
commit
145d061e94
11
lib/mysql.js
11
lib/mysql.js
|
@ -529,16 +529,23 @@ MySQL.prototype.ping = function(cb) {
|
||||||
MySQL.prototype.buildExpression = function(columnName, operator, operatorValue,
|
MySQL.prototype.buildExpression = function(columnName, operator, operatorValue,
|
||||||
propertyDefinition) {
|
propertyDefinition) {
|
||||||
if (operator === 'regexp') {
|
if (operator === 'regexp') {
|
||||||
|
var clause = columnName + ' REGEXP ?';
|
||||||
|
// By default, MySQL regexp is not case sensitive. (https://dev.mysql.com/doc/refman/5.7/en/regexp.html)
|
||||||
|
// To allow case sensitive regexp query, it has to be binded to a `BINARY` type.
|
||||||
|
// If ignore case is not specified, search it as case sensitive.
|
||||||
|
if (!operatorValue.ignoreCase) {
|
||||||
|
clause = columnName + ' REGEXP BINARY ?';
|
||||||
|
}
|
||||||
|
|
||||||
if (operatorValue.ignoreCase)
|
if (operatorValue.ignoreCase)
|
||||||
g.warn('{{MySQL}} {{regex}} syntax does not respect the {{`i`}} flag');
|
g.warn('{{MySQL}} {{regex}} syntax does not respect the {{`i`}} flag');
|
||||||
|
|
||||||
if (operatorValue.global)
|
if (operatorValue.global)
|
||||||
g.warn('{{MySQL}} {{regex}} syntax does not respect the {{`g`}} flag');
|
g.warn('{{MySQL}} {{regex}} syntax does not respect the {{`g`}} flag');
|
||||||
|
|
||||||
if (operatorValue.multiline)
|
if (operatorValue.multiline)
|
||||||
g.warn('{{MySQL}} {{regex}} syntax does not respect the {{`m`}} flag');
|
g.warn('{{MySQL}} {{regex}} syntax does not respect the {{`m`}} flag');
|
||||||
|
|
||||||
return new ParameterizedSQL(columnName + ' REGEXP ?',
|
return new ParameterizedSQL(clause,
|
||||||
[operatorValue.source]);
|
[operatorValue.source]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -633,6 +633,15 @@ describe('mysql', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('filter with case sensitive regex string', function(done) {
|
||||||
|
Post.find({where: {content: {regexp: '^a'}}}, function(err, posts) {
|
||||||
|
should.not.exist(err);
|
||||||
|
should.exist(posts);
|
||||||
|
posts.length.should.equal(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('with regex literals', function() {
|
context('with regex literals', function() {
|
||||||
|
@ -688,6 +697,16 @@ describe('mysql', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('filter with case sensitive regex literal', function(done) {
|
||||||
|
Post.find({where: {content: {regexp: /^B/}}}, function(err, posts) {
|
||||||
|
should.not.exist(err);
|
||||||
|
should.exist(posts);
|
||||||
|
posts.length.should.equal(1);
|
||||||
|
posts[0].content.should.equal('BBB');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('with regex objects', function() {
|
context('with regex objects', function() {
|
||||||
|
@ -746,6 +765,15 @@ describe('mysql', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('filter with case sensitive regex object', function(done) {
|
||||||
|
Post.find({where: {content: {regexp: new RegExp(/^a/)}}}, function(err, posts) {
|
||||||
|
should.not.exist(err);
|
||||||
|
should.exist(posts);
|
||||||
|
posts.length.should.equal(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue