mirror of https://github.com/AMT-Cheif/drift.git
Add backref support
This commit is contained in:
parent
5875e18458
commit
fbd39a0d0a
|
@ -227,7 +227,7 @@ class TableManagerWithFiltering<
|
||||||
with ProcessedTableManagerMixin<DB, T, D, FS, OS> {
|
with ProcessedTableManagerMixin<DB, T, D, FS, OS> {
|
||||||
final C Function(TableManagerState<DB, T, D, FS, OS>) getChildManager;
|
final C Function(TableManagerState<DB, T, D, FS, OS>) getChildManager;
|
||||||
const TableManagerWithFiltering(super.state, {required this.getChildManager});
|
const TableManagerWithFiltering(super.state, {required this.getChildManager});
|
||||||
C filter(ComposableFilter Function(FilterComposer f) f) {
|
C filter(ComposableFilter Function(FS f) f) {
|
||||||
final filter = f(state.filteringComposer);
|
final filter = f(state.filteringComposer);
|
||||||
return getChildManager(state.copyWith(
|
return getChildManager(state.copyWith(
|
||||||
filter: filter.expression,
|
filter: filter.expression,
|
||||||
|
@ -247,7 +247,7 @@ class TableManagerWithOrdering<
|
||||||
with ProcessedTableManagerMixin<DB, T, D, FS, OS> {
|
with ProcessedTableManagerMixin<DB, T, D, FS, OS> {
|
||||||
final C Function(TableManagerState<DB, T, D, FS, OS>) getChildManager;
|
final C Function(TableManagerState<DB, T, D, FS, OS>) getChildManager;
|
||||||
const TableManagerWithOrdering(super.state, {required this.getChildManager});
|
const TableManagerWithOrdering(super.state, {required this.getChildManager});
|
||||||
C orderBy(ComposableOrdering Function(OrderingComposer o) o) {
|
C orderBy(ComposableOrdering Function(OS o) o) {
|
||||||
final orderings = o(state.orderingComposer);
|
final orderings = o(state.orderingComposer);
|
||||||
return getChildManager(state.copyWith(
|
return getChildManager(state.copyWith(
|
||||||
orderingBuilders: orderings.orderingBuilders,
|
orderingBuilders: orderings.orderingBuilders,
|
||||||
|
@ -275,14 +275,14 @@ class RootTableManager<
|
||||||
const RootTableManager(super.state,
|
const RootTableManager(super.state,
|
||||||
{required this.getChildManagerWithFiltering,
|
{required this.getChildManagerWithFiltering,
|
||||||
required this.getChildManagerWithOrdering});
|
required this.getChildManagerWithOrdering});
|
||||||
CF filter(ComposableFilter Function(FilterComposer f) f) {
|
CF filter(ComposableFilter Function(FS f) f) {
|
||||||
final filter = f(state.filteringComposer);
|
final filter = f(state.filteringComposer);
|
||||||
return getChildManagerWithFiltering(state.copyWith(
|
return getChildManagerWithFiltering(state.copyWith(
|
||||||
filter: filter.expression,
|
filter: filter.expression,
|
||||||
joinBuilders: state.joinBuilders.union(filter.joinBuilders)));
|
joinBuilders: state.joinBuilders.union(filter.joinBuilders)));
|
||||||
}
|
}
|
||||||
|
|
||||||
CO orderBy(ComposableOrdering Function(OrderingComposer o) o) {
|
CO orderBy(ComposableOrdering Function(OS o) o) {
|
||||||
final orderings = o(state.orderingComposer);
|
final orderings = o(state.orderingComposer);
|
||||||
return getChildManagerWithOrdering(state.copyWith(
|
return getChildManagerWithOrdering(state.copyWith(
|
||||||
orderingBuilders: orderings.orderingBuilders,
|
orderingBuilders: orderings.orderingBuilders,
|
||||||
|
|
|
@ -33,7 +33,7 @@ class ManagerWriter {
|
||||||
|
|
||||||
ManagerWriter(this._scope, this._dbClassName) : _addedTables = [];
|
ManagerWriter(this._scope, this._dbClassName) : _addedTables = [];
|
||||||
|
|
||||||
void _writeTableManagers(DriftTable table) {
|
void _writeTableManagers(DriftTable table, List<DriftTable> otherTables) {
|
||||||
final leaf = _scope.leaf();
|
final leaf = _scope.leaf();
|
||||||
|
|
||||||
final filters = StringBuffer();
|
final filters = StringBuffer();
|
||||||
|
@ -98,6 +98,40 @@ ComposableOrdering ${col.nameInDart}OrderBy(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Any other table who has a reference to this table should have a back ref created for it
|
||||||
|
for (var otherTable in otherTables) {
|
||||||
|
for (var otherColumn in otherTable.columns) {
|
||||||
|
if (otherColumn.isForeignKey) {
|
||||||
|
final foreignKey = otherColumn.constraints
|
||||||
|
.whereType<ForeignKeyReference>()
|
||||||
|
.firstOrNull;
|
||||||
|
// Check if this is a foreign key
|
||||||
|
if (foreignKey == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Check if the foreign key references this table
|
||||||
|
final thisColumn = foreignKey.otherColumn;
|
||||||
|
final thisTable = thisColumn.owner;
|
||||||
|
|
||||||
|
final otherColumnName = Names(otherTable.entityInfoName);
|
||||||
|
// Check if the foreign key references this table
|
||||||
|
if (thisTable is DriftTable &&
|
||||||
|
table.schemaName == thisTable.schemaName) {
|
||||||
|
filters.write('''
|
||||||
|
ComposableFilter ${("referenced ${otherTable.dbGetterName} ${otherColumn.nameInDart}").camelCase}(
|
||||||
|
ComposableFilter Function(${otherColumnName.filterComposer} f) f) {
|
||||||
|
return referenced(
|
||||||
|
getCurrentColumn: (f) => f.${thisColumn.nameInDart},
|
||||||
|
referencedTable: state.db.${otherTable.dbGetterName},
|
||||||
|
getReferencedColumn: (f) => f.${otherColumn.nameInDart},
|
||||||
|
getReferencedComposer: (db, table) => ${otherColumnName.filterComposer}(db, table),
|
||||||
|
builder: f);
|
||||||
|
}
|
||||||
|
''');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
final names = Names(table.entityInfoName);
|
final names = Names(table.entityInfoName);
|
||||||
|
|
||||||
leaf.write("""
|
leaf.write("""
|
||||||
|
@ -201,7 +235,7 @@ class ${names.rootTableManager} extends RootTableManager<
|
||||||
|
|
||||||
void write() {
|
void write() {
|
||||||
for (var table in _addedTables) {
|
for (var table in _addedTables) {
|
||||||
_writeTableManagers(table);
|
_writeTableManagers(table, _addedTables);
|
||||||
}
|
}
|
||||||
final leaf = _scope.leaf();
|
final leaf = _scope.leaf();
|
||||||
final tableManagerGetters = StringBuffer();
|
final tableManagerGetters = StringBuffer();
|
||||||
|
|
Loading…
Reference in New Issue