Skip to content

Add ILIKE functionality #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
106 changes: 106 additions & 0 deletions test/postgresql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,112 @@ describe('postgresql connector', function() {
});
});

context('pattern matching operators', function() {
before(deleteTestFixtures);
before(createTestFixtures);

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('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('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('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('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('supports nlike for no match', function(done) {
Post.find({where: {content: {nlike: '%TestXase%'}}},
function(err, posts) {
if (err) return done(err);
posts.length.should.equal(2);
done();
});
});

it('supports case insensitive queries using ilike', function(done) {
Post.find({where: {content: {ilike: '%tesTcasE%'}}},
function(err, posts) {
if (err) return done(err);
posts.length.should.equal(1);
posts[0].content.should.equal('T1_TestCase');
done();
});
});

it('supports ilike for no match', function(done) {
Post.find({where: {content: {ilike: '%tesTxasE%'}}},
function(err, posts) {
if (err) return done(err);
posts.length.should.equal(0);
done();
});
});

it('supports negative case insensitive queries using nilike',
function(done) {
Post.find({where: {content: {nilike: '%casE%'}}}, function(err, posts) {
if (err) return done(err);
posts.length.should.equal(0);
done();
});
});

it('supports nilike for no match', function(done) {
Post.find({where: {content: {nilike: '%tesTxasE%'}}},
function(err, posts) {
if (err) return done(err);
posts.length.should.equal(2);
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() {
before(function deleteTestFixtures(done) {
Post.destroyAll(done);
Expand Down