From c5c8dc7d6d94f0af2e9a67b50a8765544625dc83 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 7 Jan 2020 11:31:35 +0100 Subject: [PATCH] Support delete statements in batches Closes #325 --- moor/CHANGELOG.md | 1 + moor/lib/src/runtime/api/batch.dart | 10 ++++++++++ moor/test/batch_test.dart | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/moor/CHANGELOG.md b/moor/CHANGELOG.md index 0b7390db..924942a4 100644 --- a/moor/CHANGELOG.md +++ b/moor/CHANGELOG.md @@ -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 diff --git a/moor/lib/src/runtime/api/batch.dart b/moor/lib/src/runtime/api/batch.dart index 75602e78..8f2e7ecd 100644 --- a/moor/lib/src/runtime/api/batch.dart +++ b/moor/lib/src/runtime/api/batch.dart @@ -94,6 +94,16 @@ class Batch { } } + /// Deletes all rows from [table] matching the provided [filter]. + /// + /// See also: + /// - [QueryEngine.delete] + void delete(TableInfo table, + Expression 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, () => []); diff --git a/moor/test/batch_test.dart b/moor/test/batch_test.dart index 997ee71a..d7dfdc56 100644 --- a/moor/test/batch_test.dart +++ b/moor/test/batch_test.dart @@ -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;', + [[]], + ), ])); });