Make Join accept bare Table type

This commit is contained in:
westito 2021-11-16 09:04:43 +01:00
parent 876f6850f5
commit 47f0465795
4 changed files with 21 additions and 20 deletions

View File

@ -21,8 +21,8 @@ class TodoItems extends Table {
}
abstract class TodoCategoryItemCount extends View {
$TodoItemsTable get todoItems;
$TodoCategoriesTable get todoCategories;
TodoItems get todoItems;
TodoCategories get todoCategories;
IntColumn get itemCount => integer().generatedAs(todoItems.id.count())();
@ -38,8 +38,8 @@ abstract class TodoCategoryItemCount extends View {
@DriftView(name: 'customViewName')
abstract class TodoItemWithCategoryNameView extends View {
$TodoItemsTable get todoItems;
$TodoCategoriesTable get todoCategories;
TodoItems get todoItems;
TodoCategories get todoCategories;
TextColumn get title => text().generatedAs(todoItems.title +
const Constant('(') +

View File

@ -530,8 +530,6 @@ class $TodoCategoryItemCountView
final _$Database _db;
final String? _alias;
$TodoCategoryItemCountView(this._db, [this._alias]);
$TodoItemsTable get todoItems => _db.todoItems;
$TodoCategoriesTable get todoCategories => _db.todoCategories;
@override
List<GeneratedColumn> get $columns => [todoCategories.name, itemCount];
@override
@ -632,8 +630,6 @@ class $TodoItemWithCategoryNameViewView extends ViewInfo<
final _$Database _db;
final String? _alias;
$TodoItemWithCategoryNameViewView(this._db, [this._alias]);
$TodoItemsTable get todoItems => _db.todoItems;
$TodoCategoriesTable get todoCategories => _db.todoCategories;
@override
List<GeneratedColumn> get $columns => [todoItems.id, title];
@override

View File

@ -27,7 +27,7 @@ class Join<T extends HasResultSet, D> extends Component {
final _JoinType type;
/// The [TableInfo] that will be added to the query
final ResultSetImplementation<T, D> table;
final Table table;
/// For joins that aren't [_JoinType.cross], contains an additional predicate
/// that must be matched for the join.
@ -42,15 +42,23 @@ class Join<T extends HasResultSet, D> extends Component {
/// Constructs a [Join] by providing the relevant fields. [on] is optional for
/// [_JoinType.cross].
Join._(this.type, this.table, this.on, {bool? includeInResult})
: includeInResult = includeInResult ?? true;
: includeInResult = includeInResult ?? true {
if (table is! ResultSetImplementation<T, D>) {
throw ArgumentError(
'Invalid table parameter. You must provide the table reference from '
'generated database object.',
'table');
}
}
@override
void writeInto(GenerationContext context) {
context.buffer.write(_joinKeywords[type]);
context.buffer.write(' JOIN ');
context.buffer.write(table.tableWithAlias);
context.watchedTables.add(table);
final resultSet = table as ResultSetImplementation<T, D>;
context.buffer.write(resultSet.tableWithAlias);
context.watchedTables.add(resultSet);
if (type != _JoinType.cross) {
context.buffer.write(' ON ');
@ -70,8 +78,7 @@ class Join<T extends HasResultSet, D> extends Component {
/// See also:
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
/// - http://www.sqlitetutorial.net/sqlite-inner-join/
Join innerJoin<T extends HasResultSet, D>(
ResultSetImplementation<T, D> other, Expression<bool?> on,
Join innerJoin<T extends HasResultSet, D>(Table other, Expression<bool?> on,
{bool? useColumns}) {
return Join._(_JoinType.inner, other, on, includeInResult: useColumns);
}
@ -84,8 +91,7 @@ Join innerJoin<T extends HasResultSet, D>(
/// See also:
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
/// - http://www.sqlitetutorial.net/sqlite-left-join/
Join leftOuterJoin<T extends HasResultSet, D>(
ResultSetImplementation<T, D> other, Expression<bool?> on,
Join leftOuterJoin<T extends HasResultSet, D>(Table other, Expression<bool?> on,
{bool? useColumns}) {
return Join._(_JoinType.leftOuter, other, on, includeInResult: useColumns);
}
@ -98,7 +104,6 @@ Join leftOuterJoin<T extends HasResultSet, D>(
/// See also:
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
/// - http://www.sqlitetutorial.net/sqlite-cross-join/
Join crossJoin<T extends HasResultSet, D>(ResultSetImplementation<T, D> other,
{bool? useColumns}) {
Join crossJoin<T extends HasResultSet, D>(Table other, {bool? useColumns}) {
return Join._(_JoinType.cross, other, null, includeInResult: useColumns);
}

View File

@ -44,7 +44,7 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
int get _returnedColumnCount {
return _joins.fold(_selectedColumns.length, (prev, join) {
if (join.includeInResult) {
return prev + join.table.$columns.length;
return prev + (join.table as ResultSetImplementation).$columns.length;
}
return prev;
});
@ -63,7 +63,7 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
for (final join in _joins) {
if (onlyResults && !join.includeInResult) continue;
yield join.table;
yield join.table as ResultSetImplementation;
}
}