Skip to content
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

Adding support for case insensitive like constraint #297

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion helpers/avg.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = require('machine').build({
// Compile the original Waterline Query
var compiledQuery;
try {
compiledQuery = Helpers.query.compileStatement(statement);
compiledQuery = Helpers.query.compileStatement(statement, query.meta);
} catch (e) {
return exits.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = require('machine').build({
// Compile the original Waterline Query
var compiledQuery;
try {
compiledQuery = Helpers.query.compileStatement(statement);
compiledQuery = Helpers.query.compileStatement(statement, query.meta);
} catch (e) {
return exits.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = require('machine').build({
// Compile the original Waterline Query
var compiledQuery;
try {
compiledQuery = Helpers.query.compileStatement(statement);
compiledQuery = Helpers.query.compileStatement(statement, query.meta);
} catch (e) {
return exits.error(e);
}
Expand Down
7 changes: 6 additions & 1 deletion helpers/private/query/compile-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
// Transform a Waterline Query Statement into a SQL query.

var PG = require('machinepack-postgresql');
var modifyWhereClause = require('./modify-where-clause');

module.exports = function compileStatement(statement, meta) {
if (statement.where) {
statement.where = modifyWhereClause(statement.where, meta);
}

module.exports = function compileStatement(statement) {
var report = PG.compileStatement({
statement: statement
}).execSync();
Expand Down
61 changes: 61 additions & 0 deletions helpers/private/query/modify-where-clause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// ███╗ ███╗ ██████╗ ██████╗ ██╗███████╗██╗ ██╗ ██╗ ██╗██╗ ██╗███████╗██████╗ ███████╗
// ████╗ ████║██╔═══██╗██╔══██╗██║██╔════╝╚██╗ ██╔╝ ██║ ██║██║ ██║██╔════╝██╔══██╗██╔════╝
// ██╔████╔██║██║ ██║██║ ██║██║█████╗ ╚████╔╝ ██║ █╗ ██║███████║█████╗ ██████╔╝█████╗
// ██║╚██╔╝██║██║ ██║██║ ██║██║██╔══╝ ╚██╔╝ ██║███╗██║██╔══██║██╔══╝ ██╔══██╗██╔══╝
// ██║ ╚═╝ ██║╚██████╔╝██████╔╝██║██║ ██║ ╚███╔███╔╝██║ ██║███████╗██║ ██║███████╗
// ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝

// ██████╗██╗ █████╗ ██╗ ██╗███████╗███████╗
// ██╔════╝██║ ██╔══██╗██║ ██║██╔════╝██╔════╝
// ██║ ██║ ███████║██║ ██║███████╗█████╗
// ██║ ██║ ██╔══██║██║ ██║╚════██║██╔══╝
// ╚██████╗███████╗██║ ██║╚██████╔╝███████║███████╗
// ╚═════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚══════╝

// Modify the where clause of a query object

var _ = require("@sailshq/lodash");

module.exports = function modifyWhereClause(whereClause, meta) {
// Handle empty `where` clause and missing meta.
if (_.keys(whereClause).length === 0 || !meta) {
return whereClause;
}

var makeLikeModifierCaseInsensitive = meta.makeLikeModifierCaseInsensitive;

// Recursively modify the `where` clause.
var queryFilter = (function recurse(branch) {
var loneKey = _.first(_.keys(branch));

// Handle AND/OR conditions
if (loneKey === "and" || loneKey === "or") {
var conjunctsOrDisjuncts = branch[loneKey];
branch[loneKey] = _.map(conjunctsOrDisjuncts, function (conjunctOrDisjunct) {
return recurse(conjunctOrDisjunct);
});

return branch;
}

// We're dealing with a constraint of some kind.
var constraintColumnName = loneKey;
var constraint = branch[constraintColumnName];

// If it's a primitive, return as is.
if (_.isString(constraint) || _.isNumber(constraint) || _.isBoolean(constraint) || _.isNull(constraint)) {
return branch;
}

// Modify `LIKE` to `ILIKE` if case insensitivity is enabled.
if (constraint.like && makeLikeModifierCaseInsensitive) {
constraint.ilike = constraint.like;
delete constraint.like;
}

return branch;
})(whereClause);

// Return the modified query filter.
return queryFilter;
};
2 changes: 1 addition & 1 deletion helpers/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module.exports = require('machine').build({
// Compile the original Waterline Query
var compiledQuery;
try {
compiledQuery = Helpers.query.compileStatement(statement);
compiledQuery = Helpers.query.compileStatement(statement, query.meta);
} catch (e) {
return exits.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = require('machine').build({
// Compile the original Waterline Query
var compiledQuery;
try {
compiledQuery = Helpers.query.compileStatement(statement);
compiledQuery = Helpers.query.compileStatement(statement, query.meta);
} catch (e) {
return exits.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ module.exports = require('machine').build({
// Compile statement into a native query.
var compiledQuery;
try {
compiledQuery = Helpers.query.compileStatement(statement);
compiledQuery = Helpers.query.compileStatement(statement, query.meta);
} catch (e) {
return exits.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/redact-passwords.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ module.exports = function redactPasswords(err) {
}
}
return err;
}
};
32 changes: 32 additions & 0 deletions test/adapter/unit/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,38 @@ describe('Unit Tests ::', function() {
});
});

it('should ensure the record is deleted with case insensitive like', function(done) {
var query = {
using: 'test_destroy',
criteria: {
where: {
fieldB: {
like: 'BAR'
}
}
},
meta: {
makeLikeModifierCaseInsensitive: true
}
};

Adapter.destroy('test', query, function(err) {
if (err) {
return done(err);
}

Adapter.find('test', query, function(err, results) {
if (err) {
return done(err);
}

assert.equal(results.length, 0);

return done();
});
});
});

// Look into the bowels of the PG Driver and ensure the Create function handles
// it's connections properly.
it('should release its connection when completed', function(done) {
Expand Down
28 changes: 28 additions & 0 deletions test/adapter/unit/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ describe('Unit Tests ::', function() {
});
});

it('should be case insensitive when `meta.makeLikeModifierCaseInsensitive` is true', function(done) {
var query = {
using: 'test_find',
criteria: {
where: {
fieldB: {
like: 'bar_2'
}
}
},
meta: {
makeLikeModifierCaseInsensitive: true
}
};

Adapter.find('test', query, function(err, results) {
if (err) {
return done(err);
}

assert(_.isArray(results));
assert.equal(results.length, 1);
assert.equal(_.first(results).fieldB, 'bAr_2');

return done();
});
});

it('should return `ref` type attributes unchanged', function(done) {
var query = {
using: 'test_find',
Expand Down
33 changes: 33 additions & 0 deletions test/adapter/unit/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ describe('Unit Tests ::', function() {
});
});

it('should be case insensitive when `meta.makeLikeModifierCaseInsensitive` is true', function(done) {
var query = {
using: 'test_update',
criteria: {
where: {
fieldB: {
like: 'bar_2'
}
}
},
valuesToSet: {
fieldA: 'FooBar2'
},
meta: {
fetch: true,
makeLikeModifierCaseInsensitive: true
}
};

Adapter.update('test', query, function(err, results) {
if (err) {
return done(err);
}

assert(_.isArray(results));
assert.equal(results.length, 1);
assert.equal(_.first(results).fieldA, 'FooBar2');
assert.equal(_.first(results).fieldB, 'bAr_2');

return done();
});
});

// Look into the bowels of the PG Driver and ensure the Create function handles
// it's connections properly.
it('should release its connection when completed', function(done) {
Expand Down