Support delete statements in batches

Closes #325
This commit is contained in:
Simon Binder 2020-01-07 11:31:35 +01:00
parent a6e4a3669c
commit c5c8dc7d6d
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 17 additions and 0 deletions

View File

@ -13,6 +13,7 @@
To create them in `onUpgrade`, use the new `createIndex` and `createTrigger` functions on a `Migrator`.
- Support for moor-file queries that run on initialization ([#280](https://github.com/simolus3/moor/issues/280))
Declare them like this `@create: INSERT INTO users VALUES ('default', 'user')`
- Support deletes in batches ([#325](https://github.com/simolus3/moor/issues/325))
## 2.2.0

View File

@ -94,6 +94,16 @@ class Batch {
}
}
/// Deletes all rows from [table] matching the provided [filter].
///
/// See also:
/// - [QueryEngine.delete]
void delete<T extends Table, D extends DataClass>(TableInfo<T, D> table,
Expression<bool, BoolType> Function(T tbl) filter) {
final stmt = DeleteStatement(_engine, table)..where(filter);
_addContext(stmt.constructQuery());
}
void _addContext(GenerationContext ctx) {
final sql = ctx.sql;
final variableSet = _createdStatements.putIfAbsent(sql, () => []);

View File

@ -37,6 +37,8 @@ void main() {
CategoriesCompanion(id: Value(1), description: Value('new1')),
CategoriesCompanion(id: Value(2), description: Value('new2')),
]);
b.delete(db.categories, (_) => const Constant(true));
});
final transaction = executor.transactions;
@ -67,6 +69,10 @@ void main() {
['new2', 2],
],
),
BatchedStatement(
'DELETE FROM categories WHERE 1;',
[[]],
),
]));
});