From ffe24bb17a629255b6208704aa0f9979c61cb1b7 Mon Sep 17 00:00:00 2001 From: westito Date: Thu, 2 Dec 2021 09:04:17 +0100 Subject: [PATCH] Fix includeJoinedTableColumns and add test --- .../query_builder/components/join.dart | 3 +- drift/test/join_test.dart | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/drift/lib/src/runtime/query_builder/components/join.dart b/drift/lib/src/runtime/query_builder/components/join.dart index 8fb3bcbf..cf3925f8 100644 --- a/drift/lib/src/runtime/query_builder/components/join.dart +++ b/drift/lib/src/runtime/query_builder/components/join.dart @@ -43,8 +43,7 @@ class Join extends Component { /// Constructs a [Join] by providing the relevant fields. [on] is optional for /// [_JoinType.cross]. - Join._(this.type, this.table, this.on, {bool? includeInResult}) - : includeInResult = includeInResult ?? true { + Join._(this.type, this.table, this.on, {this.includeInResult}) { if (table is! ResultSetImplementation) { throw ArgumentError( 'Invalid table parameter. You must provide the table reference from ' diff --git a/drift/test/join_test.dart b/drift/test/join_test.dart index 83ee7c71..09b4ba2e 100644 --- a/drift/test/join_test.dart +++ b/drift/test/join_test.dart @@ -401,6 +401,43 @@ void main() { expect(result.read(todos.id.count()), equals(10)); }); + test('use selectOnly(includeJoinedTableColumns) instead of useColumns', + () async { + final categories = db.categories; + final todos = db.todosTable; + + final query = + db.selectOnly(categories, includeJoinedTableColumns: false).join([ + innerJoin( + todos, + todos.category.equalsExp(categories.id), + ) + ]); + query + ..addColumns([categories.id, todos.id.count()]) + ..groupBy([categories.id]); + + when(executor.runSelect(any, any)).thenAnswer((_) async { + return [ + { + 'categories.id': 2, + 'c1': 10, + } + ]; + }); + + final result = await query.getSingle(); + + verify(executor.runSelect( + 'SELECT categories.id AS "categories.id", COUNT(todos.id) AS "c1" ' + 'FROM categories INNER JOIN todos ON todos.category = categories.id ' + 'GROUP BY categories.id;', + [])); + + expect(result.read(categories.id), equals(2)); + expect(result.read(todos.id.count()), equals(10)); + }); + test('injects custom error message when a table is used multiple times', () async { when(executor.runSelect(any, any)).thenAnswer((_) => Future.error('nah'));