Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,16 @@ PostgreSQL.prototype.buildExpression = function(columnName, operator,
operatorValue, propertyDefinition) {
switch (operator) {
case 'like':
return new ParameterizedSQL(columnName + " LIKE ? ESCAPE E'\\\\'",
return new ParameterizedSQL(columnName + "::TEXT LIKE ? ESCAPE E'\\\\'",
[operatorValue]);
case 'ilike':
return new ParameterizedSQL(columnName + " ILIKE ? ESCAPE E'\\\\'",
return new ParameterizedSQL(columnName + "::TEXT ILIKE ? ESCAPE E'\\\\'",
[operatorValue]);
case 'nlike':
return new ParameterizedSQL(columnName + " NOT LIKE ? ESCAPE E'\\\\'",
return new ParameterizedSQL(columnName + "::TEXT NOT LIKE ? ESCAPE E'\\\\'",
[operatorValue]);
case 'nilike':
return new ParameterizedSQL(columnName + " NOT ILIKE ? ESCAPE E'\\\\'",
return new ParameterizedSQL(columnName + "::TEXT NOT ILIKE ? ESCAPE E'\\\\'",
[operatorValue]);
case 'regexp':
if (operatorValue.global)
Expand Down
82 changes: 73 additions & 9 deletions test/postgresql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require('./init');
var async = require('async');
var should = require('should');

var Post, Expense, db, created;
var Post, Expense, db, created, PostWithDate;

describe('lazyConnect', function() {
it('should skip connect phase (lazyConnect = true)', function(done) {
Expand Down Expand Up @@ -384,18 +384,82 @@ describe('postgresql connector', function() {
});
});

it('supports like for date types (with explicit typecast)', function(done) {
PostWithDate.find({where: {created: {like: '%05%'}}}, function(err, result) {
should.not.exists(err);
result.length.should.equal(1);
done();
});
});

it('supports case insensitive queries using ilike for date types (with explicit typecast)', function(done) {
PostWithDate.find({where: {created: {ilike: '%05%'}}}, function(err, result) {
should.not.exists(err);
result.length.should.equal(1);
done();
});
});

it('supports nlike for no match for date types (with explicit typecast)', function(done) {
PostWithDate.find({where: {content: {nlike: '%07%'}}},
function(err, posts) {
should.not.exists(err);
posts.length.should.equal(2);
done();
});
});

it('supports case insensitive queries using nilike for date types (with explicit typecast)', function(done) {
PostWithDate.find({where: {created: {nilike: '%07%'}}}, function(err, result) {
should.not.exists(err);
result.length.should.equal(2);
done();
});
});

function deleteTestFixtures(done) {
Post.destroyAll(done);
Post.destroyAll(function(err) {
should.not.exist(err);
if (PostWithDate) PostWithDate.destroyAll(done);
done();
});
}

function createTestFixtures(done) {
Post.create([{
title: 't1',
content: 'T1_TestCase',
}, {
title: 't2',
content: 'T2_TheOtherCase',
}], done);
PostWithDate = db.define('PostWithDate', {
title: {type: String, length: 255, index: true},
content: {type: String},
created: {
type: String,
postgresql: {
dataType: 'timestamp with time zone',
},
},
});
db.automigrate(function(err, result) {
if (err) throw err;
Post.create([{
title: 't1',
content: 'T1_TestCase',
}, {
title: 't2',
content: 'T2_TheOtherCase',
}], function(err, result) {
should.not.exist(err);
PostWithDate.create([
{
title: 'Title 1',
content: 'Content 1',
created: '2017-05-17 12:00:01',
},
{
title: 'Title 2',
content: 'Content 2',
created: '2017-04-17 12:00:01',
},
], done);
});
});
}
});

Expand Down