Fixes join when adding columns to "regular" select

This commit is contained in:
David Martos 2021-02-17 19:58:57 +01:00
parent 07d58eb058
commit fb919eeedb
2 changed files with 43 additions and 1 deletions

View File

@ -95,7 +95,7 @@ class SimpleSelectStatement<T extends Table, D extends DataClass>
/// {@macro moor_select_addColumns} /// {@macro moor_select_addColumns}
JoinedSelectStatement addColumns(List<Expression> expressions) { JoinedSelectStatement addColumns(List<Expression> expressions) {
return join(const [])..addColumns(expressions); return join([])..addColumns(expressions);
} }
/// Orders the result by the given clauses. The clauses coming first in the /// Orders the result by the given clauses. The clauses coming first in the

View File

@ -223,6 +223,48 @@ void main() {
expect(result.read(descriptionLength), 11); expect(result.read(descriptionLength), 11);
}); });
test('supports custom columns + join', () async {
final todos = db.alias(db.todosTable, 't');
final categories = db.alias(db.categories, 'c');
final descriptionLength = categories.description.length;
final query = db.select(categories).addColumns([descriptionLength]).join([
innerJoin(
todos,
categories.id.equalsExp(todos.category),
useColumns: false,
)
]);
when(executor.runSelect(any, any)).thenAnswer((_) async {
return [
{'c.id': 3, 'c.desc': 'Description', 'c.priority': 1, 'c3': 11}
];
});
final result = await query.getSingle();
verify(executor.runSelect(
'SELECT c.id AS "c.id", c."desc" AS "c.desc", c.priority AS "c.priority"'
', LENGTH(c."desc") AS "c3" '
'FROM categories c '
'INNER JOIN todos t ON c.id = t.category;',
[],
));
expect(
result.readTable(categories),
equals(
Category(
id: 3,
description: 'Description',
priority: CategoryPriority.medium,
),
),
);
expect(result.read(descriptionLength), 11);
});
test('group by', () async { test('group by', () async {
final categories = db.alias(db.categories, 'c'); final categories = db.alias(db.categories, 'c');
final todos = db.alias(db.todosTable, 't'); final todos = db.alias(db.todosTable, 't');