diff --git a/drift/example/main.dart b/drift/example/main.dart index f26af0547..0c5ae4d33 100644 --- a/drift/example/main.dart +++ b/drift/example/main.dart @@ -115,9 +115,14 @@ Future main() async { await db.into(db.todoItems).insert(TodoItemsCompanion.insert( title: 'Another entry added later', categoryId: categoryId)); - (await db.select(db.customViewName).get()).forEach(print); - (await db.select(db.todoCategoryItemCount).get()).forEach(print); - - // Delete all todo items - await db.delete(db.todoItems).go(); + final query = db.select(db.todoItems).join([ + innerJoin(db.todoCategories, + db.todoCategories.id.equalsExp(db.todoItems.categoryId)) + ]); + + for (final row in await query.get()) { + print('has row'); + print(row.read(db.todoItems.categoryId)); + print(row.read(db.todoCategories.name)); + } } 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 e0f77c687..4df6e6371 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,14 +44,28 @@ class JoinedSelectStatement Set get watchedTables => _queriedTables().toSet(); @override - Iterable<(Expression, String)> get _expandedColumns sync* { - for (final column in _selectedColumns) { - yield (column, _columnAliases[column]!); - } + Iterable<(Expression, String)> get _expandedColumns => + _columnsWithName(null); + Iterable<(Expression, String)> _columnsWithName( + String? generatingForView) sync* { for (final table in _queriedTables(true)) { for (final column in table.$columns) { - yield (column, _nameForTableColumn(column)); + yield ( + column, + _nameForTableColumn(column, generatingForView: generatingForView) + ); + } + } + + for (final column in _selectedColumns) { + if (column is GeneratedColumn) { + yield ( + column, + _nameForTableColumn(column, generatingForView: generatingForView) + ); + } else { + yield (column, _columnAliases[column]!); } } } @@ -105,31 +119,19 @@ class JoinedSelectStatement @override void writeStartPart(GenerationContext ctx) { - // use all columns across all tables as result column for this query - _selectedColumns.insertAll( - 0, _queriedTables(true).expand((t) => t.$columns).cast()); - ctx.hasMultipleTables = true; ctx.buffer ..write(_beginOfSelect(distinct)) ..write(' '); - for (var i = 0; i < _selectedColumns.length; i++) { - if (i != 0) { + var first = true; + for (final (column, name) in _columnsWithName(ctx.generatingForView)) { + if (!first) { ctx.buffer.write(', '); } + first = false; - final column = _selectedColumns[i]; - String chosenAlias; - if (column is GeneratedColumn) { - chosenAlias = _nameForTableColumn(column, - generatingForView: ctx.generatingForView); - } else { - chosenAlias = _columnAliases[column]!; - } - - final chosenAliasEscaped = ctx.dialect.escape(chosenAlias); - + final chosenAliasEscaped = ctx.dialect.escape(name); column.writeInto(ctx); ctx.buffer ..write(' AS ') @@ -292,7 +294,15 @@ class JoinedSelectStatement final driftRow = QueryRow(row, database); return TypedResult( - readTables, driftRow, _LazyExpressionMap(_columnAliases, driftRow)); + readTables, + driftRow, + _LazyExpressionMap( + { + for (final (expr, alias) in _expandedColumns) expr: alias, + }, + driftRow, + ), + ); } Future> _mapResponse(List> rows) {