mirror of https://github.com/AMT-Cheif/drift.git
Support random ordering
This commit is contained in:
parent
61c2fff4e2
commit
32f859c14c
|
@ -6,26 +6,33 @@ enum OrderingMode {
|
|||
asc,
|
||||
|
||||
/// Descending ordering mode (highest items first)
|
||||
desc
|
||||
desc,
|
||||
|
||||
/// Random ordering mode
|
||||
random,
|
||||
}
|
||||
|
||||
const _modeToString = {
|
||||
OrderingMode.asc: 'ASC',
|
||||
OrderingMode.desc: 'DESC',
|
||||
OrderingMode.random: 'RANDOM()',
|
||||
};
|
||||
|
||||
/// A single term in a [OrderBy] clause. The priority of this term is determined
|
||||
/// by its position in [OrderBy.terms].
|
||||
class OrderingTerm extends Component {
|
||||
/// The expression after which the ordering should happen
|
||||
final Expression expression;
|
||||
final Expression? _expression;
|
||||
|
||||
/// The ordering mode (ascending or descending).
|
||||
final OrderingMode mode;
|
||||
|
||||
/// Creates an ordering term by the [expression] and the [mode] (defaults to
|
||||
/// ascending).
|
||||
OrderingTerm({required this.expression, this.mode = OrderingMode.asc});
|
||||
///
|
||||
/// When [mode] is [OrderingMode.random], the [expression] will be ignored
|
||||
OrderingTerm({required Expression expression, this.mode = OrderingMode.asc})
|
||||
: _expression = expression;
|
||||
|
||||
/// Creates an ordering term that sorts for ascending values of [expression].
|
||||
factory OrderingTerm.asc(Expression expression) {
|
||||
|
@ -37,11 +44,19 @@ class OrderingTerm extends Component {
|
|||
return OrderingTerm(expression: expression, mode: OrderingMode.desc);
|
||||
}
|
||||
|
||||
/// Creates an ordering term to get a number of random rows
|
||||
/// using sqlite random function.
|
||||
OrderingTerm.random()
|
||||
: _expression = null,
|
||||
mode = OrderingMode.random;
|
||||
|
||||
@override
|
||||
void writeInto(GenerationContext context) {
|
||||
expression.writeInto(context);
|
||||
context.writeWhitespace();
|
||||
if (mode != OrderingMode.random) {
|
||||
_expression!.writeInto(context);
|
||||
}
|
||||
context.buffer.write(_modeToString[mode]);
|
||||
context.writeWhitespace();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue