mirror of https://github.com/AMT-Cheif/drift.git
Add customStatement to batch api (#817)
This commit is contained in:
parent
c303781bb1
commit
9d9a4f4065
|
@ -139,10 +139,24 @@ class Batch {
|
||||||
_addContext(stmt.constructQuery());
|
_addContext(stmt.constructQuery());
|
||||||
}
|
}
|
||||||
|
|
||||||
void _addContext(GenerationContext ctx) {
|
/// Executes the custom [sql] statement with variables instantiated to [args].
|
||||||
final sql = ctx.sql;
|
///
|
||||||
final arguments = ctx.boundVariables;
|
/// 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 stmtIndex = _sqlToIndex.putIfAbsent(sql, () {
|
||||||
final newIndex = _createdSql.length;
|
final newIndex = _createdSql.length;
|
||||||
_createdSql.add(sql);
|
_createdSql.add(sql);
|
||||||
|
|
|
@ -42,6 +42,8 @@ void main() {
|
||||||
b.delete(db.todosTable, const TodosTableCompanion(id: Value(3)));
|
b.delete(db.todosTable, const TodosTableCompanion(id: Value(3)));
|
||||||
|
|
||||||
b.update(db.users, const UsersCompanion(name: Value('new name 2')));
|
b.update(db.users, const UsersCompanion(name: Value('new name 2')));
|
||||||
|
|
||||||
|
b.customStatement('some custom statement', [4]);
|
||||||
});
|
});
|
||||||
|
|
||||||
final transaction = executor.transactions;
|
final transaction = executor.transactions;
|
||||||
|
@ -55,6 +57,7 @@ void main() {
|
||||||
'UPDATE categories SET `desc` = ?, priority = 0 WHERE id = ?;',
|
'UPDATE categories SET `desc` = ?, priority = 0 WHERE id = ?;',
|
||||||
'DELETE FROM categories WHERE 1;',
|
'DELETE FROM categories WHERE 1;',
|
||||||
'DELETE FROM todos WHERE id = ?;',
|
'DELETE FROM todos WHERE id = ?;',
|
||||||
|
'some custom statement',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
ArgumentsForBatchedStatement(0, ['first']),
|
ArgumentsForBatchedStatement(0, ['first']),
|
||||||
|
@ -66,6 +69,7 @@ void main() {
|
||||||
ArgumentsForBatchedStatement(4, []),
|
ArgumentsForBatchedStatement(4, []),
|
||||||
ArgumentsForBatchedStatement(5, [3]),
|
ArgumentsForBatchedStatement(5, [3]),
|
||||||
ArgumentsForBatchedStatement(1, ['new name 2']),
|
ArgumentsForBatchedStatement(1, ['new name 2']),
|
||||||
|
ArgumentsForBatchedStatement(6, [4]),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue