Add functions: iif, ifnull, nullif

This commit is contained in:
Alexander Wilde 2022-12-12 23:23:25 +00:00
parent 9f1da6ff50
commit 2a4515ed51
2 changed files with 21 additions and 0 deletions

View File

@ -159,6 +159,12 @@ abstract class Expression<D extends Object> implements FunctionParameter {
return CaseWhenExpression<T>(this, when.entries.toList(), orElse);
}
/// Evaluates to `this` if [predicate] is true, otherwise evaluates to [ifFalse].
Expression<T> iif<T extends Object>(
Expression<bool> predicate, Expression<T> ifFalse) {
return FunctionCallExpression<T>('IIF', [predicate, this, ifFalse]);
}
/// Writes this expression into the [GenerationContext], assuming that there's
/// an outer expression with [precedence]. If the [Expression.precedence] of
/// `this` expression is lower, it will be wrap}ped in

View File

@ -29,6 +29,21 @@ Expression<T> coalesce<T extends Object>(List<Expression<T>> expressions) {
return FunctionCallExpression<T>('COALESCE', expressions);
}
/// Evaluates to the first expression that's not null, or null if both evaluate
/// to null. See [coalesce] if you need more than 2.
Expression<T> ifNull<T extends Object>(
Expression<T> first, Expression<T> second) {
return FunctionCallExpression<T>('IFNULL', [first, second]);
}
/// Extension defines the `nullIf` members.
extension SqlNullIf on Expression {
/// Returns null if both [matcher] matches this expression.
Expression nullIf(Expression matcher) {
return FunctionCallExpression('NULLIF', [this, matcher]);
}
}
class _NullCheck extends Expression<bool> {
final Expression _inner;
final bool _isNull;