diff --git a/lib/sql.js b/lib/sql.js index 2da4127..f83bd42 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -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; diff --git a/test/sql.test.js b/test/sql.test.js index 28fc7bb..dd89e1e 100644 --- a/test/sql.test.js +++ b/test/sql.test.js @@ -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}]}]});