Allow row.read for implicitly added columns

This commit is contained in:
Simon Binder 2023-08-23 14:44:37 +02:00
parent a84839cd66
commit b5a564f055
2 changed files with 42 additions and 27 deletions

View File

@ -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);
final query = db.select(db.todoItems).join([
innerJoin(db.todoCategories,
db.todoCategories.id.equalsExp(db.todoItems.categoryId))
]);
// Delete all todo items
await db.delete(db.todoItems).go();
for (final row in await query.get()) {
print('has row');
print(row.read(db.todoItems.categoryId));
print(row.read(db.todoCategories.name));
}
}

View File

@ -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]!);
}
}
}
@ -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 ')
@ -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) {