Write readsFrom field for custom queries

This commit is contained in:
Simon Binder 2019-06-29 14:04:01 +02:00
parent d930664581
commit 33682a0ad1
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 25 additions and 7 deletions

View File

@ -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

View File

@ -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);

View File

@ -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)',
);

View File

@ -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('}');
}
}