diff --git a/drift/lib/src/runtime/api/connection_user.dart b/drift/lib/src/runtime/api/connection_user.dart index e997309e..3b1b1528 100644 --- a/drift/lib/src/runtime/api/connection_user.dart +++ b/drift/lib/src/runtime/api/connection_user.dart @@ -223,17 +223,24 @@ abstract class DatabaseConnectionUser { /// The [distinct] parameter (defaults to false) can be used to remove /// duplicate rows from the result set. /// + /// The [includeJoinedTableColumns] parameter (defaults to true) can be used + /// to determinate join statement's `useColumns` parameter default value. Set + /// it to false if you don't want to include joined table columns by default. + /// If you leave it on true and don't set `useColumns` parameter to false in + /// join declarations, all columns of joined table will be included in query + /// by default. + /// /// For simple queries, use [select]. /// /// See also: /// - the documentation on [aggregate expressions](https://drift.simonbinder.eu/docs/getting-started/expressions/#aggregate) /// - the documentation on [group by](https://drift.simonbinder.eu/docs/advanced-features/joins/#group-by) JoinedSelectStatement selectOnly( - ResultSetImplementation table, { - bool distinct = false, - }) { + ResultSetImplementation table, + {bool distinct = false, + bool includeJoinedTableColumns = true}) { return JoinedSelectStatement( - resolvedEngine, table, [], distinct, false); + resolvedEngine, table, [], distinct, false, includeJoinedTableColumns); } /// Starts a [DeleteStatement] that can be used to delete rows from a table. diff --git a/drift/lib/src/runtime/query_builder/components/join.dart b/drift/lib/src/runtime/query_builder/components/join.dart index 9b6405ec..8fb3bcbf 100644 --- a/drift/lib/src/runtime/query_builder/components/join.dart +++ b/drift/lib/src/runtime/query_builder/components/join.dart @@ -34,10 +34,12 @@ class Join extends Component { final Expression? on; /// Whether [table] should appear in the result set (defaults to true). + /// Default value can be changed by `includeJoinedTableColumns` in + /// `selectOnly` statements. /// /// It can be useful to exclude some tables. Sometimes, tables are used in a /// join only to run aggregate functions on them. - final bool includeInResult; + final bool? includeInResult; /// Constructs a [Join] by providing the relevant fields. [on] is optional for /// [_JoinType.cross]. 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 4af4525d..2ba86d40 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 @@ -12,13 +12,16 @@ class JoinedSelectStatement /// instead. JoinedSelectStatement(DatabaseConnectionUser database, ResultSetImplementation table, this._joins, - [this.distinct = false, this._includeMainTableInResult = true]) + [this.distinct = false, + this._includeMainTableInResult = true, + this._includeJoinedTablesInResult = true]) : super(database, table); /// Whether to generate a `SELECT DISTINCT` query that will remove duplicate /// rows from the result set. final bool distinct; final bool _includeMainTableInResult; + final bool _includeJoinedTablesInResult; final List _joins; /// All columns that we're selecting from. @@ -43,7 +46,7 @@ class JoinedSelectStatement @override int get _returnedColumnCount { return _joins.fold(_selectedColumns.length, (prev, join) { - if (join.includeInResult) { + if (join.includeInResult ?? _includeJoinedTablesInResult) { return prev + (join.table as ResultSetImplementation).$columns.length; } return prev; @@ -61,7 +64,8 @@ class JoinedSelectStatement } for (final join in _joins) { - if (onlyResults && !join.includeInResult) continue; + if (onlyResults && + !(join.includeInResult ?? _includeJoinedTablesInResult)) continue; yield join.table as ResultSetImplementation; } diff --git a/drift_dev/lib/src/writer/tables/view_writer.dart b/drift_dev/lib/src/writer/tables/view_writer.dart index 6c9a4a47..c736853b 100644 --- a/drift_dev/lib/src/writer/tables/view_writer.dart +++ b/drift_dev/lib/src/writer/tables/view_writer.dart @@ -101,7 +101,8 @@ class ViewWriter extends TableOrViewWriter { buffer.write('@override\nQuery? get query => '); final query = view.viewQuery; if (query != null) { - buffer.write('(_db.selectOnly(${query.from})..addColumns(\$columns))' + buffer.write('(_db.selectOnly(${query.from}, ' + 'includeJoinedTableColumns: false)..addColumns(\$columns))' '${query.query};'); } else { buffer.write('null;\n');