Skip to content

Commit

Permalink
Allow row.read for implicitly added columns
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Aug 23, 2023
1 parent a84839c commit b5a564f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
15 changes: 10 additions & 5 deletions drift/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,14 @@ Future<void> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,28 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
Set<ResultSetImplementation> get watchedTables => _queriedTables().toSet();

@override
Iterable<(Expression<Object>, String)> get _expandedColumns sync* {
for (final column in _selectedColumns) {
yield (column, _columnAliases[column]!);
}
Iterable<(Expression<Object>, String)> get _expandedColumns =>
_columnsWithName(null);

Iterable<(Expression<Object>, 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]!);
}
}
}
Expand Down Expand Up @@ -105,31 +119,19 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>

@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<Expression>());

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 ')
Expand Down Expand Up @@ -292,7 +294,15 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>

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<List<TypedResult>> _mapResponse(List<Map<String, Object?>> rows) {
Expand Down

0 comments on commit b5a564f

Please sign in to comment.