Merge pull request #26 from strongloop/add-regex-support

Add support for RegExp operator
This commit is contained in:
Simon Ho 2015-07-27 15:25:11 -07:00
commit 7f76270940
2 changed files with 75 additions and 0 deletions

View File

@ -712,6 +712,9 @@ SQLConnector.prototype.buildExpression = function(columnName, operator, columnVa
case 'nlike':
sqlExp += ' NOT LIKE ';
break;
case 'regexp':
sqlExp += ' REGEXP ';
break;
}
var stmt = ParameterizedSQL.join([sqlExp, clause], '');
return stmt;

View File

@ -128,6 +128,78 @@ describe('sql connector', function() {
});
});
it('builds where with a regexp string that does not have flags', function() {
var where = connector.buildWhere('customer', {
name: {
regexp: '^J'
}
});
expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` REGEXP ?',
params: ['^J']
});
});
it('builds where with a regexp string that has flags', function() {
var where = connector.buildWhere('customer', {
name: {
regexp: '^J/i'
}
});
expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` REGEXP ?',
params: ['^J/i']
});
});
it('builds where with a regexp literal that does not have flags', function() {
var where = connector.buildWhere('customer', {
name: {
regexp: /^J/
}
});
expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` REGEXP ?',
params: [/^J/]
});
});
it('builds where with a regexp literal that has flags', function() {
var where = connector.buildWhere('customer', {
name: {
regexp: /^J/i
}
});
expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` REGEXP ?',
params: [/^J/i]
});
});
it('builds where with a regexp object that does not have flags', function() {
var where = connector.buildWhere('customer', {
name: {
regexp: new RegExp(/^J/)
}
});
expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` REGEXP ?',
params: [/^J/]
});
});
it('builds where with a regexp object that has flags', function() {
var where = connector.buildWhere('customer', {
name: {
regexp: new RegExp(/^J/i)
}
});
expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` REGEXP ?',
params: [new RegExp(/^J/i)]
});
});
it('builds where with nesting and/or', function() {
var where = connector.buildWhere('customer',
{and: [{name: 'John'}, {or: [{vip: true}, {address: null}]}]});