Merge pull request #1091 from duffn/ilike
Add ilike and nilike operators
This commit is contained in:
commit
1ee04421c0
|
@ -616,8 +616,8 @@ function applyFilter(filter) {
|
||||||
testInEquality({lte: example.between[1]}, value));
|
testInEquality({lte: example.between[1]}, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (example.like || example.nlike) {
|
if (example.like || example.nlike || example.ilike || example.nilike) {
|
||||||
var like = example.like || example.nlike;
|
var like = example.like || example.nlike || example.ilike || example.nilike;
|
||||||
if (typeof like === 'string') {
|
if (typeof like === 'string') {
|
||||||
like = toRegExp(like);
|
like = toRegExp(like);
|
||||||
}
|
}
|
||||||
|
@ -628,6 +628,14 @@ function applyFilter(filter) {
|
||||||
if (example.nlike) {
|
if (example.nlike) {
|
||||||
return !new RegExp(like).test(value);
|
return !new RegExp(like).test(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (example.ilike) {
|
||||||
|
return !!new RegExp(like, 'i').test(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (example.nilike) {
|
||||||
|
return !new RegExp(like, 'i').test(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (testInEquality(example, value)) {
|
if (testInEquality(example, value)) {
|
||||||
|
|
|
@ -1400,6 +1400,8 @@ var operators = {
|
||||||
neq: '!=',
|
neq: '!=',
|
||||||
like: 'LIKE',
|
like: 'LIKE',
|
||||||
nlike: 'NOT LIKE',
|
nlike: 'NOT LIKE',
|
||||||
|
ilike: 'ILIKE',
|
||||||
|
nilike: 'NOT ILIKE',
|
||||||
regexp: 'REGEXP',
|
regexp: 'REGEXP',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1626,6 +1628,8 @@ DataAccessObject._coerce = function(where) {
|
||||||
break;
|
break;
|
||||||
case 'like':
|
case 'like':
|
||||||
case 'nlike':
|
case 'nlike':
|
||||||
|
case 'ilike':
|
||||||
|
case 'nilike':
|
||||||
if (!(typeof val === 'string' || val instanceof RegExp)) {
|
if (!(typeof val === 'string' || val instanceof RegExp)) {
|
||||||
err = new Error(g.f('The %s property has invalid clause %j', p, where[p]));
|
err = new Error(g.f('The %s property has invalid clause %j', p, where[p]));
|
||||||
err.statusCode = 400;
|
err.statusCode = 400;
|
||||||
|
@ -1658,7 +1662,8 @@ DataAccessObject._coerce = function(where) {
|
||||||
operator = 'regexp';
|
operator = 'regexp';
|
||||||
} else if (operator === 'regexp' && val instanceof RegExp) {
|
} else if (operator === 'regexp' && val instanceof RegExp) {
|
||||||
// Do not coerce regex literals/objects
|
// Do not coerce regex literals/objects
|
||||||
} else if (!((operator === 'like' || operator === 'nlike') && val instanceof RegExp)) {
|
} else if (!((operator === 'like' || operator === 'nlike' ||
|
||||||
|
operator === 'ilike' || operator === 'nilike') && val instanceof RegExp)) {
|
||||||
val = DataType(val);
|
val = DataType(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1711,6 +1716,8 @@ DataAccessObject._coerce = function(where) {
|
||||||
* - neq: !=
|
* - neq: !=
|
||||||
* - like: LIKE
|
* - like: LIKE
|
||||||
* - nlike: NOT LIKE
|
* - nlike: NOT LIKE
|
||||||
|
* - ilike: ILIKE
|
||||||
|
* - nilike: NOT ILIKE
|
||||||
* - regexp: REGEXP
|
* - regexp: REGEXP
|
||||||
*
|
*
|
||||||
* You can also use `and` and `or` operations. See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.
|
* You can also use `and` and `or` operations. See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.
|
||||||
|
|
|
@ -469,6 +469,62 @@ describe('basic-querying', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var itWhenIlikeSupported = connectorCapabilities.ilike ? it : it.skip.bind(it);
|
||||||
|
|
||||||
|
itWhenIlikeSupported('should support "like" that is satisfied', function(done) {
|
||||||
|
User.find({where: {name: {like: 'John'}}}, function(err, users) {
|
||||||
|
if (err) return done(err);
|
||||||
|
users.length.should.equal(1);
|
||||||
|
users[0].name.should.equal('John Lennon');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
itWhenIlikeSupported('should support "like" that is not satisfied', function(done) {
|
||||||
|
User.find({where: {name: {like: 'Bob'}}}, function(err, users) {
|
||||||
|
if (err) return done(err);
|
||||||
|
users.length.should.equal(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
var itWhenNilikeSupported = connectorCapabilities.nilike ? it : it.skip.bind(it);
|
||||||
|
|
||||||
|
itWhenNilikeSupported('should support "nlike" that is satisfied', function(done) {
|
||||||
|
User.find({where: {name: {nlike: 'John'}}}, function(err, users) {
|
||||||
|
if (err) return done(err);
|
||||||
|
users.length.should.equal(5);
|
||||||
|
users[0].name.should.equal('Paul McCartney');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
itWhenIlikeSupported('should support "ilike" that is satisfied', function(done) {
|
||||||
|
User.find({where: {name: {ilike: 'john'}}}, function(err, users) {
|
||||||
|
if (err) return done(err);
|
||||||
|
users.length.should.equal(1);
|
||||||
|
users[0].name.should.equal('John Lennon');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
itWhenIlikeSupported('should support "ilike" that is not satisfied', function(done) {
|
||||||
|
User.find({where: {name: {ilike: 'bob'}}}, function(err, users) {
|
||||||
|
if (err) return done(err);
|
||||||
|
users.length.should.equal(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
itWhenNilikeSupported('should support "nilike" that is satisfied', function(done) {
|
||||||
|
User.find({where: {name: {nilike: 'john'}}}, function(err, users) {
|
||||||
|
if (err) return done(err);
|
||||||
|
users.length.should.equal(5);
|
||||||
|
users[0].name.should.equal('Paul McCartney');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should only include fields as specified', function(done) {
|
it('should only include fields as specified', function(done) {
|
||||||
var remaining = 0;
|
var remaining = 0;
|
||||||
|
|
||||||
|
|
|
@ -32,3 +32,7 @@ if (!('getModelBuilder' in global)) {
|
||||||
return new ModelBuilder();
|
return new ModelBuilder();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!('connectorCapabilities' in global)) {
|
||||||
|
global.connectorCapabilities = {};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue