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

Fix nin query replacement regexp issue #194

Open
wants to merge 1 commit 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
8 changes: 6 additions & 2 deletions helpers/conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function getValueInequalityOperator(value) {
return valuesThatUseIsOrIsNot.indexOf(value) > -1 ? 'is not' : '!='
}

function escapeRegex(value) {
return value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
}

/**
* Querying where column is equal to a value
* @param column {String} - Column name either table.column or column
Expand Down Expand Up @@ -161,8 +165,8 @@ conditionals.add('$nin', { cascade: false }, function(column, set, values, colle
return 'true'
}
return conditionals.get('$in').fn(column, set, values, collection, original)
.replace(new RegExp(column + ' in', 'g'), column + ' not in')
.replace(new RegExp(column + ' is', 'g'), column + ' is not');
.replace(new RegExp(escapeRegex(column) + ' in', 'g'), column + ' not in')
.replace(new RegExp(escapeRegex(column) + ' is', 'g'), column + ' is not');
});

/**
Expand Down
19 changes: 19 additions & 0 deletions test/conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,25 @@ describe('Conditions', function(){
);
});

it ('$nin with expression', function(){
var query = builder.sql({
type: 'select'
, table: 'users'
, where: {
id: {
'/* \\".* key */ (substring("id",1,1))': {
$nin: ['a']
}
}
}
});

assert.equal(
query.toString()
, `select "users".* from "users" where /* \\".* key */ (substring("id",1,1)) not in ($1)`
);
});

it ('should allow an arbitrary amount of conditions', function(){
var query = builder.sql({
type: 'select'
Expand Down