Allow Expression<bool?> in more places

This commit is contained in:
Simon Binder 2020-12-14 19:43:32 +01:00
parent 6aa022e547
commit d9cf6660ec
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
8 changed files with 28 additions and 24 deletions

View File

@ -82,7 +82,7 @@ class Batch {
/// [UpdateStatement.write] or the [documentation with examples](https://moor.simonbinder.eu/docs/getting-started/writing_queries/#updates-and-deletes)
void update<T extends Table, D extends DataClass>(
TableInfo<T, D> table, Insertable<D> row,
{Expression<bool> Function(T table)? where}) {
{Expression<bool?> Function(T table)? where}) {
_addUpdate(table, UpdateKind.update);
final stmt = UpdateStatement(_engine, table);
if (where != null) stmt.where(where);
@ -133,7 +133,7 @@ class Batch {
/// See also:
/// - [QueryEngine.delete]
void deleteWhere<T extends Table, D extends DataClass>(
TableInfo<T, D> table, Expression<bool> Function(T tbl) filter) {
TableInfo<T, D> table, Expression<bool?> Function(T tbl) filter) {
_addUpdate(table, UpdateKind.delete);
final stmt = DeleteStatement(_engine, table)..where(filter);
_addContext(stmt.constructQuery());

View File

@ -31,7 +31,7 @@ class Join<T extends Table, D extends DataClass> extends Component {
/// For joins that aren't [_JoinType.cross], contains an additional predicate
/// that must be matched for the join.
final Expression<bool>? on;
final Expression<bool?>? on;
/// Whether [table] should appear in the result set (defaults to true).
///

View File

@ -9,7 +9,7 @@ part of '../query_builder.dart';
///
/// This is equivalent to the `COUNT(*) FILTER (WHERE filter)` sql function. The
/// filter will be omitted if null.
Expression<int> countAll({Expression<bool>? filter}) {
Expression<int> countAll({Expression<bool?>? filter}) {
return _AggregateExpression('COUNT', const _StarFunctionParameter(),
filter: filter);
}
@ -24,7 +24,7 @@ extension BaseAggregate<DT> on Expression<DT> {
/// counted twice. An optional [filter] can be used to only include values
/// matching the filter. Note that [filter] is only available from sqlite
/// 3.30 and most devices will use an older sqlite version.
Expression<int> count({bool? distinct, Expression<bool>? filter}) {
Expression<int> count({bool? distinct, Expression<bool?>? filter}) {
return _AggregateExpression('COUNT', this,
filter: filter, distinct: distinct);
}
@ -88,7 +88,7 @@ class _AggregateExpression<D> extends Expression<D> {
final Where? filter;
_AggregateExpression(this.functionName, this.parameter,
{Expression<bool>? filter, bool? distinct})
{Expression<bool?>? filter, bool? distinct})
: filter = filter != null ? Where(filter) : null,
distinct = distinct ?? false;

View File

@ -4,53 +4,57 @@ part of '../query_builder.dart';
extension ComparableExpr<DT extends Comparable<dynamic>?> on Expression<DT> {
/// Returns an expression that is true if this expression is strictly bigger
/// than the other expression.
Expression<bool> isBiggerThan(Expression<DT> other) {
Expression<bool?> isBiggerThan(Expression<DT> other) {
return _Comparison(this, _ComparisonOperator.more, other);
}
/// Returns an expression that is true if this expression is strictly bigger
/// than the other value.
Expression<bool> isBiggerThanValue(DT other) => isBiggerThan(Variable(other));
Expression<bool?> isBiggerThanValue(DT other) {
return isBiggerThan(Variable(other));
}
/// Returns an expression that is true if this expression is bigger than or
/// equal to he other expression.
Expression<bool> isBiggerOrEqual(Expression<DT> other) {
Expression<bool?> isBiggerOrEqual(Expression<DT> other) {
return _Comparison(this, _ComparisonOperator.moreOrEqual, other);
}
/// Returns an expression that is true if this expression is bigger than or
/// equal to he other value.
Expression<bool> isBiggerOrEqualValue(DT other) =>
isBiggerOrEqual(Variable(other));
Expression<bool?> isBiggerOrEqualValue(DT other) {
return isBiggerOrEqual(Variable(other));
}
/// Returns an expression that is true if this expression is strictly smaller
/// than the other expression.
Expression<bool> isSmallerThan(Expression<DT> other) {
Expression<bool?> isSmallerThan(Expression<DT> other) {
return _Comparison(this, _ComparisonOperator.less, other);
}
/// Returns an expression that is true if this expression is strictly smaller
/// than the other value.
Expression<bool> isSmallerThanValue(DT other) =>
Expression<bool?> isSmallerThanValue(DT other) =>
isSmallerThan(Variable(other));
/// Returns an expression that is true if this expression is smaller than or
/// equal to he other expression.
Expression<bool> isSmallerOrEqual(Expression<DT> other) {
Expression<bool?> isSmallerOrEqual(Expression<DT> other) {
return _Comparison(this, _ComparisonOperator.lessOrEqual, other);
}
/// Returns an expression that is true if this expression is smaller than or
/// equal to he other value.
Expression<bool> isSmallerOrEqualValue(DT other) =>
isSmallerOrEqual(Variable(other));
Expression<bool?> isSmallerOrEqualValue(DT other) {
return isSmallerOrEqual(Variable(other));
}
/// Returns an expression evaluating to true if this expression is between
/// [lower] and [higher] (both inclusive).
///
/// If [not] is set, the expression will be negated. To compare this
/// expression against two values, see
Expression<bool> isBetween(Expression<DT> lower, Expression<DT> higher,
Expression<bool?> isBetween(Expression<DT> lower, Expression<DT> higher,
{bool not = false}) {
return _BetweenExpression(
target: this, lower: lower, higher: higher, not: not);
@ -60,7 +64,7 @@ extension ComparableExpr<DT extends Comparable<dynamic>?> on Expression<DT> {
/// [lower] and [higher] (both inclusive).
///
/// If [not] is set, the expression will be negated.
Expression<bool> isBetweenValues(DT lower, DT higher, {bool not = false}) {
Expression<bool?> isBetweenValues(DT lower, DT higher, {bool not = false}) {
return _BetweenExpression(
target: this,
lower: Variable<DT>(lower),
@ -70,7 +74,7 @@ extension ComparableExpr<DT extends Comparable<dynamic>?> on Expression<DT> {
}
}
class _BetweenExpression extends Expression<bool> {
class _BetweenExpression extends Expression<bool?> {
final Expression target;
// https://www.sqlite.org/lang_expr.html#between

View File

@ -58,13 +58,13 @@ abstract class Expression<D> implements FunctionParameter {
/// An expression that is true if `this` resolves to any of the values in
/// [values].
Expression<bool> isIn(Iterable<D> values) {
Expression<bool?> isIn(Iterable<D> values) {
return _InExpression(this, values.toList(), false);
}
/// An expression that is true if `this` does not resolve to any of the values
/// in [values].
Expression<bool> isNotIn(Iterable<D> values) {
Expression<bool?> isNotIn(Iterable<D> values) {
return _InExpression(this, values.toList(), true);
}

View File

@ -1,6 +1,6 @@
part of '../query_builder.dart';
class _InExpression<T> extends Expression<bool> {
class _InExpression<T> extends Expression<bool?> {
final Expression<T> _expression;
final List<T> _values;
final bool _not;

View File

@ -176,7 +176,7 @@ mixin SingleTableQueryMixin<T extends Table, D extends DataClass>
/// which explains how to express most SQL expressions in Dart.
/// If you want to remove duplicate rows from a query, use the `distinct`
/// parameter on [QueryEngine.select].
void where(Expression<bool> Function(T tbl) filter) {
void where(Expression<bool?> Function(T tbl) filter) {
final predicate = filter(table.asDslTable);
if (whereExpr == null) {

View File

@ -165,7 +165,7 @@ class JoinedSelectStatement<FirstT extends Table, FirstD extends DataClass>
/// Groups the result by values in [expressions].
///
/// An optional [having] attribute can be set to exclude certain groups.
void groupBy(Iterable<Expression> expressions, {Expression<bool>? having}) {
void groupBy(Iterable<Expression> expressions, {Expression<bool?>? having}) {
_groupBy = GroupBy._(expressions.toList(), having);
}