From d9cf6660ecf24f6d54cb00aceb94097589f2f80a Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Mon, 14 Dec 2020 19:43:32 +0100 Subject: [PATCH] Allow Expression in more places --- moor/lib/src/runtime/api/batch.dart | 4 +-- .../query_builder/components/join.dart | 2 +- .../query_builder/expressions/aggregate.dart | 6 ++-- .../query_builder/expressions/comparable.dart | 30 +++++++++++-------- .../query_builder/expressions/expression.dart | 4 +-- .../runtime/query_builder/expressions/in.dart | 2 +- .../query_builder/statements/query.dart | 2 +- .../statements/select/select_with_join.dart | 2 +- 8 files changed, 28 insertions(+), 24 deletions(-) diff --git a/moor/lib/src/runtime/api/batch.dart b/moor/lib/src/runtime/api/batch.dart index 4f00c795..85328e1f 100644 --- a/moor/lib/src/runtime/api/batch.dart +++ b/moor/lib/src/runtime/api/batch.dart @@ -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( TableInfo table, Insertable row, - {Expression Function(T table)? where}) { + {Expression 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( - TableInfo table, Expression Function(T tbl) filter) { + TableInfo table, Expression Function(T tbl) filter) { _addUpdate(table, UpdateKind.delete); final stmt = DeleteStatement(_engine, table)..where(filter); _addContext(stmt.constructQuery()); diff --git a/moor/lib/src/runtime/query_builder/components/join.dart b/moor/lib/src/runtime/query_builder/components/join.dart index 57d822aa..faa925ae 100644 --- a/moor/lib/src/runtime/query_builder/components/join.dart +++ b/moor/lib/src/runtime/query_builder/components/join.dart @@ -31,7 +31,7 @@ class Join extends Component { /// For joins that aren't [_JoinType.cross], contains an additional predicate /// that must be matched for the join. - final Expression? on; + final Expression? on; /// Whether [table] should appear in the result set (defaults to true). /// diff --git a/moor/lib/src/runtime/query_builder/expressions/aggregate.dart b/moor/lib/src/runtime/query_builder/expressions/aggregate.dart index 8f7eb0e0..996c8215 100644 --- a/moor/lib/src/runtime/query_builder/expressions/aggregate.dart +++ b/moor/lib/src/runtime/query_builder/expressions/aggregate.dart @@ -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 countAll({Expression? filter}) { +Expression countAll({Expression? filter}) { return _AggregateExpression('COUNT', const _StarFunctionParameter(), filter: filter); } @@ -24,7 +24,7 @@ extension BaseAggregate
on Expression
{ /// 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 count({bool? distinct, Expression? filter}) { + Expression count({bool? distinct, Expression? filter}) { return _AggregateExpression('COUNT', this, filter: filter, distinct: distinct); } @@ -88,7 +88,7 @@ class _AggregateExpression extends Expression { final Where? filter; _AggregateExpression(this.functionName, this.parameter, - {Expression? filter, bool? distinct}) + {Expression? filter, bool? distinct}) : filter = filter != null ? Where(filter) : null, distinct = distinct ?? false; diff --git a/moor/lib/src/runtime/query_builder/expressions/comparable.dart b/moor/lib/src/runtime/query_builder/expressions/comparable.dart index 8d80b39f..138b8284 100644 --- a/moor/lib/src/runtime/query_builder/expressions/comparable.dart +++ b/moor/lib/src/runtime/query_builder/expressions/comparable.dart @@ -4,53 +4,57 @@ part of '../query_builder.dart'; extension ComparableExpr
?> on Expression
{ /// Returns an expression that is true if this expression is strictly bigger /// than the other expression. - Expression isBiggerThan(Expression
other) { + Expression isBiggerThan(Expression
other) { return _Comparison(this, _ComparisonOperator.more, other); } /// Returns an expression that is true if this expression is strictly bigger /// than the other value. - Expression isBiggerThanValue(DT other) => isBiggerThan(Variable(other)); + Expression 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 isBiggerOrEqual(Expression
other) { + Expression isBiggerOrEqual(Expression
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 isBiggerOrEqualValue(DT other) => - isBiggerOrEqual(Variable(other)); + Expression isBiggerOrEqualValue(DT other) { + return isBiggerOrEqual(Variable(other)); + } /// Returns an expression that is true if this expression is strictly smaller /// than the other expression. - Expression isSmallerThan(Expression
other) { + Expression isSmallerThan(Expression
other) { return _Comparison(this, _ComparisonOperator.less, other); } /// Returns an expression that is true if this expression is strictly smaller /// than the other value. - Expression isSmallerThanValue(DT other) => + Expression 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 isSmallerOrEqual(Expression
other) { + Expression isSmallerOrEqual(Expression
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 isSmallerOrEqualValue(DT other) => - isSmallerOrEqual(Variable(other)); + Expression 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 isBetween(Expression
lower, Expression
higher, + Expression isBetween(Expression
lower, Expression
higher, {bool not = false}) { return _BetweenExpression( target: this, lower: lower, higher: higher, not: not); @@ -60,7 +64,7 @@ extension ComparableExpr
?> on Expression
{ /// [lower] and [higher] (both inclusive). /// /// If [not] is set, the expression will be negated. - Expression isBetweenValues(DT lower, DT higher, {bool not = false}) { + Expression isBetweenValues(DT lower, DT higher, {bool not = false}) { return _BetweenExpression( target: this, lower: Variable
(lower), @@ -70,7 +74,7 @@ extension ComparableExpr
?> on Expression
{ } } -class _BetweenExpression extends Expression { +class _BetweenExpression extends Expression { final Expression target; // https://www.sqlite.org/lang_expr.html#between diff --git a/moor/lib/src/runtime/query_builder/expressions/expression.dart b/moor/lib/src/runtime/query_builder/expressions/expression.dart index e2a0ae0b..fe7a4854 100644 --- a/moor/lib/src/runtime/query_builder/expressions/expression.dart +++ b/moor/lib/src/runtime/query_builder/expressions/expression.dart @@ -58,13 +58,13 @@ abstract class Expression implements FunctionParameter { /// An expression that is true if `this` resolves to any of the values in /// [values]. - Expression isIn(Iterable values) { + Expression isIn(Iterable 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 isNotIn(Iterable values) { + Expression isNotIn(Iterable values) { return _InExpression(this, values.toList(), true); } diff --git a/moor/lib/src/runtime/query_builder/expressions/in.dart b/moor/lib/src/runtime/query_builder/expressions/in.dart index 76367bc4..5da25521 100644 --- a/moor/lib/src/runtime/query_builder/expressions/in.dart +++ b/moor/lib/src/runtime/query_builder/expressions/in.dart @@ -1,6 +1,6 @@ part of '../query_builder.dart'; -class _InExpression extends Expression { +class _InExpression extends Expression { final Expression _expression; final List _values; final bool _not; diff --git a/moor/lib/src/runtime/query_builder/statements/query.dart b/moor/lib/src/runtime/query_builder/statements/query.dart index d0b65f02..3508017d 100644 --- a/moor/lib/src/runtime/query_builder/statements/query.dart +++ b/moor/lib/src/runtime/query_builder/statements/query.dart @@ -176,7 +176,7 @@ mixin SingleTableQueryMixin /// 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 Function(T tbl) filter) { + void where(Expression Function(T tbl) filter) { final predicate = filter(table.asDslTable); if (whereExpr == null) { diff --git a/moor/lib/src/runtime/query_builder/statements/select/select_with_join.dart b/moor/lib/src/runtime/query_builder/statements/select/select_with_join.dart index 21db5d3a..70b259f9 100644 --- a/moor/lib/src/runtime/query_builder/statements/select/select_with_join.dart +++ b/moor/lib/src/runtime/query_builder/statements/select/select_with_join.dart @@ -165,7 +165,7 @@ class JoinedSelectStatement /// Groups the result by values in [expressions]. /// /// An optional [having] attribute can be set to exclude certain groups. - void groupBy(Iterable expressions, {Expression? having}) { + void groupBy(Iterable expressions, {Expression? having}) { _groupBy = GroupBy._(expressions.toList(), having); }