Add customStatement to batch api (#817)

This commit is contained in:
Simon Binder 2020-09-11 11:19:18 +02:00
parent c303781bb1
commit 9d9a4f4065
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 21 additions and 3 deletions

View File

@ -139,10 +139,24 @@ class Batch {
_addContext(stmt.constructQuery());
}
void _addContext(GenerationContext ctx) {
final sql = ctx.sql;
final arguments = ctx.boundVariables;
/// Executes the custom [sql] statement with variables instantiated to [args].
///
/// The statement will be added to this batch and executed when the batch
/// completes. So, this method returns synchronously and it's not possible to
/// inspect the return value of individual statements.
///
/// See also:
/// - [QueryEngine.customStatement], the equivalent method outside of
/// batches.
void customStatement(String sql, [List<dynamic> args]) {
_addSqlAndArguments(sql, args);
}
void _addContext(GenerationContext ctx) {
_addSqlAndArguments(ctx.sql, ctx.boundVariables);
}
void _addSqlAndArguments(String sql, List<dynamic> arguments) {
final stmtIndex = _sqlToIndex.putIfAbsent(sql, () {
final newIndex = _createdSql.length;
_createdSql.add(sql);

View File

@ -42,6 +42,8 @@ void main() {
b.delete(db.todosTable, const TodosTableCompanion(id: Value(3)));
b.update(db.users, const UsersCompanion(name: Value('new name 2')));
b.customStatement('some custom statement', [4]);
});
final transaction = executor.transactions;
@ -55,6 +57,7 @@ void main() {
'UPDATE categories SET `desc` = ?, priority = 0 WHERE id = ?;',
'DELETE FROM categories WHERE 1;',
'DELETE FROM todos WHERE id = ?;',
'some custom statement',
],
[
ArgumentsForBatchedStatement(0, ['first']),
@ -66,6 +69,7 @@ void main() {
ArgumentsForBatchedStatement(4, []),
ArgumentsForBatchedStatement(5, [3]),
ArgumentsForBatchedStatement(1, ['new name 2']),
ArgumentsForBatchedStatement(6, [4]),
],
),
),