From 56b70b26cb748d310b7540b7e74c04ade6ceb562 Mon Sep 17 00:00:00 2001 From: "Neil A. Beardsley" Date: Fri, 8 Nov 2013 00:13:57 -0800 Subject: [PATCH 1/2] Adds support for REGEXP in WHERE statements. --- lib/model.js | 6 +++++- lib/query.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index 5194867..8e05409 100644 --- a/lib/model.js +++ b/lib/model.js @@ -134,7 +134,11 @@ Model.find = function (query, fields, options, callback) { Object.keys(query).forEach(function (key) { if(_.isArray(query[key])) { q.where(key).in(query[key]); - } else { + } + else if ((_.isObject(query[key])) && query[key]['$regex']) { + q.where(key).regex(query[key]['$regex']); + } + else { q.where(key).equals(query[key]); } }); diff --git a/lib/query.js b/lib/query.js index 2a0641c..3581593 100644 --- a/lib/query.js +++ b/lib/query.js @@ -444,6 +444,14 @@ Query.prototype.in = function () { return this; }; +Query.prototype.regex = function (regex) { + this.data.stmt.op = 'REGEXP'; + // Strip the forward slashes from the JS RegExp object + regex = regex.source; + this.data.stmt.value = regex; + return this; +} + Query.prototype.like = function (value) { this.data.stmt.op = 'LIKE'; this.data.stmt.value = value; @@ -493,6 +501,10 @@ var opPatterns = { return '('+values.map(function() { return '?'; }).join(',')+')'; }, + 'REGEXP': function (regex) { + return "'" + regex + "'"; + }, + 'IS': function () { return 'NULL'; } From 4316f9168c0d5273094db8da2a24372e2d208bb9 Mon Sep 17 00:00:00 2001 From: "Neil A. Beardsley" Date: Wed, 20 Nov 2013 00:21:09 -0800 Subject: [PATCH 2/2] Adds regex unit test. --- lib/query.js | 2 +- test/query-select-regexp.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/query-select-regexp.js diff --git a/lib/query.js b/lib/query.js index 3581593..7c11587 100644 --- a/lib/query.js +++ b/lib/query.js @@ -502,7 +502,7 @@ var opPatterns = { }, 'REGEXP': function (regex) { - return "'" + regex + "'"; + return '?'; }, 'IS': function () { diff --git a/test/query-select-regexp.js b/test/query-select-regexp.js new file mode 100644 index 0000000..3a69b26 --- /dev/null +++ b/test/query-select-regexp.js @@ -0,0 +1,31 @@ +var Query = require('../lib/query'); +var assert = require('assert'); +var seed = require('./fixtures/seed'); +var db = seed.db; +require('mocha'); + +describe('Select Query', function () { + describe('regexp', function () { + var expectedSQL = + "SELECT "+ + "`users`.`id` AS users_id, "+ + "`users`.`name` AS users_name "+ + "FROM `users` "+ + "WHERE `users`.`name` REGEXP ?"; + var expectedValues= ['.*[Ss]mith.*']; + + it('should generate the proper SQL', function () { + var query = new Query(db.model('User')); + var regex = new RegExp('.*[Ss]mith.*'); + query + .select('name') + .where('name') + .regex(regex); + var sql = query.toString(); + assert.equal(expectedSQL, sql); + assert.deepEqual(expectedValues, query.values()); + }); + + }); +}); +