mirror of https://github.com/AMT-Cheif/drift.git
Write readsFrom field for custom queries
This commit is contained in:
parent
d930664581
commit
33682a0ad1
|
@ -1073,9 +1073,12 @@ abstract class _$TodoDb extends GeneratedDatabase {
|
|||
|
||||
Stream<List<AllTodosWithCategoryResult>> watchAllTodosWithCategory() {
|
||||
return customSelectStream(
|
||||
'SELECT t.*, c.id as catId, c."desc" as catDesc FROM todos t INNER JOIN categories c ON c.id = t.category',
|
||||
variables: [])
|
||||
.map((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList());
|
||||
'SELECT t.*, c.id as catId, c."desc" as catDesc FROM todos t INNER JOIN categories c ON c.id = t.category',
|
||||
variables: [],
|
||||
readsFrom: {
|
||||
categories,
|
||||
todosTable
|
||||
}).map((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList());
|
||||
}
|
||||
|
||||
TodosForUserResult _rowToTodosForUserResult(QueryRow row) {
|
||||
|
@ -1101,7 +1104,12 @@ abstract class _$TodoDb extends GeneratedDatabase {
|
|||
'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),
|
||||
]).map((rows) => rows.map(_rowToTodosForUserResult).toList());
|
||||
],
|
||||
readsFrom: {
|
||||
users,
|
||||
todosTable,
|
||||
sharedTodos
|
||||
}).map((rows) => rows.map(_rowToTodosForUserResult).toList());
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:moor_generator/src/model/specified_column.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
class SpecifiedTable {
|
||||
final ClassElement fromClass;
|
||||
|
@ -9,6 +10,7 @@ class SpecifiedTable {
|
|||
/// The name for the data class associated with this table
|
||||
final String dartTypeName;
|
||||
|
||||
String get tableFieldName => ReCase(fromClass.name).camelCase;
|
||||
String get tableInfoName => tableInfoNameForTableClass(fromClass);
|
||||
String get updateCompanionName => _updateCompanionName(fromClass);
|
||||
|
||||
|
|
|
@ -34,13 +34,12 @@ class DatabaseWriter {
|
|||
final tableGetters = <String>[];
|
||||
|
||||
for (var table in db.tables) {
|
||||
final tableFieldName = ReCase(table.fromClass.name).camelCase;
|
||||
tableGetters.add(tableFieldName);
|
||||
tableGetters.add(table.tableFieldName);
|
||||
final tableClassName = table.tableInfoName;
|
||||
|
||||
writeMemoizedGetter(
|
||||
buffer: buffer,
|
||||
getterName: tableFieldName,
|
||||
getterName: table.tableFieldName,
|
||||
returnType: tableClassName,
|
||||
code: '$tableClassName(this)',
|
||||
);
|
||||
|
|
|
@ -66,7 +66,11 @@ class QueryWriter {
|
|||
buffer
|
||||
..write(') {\n')
|
||||
..write('return customSelectStream(${asDartLiteral(query.sql)},');
|
||||
|
||||
_writeVariables(buffer);
|
||||
buffer.write(',');
|
||||
_writeReadsFrom(buffer);
|
||||
|
||||
buffer
|
||||
..write(')')
|
||||
..write('.map((rows) => rows.map(${_nameOfMappingMethod()}).toList());\n')
|
||||
|
@ -92,4 +96,9 @@ class QueryWriter {
|
|||
|
||||
buffer..write(']');
|
||||
}
|
||||
|
||||
void _writeReadsFrom(StringBuffer buffer) {
|
||||
final from = _select.readsFrom.map((t) => t.tableFieldName).join(', ');
|
||||
buffer..write('readsFrom: {')..write(from)..write('}');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue