platform/angel_orm_generator/lib/src/readers.dart

67 lines
2.1 KiB
Dart
Raw Normal View History

2018-08-24 13:03:31 +00:00
import 'package:analyzer/dart/constant/value.dart';
2019-04-03 09:57:27 +00:00
import 'package:analyzer/dart/element/element.dart';
2019-04-02 23:05:13 +00:00
import 'package:analyzer/dart/element/type.dart';
2018-08-24 13:03:31 +00:00
import 'package:angel_orm/angel_orm.dart';
import 'package:source_gen/source_gen.dart';
2019-04-02 23:05:13 +00:00
import 'orm_build_context.dart';
2018-08-24 13:03:31 +00:00
2018-08-24 13:17:04 +00:00
const TypeChecker columnTypeChecker = const TypeChecker.fromRuntime(Column);
2018-08-24 14:17:12 +00:00
Orm reviveORMAnnotation(ConstantReader reader) {
2018-12-01 19:03:43 +00:00
return Orm(tableName: reader.peek('tableName')?.stringValue);
2018-08-24 13:03:31 +00:00
}
class ColumnReader {
final ConstantReader reader;
ColumnReader(this.reader);
bool get isNullable => reader.peek('isNullable')?.boolValue ?? true;
int get length => reader.peek('length')?.intValue;
DartObject get defaultValue => reader.peek('defaultValue')?.objectValue;
}
2019-04-02 23:05:13 +00:00
class RelationshipReader {
final int type;
final String localKey;
final String foreignKey;
final String foreignTable;
final bool cascadeOnDelete;
final DartType through;
final OrmBuildContext foreign;
final OrmBuildContext throughContext;
const RelationshipReader(this.type,
{this.localKey,
this.foreignKey,
this.foreignTable,
this.cascadeOnDelete,
this.through,
this.foreign,
this.throughContext});
bool get isManyToMany =>
type == RelationshipType.hasMany && throughContext != null;
2019-04-03 09:57:27 +00:00
FieldElement findLocalField(OrmBuildContext ctx) {
return ctx.effectiveFields.firstWhere(
(f) => ctx.buildContext.resolveFieldName(f.name) == localKey,
orElse: () {
throw '${ctx.buildContext.clazz.name} has no field that maps to the name "$localKey", '
'but it has a @HasMany() relation that expects such a field.';
});
}
FieldElement findForeignField(OrmBuildContext ctx) {
var foreign = throughContext ?? this.foreign;
return foreign.effectiveFields.firstWhere(
(f) => foreign.buildContext.resolveFieldName(f.name) == foreignKey,
orElse: () {
throw '${foreign.buildContext.clazz.name} has no field that maps to the name "$foreignKey", '
'but ${ctx.buildContext.clazz.name} has a @HasMany() relation that expects such a field.';
});
}
2019-04-02 23:05:13 +00:00
}