add join useColumns option to selectOnly

This commit is contained in:
westito 2021-11-30 15:20:19 +01:00
parent 9e443dba2a
commit 0e61940d70
4 changed files with 23 additions and 9 deletions

View File

@ -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<T, R> selectOnly<T extends HasResultSet, R>(
ResultSetImplementation<T, R> table, {
bool distinct = false,
}) {
ResultSetImplementation<T, R> table,
{bool distinct = false,
bool includeJoinedTableColumns = true}) {
return JoinedSelectStatement<T, R>(
resolvedEngine, table, [], distinct, false);
resolvedEngine, table, [], distinct, false, includeJoinedTableColumns);
}
/// Starts a [DeleteStatement] that can be used to delete rows from a table.

View File

@ -34,10 +34,12 @@ class Join<T extends HasResultSet, D> extends Component {
final Expression<bool?>? 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].

View File

@ -12,13 +12,16 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
/// instead.
JoinedSelectStatement(DatabaseConnectionUser database,
ResultSetImplementation<FirstT, FirstD> 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<Join> _joins;
/// All columns that we're selecting from.
@ -43,7 +46,7 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
@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<FirstT extends HasResultSet, FirstD>
}
for (final join in _joins) {
if (onlyResults && !join.includeInResult) continue;
if (onlyResults &&
!(join.includeInResult ?? _includeJoinedTablesInResult)) continue;
yield join.table as ResultSetImplementation;
}

View File

@ -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');