From 49f3f64bcc1aa3b960b5dd3562b143e6d58cac25 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 24 May 2022 18:41:48 +0200 Subject: [PATCH] Fix view mapping generation (#1855) --- drift/example/main.g.dart | 6 +++--- drift/test/generated/todos.g.dart | 4 ++-- .../select_integration_test.dart | 15 +++++++++++++++ .../lib/src/writer/tables/data_class_writer.dart | 5 +---- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/drift/example/main.g.dart b/drift/example/main.g.dart index f5d7d5b8..2d67b603 100644 --- a/drift/example/main.g.dart +++ b/drift/example/main.g.dart @@ -479,8 +479,8 @@ class TodoCategoryItemCountData extends DataClass { {String? prefix}) { final effectivePrefix = prefix ?? ''; return TodoCategoryItemCountData( - name: const StringType().mapFromDatabaseResponse( - data['${effectivePrefix}todo_categories.name'])!, + name: const StringType() + .mapFromDatabaseResponse(data['${effectivePrefix}name'])!, itemCount: const IntType() .mapFromDatabaseResponse(data['${effectivePrefix}item_count'])!, ); @@ -590,7 +590,7 @@ class TodoItemWithCategoryNameViewData extends DataClass { final effectivePrefix = prefix ?? ''; return TodoItemWithCategoryNameViewData( id: const IntType() - .mapFromDatabaseResponse(data['${effectivePrefix}todo_items.id'])!, + .mapFromDatabaseResponse(data['${effectivePrefix}id'])!, title: const StringType() .mapFromDatabaseResponse(data['${effectivePrefix}title'])!, ); diff --git a/drift/test/generated/todos.g.dart b/drift/test/generated/todos.g.dart index 96974684..b015d6e9 100644 --- a/drift/test/generated/todos.g.dart +++ b/drift/test/generated/todos.g.dart @@ -1512,9 +1512,9 @@ class TodoWithCategoryViewData extends DataClass { final effectivePrefix = prefix ?? ''; return TodoWithCategoryViewData( title: const StringType() - .mapFromDatabaseResponse(data['${effectivePrefix}todos.title']), + .mapFromDatabaseResponse(data['${effectivePrefix}title']), description: const StringType() - .mapFromDatabaseResponse(data['${effectivePrefix}categories.desc'])!, + .mapFromDatabaseResponse(data['${effectivePrefix}desc'])!, ); } factory TodoWithCategoryViewData.fromJson(Map json, diff --git a/drift/test/integration_tests/select_integration_test.dart b/drift/test/integration_tests/select_integration_test.dart index 712f567d..61529f4f 100644 --- a/drift/test/integration_tests/select_integration_test.dart +++ b/drift/test/integration_tests/select_integration_test.dart @@ -28,4 +28,19 @@ void main() { .get(); expect(rows.isSorted((a, b) => a.id.compareTo(b.id)), isFalse); }); + + test('can select view', () async { + final category = await db.categories.insertReturning( + CategoriesCompanion.insert(description: 'category description')); + await db.todosTable.insertOne(TodosTableCompanion.insert( + content: 'some content', + title: const Value('title'), + category: Value(category.id))); + + final result = await db.todoWithCategoryView.select().getSingle(); + expect( + result, + TodoWithCategoryViewData( + description: 'category description', title: 'title')); + }); } diff --git a/drift_dev/lib/src/writer/tables/data_class_writer.dart b/drift_dev/lib/src/writer/tables/data_class_writer.dart index fe8e7d9c..2547ded1 100644 --- a/drift_dev/lib/src/writer/tables/data_class_writer.dart +++ b/drift_dev/lib/src/writer/tables/data_class_writer.dart @@ -363,10 +363,7 @@ class RowMappingWriter { void writeArguments(StringBuffer buffer) { String readAndMap(MoorColumn column) { - var columnName = column.name.name; - if (column.table != null && column.table != table) { - columnName = '${column.table!.sqlName}.${column.name.name}'; - } + final columnName = column.name.name; final rawData = "data['\${effectivePrefix}$columnName']"; final sqlType = 'const ${sqlTypes[column.type]}()'; var loadType = '$sqlType.mapFromDatabaseResponse($rawData)';