From 643661cf868c37b90bc7bf74c789087b5d3809ef Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 7 Dec 2018 21:57:09 -0500 Subject: [PATCH] all has one tests pass --- .../lib/src/orm_build_context.dart | 21 ++++++++++++++++--- .../lib/src/orm_generator.dart | 4 +++- angel_orm_generator/test/has_one_test.dart | 1 + angel_orm_generator/test/migrations/foot.sql | 7 +++++++ angel_orm_generator/test/migrations/leg.sql | 6 ++++++ angel_orm_generator/test/models/author.g.dart | 1 + angel_orm_generator/test/models/book.g.dart | 1 + angel_orm_generator/test/models/car.g.dart | 1 + .../test/models/customer.g.dart | 1 + angel_orm_generator/test/models/foot.dart | 2 +- angel_orm_generator/test/models/foot.g.dart | 3 ++- angel_orm_generator/test/models/fruit.g.dart | 1 + angel_orm_generator/test/models/leg.g.dart | 10 ++++++++- angel_orm_generator/test/models/order.g.dart | 1 + angel_orm_generator/test/models/role.g.dart | 1 + angel_orm_generator/test/models/tree.dart | 1 + 16 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 angel_orm_generator/test/migrations/foot.sql create mode 100644 angel_orm_generator/test/migrations/leg.sql diff --git a/angel_orm_generator/lib/src/orm_build_context.dart b/angel_orm_generator/lib/src/orm_build_context.dart index ebc4872d..487635d1 100644 --- a/angel_orm_generator/lib/src/orm_build_context.dart +++ b/angel_orm_generator/lib/src/orm_build_context.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.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_serialize/angel_serialize.dart'; import 'package:angel_serialize_generator/angel_serialize_generator.dart'; @@ -112,16 +113,18 @@ Future buildOrmContext( 'Cannot apply relationship to field "${field.name}" - ${field.type.name} is not assignable to Model.'); } else { try { + var modelType = firstModelAncestor(field.type) ?? field.type; + foreign = await buildOrmContext( - field.type.element as ClassElement, + modelType.element as ClassElement, new ConstantReader(const TypeChecker.fromRuntime(Serializable) - .firstAnnotationOf(field.type.element)), + .firstAnnotationOf(modelType.element)), buildStep, resolver, autoSnakeCaseNames, autoIdAndDateFields); var ormAnn = const TypeChecker.fromRuntime(Orm) - .firstAnnotationOf(field.type.element); + .firstAnnotationOf(modelType.element); if (ormAnn != null) { foreignTable = @@ -243,3 +246,15 @@ class RelationFieldImpl extends ShimFieldImpl { RelationFieldImpl(String name, DartType type, this.originalFieldName) : 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; + } +} diff --git a/angel_orm_generator/lib/src/orm_generator.dart b/angel_orm_generator/lib/src/orm_generator.dart index 768a1202..55a1396b 100644 --- a/angel_orm_generator/lib/src/orm_generator.dart +++ b/angel_orm_generator/lib/src/orm_generator.dart @@ -153,6 +153,7 @@ class OrmGenerator extends GeneratorForAnnotation { args[field.name] = expr; } + b.statements.add(new Code('if (row.every((x) => x == null)) return null;')); b.addExpression(ctx.buildContext.modelClassType .newInstance([], args).assignVar('model')); @@ -200,7 +201,8 @@ class OrmGenerator extends GeneratorForAnnotation { ..body = new Block((b) { ctx.relations.forEach((fieldName, relation) { //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 additionalFields = foreign.effectiveFields .where((f) => f.name != 'id' || !isSpecialId(f)) diff --git a/angel_orm_generator/test/has_one_test.dart b/angel_orm_generator/test/has_one_test.dart index f4366042..2879569c 100644 --- a/angel_orm_generator/test/has_one_test.dart +++ b/angel_orm_generator/test/has_one_test.dart @@ -20,6 +20,7 @@ main() { test('sets to null if no child', () async { var query = new LegQuery()..where.id.equals(int.parse(originalLeg.id)); var leg = await query.getOne(executor); + print(leg.toJson()); expect(leg.name, originalLeg.name); expect(leg.id, originalLeg.id); expect(leg.foot, isNull); diff --git a/angel_orm_generator/test/migrations/foot.sql b/angel_orm_generator/test/migrations/foot.sql new file mode 100644 index 00000000..af8bfc1a --- /dev/null +++ b/angel_orm_generator/test/migrations/foot.sql @@ -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 +); \ No newline at end of file diff --git a/angel_orm_generator/test/migrations/leg.sql b/angel_orm_generator/test/migrations/leg.sql new file mode 100644 index 00000000..abc00d39 --- /dev/null +++ b/angel_orm_generator/test/migrations/leg.sql @@ -0,0 +1,6 @@ +CREATE TEMPORARY TABLE "legs" ( + id serial PRIMARY KEY, + name varchar(255) NOT NULL, + created_at timestamp, + updated_at timestamp +); \ No newline at end of file diff --git a/angel_orm_generator/test/models/author.g.dart b/angel_orm_generator/test/models/author.g.dart index d04e2c68..65f37a20 100644 --- a/angel_orm_generator/test/models/author.g.dart +++ b/angel_orm_generator/test/models/author.g.dart @@ -29,6 +29,7 @@ class AuthorQuery extends Query { } static Author parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Author( id: row[0].toString(), name: (row[1] as String), diff --git a/angel_orm_generator/test/models/book.g.dart b/angel_orm_generator/test/models/book.g.dart index b12d60f2..ec996ed8 100644 --- a/angel_orm_generator/test/models/book.g.dart +++ b/angel_orm_generator/test/models/book.g.dart @@ -43,6 +43,7 @@ class BookQuery extends Query { } static Book parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Book( id: row[0].toString(), name: (row[3] as String), diff --git a/angel_orm_generator/test/models/car.g.dart b/angel_orm_generator/test/models/car.g.dart index 13240eeb..cf25024e 100644 --- a/angel_orm_generator/test/models/car.g.dart +++ b/angel_orm_generator/test/models/car.g.dart @@ -37,6 +37,7 @@ class CarQuery extends Query { } static Car parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Car( id: row[0].toString(), make: (row[1] as String), diff --git a/angel_orm_generator/test/models/customer.g.dart b/angel_orm_generator/test/models/customer.g.dart index 9b22874e..c12fe984 100644 --- a/angel_orm_generator/test/models/customer.g.dart +++ b/angel_orm_generator/test/models/customer.g.dart @@ -29,6 +29,7 @@ class CustomerQuery extends Query { } static Customer parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Customer( id: row[0].toString(), createdAt: (row[1] as DateTime), diff --git a/angel_orm_generator/test/models/foot.dart b/angel_orm_generator/test/models/foot.dart index d25d6489..0fd189ce 100644 --- a/angel_orm_generator/test/models/foot.dart +++ b/angel_orm_generator/test/models/foot.dart @@ -7,7 +7,7 @@ part 'foot.g.dart'; part 'foot.serializer.g.dart'; @serializable -@orm +@Orm(tableName: 'feet') class _Foot extends Model { int legId, nToes; } diff --git a/angel_orm_generator/test/models/foot.g.dart b/angel_orm_generator/test/models/foot.g.dart index 443503ae..f7936160 100644 --- a/angel_orm_generator/test/models/foot.g.dart +++ b/angel_orm_generator/test/models/foot.g.dart @@ -15,7 +15,7 @@ class FootQuery extends Query { @override get tableName { - return 'foots'; + return 'feet'; } @override @@ -29,6 +29,7 @@ class FootQuery extends Query { } static Foot parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Foot( id: row[0].toString(), legId: (row[1] as int), diff --git a/angel_orm_generator/test/models/fruit.g.dart b/angel_orm_generator/test/models/fruit.g.dart index e00ccc29..6ebdbfb3 100644 --- a/angel_orm_generator/test/models/fruit.g.dart +++ b/angel_orm_generator/test/models/fruit.g.dart @@ -29,6 +29,7 @@ class FruitQuery extends Query { } static Fruit parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Fruit( id: row[0].toString(), treeId: (row[1] as int), diff --git a/angel_orm_generator/test/models/leg.g.dart b/angel_orm_generator/test/models/leg.g.dart index 7b53d80f..1f9420f7 100644 --- a/angel_orm_generator/test/models/leg.g.dart +++ b/angel_orm_generator/test/models/leg.g.dart @@ -7,7 +7,14 @@ part of angel_orm_generator.test.models.leg; // ************************************************************************** class LegQuery extends Query { - LegQuery() {} + LegQuery() { + leftJoin('feet', 'id', 'leg_id', additionalFields: const [ + 'leg_id', + 'n_toes', + 'created_at', + 'updated_at' + ]); + } @override final LegQueryValues values = new LegQueryValues(); @@ -31,6 +38,7 @@ class LegQuery extends Query { } static Leg parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Leg( id: row[0].toString(), name: (row[1] as String), diff --git a/angel_orm_generator/test/models/order.g.dart b/angel_orm_generator/test/models/order.g.dart index 2210b6b2..b959e436 100644 --- a/angel_orm_generator/test/models/order.g.dart +++ b/angel_orm_generator/test/models/order.g.dart @@ -37,6 +37,7 @@ class OrderQuery extends Query { } static Order parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Order( id: row[0].toString(), customerId: (row[1] as int), diff --git a/angel_orm_generator/test/models/role.g.dart b/angel_orm_generator/test/models/role.g.dart index 0c03456f..645f1558 100644 --- a/angel_orm_generator/test/models/role.g.dart +++ b/angel_orm_generator/test/models/role.g.dart @@ -29,6 +29,7 @@ class RoleQuery extends Query { } static Role parseRow(List row) { + if (row.every((x) => x == null)) return null; var model = new Role( id: row[0].toString(), name: (row[1] as String), diff --git a/angel_orm_generator/test/models/tree.dart b/angel_orm_generator/test/models/tree.dart index ceb93600..d5e38780 100644 --- a/angel_orm_generator/test/models/tree.dart +++ b/angel_orm_generator/test/models/tree.dart @@ -3,6 +3,7 @@ library angel_orm_generator.test.models.tree; import 'package:angel_model/angel_model.dart'; import 'package:angel_orm/angel_orm.dart'; import 'package:angel_serialize/angel_serialize.dart'; +import 'package:collection/collection.dart'; import 'fruit.dart'; part 'tree.g.dart'; part 'tree.serializer.g.dart';