diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index b57fe126..24909d5c 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -1081,4 +1081,49 @@ mixin _$SomeDaoMixin on DatabaseAccessor { $UsersTable get users => db.users; $SharedTodosTable get sharedTodos => db.sharedTodos; $TodosTableTable get todosTable => db.todosTable; + TodosForUserResult _rowToTodosForUserResult(QueryRow row) { + return TodosForUserResult( + id: row.readInt('id'), + title: row.readString('title'), + content: row.readString('content'), + targetDate: row.readDateTime('target_date'), + category: row.readInt('category'), + ); + } + + Future> todosForUser(int user) { + return 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), + ]).then((rows) => rows.map(_rowToTodosForUserResult).toList()); + } + + Stream> watchTodosForUser(int user) { + return customSelectStream( + '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), + ], + readsFrom: { + users, + todosTable, + sharedTodos + }).map((rows) => rows.map(_rowToTodosForUserResult).toList()); + } +} + +class TodosForUserResult { + final int id; + final String title; + final String content; + final DateTime targetDate; + final int category; + TodosForUserResult({ + this.id, + this.title, + this.content, + this.targetDate, + this.category, + }); } diff --git a/moor_generator/lib/src/dao_generator.dart b/moor_generator/lib/src/dao_generator.dart index 5b9ab7fe..772dab0f 100644 --- a/moor_generator/lib/src/dao_generator.dart +++ b/moor_generator/lib/src/dao_generator.dart @@ -1,9 +1,14 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:build/build.dart'; +import 'package:moor_generator/src/parser/sql/sql_parser.dart'; import 'package:moor_generator/src/shared_state.dart'; import 'package:moor/moor.dart'; +import 'package:moor_generator/src/writer/query_writer.dart'; +import 'package:moor_generator/src/writer/result_set_writer.dart'; import 'package:source_gen/source_gen.dart'; +import 'model/sql_query.dart'; + class DaoGenerator extends GeneratorForAnnotation { final SharedState state; @@ -15,7 +20,8 @@ class DaoGenerator extends GeneratorForAnnotation { final tableTypes = annotation.peek('tables').listValue.map((obj) => obj.toTypeValue()); final parsedTables = - tableTypes.map((type) => state.parseType(type, element)); + tableTypes.map((type) => state.parseType(type, element)).toList(); + final queries = annotation.peek('queries')?.listValue ?? []; if (element is! ClassElement) { throw InvalidGenerationSourceError( @@ -24,6 +30,7 @@ class DaoGenerator extends GeneratorForAnnotation { } final enclosingClass = element as ClassElement; + var resolvedQueries = []; final dbType = enclosingClass.supertype; if (dbType.name != 'DatabaseAccessor') { @@ -41,6 +48,13 @@ class DaoGenerator extends GeneratorForAnnotation { element: element); } + if (queries.isNotEmpty) { + final parser = SqlParser(state.options, parsedTables, queries)..parse(); + state.errors.errors.addAll(parser.errors); + + resolvedQueries = parser.foundQueries; + } + // finally, we can write the mixin final buffer = StringBuffer(); @@ -55,8 +69,19 @@ class DaoGenerator extends GeneratorForAnnotation { buffer.write('$infoType get $getterName => db.$getterName;\n'); } + for (var query in resolvedQueries) { + QueryWriter(query).writeInto(buffer); + } + buffer.write('}'); + // if the queries introduced additional classes, also write those + for (final query in resolvedQueries) { + if (query is SqlSelectQuery && query.resultSet.matchingTable == null) { + ResultSetWriter(query).write(buffer); + } + } + return buffer.toString(); } } diff --git a/moor_generator/lib/src/writer/database_writer.dart b/moor_generator/lib/src/writer/database_writer.dart index 53f0d85b..aa255893 100644 --- a/moor_generator/lib/src/writer/database_writer.dart +++ b/moor_generator/lib/src/writer/database_writer.dart @@ -21,7 +21,7 @@ class DatabaseWriter { // Write additional classes to hold the result of custom queries for (final query in db.queries) { - if (query is SqlSelectQuery) { + if (query is SqlSelectQuery && query.resultSet.matchingTable == null) { ResultSetWriter(query).write(buffer); } }