mirror of https://github.com/AMT-Cheif/drift.git
Rename `nullsOrder` to `nulls`, move sql strings to enums, add `nulls` argument to the factory methods
This commit is contained in:
parent
8cb029a73e
commit
a4bff1f9ef
|
@ -3,30 +3,28 @@ part of '../query_builder.dart';
|
|||
/// Describes how to order rows
|
||||
enum OrderingMode {
|
||||
/// Ascending ordering mode (lowest items first)
|
||||
asc,
|
||||
asc._('ASC'),
|
||||
|
||||
/// Descending ordering mode (highest items first)
|
||||
desc
|
||||
}
|
||||
desc._('DESC');
|
||||
|
||||
const _modeToString = {
|
||||
OrderingMode.asc: 'ASC',
|
||||
OrderingMode.desc: 'DESC',
|
||||
};
|
||||
final String _mode;
|
||||
|
||||
const OrderingMode._(this._mode);
|
||||
}
|
||||
|
||||
/// Describes how to order nulls
|
||||
enum NullsOrder {
|
||||
/// Place NULLs at the start
|
||||
first,
|
||||
first._('NULLS FIRST'),
|
||||
|
||||
/// Place NULLs at the end
|
||||
last,
|
||||
}
|
||||
last._('NULLS LAST');
|
||||
|
||||
const _nullsOrderToString = {
|
||||
NullsOrder.first: 'NULLS FIRST',
|
||||
NullsOrder.last: 'NULLS LAST',
|
||||
};
|
||||
final String _order;
|
||||
|
||||
const NullsOrder._(this._order);
|
||||
}
|
||||
|
||||
/// A single term in a [OrderBy] clause. The priority of this term is determined
|
||||
/// by its position in [OrderBy.terms].
|
||||
|
@ -38,29 +36,39 @@ class OrderingTerm extends Component {
|
|||
final OrderingMode mode;
|
||||
|
||||
/// How to order NULLs.
|
||||
/// When [nullsOrder] is [null], then it's ignored.
|
||||
/// When [nulls] is [null], then it's ignored.
|
||||
///
|
||||
/// Note that this feature are only available in sqlite3 version `3.30.0` and
|
||||
/// newer. When using `sqlite3_flutter_libs` or a web database, this is not
|
||||
/// a problem.
|
||||
final NullsOrder? nullsOrder;
|
||||
final NullsOrder? nulls;
|
||||
|
||||
/// Creates an ordering term by the [expression] and the [mode] (defaults to
|
||||
/// ascending).
|
||||
/// Creates an ordering term by the [expression], the [mode] (defaults to
|
||||
/// ascending) and the [nulls].
|
||||
OrderingTerm({
|
||||
required this.expression,
|
||||
this.mode = OrderingMode.asc,
|
||||
this.nullsOrder,
|
||||
this.nulls,
|
||||
});
|
||||
|
||||
/// Creates an ordering term that sorts for ascending values of [expression].
|
||||
factory OrderingTerm.asc(Expression expression) {
|
||||
return OrderingTerm(expression: expression, mode: OrderingMode.asc);
|
||||
/// Creates an ordering term that sorts for ascending values
|
||||
/// of [expression] and the [nulls].
|
||||
factory OrderingTerm.asc(Expression expression, [NullsOrder? nulls]) {
|
||||
return OrderingTerm(
|
||||
expression: expression,
|
||||
mode: OrderingMode.asc,
|
||||
nulls: nulls,
|
||||
);
|
||||
}
|
||||
|
||||
/// Creates an ordering term that sorts for descending values of [expression].
|
||||
factory OrderingTerm.desc(Expression expression) {
|
||||
return OrderingTerm(expression: expression, mode: OrderingMode.desc);
|
||||
/// Creates an ordering term that sorts for descending values
|
||||
/// of [expression] and the [nulls].
|
||||
factory OrderingTerm.desc(Expression expression, [NullsOrder? nulls]) {
|
||||
return OrderingTerm(
|
||||
expression: expression,
|
||||
mode: OrderingMode.desc,
|
||||
nulls: nulls,
|
||||
);
|
||||
}
|
||||
|
||||
/// Creates an ordering term to get a number of random rows
|
||||
|
@ -73,10 +81,10 @@ class OrderingTerm extends Component {
|
|||
void writeInto(GenerationContext context) {
|
||||
expression.writeInto(context);
|
||||
context.writeWhitespace();
|
||||
context.buffer.write(_modeToString[mode]);
|
||||
if (nullsOrder != null) {
|
||||
context.buffer.write(mode._mode);
|
||||
if (nulls != null) {
|
||||
context.writeWhitespace();
|
||||
context.buffer.write(_nullsOrderToString[nullsOrder]);
|
||||
context.buffer.write(nulls?._order);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ void main() {
|
|||
query.orderBy([
|
||||
(tbl) => OrderingTerm(
|
||||
expression: tbl.name,
|
||||
nullsOrder: NullsOrder.last,
|
||||
nulls: NullsOrder.last,
|
||||
),
|
||||
]);
|
||||
await query.get();
|
||||
|
@ -44,7 +44,7 @@ void main() {
|
|||
query.orderBy([
|
||||
(tbl) => OrderingTerm(
|
||||
expression: tbl.name,
|
||||
nullsOrder: NullsOrder.first,
|
||||
nulls: NullsOrder.first,
|
||||
),
|
||||
]);
|
||||
await query.get();
|
||||
|
@ -59,14 +59,14 @@ void main() {
|
|||
query.orderBy([
|
||||
(tbl) => OrderingTerm(
|
||||
expression: tbl.name,
|
||||
nullsOrder: NullsOrder.first,
|
||||
nulls: NullsOrder.first,
|
||||
),
|
||||
(tbl) => OrderingTerm(
|
||||
expression: tbl.creationTime,
|
||||
),
|
||||
(tbl) => OrderingTerm(
|
||||
expression: tbl.profilePicture,
|
||||
nullsOrder: NullsOrder.last,
|
||||
nulls: NullsOrder.last,
|
||||
),
|
||||
]);
|
||||
await query.get();
|
||||
|
|
Loading…
Reference in New Issue