Add trim() function for text (#1012)

This commit is contained in:
Simon Binder 2021-01-21 20:14:37 +01:00
parent 2e2d1b4e6d
commit 362b61a178
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 59 additions and 18 deletions

View File

@ -2,6 +2,7 @@
- Reverted the `SelectableUtils` extension, its members have been added back to the - Reverted the `SelectableUtils` extension, its members have been added back to the
`Selectable` class. `Selectable` class.
- Add `trim`, `trimLeft()` and `trimRight()` extensions for text expressions
## 4.0.0-nullsafety.1 ## 4.0.0-nullsafety.1

View File

@ -105,6 +105,21 @@ extension StringExpressionOperators on Expression<String?> {
Expression<int?> get length { Expression<int?> get length {
return FunctionCallExpression('LENGTH', [this]); return FunctionCallExpression('LENGTH', [this]);
} }
/// Removes spaces from both ends of this string.
Expression<String?> trim() {
return FunctionCallExpression('TRIM', [this]);
}
/// Removes spaces from the beginning of this string.
Expression<String?> trimLeft() {
return FunctionCallExpression('LTRIM', [this]);
}
/// Removes spaces from the end of this string.
Expression<String?> trimRight() {
return FunctionCallExpression('RTRIM', [this]);
}
} }
/// A `text LIKE pattern` expression that will be true if the first expression /// A `text LIKE pattern` expression that will be true if the first expression

View File

@ -19,6 +19,11 @@ void main() {
tearDown(() => db.close()); tearDown(() => db.close());
Future<T> eval<T>(Expression<T> expr) {
final query = db.selectOnly(db.users)..addColumns([expr]);
return query.getSingle().then((row) => row.read(expr));
}
test('plus and minus on DateTimes', () async { test('plus and minus on DateTimes', () async {
const nowExpr = currentDateAndTime; const nowExpr = currentDateAndTime;
final tomorrow = nowExpr + const Duration(days: 1); final tomorrow = nowExpr + const Duration(days: 1);
@ -33,31 +38,40 @@ void main() {
const Duration(days: 1).inSeconds); const Duration(days: 1).inSeconds);
}); });
test('datetime.date format', () async { test('datetime.date format', () {
final expr = Variable.withDateTime(DateTime(2020, 09, 04, 8, 55)); final expr = Variable.withDateTime(DateTime(2020, 09, 04, 8, 55));
final asDate = expr.date; final asDate = expr.date;
final row = expect(eval(asDate), completion('2020-09-04'));
await (db.selectOnly(db.users)..addColumns([asDate])).getSingle();
expect(row.read(asDate), '2020-09-04');
}); });
test('text contains', () async { group('text', () {
const stringLiteral = Constant('Some sql string literal'); test('contains', () {
final containsSql = stringLiteral.contains('sql'); const stringLiteral = Constant('Some sql string literal');
final containsSql = stringLiteral.contains('sql');
final row = expect(eval(containsSql), completion(isTrue));
await (db.selectOnly(db.users)..addColumns([containsSql])).getSingle(); });
expect(row.read(containsSql), isTrue); test('trim()', () {
const literal = Constant(' hello world ');
expect(eval(literal.trim()), completion('hello world'));
});
test('trimLeft()', () {
const literal = Constant(' hello world ');
expect(eval(literal.trimLeft()), completion('hello world '));
});
test('trimRight()', () {
const literal = Constant(' hello world ');
expect(eval(literal.trimRight()), completion(' hello world'));
});
}); });
test('coalesce', () async { test('coalesce', () async {
final expr = coalesce<int>([const Constant(null), const Constant(3)]); final expr = coalesce<int>([const Constant(null), const Constant(3)]);
final row = await (db.selectOnly(db.users)..addColumns([expr])).getSingle(); expect(eval(expr), completion(3));
expect(row.read(expr), equals(3));
}); });
} }

View File

@ -36,9 +36,20 @@ void main() {
expression.contains('foo bar'), generates('col LIKE ?', ['%foo bar%'])); expression.contains('foo bar'), generates('col LIKE ?', ['%foo bar%']));
}); });
test('can use string functions', () { group('can use string functions', () {
expect(expression.upper(), generates('UPPER(col)')); final tests = {
expect(expression.lower(), generates('LOWER(col)')); expression.upper(): 'UPPER(col)',
expect(expression.length, generates('LENGTH(col)')); expression.lower(): 'LOWER(col)',
expression.trim(): 'TRIM(col)',
expression.trimLeft(): 'LTRIM(col)',
expression.trimRight(): 'RTRIM(col)',
expression.length: 'LENGTH(col)',
};
tests.forEach((expr, sql) {
test(sql, () {
expect(expr, generates(sql));
});
});
}); });
} }