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() {
|
Future<List<AllTodosWithCategoryResult>> allTodosWithCategory(
|
||||||
return customSelect(
|
{QueryEngine operateOn}) {
|
||||||
'SELECT t.*, c.id as catId, c."desc" as catDesc FROM todos t INNER JOIN categories c ON c.id = t.category',
|
return (operateOn ?? this).customSelect(
|
||||||
variables: [])
|
'SELECT t.*, c.id as catId, c."desc" as catDesc FROM todos t INNER JOIN categories c ON c.id = t.category',
|
||||||
.then((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList());
|
variables: []).then((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream<List<AllTodosWithCategoryResult>> watchAllTodosWithCategory() {
|
Stream<List<AllTodosWithCategoryResult>> watchAllTodosWithCategory() {
|
||||||
|
@ -1091,8 +1091,8 @@ mixin _$SomeDaoMixin on DatabaseAccessor<TodoDb> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<TodoEntry>> todosForUser(int user) {
|
Future<List<TodoEntry>> todosForUser(int user, {QueryEngine operateOn}) {
|
||||||
return customSelect(
|
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',
|
'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: [
|
variables: [
|
||||||
Variable.withInt(user),
|
Variable.withInt(user),
|
||||||
|
|
|
@ -49,7 +49,8 @@ class QueryWriter {
|
||||||
_writeParameters(buffer);
|
_writeParameters(buffer);
|
||||||
buffer
|
buffer
|
||||||
..write(') {\n')
|
..write(') {\n')
|
||||||
..write('return customSelect(${asDartLiteral(query.sql)},');
|
..write('return (operateOn ?? this).') // use custom engine, if set
|
||||||
|
..write('customSelect(${asDartLiteral(query.sql)},');
|
||||||
_writeVariables(buffer);
|
_writeVariables(buffer);
|
||||||
buffer
|
buffer
|
||||||
..write(')')
|
..write(')')
|
||||||
|
@ -62,7 +63,10 @@ class QueryWriter {
|
||||||
final upperQueryName = ReCase(query.name).pascalCase;
|
final upperQueryName = ReCase(query.name).pascalCase;
|
||||||
buffer.write(
|
buffer.write(
|
||||||
'Stream<List<${_select.resultClassName}>> watch$upperQueryName(');
|
'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
|
buffer
|
||||||
..write(') {\n')
|
..write(') {\n')
|
||||||
..write('return customSelectStream(${asDartLiteral(query.sql)},');
|
..write('return customSelectStream(${asDartLiteral(query.sql)},');
|
||||||
|
@ -77,12 +81,20 @@ class QueryWriter {
|
||||||
..write('\n}\n');
|
..write('\n}\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
void _writeParameters(StringBuffer buffer) {
|
void _writeParameters(StringBuffer buffer,
|
||||||
|
{bool dontOverrideEngine = false}) {
|
||||||
final paramList = query.variables
|
final paramList = query.variables
|
||||||
.map((v) => '${dartTypeNames[v.type]} ${v.dartParameterName}')
|
.map((v) => '${dartTypeNames[v.type]} ${v.dartParameterName}')
|
||||||
.join(', ');
|
.join(', ');
|
||||||
|
|
||||||
buffer.write(paramList);
|
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) {
|
void _writeVariables(StringBuffer buffer) {
|
||||||
|
|
Loading…
Reference in New Issue