Batch: Delete method for insertables

This commit is contained in:
Simon Binder 2020-01-08 12:14:04 +01:00
parent c2ec06c1de
commit cefa290d7e
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 20 additions and 2 deletions

View File

@ -94,11 +94,22 @@ class Batch {
}
}
/// Deletes [row] from the [table] when this batch is executed.
///
/// See also:
/// - [QueryEngine.delete]
/// - [DeleteStatement.delete]
void delete<T extends Table, D extends DataClass>(
TableInfo<T, D> table, Insertable<D> row) {
final stmt = DeleteStatement(_engine, table)..whereSamePrimaryKey(row);
_addContext(stmt.constructQuery());
}
/// 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,
void deleteWhere<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());

View File

@ -38,7 +38,8 @@ void main() {
CategoriesCompanion(id: Value(2), description: Value('new2')),
]);
b.delete(db.categories, (_) => const Constant(true));
b.deleteWhere(db.categories, (_) => const Constant(true));
b.delete(db.todosTable, const TodosTableCompanion(id: Value(3)));
});
final transaction = executor.transactions;
@ -73,6 +74,12 @@ void main() {
'DELETE FROM categories WHERE 1;',
[[]],
),
BatchedStatement(
'DELETE FROM todos WHERE id = ?;',
[
[3]
],
),
]));
});