mirror of https://github.com/AMT-Cheif/drift.git
Support inheritance in table definitions (#169)
This commit is contained in:
parent
77fcf7a7ba
commit
337e260667
|
@ -1,3 +1,8 @@
|
||||||
|
## unreleased
|
||||||
|
|
||||||
|
- Accept inheritance in table definitions (e.g. if an abstract class declared as `IntColumn get foo => integer()()`,
|
||||||
|
tables inheriting from that class will also have a `foo` column)
|
||||||
|
|
||||||
## 2.0.1
|
## 2.0.1
|
||||||
|
|
||||||
- Escape `\r` characters in generated Dart literals
|
- Escape `\r` characters in generated Dart literals
|
||||||
|
|
|
@ -110,7 +110,10 @@ class TableParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<SpecifiedColumn>> _parseColumns(ClassElement element) {
|
Future<List<SpecifiedColumn>> _parseColumns(ClassElement element) {
|
||||||
final columns = element.fields
|
final columns = element.allSupertypes
|
||||||
|
.map((t) => t.element)
|
||||||
|
.followedBy([element])
|
||||||
|
.expand((e) => e.fields)
|
||||||
.where((field) => isColumn(field.type) && field.getter != null);
|
.where((field) => isColumn(field.type) && field.getter != null);
|
||||||
|
|
||||||
return Future.wait(columns.map((field) async {
|
return Future.wait(columns.map((field) async {
|
||||||
|
|
|
@ -6,5 +6,7 @@ bool isFromMoor(DartType type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isColumn(DartType type) {
|
bool isColumn(DartType type) {
|
||||||
return isFromMoor(type) && type.name.contains('Column');
|
return isFromMoor(type) &&
|
||||||
|
type.name.contains('Column') &&
|
||||||
|
!type.name.contains('Builder');
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,18 @@ void main() {
|
||||||
String constructTableName() => 'my-table-name';
|
String constructTableName() => 'my-table-name';
|
||||||
String get tableName => constructTableName();
|
String get tableName => constructTableName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mixin IntIdTable on Table {
|
||||||
|
IntColumn get id => integer().autoIncrement()();
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class HasNameTable extends Table {
|
||||||
|
TextColumn get name => text()();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foos extends HasNameTable with IntIdTable {
|
||||||
|
|
||||||
|
}
|
||||||
'''
|
'''
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -143,4 +155,10 @@ void main() {
|
||||||
expect(table.primaryKey, containsAll(table.columns));
|
expect(table.primaryKey, containsAll(table.columns));
|
||||||
expect(table.columns.any((column) => column.hasAI), isFalse);
|
expect(table.columns.any((column) => column.hasAI), isFalse);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('handles inheritance in column definitions', () async {
|
||||||
|
final table = await parse('Foos');
|
||||||
|
|
||||||
|
expect(table.columns.map((c) => c.name.name), containsAll(['id', 'name']));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue