mirror of https://github.com/AMT-Cheif/drift.git
Make Join accept bare Table type
This commit is contained in:
parent
876f6850f5
commit
47f0465795
|
@ -21,8 +21,8 @@ class TodoItems extends Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class TodoCategoryItemCount extends View {
|
abstract class TodoCategoryItemCount extends View {
|
||||||
$TodoItemsTable get todoItems;
|
TodoItems get todoItems;
|
||||||
$TodoCategoriesTable get todoCategories;
|
TodoCategories get todoCategories;
|
||||||
|
|
||||||
IntColumn get itemCount => integer().generatedAs(todoItems.id.count())();
|
IntColumn get itemCount => integer().generatedAs(todoItems.id.count())();
|
||||||
|
|
||||||
|
@ -38,8 +38,8 @@ abstract class TodoCategoryItemCount extends View {
|
||||||
|
|
||||||
@DriftView(name: 'customViewName')
|
@DriftView(name: 'customViewName')
|
||||||
abstract class TodoItemWithCategoryNameView extends View {
|
abstract class TodoItemWithCategoryNameView extends View {
|
||||||
$TodoItemsTable get todoItems;
|
TodoItems get todoItems;
|
||||||
$TodoCategoriesTable get todoCategories;
|
TodoCategories get todoCategories;
|
||||||
|
|
||||||
TextColumn get title => text().generatedAs(todoItems.title +
|
TextColumn get title => text().generatedAs(todoItems.title +
|
||||||
const Constant('(') +
|
const Constant('(') +
|
||||||
|
|
|
@ -530,8 +530,6 @@ class $TodoCategoryItemCountView
|
||||||
final _$Database _db;
|
final _$Database _db;
|
||||||
final String? _alias;
|
final String? _alias;
|
||||||
$TodoCategoryItemCountView(this._db, [this._alias]);
|
$TodoCategoryItemCountView(this._db, [this._alias]);
|
||||||
$TodoItemsTable get todoItems => _db.todoItems;
|
|
||||||
$TodoCategoriesTable get todoCategories => _db.todoCategories;
|
|
||||||
@override
|
@override
|
||||||
List<GeneratedColumn> get $columns => [todoCategories.name, itemCount];
|
List<GeneratedColumn> get $columns => [todoCategories.name, itemCount];
|
||||||
@override
|
@override
|
||||||
|
@ -632,8 +630,6 @@ class $TodoItemWithCategoryNameViewView extends ViewInfo<
|
||||||
final _$Database _db;
|
final _$Database _db;
|
||||||
final String? _alias;
|
final String? _alias;
|
||||||
$TodoItemWithCategoryNameViewView(this._db, [this._alias]);
|
$TodoItemWithCategoryNameViewView(this._db, [this._alias]);
|
||||||
$TodoItemsTable get todoItems => _db.todoItems;
|
|
||||||
$TodoCategoriesTable get todoCategories => _db.todoCategories;
|
|
||||||
@override
|
@override
|
||||||
List<GeneratedColumn> get $columns => [todoItems.id, title];
|
List<GeneratedColumn> get $columns => [todoItems.id, title];
|
||||||
@override
|
@override
|
||||||
|
|
|
@ -27,7 +27,7 @@ class Join<T extends HasResultSet, D> extends Component {
|
||||||
final _JoinType type;
|
final _JoinType type;
|
||||||
|
|
||||||
/// The [TableInfo] that will be added to the query
|
/// 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
|
/// For joins that aren't [_JoinType.cross], contains an additional predicate
|
||||||
/// that must be matched for the join.
|
/// 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
|
/// Constructs a [Join] by providing the relevant fields. [on] is optional for
|
||||||
/// [_JoinType.cross].
|
/// [_JoinType.cross].
|
||||||
Join._(this.type, this.table, this.on, {bool? includeInResult})
|
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
|
@override
|
||||||
void writeInto(GenerationContext context) {
|
void writeInto(GenerationContext context) {
|
||||||
context.buffer.write(_joinKeywords[type]);
|
context.buffer.write(_joinKeywords[type]);
|
||||||
context.buffer.write(' JOIN ');
|
context.buffer.write(' JOIN ');
|
||||||
|
|
||||||
context.buffer.write(table.tableWithAlias);
|
final resultSet = table as ResultSetImplementation<T, D>;
|
||||||
context.watchedTables.add(table);
|
context.buffer.write(resultSet.tableWithAlias);
|
||||||
|
context.watchedTables.add(resultSet);
|
||||||
|
|
||||||
if (type != _JoinType.cross) {
|
if (type != _JoinType.cross) {
|
||||||
context.buffer.write(' ON ');
|
context.buffer.write(' ON ');
|
||||||
|
@ -70,8 +78,7 @@ class Join<T extends HasResultSet, D> extends Component {
|
||||||
/// See also:
|
/// See also:
|
||||||
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
|
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
|
||||||
/// - http://www.sqlitetutorial.net/sqlite-inner-join/
|
/// - http://www.sqlitetutorial.net/sqlite-inner-join/
|
||||||
Join innerJoin<T extends HasResultSet, D>(
|
Join innerJoin<T extends HasResultSet, D>(Table other, Expression<bool?> on,
|
||||||
ResultSetImplementation<T, D> other, Expression<bool?> on,
|
|
||||||
{bool? useColumns}) {
|
{bool? useColumns}) {
|
||||||
return Join._(_JoinType.inner, other, on, includeInResult: useColumns);
|
return Join._(_JoinType.inner, other, on, includeInResult: useColumns);
|
||||||
}
|
}
|
||||||
|
@ -84,8 +91,7 @@ Join innerJoin<T extends HasResultSet, D>(
|
||||||
/// See also:
|
/// See also:
|
||||||
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
|
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
|
||||||
/// - http://www.sqlitetutorial.net/sqlite-left-join/
|
/// - http://www.sqlitetutorial.net/sqlite-left-join/
|
||||||
Join leftOuterJoin<T extends HasResultSet, D>(
|
Join leftOuterJoin<T extends HasResultSet, D>(Table other, Expression<bool?> on,
|
||||||
ResultSetImplementation<T, D> other, Expression<bool?> on,
|
|
||||||
{bool? useColumns}) {
|
{bool? useColumns}) {
|
||||||
return Join._(_JoinType.leftOuter, other, on, includeInResult: useColumns);
|
return Join._(_JoinType.leftOuter, other, on, includeInResult: useColumns);
|
||||||
}
|
}
|
||||||
|
@ -98,7 +104,6 @@ Join leftOuterJoin<T extends HasResultSet, D>(
|
||||||
/// See also:
|
/// See also:
|
||||||
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
|
/// - https://drift.simonbinder.eu/docs/advanced-features/joins/#joins
|
||||||
/// - http://www.sqlitetutorial.net/sqlite-cross-join/
|
/// - http://www.sqlitetutorial.net/sqlite-cross-join/
|
||||||
Join crossJoin<T extends HasResultSet, D>(ResultSetImplementation<T, D> other,
|
Join crossJoin<T extends HasResultSet, D>(Table other, {bool? useColumns}) {
|
||||||
{bool? useColumns}) {
|
|
||||||
return Join._(_JoinType.cross, other, null, includeInResult: useColumns);
|
return Join._(_JoinType.cross, other, null, includeInResult: useColumns);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
|
||||||
int get _returnedColumnCount {
|
int get _returnedColumnCount {
|
||||||
return _joins.fold(_selectedColumns.length, (prev, join) {
|
return _joins.fold(_selectedColumns.length, (prev, join) {
|
||||||
if (join.includeInResult) {
|
if (join.includeInResult) {
|
||||||
return prev + join.table.$columns.length;
|
return prev + (join.table as ResultSetImplementation).$columns.length;
|
||||||
}
|
}
|
||||||
return prev;
|
return prev;
|
||||||
});
|
});
|
||||||
|
@ -63,7 +63,7 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
|
||||||
for (final join in _joins) {
|
for (final join in _joins) {
|
||||||
if (onlyResults && !join.includeInResult) continue;
|
if (onlyResults && !join.includeInResult) continue;
|
||||||
|
|
||||||
yield join.table;
|
yield join.table as ResultSetImplementation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue