From f44dddf55968b276a59a38fc0f54084107e11052 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 15 Dec 2016 13:48:38 +0330 Subject: [PATCH 1/4] Add ILIKE functionality --- lib/postgresql.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/postgresql.js b/lib/postgresql.js index a947a0ee..f7013bb2 100644 --- a/lib/postgresql.js +++ b/lib/postgresql.js @@ -386,9 +386,15 @@ PostgreSQL.prototype.buildExpression = function(columnName, operator, case 'like': return new ParameterizedSQL(columnName + " LIKE ? ESCAPE E'\\\\'", [operatorValue]); + case 'ilike': + return new ParameterizedSQL(columnName + " ILIKE ? ESCAPE '\\'", + [operatorValue]); case 'nlike': return new ParameterizedSQL(columnName + " NOT LIKE ? ESCAPE E'\\\\'", [operatorValue]); + case 'nilike': + return new ParameterizedSQL(columnName + " NOT ILIKE ? ESCAPE '\\'", + [operatorValue]); case 'regexp': if (operatorValue.global) g.warn('{{PostgreSQL}} regex syntax does not respect the {{`g`}} flag'); From 5cbb4abae121a101bcdcfc91684dd85354bc9d1e Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Sat, 18 Feb 2017 09:18:34 +0330 Subject: [PATCH 2/4] Add tests for pattern matching operators --- test/postgresql.test.js | 115 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/test/postgresql.test.js b/test/postgresql.test.js index ecf05800..2f7df0f0 100644 --- a/test/postgresql.test.js +++ b/test/postgresql.test.js @@ -230,6 +230,121 @@ describe('postgresql connector', function() { }); }); + context('pattern matching operators', function() { + before(function deleteTestFixtures(done) { + Post.destroyAll(done); + }); + before(function createTextFixtures(done) { + Post.create([{ + title: 't1', + content: 'T1_TestCase', + }, { + title: 't2', + content: 'T2_TheOtherCase', + }], done); + }); + after(function deleteTestFixtures(done) { + Post.destroyAll(done); + }); + + it('should support case sensitive queries using like', + function(done) { + Post.find({where: {content: {like: '%TestCase%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(1); + posts[0].content.should.equal('T1_TestCase'); + done(); + }); + }); + + it('should not support case insensitive queries using like', + function(done) { + Post.find({where: {content: {like: '%tesTcasE%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(0); + done(); + }); + }); + + it('should support like for no match', function(done) { + Post.find({where: {content: {like: '%TestXase%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(0); + done(); + }); + }); + + it('should support negative case sensitive queries using nlike', + function(done) { + Post.find({where: {content: {nlike: '%Case%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(0); + done(); + }); + }); + + it('should not support negative case insensitive queries using nlike', + function(done) { + Post.find({where: {content: {nlike: '%casE%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(2); + done(); + }); + }); + + it('should support nlike for no match', function(done) { + Post.find({where: {content: {nlike: '%TestXase%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(2); + done(); + }); + }); + + it('should support case insensitive queries using ilike', function(done) { + Post.find({where: {content: {ilike: '%tesTcasE%'}}}, + function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('T1_TestCase'); + done(); + }); + }); + + it('should support ilike for no match', function(done) { + Post.find({where: {content: {ilike: '%tesTxasE%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(0); + done(); + }); + }); + + it('should support negative case insensitive queries using nilike', + function(done) { + Post.find({where: {content: {nilike: '%casE%'}}}, + function(err, posts) { + should.not.exist(err); + posts.length.should.equal(0); + done(); + }); + }); + + it('should support nilike for no match', function(done) { + Post.find({where: {content: {nilike: '%tesTxasE%'}}}, + function(err, posts) { + should.not.exists(err); + posts.length.should.equal(2); + done(); + }); + }); + }); + context('regexp operator', function() { before(function deleteTestFixtures(done) { Post.destroyAll(done); From 2706edd32a3d7a92d7ed9e24f8be34fa55c53d46 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Tue, 21 Feb 2017 16:16:18 +0330 Subject: [PATCH 3/4] Improve tests for better code style consistency - Change test descriptions to match the new style - Change error handling mechanism for better stack traces --- test/postgresql.test.js | 100 ++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 55 deletions(-) diff --git a/test/postgresql.test.js b/test/postgresql.test.js index 2f7df0f0..24ee97b3 100644 --- a/test/postgresql.test.js +++ b/test/postgresql.test.js @@ -247,98 +247,88 @@ describe('postgresql connector', function() { Post.destroyAll(done); }); - it('should support case sensitive queries using like', - function(done) { - Post.find({where: {content: {like: '%TestCase%'}}}, - function(err, posts) { - should.not.exists(err); - posts.length.should.equal(1); - posts[0].content.should.equal('T1_TestCase'); - done(); - }); + it('supports case sensitive queries using like', function(done) { + Post.find({where: {content: {like: '%TestCase%'}}}, function(err, posts) { + if (err) return done(err); + posts.length.should.equal(1); + posts[0].content.should.equal('T1_TestCase'); + done(); }); + }); - it('should not support case insensitive queries using like', - function(done) { - Post.find({where: {content: {like: '%tesTcasE%'}}}, - function(err, posts) { - should.not.exists(err); - posts.length.should.equal(0); - done(); - }); + it('rejects case insensitive queries using like', function(done) { + Post.find({where: {content: {like: '%tesTcasE%'}}}, function(err, posts) { + if (err) return done(err); + posts.length.should.equal(0); + done(); }); + }); - it('should support like for no match', function(done) { - Post.find({where: {content: {like: '%TestXase%'}}}, - function(err, posts) { - should.not.exists(err); - posts.length.should.equal(0); - done(); - }); + it('supports like for no match', function(done) { + Post.find({where: {content: {like: '%TestXase%'}}}, function(err, posts) { + if (err) return done(err); + posts.length.should.equal(0); + done(); + }); }); - it('should support negative case sensitive queries using nlike', - function(done) { - Post.find({where: {content: {nlike: '%Case%'}}}, - function(err, posts) { - should.not.exists(err); - posts.length.should.equal(0); - done(); - }); + it('supports negative case sensitive queries using nlike', function(done) { + Post.find({where: {content: {nlike: '%Case%'}}}, function(err, posts) { + if (err) return done(err); + posts.length.should.equal(0); + done(); }); + }); - it('should not support negative case insensitive queries using nlike', - function(done) { - Post.find({where: {content: {nlike: '%casE%'}}}, - function(err, posts) { - should.not.exists(err); - posts.length.should.equal(2); - done(); - }); + it('rejects negative case insensitive queries using nlike', function(done) { + Post.find({where: {content: {nlike: '%casE%'}}}, function(err, posts) { + if (err) return done(err); + posts.length.should.equal(2); + done(); }); + }); - it('should support nlike for no match', function(done) { + it('supports nlike for no match', function(done) { Post.find({where: {content: {nlike: '%TestXase%'}}}, function(err, posts) { - should.not.exists(err); + if (err) return done(err); posts.length.should.equal(2); done(); }); }); - it('should support case insensitive queries using ilike', function(done) { + it('supports case insensitive queries using ilike', function(done) { Post.find({where: {content: {ilike: '%tesTcasE%'}}}, function(err, posts) { - should.not.exist(err); + if (err) return done(err); posts.length.should.equal(1); posts[0].content.should.equal('T1_TestCase'); done(); }); }); - it('should support ilike for no match', function(done) { + it('supports ilike for no match', function(done) { Post.find({where: {content: {ilike: '%tesTxasE%'}}}, function(err, posts) { - should.not.exists(err); + if (err) return done(err); posts.length.should.equal(0); done(); }); }); - it('should support negative case insensitive queries using nilike', + it('supports negative case insensitive queries using nilike', function(done) { - Post.find({where: {content: {nilike: '%casE%'}}}, - function(err, posts) { - should.not.exist(err); - posts.length.should.equal(0); - done(); - }); + Post.find({where: {content: {nilike: '%casE%'}}}, function(err, posts) { + if (err) return done(err); + posts.length.should.equal(0); + done(); + }); }); - it('should support nilike for no match', function(done) { + it('supports nilike for no match', function(done) { Post.find({where: {content: {nilike: '%tesTxasE%'}}}, function(err, posts) { - should.not.exists(err); + if (err) return done(err); posts.length.should.equal(2); done(); }); From 7f3b4d6b0991502806e8da848cafeab6fc7af2be Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Wed, 1 Mar 2017 01:31:22 +0330 Subject: [PATCH 4/4] Fix code style inconsistencies in ilike tests --- test/postgresql.test.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/test/postgresql.test.js b/test/postgresql.test.js index 24ee97b3..0abd70e3 100644 --- a/test/postgresql.test.js +++ b/test/postgresql.test.js @@ -231,21 +231,8 @@ describe('postgresql connector', function() { }); context('pattern matching operators', function() { - before(function deleteTestFixtures(done) { - Post.destroyAll(done); - }); - before(function createTextFixtures(done) { - Post.create([{ - title: 't1', - content: 'T1_TestCase', - }, { - title: 't2', - content: 'T2_TheOtherCase', - }], done); - }); - after(function deleteTestFixtures(done) { - Post.destroyAll(done); - }); + before(deleteTestFixtures); + before(createTestFixtures); it('supports case sensitive queries using like', function(done) { Post.find({where: {content: {like: '%TestCase%'}}}, function(err, posts) { @@ -333,6 +320,20 @@ describe('postgresql connector', function() { done(); }); }); + + function deleteTestFixtures(done) { + Post.destroyAll(done); + } + + function createTestFixtures(done) { + Post.create([{ + title: 't1', + content: 'T1_TestCase', + }, { + title: 't2', + content: 'T2_TheOtherCase', + }], done); + } }); context('regexp operator', function() {