all has one tests pass
This commit is contained in:
parent
43736c4f48
commit
643661cf86
16 changed files with 55 additions and 7 deletions
|
@ -3,6 +3,7 @@ import 'dart:async';
|
||||||
import 'package:analyzer/dart/constant/value.dart';
|
import 'package:analyzer/dart/constant/value.dart';
|
||||||
import 'package:analyzer/dart/element/element.dart';
|
import 'package:analyzer/dart/element/element.dart';
|
||||||
import 'package:analyzer/dart/element/type.dart';
|
import 'package:analyzer/dart/element/type.dart';
|
||||||
|
import 'package:angel_model/angel_model.dart';
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:angel_serialize/angel_serialize.dart';
|
import 'package:angel_serialize/angel_serialize.dart';
|
||||||
import 'package:angel_serialize_generator/angel_serialize_generator.dart';
|
import 'package:angel_serialize_generator/angel_serialize_generator.dart';
|
||||||
|
@ -112,16 +113,18 @@ Future<OrmBuildContext> buildOrmContext(
|
||||||
'Cannot apply relationship to field "${field.name}" - ${field.type.name} is not assignable to Model.');
|
'Cannot apply relationship to field "${field.name}" - ${field.type.name} is not assignable to Model.');
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
var modelType = firstModelAncestor(field.type) ?? field.type;
|
||||||
|
|
||||||
foreign = await buildOrmContext(
|
foreign = await buildOrmContext(
|
||||||
field.type.element as ClassElement,
|
modelType.element as ClassElement,
|
||||||
new ConstantReader(const TypeChecker.fromRuntime(Serializable)
|
new ConstantReader(const TypeChecker.fromRuntime(Serializable)
|
||||||
.firstAnnotationOf(field.type.element)),
|
.firstAnnotationOf(modelType.element)),
|
||||||
buildStep,
|
buildStep,
|
||||||
resolver,
|
resolver,
|
||||||
autoSnakeCaseNames,
|
autoSnakeCaseNames,
|
||||||
autoIdAndDateFields);
|
autoIdAndDateFields);
|
||||||
var ormAnn = const TypeChecker.fromRuntime(Orm)
|
var ormAnn = const TypeChecker.fromRuntime(Orm)
|
||||||
.firstAnnotationOf(field.type.element);
|
.firstAnnotationOf(modelType.element);
|
||||||
|
|
||||||
if (ormAnn != null) {
|
if (ormAnn != null) {
|
||||||
foreignTable =
|
foreignTable =
|
||||||
|
@ -243,3 +246,15 @@ class RelationFieldImpl extends ShimFieldImpl {
|
||||||
RelationFieldImpl(String name, DartType type, this.originalFieldName)
|
RelationFieldImpl(String name, DartType type, this.originalFieldName)
|
||||||
: super(name, type);
|
: super(name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InterfaceType firstModelAncestor(DartType type) {
|
||||||
|
if (type is InterfaceType) {
|
||||||
|
if (const TypeChecker.fromRuntime(Model).isExactlyType(type.superclass)) {
|
||||||
|
return type;
|
||||||
|
} else {
|
||||||
|
return firstModelAncestor(type.superclass);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -153,6 +153,7 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
args[field.name] = expr;
|
args[field.name] = expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b.statements.add(new Code('if (row.every((x) => x == null)) return null;'));
|
||||||
b.addExpression(ctx.buildContext.modelClassType
|
b.addExpression(ctx.buildContext.modelClassType
|
||||||
.newInstance([], args).assignVar('model'));
|
.newInstance([], args).assignVar('model'));
|
||||||
|
|
||||||
|
@ -200,7 +201,8 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
..body = new Block((b) {
|
..body = new Block((b) {
|
||||||
ctx.relations.forEach((fieldName, relation) {
|
ctx.relations.forEach((fieldName, relation) {
|
||||||
//var name = ctx.buildContext.resolveFieldName(fieldName);
|
//var name = ctx.buildContext.resolveFieldName(fieldName);
|
||||||
if (relation.type == RelationshipType.belongsTo) {
|
if (relation.type == RelationshipType.belongsTo ||
|
||||||
|
relation.type == RelationshipType.hasOne) {
|
||||||
var foreign = ctx.relationTypes[relation];
|
var foreign = ctx.relationTypes[relation];
|
||||||
var additionalFields = foreign.effectiveFields
|
var additionalFields = foreign.effectiveFields
|
||||||
.where((f) => f.name != 'id' || !isSpecialId(f))
|
.where((f) => f.name != 'id' || !isSpecialId(f))
|
||||||
|
|
|
@ -20,6 +20,7 @@ main() {
|
||||||
test('sets to null if no child', () async {
|
test('sets to null if no child', () async {
|
||||||
var query = new LegQuery()..where.id.equals(int.parse(originalLeg.id));
|
var query = new LegQuery()..where.id.equals(int.parse(originalLeg.id));
|
||||||
var leg = await query.getOne(executor);
|
var leg = await query.getOne(executor);
|
||||||
|
print(leg.toJson());
|
||||||
expect(leg.name, originalLeg.name);
|
expect(leg.name, originalLeg.name);
|
||||||
expect(leg.id, originalLeg.id);
|
expect(leg.id, originalLeg.id);
|
||||||
expect(leg.foot, isNull);
|
expect(leg.foot, isNull);
|
||||||
|
|
7
angel_orm_generator/test/migrations/foot.sql
Normal file
7
angel_orm_generator/test/migrations/foot.sql
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
CREATE TEMPORARY TABLE "feet" (
|
||||||
|
id serial PRIMARY KEY,
|
||||||
|
leg_id int NOT NULL,
|
||||||
|
n_toes int NOT NULL,
|
||||||
|
created_at timestamp,
|
||||||
|
updated_at timestamp
|
||||||
|
);
|
6
angel_orm_generator/test/migrations/leg.sql
Normal file
6
angel_orm_generator/test/migrations/leg.sql
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
CREATE TEMPORARY TABLE "legs" (
|
||||||
|
id serial PRIMARY KEY,
|
||||||
|
name varchar(255) NOT NULL,
|
||||||
|
created_at timestamp,
|
||||||
|
updated_at timestamp
|
||||||
|
);
|
|
@ -29,6 +29,7 @@ class AuthorQuery extends Query<Author, AuthorQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Author parseRow(List row) {
|
static Author parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Author(
|
var model = new Author(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
name: (row[1] as String),
|
name: (row[1] as String),
|
||||||
|
|
|
@ -43,6 +43,7 @@ class BookQuery extends Query<Book, BookQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Book parseRow(List row) {
|
static Book parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Book(
|
var model = new Book(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
name: (row[3] as String),
|
name: (row[3] as String),
|
||||||
|
|
|
@ -37,6 +37,7 @@ class CarQuery extends Query<Car, CarQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Car parseRow(List row) {
|
static Car parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Car(
|
var model = new Car(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
make: (row[1] as String),
|
make: (row[1] as String),
|
||||||
|
|
|
@ -29,6 +29,7 @@ class CustomerQuery extends Query<Customer, CustomerQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Customer parseRow(List row) {
|
static Customer parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Customer(
|
var model = new Customer(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
createdAt: (row[1] as DateTime),
|
createdAt: (row[1] as DateTime),
|
||||||
|
|
|
@ -7,7 +7,7 @@ part 'foot.g.dart';
|
||||||
part 'foot.serializer.g.dart';
|
part 'foot.serializer.g.dart';
|
||||||
|
|
||||||
@serializable
|
@serializable
|
||||||
@orm
|
@Orm(tableName: 'feet')
|
||||||
class _Foot extends Model {
|
class _Foot extends Model {
|
||||||
int legId, nToes;
|
int legId, nToes;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ class FootQuery extends Query<Foot, FootQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get tableName {
|
get tableName {
|
||||||
return 'foots';
|
return 'feet';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -29,6 +29,7 @@ class FootQuery extends Query<Foot, FootQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Foot parseRow(List row) {
|
static Foot parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Foot(
|
var model = new Foot(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
legId: (row[1] as int),
|
legId: (row[1] as int),
|
||||||
|
|
|
@ -29,6 +29,7 @@ class FruitQuery extends Query<Fruit, FruitQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Fruit parseRow(List row) {
|
static Fruit parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Fruit(
|
var model = new Fruit(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
treeId: (row[1] as int),
|
treeId: (row[1] as int),
|
||||||
|
|
|
@ -7,7 +7,14 @@ part of angel_orm_generator.test.models.leg;
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
class LegQuery extends Query<Leg, LegQueryWhere> {
|
class LegQuery extends Query<Leg, LegQueryWhere> {
|
||||||
LegQuery() {}
|
LegQuery() {
|
||||||
|
leftJoin('feet', 'id', 'leg_id', additionalFields: const [
|
||||||
|
'leg_id',
|
||||||
|
'n_toes',
|
||||||
|
'created_at',
|
||||||
|
'updated_at'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final LegQueryValues values = new LegQueryValues();
|
final LegQueryValues values = new LegQueryValues();
|
||||||
|
@ -31,6 +38,7 @@ class LegQuery extends Query<Leg, LegQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Leg parseRow(List row) {
|
static Leg parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Leg(
|
var model = new Leg(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
name: (row[1] as String),
|
name: (row[1] as String),
|
||||||
|
|
|
@ -37,6 +37,7 @@ class OrderQuery extends Query<Order, OrderQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Order parseRow(List row) {
|
static Order parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Order(
|
var model = new Order(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
customerId: (row[1] as int),
|
customerId: (row[1] as int),
|
||||||
|
|
|
@ -29,6 +29,7 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Role parseRow(List row) {
|
static Role parseRow(List row) {
|
||||||
|
if (row.every((x) => x == null)) return null;
|
||||||
var model = new Role(
|
var model = new Role(
|
||||||
id: row[0].toString(),
|
id: row[0].toString(),
|
||||||
name: (row[1] as String),
|
name: (row[1] as String),
|
||||||
|
|
|
@ -3,6 +3,7 @@ library angel_orm_generator.test.models.tree;
|
||||||
import 'package:angel_model/angel_model.dart';
|
import 'package:angel_model/angel_model.dart';
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:angel_serialize/angel_serialize.dart';
|
import 'package:angel_serialize/angel_serialize.dart';
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
import 'fruit.dart';
|
import 'fruit.dart';
|
||||||
part 'tree.g.dart';
|
part 'tree.g.dart';
|
||||||
part 'tree.serializer.g.dart';
|
part 'tree.serializer.g.dart';
|
||||||
|
|
Loading…
Reference in a new issue