mirror of https://github.com/AMT-Cheif/drift.git
commit
6d1a4d3c3c
|
@ -8,6 +8,7 @@
|
|||
- Allow the generator to emit correct SQL code when using arrays with the
|
||||
`new_sql_code_generation` option in specific scenarios.
|
||||
- Add the `generatedAs` method to declare generated columns for Dart tables.
|
||||
- Add `OrderingTerm.random` to fetch rows in a random order.
|
||||
- Improved support for pausing query stream subscriptions. Instead of buffering events,
|
||||
query streams will suspend fetching data if all listeners are paused.
|
||||
|
||||
|
|
|
@ -37,6 +37,12 @@ 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.
|
||||
factory OrderingTerm.random() {
|
||||
return OrderingTerm(expression: FunctionCallExpression('random', []));
|
||||
}
|
||||
|
||||
@override
|
||||
void writeInto(GenerationContext context) {
|
||||
expression.writeInto(context);
|
||||
|
|
|
@ -13,6 +13,8 @@ void main() {
|
|||
db = TodoDb(NativeDatabase.memory());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
||||
test('insertOnConflictUpdate', () async {
|
||||
await db.into(db.categories).insert(
|
||||
CategoriesCompanion.insert(description: 'original description'));
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
@TestOn('vm')
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../data/tables/todos.dart';
|
||||
|
||||
void main() {
|
||||
late TodoDb db;
|
||||
|
||||
setUp(() {
|
||||
db = TodoDb(NativeDatabase.memory());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
||||
test('can use random ordering', () async {
|
||||
await db.batch((b) {
|
||||
b.insertAll(db.users, [
|
||||
for (var i = 0; i < 1000; i++)
|
||||
UsersCompanion.insert(
|
||||
name: 'user name', profilePicture: Uint8List(0)),
|
||||
]);
|
||||
});
|
||||
|
||||
final rows = await (db.select(db.users)
|
||||
..orderBy([(_) => OrderingTerm.random()]))
|
||||
.get();
|
||||
expect(rows.isSorted((a, b) => a.id.compareTo(b.id)), isFalse);
|
||||
});
|
||||
}
|
|
@ -70,6 +70,14 @@ void main() {
|
|||
argThat(isEmpty)));
|
||||
});
|
||||
|
||||
test('with random order by clause', () async {
|
||||
await (db.select(db.users)..orderBy([(u) => OrderingTerm.random()]))
|
||||
.get();
|
||||
|
||||
verify(executor.runSelect(
|
||||
'SELECT * FROM users ORDER BY random() ASC;', argThat(isEmpty)));
|
||||
});
|
||||
|
||||
test('with complex predicates', () async {
|
||||
await (db.select(db.users)
|
||||
..where((u) =>
|
||||
|
|
Loading…
Reference in New Issue