Rename `nullsOrder` to `nulls`, move sql strings to enums, add `nulls` argument to the factory methods

This commit is contained in:
Vorkytaka 2022-08-28 20:20:55 +03:00
parent 8cb029a73e
commit a4bff1f9ef
2 changed files with 40 additions and 32 deletions

View File

@ -3,30 +3,28 @@ part of '../query_builder.dart';
/// Describes how to order rows /// Describes how to order rows
enum OrderingMode { enum OrderingMode {
/// Ascending ordering mode (lowest items first) /// Ascending ordering mode (lowest items first)
asc, asc._('ASC'),
/// Descending ordering mode (highest items first) /// Descending ordering mode (highest items first)
desc desc._('DESC');
}
const _modeToString = { final String _mode;
OrderingMode.asc: 'ASC',
OrderingMode.desc: 'DESC', const OrderingMode._(this._mode);
}; }
/// Describes how to order nulls /// Describes how to order nulls
enum NullsOrder { enum NullsOrder {
/// Place NULLs at the start /// Place NULLs at the start
first, first._('NULLS FIRST'),
/// Place NULLs at the end /// Place NULLs at the end
last, last._('NULLS LAST');
}
const _nullsOrderToString = { final String _order;
NullsOrder.first: 'NULLS FIRST',
NullsOrder.last: 'NULLS LAST', const NullsOrder._(this._order);
}; }
/// A single term in a [OrderBy] clause. The priority of this term is determined /// A single term in a [OrderBy] clause. The priority of this term is determined
/// by its position in [OrderBy.terms]. /// by its position in [OrderBy.terms].
@ -38,29 +36,39 @@ class OrderingTerm extends Component {
final OrderingMode mode; final OrderingMode mode;
/// How to order NULLs. /// 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 /// 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 /// newer. When using `sqlite3_flutter_libs` or a web database, this is not
/// a problem. /// a problem.
final NullsOrder? nullsOrder; final NullsOrder? nulls;
/// Creates an ordering term by the [expression] and the [mode] (defaults to /// Creates an ordering term by the [expression], the [mode] (defaults to
/// ascending). /// ascending) and the [nulls].
OrderingTerm({ OrderingTerm({
required this.expression, required this.expression,
this.mode = OrderingMode.asc, this.mode = OrderingMode.asc,
this.nullsOrder, this.nulls,
}); });
/// Creates an ordering term that sorts for ascending values of [expression]. /// Creates an ordering term that sorts for ascending values
factory OrderingTerm.asc(Expression expression) { /// of [expression] and the [nulls].
return OrderingTerm(expression: expression, mode: OrderingMode.asc); 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]. /// Creates an ordering term that sorts for descending values
factory OrderingTerm.desc(Expression expression) { /// of [expression] and the [nulls].
return OrderingTerm(expression: expression, mode: OrderingMode.desc); 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 /// Creates an ordering term to get a number of random rows
@ -73,10 +81,10 @@ class OrderingTerm extends Component {
void writeInto(GenerationContext context) { void writeInto(GenerationContext context) {
expression.writeInto(context); expression.writeInto(context);
context.writeWhitespace(); context.writeWhitespace();
context.buffer.write(_modeToString[mode]); context.buffer.write(mode._mode);
if (nullsOrder != null) { if (nulls != null) {
context.writeWhitespace(); context.writeWhitespace();
context.buffer.write(_nullsOrderToString[nullsOrder]); context.buffer.write(nulls?._order);
} }
} }
} }

View File

@ -29,7 +29,7 @@ void main() {
query.orderBy([ query.orderBy([
(tbl) => OrderingTerm( (tbl) => OrderingTerm(
expression: tbl.name, expression: tbl.name,
nullsOrder: NullsOrder.last, nulls: NullsOrder.last,
), ),
]); ]);
await query.get(); await query.get();
@ -44,7 +44,7 @@ void main() {
query.orderBy([ query.orderBy([
(tbl) => OrderingTerm( (tbl) => OrderingTerm(
expression: tbl.name, expression: tbl.name,
nullsOrder: NullsOrder.first, nulls: NullsOrder.first,
), ),
]); ]);
await query.get(); await query.get();
@ -59,14 +59,14 @@ void main() {
query.orderBy([ query.orderBy([
(tbl) => OrderingTerm( (tbl) => OrderingTerm(
expression: tbl.name, expression: tbl.name,
nullsOrder: NullsOrder.first, nulls: NullsOrder.first,
), ),
(tbl) => OrderingTerm( (tbl) => OrderingTerm(
expression: tbl.creationTime, expression: tbl.creationTime,
), ),
(tbl) => OrderingTerm( (tbl) => OrderingTerm(
expression: tbl.profilePicture, expression: tbl.profilePicture,
nullsOrder: NullsOrder.last, nulls: NullsOrder.last,
), ),
]); ]);
await query.get(); await query.get();