Merge pull request #1567 from Husssam12/develop

Add random ordering
This commit is contained in:
Simon Binder 2021-11-29 11:49:40 +01:00 committed by GitHub
commit 6d1a4d3c3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 0 deletions

View File

@ -8,6 +8,7 @@
- Allow the generator to emit correct SQL code when using arrays with the - Allow the generator to emit correct SQL code when using arrays with the
`new_sql_code_generation` option in specific scenarios. `new_sql_code_generation` option in specific scenarios.
- Add the `generatedAs` method to declare generated columns for Dart tables. - 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, - Improved support for pausing query stream subscriptions. Instead of buffering events,
query streams will suspend fetching data if all listeners are paused. query streams will suspend fetching data if all listeners are paused.

View File

@ -37,6 +37,12 @@ class OrderingTerm extends Component {
return OrderingTerm(expression: expression, mode: OrderingMode.desc); 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 @override
void writeInto(GenerationContext context) { void writeInto(GenerationContext context) {
expression.writeInto(context); expression.writeInto(context);

View File

@ -13,6 +13,8 @@ void main() {
db = TodoDb(NativeDatabase.memory()); db = TodoDb(NativeDatabase.memory());
}); });
tearDown(() => db.close());
test('insertOnConflictUpdate', () async { test('insertOnConflictUpdate', () async {
await db.into(db.categories).insert( await db.into(db.categories).insert(
CategoriesCompanion.insert(description: 'original description')); CategoriesCompanion.insert(description: 'original description'));

View File

@ -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);
});
}

View File

@ -70,6 +70,14 @@ void main() {
argThat(isEmpty))); 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 { test('with complex predicates', () async {
await (db.select(db.users) await (db.select(db.users)
..where((u) => ..where((u) =>