Support contains (#527)

This commit is contained in:
Simon Binder 2020-04-30 11:51:09 +02:00
parent e90d72f25f
commit 81a476662c
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 22 additions and 0 deletions

View File

@ -17,6 +17,13 @@ extension StringExpressionOperators on Expression<String> {
return _LikeOperator(this, Variable.withString(regex), operator: 'REGEXP');
}
/// Whether this expression contains [substring].
///
/// This is equivalent to calling [like] with `%<substring>%`.
Expression<bool> contains(String substring) {
return like('%$substring%');
}
/// Uses the given [collate] sequence when comparing this column to other
/// values.
Expression<String> collate(Collate collate) {

View File

@ -32,4 +32,14 @@ void main() {
expect(row.read(tomorrowStamp) - row.read(nowStamp),
const Duration(days: 1).inSeconds);
});
test('text contains', () async {
const stringLiteral = Constant('Some sql string literal');
final containsSql = stringLiteral.contains('sql');
final row =
await (db.selectOnly(db.users)..addColumns([containsSql])).getSingle();
expect(row.read(containsSql), isTrue);
});
}

View File

@ -31,6 +31,11 @@ void main() {
expect(ctx.boundVariables, isEmpty);
});
test('can use contains', () {
expect(
expression.contains('foo bar'), generates('col LIKE ?', ['%foo bar%']));
});
test('can use string functions', () {
expect(expression.upper(), generates('UPPER(col)'));
expect(expression.lower(), generates('LOWER(col)'));