mirror of https://github.com/AMT-Cheif/drift.git
Allow Expression<bool?> in more places
This commit is contained in:
parent
6aa022e547
commit
d9cf6660ec
|
@ -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)
|
/// [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>(
|
void update<T extends Table, D extends DataClass>(
|
||||||
TableInfo<T, D> table, Insertable<D> row,
|
TableInfo<T, D> table, Insertable<D> row,
|
||||||
{Expression<bool> Function(T table)? where}) {
|
{Expression<bool?> Function(T table)? where}) {
|
||||||
_addUpdate(table, UpdateKind.update);
|
_addUpdate(table, UpdateKind.update);
|
||||||
final stmt = UpdateStatement(_engine, table);
|
final stmt = UpdateStatement(_engine, table);
|
||||||
if (where != null) stmt.where(where);
|
if (where != null) stmt.where(where);
|
||||||
|
@ -133,7 +133,7 @@ class Batch {
|
||||||
/// See also:
|
/// See also:
|
||||||
/// - [QueryEngine.delete]
|
/// - [QueryEngine.delete]
|
||||||
void deleteWhere<T extends Table, D extends DataClass>(
|
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);
|
_addUpdate(table, UpdateKind.delete);
|
||||||
final stmt = DeleteStatement(_engine, table)..where(filter);
|
final stmt = DeleteStatement(_engine, table)..where(filter);
|
||||||
_addContext(stmt.constructQuery());
|
_addContext(stmt.constructQuery());
|
||||||
|
|
|
@ -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
|
/// For joins that aren't [_JoinType.cross], contains an additional predicate
|
||||||
/// that must be matched for the join.
|
/// 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).
|
/// Whether [table] should appear in the result set (defaults to true).
|
||||||
///
|
///
|
||||||
|
|
|
@ -9,7 +9,7 @@ part of '../query_builder.dart';
|
||||||
///
|
///
|
||||||
/// This is equivalent to the `COUNT(*) FILTER (WHERE filter)` sql function. The
|
/// This is equivalent to the `COUNT(*) FILTER (WHERE filter)` sql function. The
|
||||||
/// filter will be omitted if null.
|
/// filter will be omitted if null.
|
||||||
Expression<int> countAll({Expression<bool>? filter}) {
|
Expression<int> countAll({Expression<bool?>? filter}) {
|
||||||
return _AggregateExpression('COUNT', const _StarFunctionParameter(),
|
return _AggregateExpression('COUNT', const _StarFunctionParameter(),
|
||||||
filter: filter);
|
filter: filter);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ extension BaseAggregate<DT> on Expression<DT> {
|
||||||
/// counted twice. An optional [filter] can be used to only include values
|
/// counted twice. An optional [filter] can be used to only include values
|
||||||
/// matching the filter. Note that [filter] is only available from sqlite
|
/// matching the filter. Note that [filter] is only available from sqlite
|
||||||
/// 3.30 and most devices will use an older sqlite version.
|
/// 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,
|
return _AggregateExpression('COUNT', this,
|
||||||
filter: filter, distinct: distinct);
|
filter: filter, distinct: distinct);
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ class _AggregateExpression<D> extends Expression<D> {
|
||||||
final Where? filter;
|
final Where? filter;
|
||||||
|
|
||||||
_AggregateExpression(this.functionName, this.parameter,
|
_AggregateExpression(this.functionName, this.parameter,
|
||||||
{Expression<bool>? filter, bool? distinct})
|
{Expression<bool?>? filter, bool? distinct})
|
||||||
: filter = filter != null ? Where(filter) : null,
|
: filter = filter != null ? Where(filter) : null,
|
||||||
distinct = distinct ?? false;
|
distinct = distinct ?? false;
|
||||||
|
|
||||||
|
|
|
@ -4,53 +4,57 @@ part of '../query_builder.dart';
|
||||||
extension ComparableExpr<DT extends Comparable<dynamic>?> on Expression<DT> {
|
extension ComparableExpr<DT extends Comparable<dynamic>?> on Expression<DT> {
|
||||||
/// Returns an expression that is true if this expression is strictly bigger
|
/// Returns an expression that is true if this expression is strictly bigger
|
||||||
/// than the other expression.
|
/// than the other expression.
|
||||||
Expression<bool> isBiggerThan(Expression<DT> other) {
|
Expression<bool?> isBiggerThan(Expression<DT> other) {
|
||||||
return _Comparison(this, _ComparisonOperator.more, other);
|
return _Comparison(this, _ComparisonOperator.more, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an expression that is true if this expression is strictly bigger
|
/// Returns an expression that is true if this expression is strictly bigger
|
||||||
/// than the other value.
|
/// 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
|
/// Returns an expression that is true if this expression is bigger than or
|
||||||
/// equal to he other expression.
|
/// equal to he other expression.
|
||||||
Expression<bool> isBiggerOrEqual(Expression<DT> other) {
|
Expression<bool?> isBiggerOrEqual(Expression<DT> other) {
|
||||||
return _Comparison(this, _ComparisonOperator.moreOrEqual, other);
|
return _Comparison(this, _ComparisonOperator.moreOrEqual, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an expression that is true if this expression is bigger than or
|
/// Returns an expression that is true if this expression is bigger than or
|
||||||
/// equal to he other value.
|
/// equal to he other value.
|
||||||
Expression<bool> isBiggerOrEqualValue(DT other) =>
|
Expression<bool?> isBiggerOrEqualValue(DT other) {
|
||||||
isBiggerOrEqual(Variable(other));
|
return isBiggerOrEqual(Variable(other));
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns an expression that is true if this expression is strictly smaller
|
/// Returns an expression that is true if this expression is strictly smaller
|
||||||
/// than the other expression.
|
/// than the other expression.
|
||||||
Expression<bool> isSmallerThan(Expression<DT> other) {
|
Expression<bool?> isSmallerThan(Expression<DT> other) {
|
||||||
return _Comparison(this, _ComparisonOperator.less, other);
|
return _Comparison(this, _ComparisonOperator.less, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an expression that is true if this expression is strictly smaller
|
/// Returns an expression that is true if this expression is strictly smaller
|
||||||
/// than the other value.
|
/// than the other value.
|
||||||
Expression<bool> isSmallerThanValue(DT other) =>
|
Expression<bool?> isSmallerThanValue(DT other) =>
|
||||||
isSmallerThan(Variable(other));
|
isSmallerThan(Variable(other));
|
||||||
|
|
||||||
/// Returns an expression that is true if this expression is smaller than or
|
/// Returns an expression that is true if this expression is smaller than or
|
||||||
/// equal to he other expression.
|
/// equal to he other expression.
|
||||||
Expression<bool> isSmallerOrEqual(Expression<DT> other) {
|
Expression<bool?> isSmallerOrEqual(Expression<DT> other) {
|
||||||
return _Comparison(this, _ComparisonOperator.lessOrEqual, other);
|
return _Comparison(this, _ComparisonOperator.lessOrEqual, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an expression that is true if this expression is smaller than or
|
/// Returns an expression that is true if this expression is smaller than or
|
||||||
/// equal to he other value.
|
/// equal to he other value.
|
||||||
Expression<bool> isSmallerOrEqualValue(DT other) =>
|
Expression<bool?> isSmallerOrEqualValue(DT other) {
|
||||||
isSmallerOrEqual(Variable(other));
|
return isSmallerOrEqual(Variable(other));
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns an expression evaluating to true if this expression is between
|
/// Returns an expression evaluating to true if this expression is between
|
||||||
/// [lower] and [higher] (both inclusive).
|
/// [lower] and [higher] (both inclusive).
|
||||||
///
|
///
|
||||||
/// If [not] is set, the expression will be negated. To compare this
|
/// If [not] is set, the expression will be negated. To compare this
|
||||||
/// expression against two values, see
|
/// 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}) {
|
{bool not = false}) {
|
||||||
return _BetweenExpression(
|
return _BetweenExpression(
|
||||||
target: this, lower: lower, higher: higher, not: not);
|
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).
|
/// [lower] and [higher] (both inclusive).
|
||||||
///
|
///
|
||||||
/// If [not] is set, the expression will be negated.
|
/// 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(
|
return _BetweenExpression(
|
||||||
target: this,
|
target: this,
|
||||||
lower: Variable<DT>(lower),
|
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;
|
final Expression target;
|
||||||
|
|
||||||
// https://www.sqlite.org/lang_expr.html#between
|
// https://www.sqlite.org/lang_expr.html#between
|
||||||
|
|
|
@ -58,13 +58,13 @@ abstract class Expression<D> implements FunctionParameter {
|
||||||
|
|
||||||
/// An expression that is true if `this` resolves to any of the values in
|
/// An expression that is true if `this` resolves to any of the values in
|
||||||
/// [values].
|
/// [values].
|
||||||
Expression<bool> isIn(Iterable<D> values) {
|
Expression<bool?> isIn(Iterable<D> values) {
|
||||||
return _InExpression(this, values.toList(), false);
|
return _InExpression(this, values.toList(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An expression that is true if `this` does not resolve to any of the values
|
/// An expression that is true if `this` does not resolve to any of the values
|
||||||
/// in [values].
|
/// in [values].
|
||||||
Expression<bool> isNotIn(Iterable<D> values) {
|
Expression<bool?> isNotIn(Iterable<D> values) {
|
||||||
return _InExpression(this, values.toList(), true);
|
return _InExpression(this, values.toList(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
part of '../query_builder.dart';
|
part of '../query_builder.dart';
|
||||||
|
|
||||||
class _InExpression<T> extends Expression<bool> {
|
class _InExpression<T> extends Expression<bool?> {
|
||||||
final Expression<T> _expression;
|
final Expression<T> _expression;
|
||||||
final List<T> _values;
|
final List<T> _values;
|
||||||
final bool _not;
|
final bool _not;
|
||||||
|
|
|
@ -176,7 +176,7 @@ mixin SingleTableQueryMixin<T extends Table, D extends DataClass>
|
||||||
/// which explains how to express most SQL expressions in Dart.
|
/// which explains how to express most SQL expressions in Dart.
|
||||||
/// If you want to remove duplicate rows from a query, use the `distinct`
|
/// If you want to remove duplicate rows from a query, use the `distinct`
|
||||||
/// parameter on [QueryEngine.select].
|
/// 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);
|
final predicate = filter(table.asDslTable);
|
||||||
|
|
||||||
if (whereExpr == null) {
|
if (whereExpr == null) {
|
||||||
|
|
|
@ -165,7 +165,7 @@ class JoinedSelectStatement<FirstT extends Table, FirstD extends DataClass>
|
||||||
/// Groups the result by values in [expressions].
|
/// Groups the result by values in [expressions].
|
||||||
///
|
///
|
||||||
/// An optional [having] attribute can be set to exclude certain groups.
|
/// 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);
|
_groupBy = GroupBy._(expressions.toList(), having);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue