diff --git a/drift/example/main.dart b/drift/example/main.dart index 94cf6922..d548a7d0 100644 --- a/drift/example/main.dart +++ b/drift/example/main.dart @@ -21,8 +21,8 @@ class TodoItems extends Table { } abstract class TodoCategoryItemCount extends View { - $TodoItemsTable get todoItems; - $TodoCategoriesTable get todoCategories; + TodoItems get todoItems; + TodoCategories get todoCategories; IntColumn get itemCount => integer().generatedAs(todoItems.id.count())(); @@ -38,8 +38,8 @@ abstract class TodoCategoryItemCount extends View { @DriftView(name: 'customViewName') abstract class TodoItemWithCategoryNameView extends View { - $TodoItemsTable get todoItems; - $TodoCategoriesTable get todoCategories; + TodoItems get todoItems; + TodoCategories get todoCategories; TextColumn get title => text().generatedAs(todoItems.title + const Constant('(') + diff --git a/drift/example/main.g.dart b/drift/example/main.g.dart index f2a2a6de..a91746a7 100644 --- a/drift/example/main.g.dart +++ b/drift/example/main.g.dart @@ -530,8 +530,6 @@ class $TodoCategoryItemCountView final _$Database _db; final String? _alias; $TodoCategoryItemCountView(this._db, [this._alias]); - $TodoItemsTable get todoItems => _db.todoItems; - $TodoCategoriesTable get todoCategories => _db.todoCategories; @override List get $columns => [todoCategories.name, itemCount]; @override @@ -632,8 +630,6 @@ class $TodoItemWithCategoryNameViewView extends ViewInfo< final _$Database _db; final String? _alias; $TodoItemWithCategoryNameViewView(this._db, [this._alias]); - $TodoItemsTable get todoItems => _db.todoItems; - $TodoCategoriesTable get todoCategories => _db.todoCategories; @override List get $columns => [todoItems.id, title]; @override diff --git a/drift/lib/src/runtime/query_builder/components/join.dart b/drift/lib/src/runtime/query_builder/components/join.dart index 3745ff2d..425dddba 100644 --- a/drift/lib/src/runtime/query_builder/components/join.dart +++ b/drift/lib/src/runtime/query_builder/components/join.dart @@ -27,7 +27,7 @@ class Join extends Component { final _JoinType type; /// The [TableInfo] that will be added to the query - final ResultSetImplementation table; + final Table table; /// For joins that aren't [_JoinType.cross], contains an additional predicate /// that must be matched for the join. @@ -42,15 +42,23 @@ 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; + : includeInResult = includeInResult ?? true { + if (table is! ResultSetImplementation) { + throw ArgumentError( + 'Invalid table parameter. You must provide the table reference from ' + 'generated database object.', + 'table'); + } + } @override void writeInto(GenerationContext context) { context.buffer.write(_joinKeywords[type]); context.buffer.write(' JOIN '); - context.buffer.write(table.tableWithAlias); - context.watchedTables.add(table); + final resultSet = table as ResultSetImplementation; + context.buffer.write(resultSet.tableWithAlias); + context.watchedTables.add(resultSet); if (type != _JoinType.cross) { context.buffer.write(' ON '); @@ -70,8 +78,7 @@ class Join extends Component { /// See also: /// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins /// - http://www.sqlitetutorial.net/sqlite-inner-join/ -Join innerJoin( - ResultSetImplementation other, Expression on, +Join innerJoin(Table other, Expression on, {bool? useColumns}) { return Join._(_JoinType.inner, other, on, includeInResult: useColumns); } @@ -84,8 +91,7 @@ Join innerJoin( /// See also: /// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins /// - http://www.sqlitetutorial.net/sqlite-left-join/ -Join leftOuterJoin( - ResultSetImplementation other, Expression on, +Join leftOuterJoin(Table other, Expression on, {bool? useColumns}) { return Join._(_JoinType.leftOuter, other, on, includeInResult: useColumns); } @@ -98,7 +104,6 @@ Join leftOuterJoin( /// See also: /// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins /// - http://www.sqlitetutorial.net/sqlite-cross-join/ -Join crossJoin(ResultSetImplementation other, - {bool? useColumns}) { +Join crossJoin(Table other, {bool? useColumns}) { return Join._(_JoinType.cross, other, null, includeInResult: useColumns); } diff --git a/drift/lib/src/runtime/query_builder/statements/select/select_with_join.dart b/drift/lib/src/runtime/query_builder/statements/select/select_with_join.dart index 0ae0ba5e..4af4525d 100644 --- a/drift/lib/src/runtime/query_builder/statements/select/select_with_join.dart +++ b/drift/lib/src/runtime/query_builder/statements/select/select_with_join.dart @@ -44,7 +44,7 @@ class JoinedSelectStatement int get _returnedColumnCount { return _joins.fold(_selectedColumns.length, (prev, join) { if (join.includeInResult) { - return prev + join.table.$columns.length; + return prev + (join.table as ResultSetImplementation).$columns.length; } return prev; }); @@ -63,7 +63,7 @@ class JoinedSelectStatement for (final join in _joins) { if (onlyResults && !join.includeInResult) continue; - yield join.table; + yield join.table as ResultSetImplementation; } }