mirror of https://github.com/AMT-Cheif/drift.git
Write query engine override param for generated queries
This allows them to be used in transactions and post- migration callbacks.
This commit is contained in:
parent
53ea5835a8
commit
b4de942915
|
@ -1051,11 +1051,11 @@ abstract class _$TodoDb extends GeneratedDatabase {
|
|||
);
|
||||
}
|
||||
|
||||
Future<List<AllTodosWithCategoryResult>> allTodosWithCategory() {
|
||||
return customSelect(
|
||||
'SELECT t.*, c.id as catId, c."desc" as catDesc FROM todos t INNER JOIN categories c ON c.id = t.category',
|
||||
variables: [])
|
||||
.then((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList());
|
||||
Future<List<AllTodosWithCategoryResult>> allTodosWithCategory(
|
||||
{QueryEngine operateOn}) {
|
||||
return (operateOn ?? this).customSelect(
|
||||
'SELECT t.*, c.id as catId, c."desc" as catDesc FROM todos t INNER JOIN categories c ON c.id = t.category',
|
||||
variables: []).then((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList());
|
||||
}
|
||||
|
||||
Stream<List<AllTodosWithCategoryResult>> watchAllTodosWithCategory() {
|
||||
|
@ -1091,8 +1091,8 @@ mixin _$SomeDaoMixin on DatabaseAccessor<TodoDb> {
|
|||
);
|
||||
}
|
||||
|
||||
Future<List<TodoEntry>> todosForUser(int user) {
|
||||
return customSelect(
|
||||
Future<List<TodoEntry>> todosForUser(int user, {QueryEngine operateOn}) {
|
||||
return (operateOn ?? this).customSelect(
|
||||
'SELECT t.* FROM todos t INNER JOIN shared_todos st ON st.todo = t.id INNER JOIN users u ON u.id = st.user WHERE u.id = :user',
|
||||
variables: [
|
||||
Variable.withInt(user),
|
||||
|
|
|
@ -49,7 +49,8 @@ class QueryWriter {
|
|||
_writeParameters(buffer);
|
||||
buffer
|
||||
..write(') {\n')
|
||||
..write('return customSelect(${asDartLiteral(query.sql)},');
|
||||
..write('return (operateOn ?? this).') // use custom engine, if set
|
||||
..write('customSelect(${asDartLiteral(query.sql)},');
|
||||
_writeVariables(buffer);
|
||||
buffer
|
||||
..write(')')
|
||||
|
@ -62,7 +63,10 @@ class QueryWriter {
|
|||
final upperQueryName = ReCase(query.name).pascalCase;
|
||||
buffer.write(
|
||||
'Stream<List<${_select.resultClassName}>> watch$upperQueryName(');
|
||||
_writeParameters(buffer);
|
||||
// don't supply an engine override parameter because select streams cannot
|
||||
// be used in transaction or similar context, only on the main database
|
||||
// engine.
|
||||
_writeParameters(buffer, dontOverrideEngine: true);
|
||||
buffer
|
||||
..write(') {\n')
|
||||
..write('return customSelectStream(${asDartLiteral(query.sql)},');
|
||||
|
@ -77,12 +81,20 @@ class QueryWriter {
|
|||
..write('\n}\n');
|
||||
}
|
||||
|
||||
void _writeParameters(StringBuffer buffer) {
|
||||
void _writeParameters(StringBuffer buffer,
|
||||
{bool dontOverrideEngine = false}) {
|
||||
final paramList = query.variables
|
||||
.map((v) => '${dartTypeNames[v.type]} ${v.dartParameterName}')
|
||||
.join(', ');
|
||||
|
||||
buffer.write(paramList);
|
||||
|
||||
// write named optional parameter to configure the query engine used to
|
||||
// execute the statement,
|
||||
if (!dontOverrideEngine) {
|
||||
if (query.variables.isNotEmpty) buffer.write(', ');
|
||||
buffer.write('{QueryEngine operateOn}');
|
||||
}
|
||||
}
|
||||
|
||||
void _writeVariables(StringBuffer buffer) {
|
||||
|
|
Loading…
Reference in New Issue