From 2c57a8077113c67c002b8657a0043ec5e11d81e4 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 20 Dec 2019 18:23:30 +0100 Subject: [PATCH] Revert using mapFromRow() for matched tables in queries --- moor/lib/src/runtime/api/query_engine.dart | 9 ++++---- moor/test/data/tables/custom_tables.g.dart | 11 +++++++-- moor/test/data/tables/todos.g.dart | 16 +++++++++++-- .../lib/src/writer/queries/query_writer.dart | 23 +++++++------------ 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/moor/lib/src/runtime/api/query_engine.dart b/moor/lib/src/runtime/api/query_engine.dart index b8dcb803..355d86c0 100644 --- a/moor/lib/src/runtime/api/query_engine.dart +++ b/moor/lib/src/runtime/api/query_engine.dart @@ -300,10 +300,11 @@ mixin QueryEngine on DatabaseConnectionUser { /// Will be used by generated code to resolve inline Dart components in sql. @protected - GenerationContext $write(Component component) { - // for good measure, act like there are multiple tables. This will make - // column references be explicit about their table name. - final context = GenerationContext.fromDb(this)..hasMultipleTables = true; + GenerationContext $write(Component component, {bool hasMultipleTables}) { + final context = GenerationContext.fromDb(this); + if (hasMultipleTables != null) { + context.hasMultipleTables = hasMultipleTables; + } // we don't want ORDER BY clauses to write the ORDER BY tokens because those // are already declared in sql diff --git a/moor/test/data/tables/custom_tables.g.dart b/moor/test/data/tables/custom_tables.g.dart index 870d0b7f..38c9e313 100644 --- a/moor/test/data/tables/custom_tables.g.dart +++ b/moor/test/data/tables/custom_tables.g.dart @@ -1113,7 +1113,10 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { Email _email; Email get email => _email ??= Email(this); Config _rowToConfig(QueryRow row) { - return config.mapFromRow(row); + return Config( + configKey: row.readString('config_key'), + configValue: row.readString('config_value'), + ); } Selectable readConfig(String var1) { @@ -1155,7 +1158,11 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { } EMail _rowToEMail(QueryRow row) { - return email.mapFromRow(row); + return EMail( + sender: row.readString('sender'), + title: row.readString('title'), + body: row.readString('body'), + ); } Selectable searchEmails(String term) { diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index 6ebb697c..3bbd9765 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -1372,7 +1372,13 @@ abstract class _$TodoDb extends GeneratedDatabase { } TodoEntry _rowToTodoEntry(QueryRow row) { - return todosTable.mapFromRow(row); + return TodoEntry( + id: row.readInt('id'), + title: row.readString('title'), + content: row.readString('content'), + targetDate: row.readDateTime('target_date'), + category: row.readInt('category'), + ); } Selectable withInQuery(String var1, String var2, List var3) { @@ -1493,7 +1499,13 @@ mixin _$SomeDaoMixin on DatabaseAccessor { $SharedTodosTable get sharedTodos => db.sharedTodos; $TodosTableTable get todosTable => db.todosTable; TodoEntry _rowToTodoEntry(QueryRow row) { - return todosTable.mapFromRow(row); + return TodoEntry( + id: row.readInt('id'), + title: row.readString('title'), + content: row.readString('content'), + targetDate: row.readDateTime('target_date'), + category: row.readInt('category'), + ); } Selectable todosForUserQuery(int user) { diff --git a/moor_generator/lib/src/writer/queries/query_writer.dart b/moor_generator/lib/src/writer/queries/query_writer.dart index 46ab32e6..c4eedaac 100644 --- a/moor_generator/lib/src/writer/queries/query_writer.dart +++ b/moor_generator/lib/src/writer/queries/query_writer.dart @@ -98,22 +98,15 @@ class QueryWriter { ..write('${_select.resultClassName} ${_nameOfMappingMethod()}') ..write('(QueryRow row) {\n'); - // If we're matching an existing table from moor - let's just use the - // mapping method we're generating for each table! - if (_select.resultSet.matchingTable != null) { - final table = _select.resultSet.matchingTable; - _buffer.write('return ${table.tableFieldName}.mapFromRow(row);\n'); - } else { - // For more complex results, generate a custom constructor call - _buffer.write('return ${_select.resultClassName}('); - for (final column in _select.resultSet.columns) { - final fieldName = _select.resultSet.dartNameFor(column); - _buffer.write('$fieldName: ${_readingCode(column)},'); - } - - _buffer.write(');\n'); + // note that, even if the result set has a matching table, we can't just + // use the mapFromRow() function of that table - the column names might + // be different! + _buffer.write('return ${_select.resultClassName}('); + for (final column in _select.resultSet.columns) { + final fieldName = _select.resultSet.dartNameFor(column); + _buffer.write('$fieldName: ${_readingCode(column)},'); } - _buffer.write('}\n'); + _buffer.write(');\n}\n'); _writtenMappingMethods.add(_nameOfMappingMethod()); }