mirror of https://github.com/AMT-Cheif/drift.git
Make the dao generator use the same logic as the main one
This commit is contained in:
parent
7becfdcb85
commit
1ea1c12592
|
@ -1,9 +1,7 @@
|
|||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:moor_generator/src/shared_state.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:moor_generator/src/model/specified_table.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
|
||||
class DaoGenerator extends GeneratorForAnnotation<UseDao> {
|
||||
|
@ -16,6 +14,8 @@ class DaoGenerator extends GeneratorForAnnotation<UseDao> {
|
|||
Element element, ConstantReader annotation, BuildStep buildStep) {
|
||||
final tableTypes =
|
||||
annotation.peek('tables').listValue.map((obj) => obj.toTypeValue());
|
||||
final parsedTables =
|
||||
tableTypes.map((type) => state.parseType(type, element));
|
||||
|
||||
if (element is! ClassElement) {
|
||||
throw InvalidGenerationSourceError(
|
||||
|
@ -49,11 +49,9 @@ class DaoGenerator extends GeneratorForAnnotation<UseDao> {
|
|||
buffer.write('mixin _\$${daoName}Mixin on '
|
||||
'DatabaseAccessor<${dbImpl.displayName}> {\n');
|
||||
|
||||
for (var table in tableTypes) {
|
||||
final infoType =
|
||||
tableInfoNameForTableClass(table.element as ClassElement);
|
||||
final getterName = ReCase(table.name).camelCase;
|
||||
|
||||
for (var table in parsedTables) {
|
||||
final infoType = table.tableInfoName;
|
||||
final getterName = table.tableFieldName;
|
||||
buffer.write('$infoType get $getterName => db.$getterName;\n');
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import 'package:moor/moor.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:moor_generator/src/errors.dart';
|
||||
import 'package:moor_generator/src/model/specified_database.dart';
|
||||
import 'package:moor_generator/src/model/specified_table.dart';
|
||||
import 'package:moor_generator/src/options.dart';
|
||||
|
@ -34,17 +33,7 @@ class MoorGenerator extends GeneratorForAnnotation<UseMoor> {
|
|||
var resolvedQueries = <SqlQuery>[];
|
||||
|
||||
for (var table in tableTypes) {
|
||||
if (!state.tableTypeChecker.isAssignableFrom(table.element)) {
|
||||
state.errors.add(MoorError(
|
||||
critical: true,
|
||||
message: 'The type $table is not a moor table',
|
||||
affectedElement: element));
|
||||
} else {
|
||||
final specifiedTable =
|
||||
state.tableParser.parse(table.element as ClassElement);
|
||||
state.foundTables[table] = specifiedTable;
|
||||
tablesForThisDb.add(specifiedTable);
|
||||
}
|
||||
tablesForThisDb.add(state.parseType(table, element));
|
||||
}
|
||||
|
||||
if (state.errors.errors.isNotEmpty) {
|
||||
|
|
|
@ -34,4 +34,18 @@ class SharedState {
|
|||
final result = ParsedLibraryResultImpl.tmp(element.library);
|
||||
return result.getElementDeclaration(element);
|
||||
}
|
||||
|
||||
SpecifiedTable parseType(DartType type, Element initializedBy) {
|
||||
return foundTables.putIfAbsent(type, () {
|
||||
if (!tableTypeChecker.isAssignableFrom(type.element)) {
|
||||
errors.add(MoorError(
|
||||
critical: true,
|
||||
message: 'The type $type is not a moor table',
|
||||
affectedElement: initializedBy,
|
||||
));
|
||||
} else {
|
||||
return tableParser.parse(type.element as ClassElement);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue