From 399eebc67c7878ad2d3131037ebcb8d71bd72f19 Mon Sep 17 00:00:00 2001 From: thomashii Date: Thu, 6 Jan 2022 20:59:02 +0800 Subject: [PATCH] Updated ORM --- packages/orm/angel_orm_generator/CHANGELOG.md | 4 + .../lib/src/orm_generator.dart | 12 +- packages/orm/angel_orm_generator/pubspec.yaml | 2 +- packages/orm/angel_orm_test/CHANGELOG.md | 4 + .../angel_orm_test/lib/src/models/book.g.dart | 48 +++++-- .../angel_orm_test/lib/src/models/car.g.dart | 31 +++-- .../lib/src/models/custom_expr.g.dart | 46 +++++-- .../lib/src/models/email_indexed.g.dart | 59 +++++++-- .../lib/src/models/fortune.g.dart | 18 ++- .../lib/src/models/has_car.g.dart | 26 +++- .../lib/src/models/has_map.g.dart | 20 ++- .../angel_orm_test/lib/src/models/leg.g.dart | 50 +++++-- .../lib/src/models/order.g.dart | 49 +++++-- .../angel_orm_test/lib/src/models/tree.g.dart | 55 ++++++-- .../lib/src/models/unorthodox.g.dart | 125 ++++++++++++++---- .../angel_orm_test/lib/src/models/user.g.dart | 70 +++++++--- .../lib/src/models/world.g.dart | 19 ++- packages/orm/angel_orm_test/pubspec.yaml | 2 +- 18 files changed, 479 insertions(+), 161 deletions(-) diff --git a/packages/orm/angel_orm_generator/CHANGELOG.md b/packages/orm/angel_orm_generator/CHANGELOG.md index 49af1d4f..c445e772 100644 --- a/packages/orm/angel_orm_generator/CHANGELOG.md +++ b/packages/orm/angel_orm_generator/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 4.2.0 + +* Added `select(List fields)` + ## 4.1.3 * Fixed issue #24, incorrect class casting diff --git a/packages/orm/angel_orm_generator/lib/src/orm_generator.dart b/packages/orm/angel_orm_generator/lib/src/orm_generator.dart index b0acc9cf..eb75ef9b 100644 --- a/packages/orm/angel_orm_generator/lib/src/orm_generator.dart +++ b/packages/orm/angel_orm_generator/lib/src/orm_generator.dart @@ -248,8 +248,13 @@ class OrmGenerator extends GeneratorForAnnotation { type = refer('int'); } - literalNum(i++); - var expr = (refer('row').index(CodeExpression(Code('_index++')))); + // Reverted since as `continue` for `RelationshipFieldImp` will + // cause `_index` requires +2 or +3 in order to map to the correct + // fields + // + //literalNum(i++); + //var expr = (refer('row').index(CodeExpression(Code('_index++')))); + var expr = (refer('row').index(literalNum(i++))); if (isSpecialId(ctx, field)) { expr = expr.property('toString').call([]); } else if (field is RelationFieldImpl) { @@ -277,7 +282,7 @@ class OrmGenerator extends GeneratorForAnnotation { b.statements.add(Code( 'if (row.every((x) => x == null)) { return Optional.empty(); }')); - b.addExpression(refer('0').assignVar('_index')); + //b.addExpression(refer('0').assignVar('_index')); b.addExpression(ctx.buildContext.modelClassType .newInstance([], args).assignVar('model')); @@ -310,6 +315,7 @@ class OrmGenerator extends GeneratorForAnnotation { var parsed = refer( '${foreign.buildContext.modelClassNameRecase.pascalCase}Query') + .newInstance([]) .property('parseRow') .call([skipToList]); diff --git a/packages/orm/angel_orm_generator/pubspec.yaml b/packages/orm/angel_orm_generator/pubspec.yaml index 1b911e74..202214d6 100644 --- a/packages/orm/angel_orm_generator/pubspec.yaml +++ b/packages/orm/angel_orm_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_orm_generator -version: 4.1.3 +version: 4.2.0 description: Code generators for Angel3 ORM. Generates query builder classes. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_generator diff --git a/packages/orm/angel_orm_test/CHANGELOG.md b/packages/orm/angel_orm_test/CHANGELOG.md index 4eb05599..f68eadc4 100644 --- a/packages/orm/angel_orm_test/CHANGELOG.md +++ b/packages/orm/angel_orm_test/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 3.0.5 + +* Updated test cases + ## 3.0.4 * Updated test cases diff --git a/packages/orm/angel_orm_test/lib/src/models/book.g.dart b/packages/orm/angel_orm_test/lib/src/models/book.g.dart index 27c20d09..417b84af 100644 --- a/packages/orm/angel_orm_test/lib/src/models/book.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/book.g.dart @@ -68,6 +68,8 @@ class BookQuery extends Query { @override final BookQueryValues values = BookQueryValues(); + List _selectedFields = []; + BookQueryWhere? _where; late AuthorQuery _author; @@ -86,7 +88,7 @@ class BookQuery extends Query { @override List get fields { - return const [ + const _fields = [ 'id', 'created_at', 'updated_at', @@ -94,6 +96,14 @@ class BookQuery extends Query { 'partner_author_id', 'name' ]; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + BookQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -106,23 +116,23 @@ class BookQuery extends Query { return BookQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Book( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - name: (row[5] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + name: fields.contains('name') ? (row[5] as String?) : null); if (row.length > 6) { - var modelOpt = AuthorQuery.parseRow(row.skip(6).take(4).toList()); + var modelOpt = AuthorQuery().parseRow(row.skip(6).take(4).toList()); modelOpt.ifPresent((m) { model = model.copyWith(author: m); }); } if (row.length > 10) { - var modelOpt = AuthorQuery.parseRow(row.skip(10).take(4).toList()); + var modelOpt = AuthorQuery().parseRow(row.skip(10).take(4).toList()); modelOpt.ifPresent((m) { model = model.copyWith(partnerAuthor: m); }); @@ -232,6 +242,8 @@ class AuthorQuery extends Query { @override final AuthorQueryValues values = AuthorQueryValues(); + List _selectedFields = []; + AuthorQueryWhere? _where; @override @@ -246,7 +258,15 @@ class AuthorQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'name']; + const _fields = ['id', 'created_at', 'updated_at', 'name']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + AuthorQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -259,15 +279,15 @@ class AuthorQuery extends Query { return AuthorQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Author( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - name: (row[3] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + name: fields.contains('name') ? (row[3] as String?) : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/car.g.dart b/packages/orm/angel_orm_test/lib/src/models/car.g.dart index 6a055cf1..e7329ddc 100644 --- a/packages/orm/angel_orm_test/lib/src/models/car.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/car.g.dart @@ -40,6 +40,8 @@ class CarQuery extends Query { @override final CarQueryValues values = CarQueryValues(); + List _selectedFields = []; + CarQueryWhere? _where; @override @@ -54,7 +56,7 @@ class CarQuery extends Query { @override List get fields { - return const [ + const _fields = [ 'id', 'created_at', 'updated_at', @@ -63,6 +65,14 @@ class CarQuery extends Query { 'family_friendly', 'recalled_at' ]; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + CarQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -75,18 +85,21 @@ class CarQuery extends Query { return CarQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Car( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - make: (row[3] as String?), - description: (row[4] as String?), - familyFriendly: (row[5] as bool?), - recalledAt: (row[6] as DateTime?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + make: fields.contains('make') ? (row[3] as String?) : null, + description: + fields.contains('description') ? (row[4] as String?) : null, + familyFriendly: + fields.contains('family_friendly') ? (row[5] as bool?) : null, + recalledAt: + fields.contains('recalled_at') ? (row[6] as DateTime?) : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/custom_expr.g.dart b/packages/orm/angel_orm_test/lib/src/models/custom_expr.g.dart index 24b8427a..a5335c28 100644 --- a/packages/orm/angel_orm_test/lib/src/models/custom_expr.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/custom_expr.g.dart @@ -58,6 +58,8 @@ class NumbersQuery extends Query { @override final NumbersQueryValues values = NumbersQueryValues(); + List _selectedFields = []; + NumbersQueryWhere? _where; @override @@ -72,7 +74,15 @@ class NumbersQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'two']; + const _fields = ['id', 'created_at', 'updated_at', 'two']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + NumbersQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -85,15 +95,15 @@ class NumbersQuery extends Query { return NumbersQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Numbers( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - two: (row[3] as int?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + two: fields.contains('two') ? (row[3] as int?) : null); return Optional.of(model); } @@ -163,6 +173,8 @@ class AlphabetQuery extends Query { @override final AlphabetQueryValues values = AlphabetQueryValues(); + List _selectedFields = []; + AlphabetQueryWhere? _where; late NumbersQuery _numbers; @@ -179,7 +191,15 @@ class AlphabetQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'value', 'numbers_id']; + const _fields = ['id', 'created_at', 'updated_at', 'value', 'numbers_id']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + AlphabetQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -192,17 +212,17 @@ class AlphabetQuery extends Query { return AlphabetQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Alphabet( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - value: (row[3] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + value: fields.contains('value') ? (row[3] as String?) : null); if (row.length > 5) { - var modelOpt = NumbersQuery.parseRow(row.skip(5).take(4).toList()); + var modelOpt = NumbersQuery().parseRow(row.skip(5).take(4).toList()); modelOpt.ifPresent((m) { model = model.copyWith(numbers: m); }); diff --git a/packages/orm/angel_orm_test/lib/src/models/email_indexed.g.dart b/packages/orm/angel_orm_test/lib/src/models/email_indexed.g.dart index 7c7d4465..ea648f30 100644 --- a/packages/orm/angel_orm_test/lib/src/models/email_indexed.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/email_indexed.g.dart @@ -75,6 +75,8 @@ class RoleQuery extends Query { @override final RoleQueryValues values = RoleQueryValues(); + List _selectedFields = []; + RoleQueryWhere? _where; @override @@ -89,7 +91,15 @@ class RoleQuery extends Query { @override List get fields { - return const ['role']; + const _fields = ['role']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + RoleQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -102,13 +112,14 @@ class RoleQuery extends Query { return RoleQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } - var model = Role(role: (row[0] as String?)); + var model = + Role(role: fields.contains('role') ? (row[0] as String?) : null); if (row.length > 1) { - var modelOpt = UserQuery.parseRow(row.skip(1).take(3).toList()); + var modelOpt = UserQuery().parseRow(row.skip(1).take(3).toList()); modelOpt.ifPresent((m) { model = model.copyWith(users: [m]); }); @@ -228,6 +239,8 @@ class RoleUserQuery extends Query { @override final RoleUserQueryValues values = RoleUserQueryValues(); + List _selectedFields = []; + RoleUserQueryWhere? _where; late RoleQuery _role; @@ -246,7 +259,15 @@ class RoleUserQuery extends Query { @override List get fields { - return const ['role_role', 'user_email']; + const _fields = ['role_role', 'user_email']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + RoleUserQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -259,19 +280,19 @@ class RoleUserQuery extends Query { return RoleUserQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = RoleUser(); if (row.length > 2) { - var modelOpt = RoleQuery.parseRow(row.skip(2).take(1).toList()); + var modelOpt = RoleQuery().parseRow(row.skip(2).take(1).toList()); modelOpt.ifPresent((m) { model = model.copyWith(role: m); }); } if (row.length > 3) { - var modelOpt = UserQuery.parseRow(row.skip(3).take(3).toList()); + var modelOpt = UserQuery().parseRow(row.skip(3).take(3).toList()); modelOpt.ifPresent((m) { model = model.copyWith(user: m); }); @@ -350,6 +371,8 @@ class UserQuery extends Query { @override final UserQueryValues values = UserQueryValues(); + List _selectedFields = []; + UserQueryWhere? _where; @override @@ -364,7 +387,15 @@ class UserQuery extends Query { @override List get fields { - return const ['email', 'name', 'password']; + const _fields = ['email', 'name', 'password']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + UserQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -377,16 +408,16 @@ class UserQuery extends Query { return UserQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = User( - email: (row[0] as String?), - name: (row[1] as String?), - password: (row[2] as String?)); + email: fields.contains('email') ? (row[0] as String?) : null, + name: fields.contains('name') ? (row[1] as String?) : null, + password: fields.contains('password') ? (row[2] as String?) : null); if (row.length > 3) { - var modelOpt = RoleQuery.parseRow(row.skip(3).take(1).toList()); + var modelOpt = RoleQuery().parseRow(row.skip(3).take(1).toList()); modelOpt.ifPresent((m) { model = model.copyWith(roles: [m]); }); diff --git a/packages/orm/angel_orm_test/lib/src/models/fortune.g.dart b/packages/orm/angel_orm_test/lib/src/models/fortune.g.dart index 94cf60c1..dff7bbb5 100644 --- a/packages/orm/angel_orm_test/lib/src/models/fortune.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/fortune.g.dart @@ -36,6 +36,8 @@ class FortuneQuery extends Query { @override final FortuneQueryValues values = FortuneQueryValues(); + List _selectedFields = []; + FortuneQueryWhere? _where; @override @@ -50,7 +52,15 @@ class FortuneQuery extends Query { @override List get fields { - return const ['id', 'message']; + const _fields = ['id', 'message']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + FortuneQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -63,11 +73,13 @@ class FortuneQuery extends Query { return FortuneQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } - var model = Fortune(id: (row[0] as int?), message: (row[1] as String?)); + var model = Fortune( + id: fields.contains('id') ? (row[0] as int?) : null, + message: fields.contains('message') ? (row[1] as String?) : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/has_car.g.dart b/packages/orm/angel_orm_test/lib/src/models/has_car.g.dart index ad105820..5c6e39ac 100644 --- a/packages/orm/angel_orm_test/lib/src/models/has_car.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/has_car.g.dart @@ -38,6 +38,8 @@ class HasCarQuery extends Query { @override final HasCarQueryValues values = HasCarQueryValues(); + List _selectedFields = []; + HasCarQueryWhere? _where; @override @@ -52,7 +54,15 @@ class HasCarQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'type']; + const _fields = ['id', 'created_at', 'updated_at', 'type']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + HasCarQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -65,15 +75,19 @@ class HasCarQuery extends Query { return HasCarQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = HasCar( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - type: row[3] == null ? null : CarType?.values[(row[3] as int)]); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + type: fields.contains('type') + ? row[3] == null + ? null + : CarType?.values[(row[3] as int)] + : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/has_map.g.dart b/packages/orm/angel_orm_test/lib/src/models/has_map.g.dart index 11b8bcb3..5ae1f6a5 100644 --- a/packages/orm/angel_orm_test/lib/src/models/has_map.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/has_map.g.dart @@ -38,6 +38,8 @@ class HasMapQuery extends Query { @override final HasMapQueryValues values = HasMapQueryValues(); + List _selectedFields = []; + HasMapQueryWhere? _where; @override @@ -52,7 +54,15 @@ class HasMapQuery extends Query { @override List get fields { - return const ['value', 'list']; + const _fields = ['value', 'list']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + HasMapQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -65,13 +75,15 @@ class HasMapQuery extends Query { return HasMapQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = HasMap( - value: (row[0] as Map?), - list: (row[1] as List?)); + value: fields.contains('value') + ? (row[0] as Map?) + : null, + list: fields.contains('list') ? (row[1] as List?) : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/leg.g.dart b/packages/orm/angel_orm_test/lib/src/models/leg.g.dart index 109d0123..e2d692dc 100644 --- a/packages/orm/angel_orm_test/lib/src/models/leg.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/leg.g.dart @@ -66,6 +66,8 @@ class LegQuery extends Query { @override final LegQueryValues values = LegQueryValues(); + List _selectedFields = []; + LegQueryWhere? _where; late FootQuery _foot; @@ -82,7 +84,15 @@ class LegQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'name']; + const _fields = ['id', 'created_at', 'updated_at', 'name']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + LegQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -95,17 +105,17 @@ class LegQuery extends Query { return LegQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Leg( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - name: (row[3] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + name: fields.contains('name') ? (row[3] as String?) : null); if (row.length > 4) { - var modelOpt = FootQuery.parseRow(row.skip(4).take(5).toList()); + var modelOpt = FootQuery().parseRow(row.skip(4).take(5).toList()); modelOpt.ifPresent((m) { model = model.copyWith(foot: m); }); @@ -187,6 +197,8 @@ class FootQuery extends Query { @override final FootQueryValues values = FootQueryValues(); + List _selectedFields = []; + FootQueryWhere? _where; @override @@ -201,7 +213,15 @@ class FootQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'leg_id', 'n_toes']; + const _fields = ['id', 'created_at', 'updated_at', 'leg_id', 'n_toes']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + FootQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -214,16 +234,18 @@ class FootQuery extends Query { return FootQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Foot( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - legId: (row[3] as int?), - nToes: double.tryParse(row[4].toString())); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + legId: fields.contains('leg_id') ? (row[3] as int?) : null, + nToes: fields.contains('n_toes') + ? double.tryParse(row[4].toString()) + : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/order.g.dart b/packages/orm/angel_orm_test/lib/src/models/order.g.dart index b5b3172b..10c7b953 100644 --- a/packages/orm/angel_orm_test/lib/src/models/order.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/order.g.dart @@ -62,6 +62,8 @@ class OrderQuery extends Query { @override final OrderQueryValues values = OrderQueryValues(); + List _selectedFields = []; + OrderQueryWhere? _where; late CustomerQuery _customer; @@ -78,7 +80,7 @@ class OrderQuery extends Query { @override List get fields { - return const [ + const _fields = [ 'id', 'created_at', 'updated_at', @@ -87,6 +89,14 @@ class OrderQuery extends Query { 'order_date', 'shipper_id' ]; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + OrderQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -99,19 +109,19 @@ class OrderQuery extends Query { return OrderQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Order( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - employeeId: (row[4] as int?), - orderDate: (row[5] as DateTime?), - shipperId: (row[6] as int?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + employeeId: fields.contains('employee_id') ? (row[4] as int?) : null, + orderDate: fields.contains('order_date') ? (row[5] as DateTime?) : null, + shipperId: fields.contains('shipper_id') ? (row[6] as int?) : null); if (row.length > 7) { - var modelOpt = CustomerQuery.parseRow(row.skip(7).take(3).toList()); + var modelOpt = CustomerQuery().parseRow(row.skip(7).take(3).toList()); modelOpt.ifPresent((m) { model = model.copyWith(customer: m); }); @@ -231,6 +241,8 @@ class CustomerQuery extends Query { @override final CustomerQueryValues values = CustomerQueryValues(); + List _selectedFields = []; + CustomerQueryWhere? _where; @override @@ -245,7 +257,15 @@ class CustomerQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at']; + const _fields = ['id', 'created_at', 'updated_at']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + CustomerQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -258,14 +278,15 @@ class CustomerQuery extends Query { return CustomerQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Customer( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: + fields.contains('updated_at') ? (row[2] as DateTime?) : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/tree.g.dart b/packages/orm/angel_orm_test/lib/src/models/tree.g.dart index f3a9df1e..e0f2086d 100644 --- a/packages/orm/angel_orm_test/lib/src/models/tree.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/tree.g.dart @@ -65,6 +65,8 @@ class TreeQuery extends Query { @override final TreeQueryValues values = TreeQueryValues(); + List _selectedFields = []; + TreeQueryWhere? _where; late FruitQuery _fruits; @@ -81,7 +83,15 @@ class TreeQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'rings']; + const _fields = ['id', 'created_at', 'updated_at', 'rings']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + TreeQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -94,17 +104,17 @@ class TreeQuery extends Query { return TreeQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Tree( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - rings: (row[3] as int?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + rings: fields.contains('rings') ? (row[3] as int?) : null); if (row.length > 4) { - var modelOpt = FruitQuery.parseRow(row.skip(4).take(5).toList()); + var modelOpt = FruitQuery().parseRow(row.skip(4).take(5).toList()); modelOpt.ifPresent((m) { model = model.copyWith(fruits: [m]); }); @@ -240,6 +250,8 @@ class FruitQuery extends Query { @override final FruitQueryValues values = FruitQueryValues(); + List _selectedFields = []; + FruitQueryWhere? _where; @override @@ -254,7 +266,21 @@ class FruitQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'tree_id', 'common_name']; + const _fields = [ + 'id', + 'created_at', + 'updated_at', + 'tree_id', + 'common_name' + ]; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + FruitQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -267,16 +293,17 @@ class FruitQuery extends Query { return FruitQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Fruit( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - treeId: (row[3] as int?), - commonName: (row[4] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + treeId: fields.contains('tree_id') ? (row[3] as int?) : null, + commonName: + fields.contains('common_name') ? (row[4] as String?) : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/lib/src/models/unorthodox.g.dart b/packages/orm/angel_orm_test/lib/src/models/unorthodox.g.dart index 8c00498c..3231efef 100644 --- a/packages/orm/angel_orm_test/lib/src/models/unorthodox.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/unorthodox.g.dart @@ -116,6 +116,8 @@ class UnorthodoxQuery extends Query { @override final UnorthodoxQueryValues values = UnorthodoxQueryValues(); + List _selectedFields = []; + UnorthodoxQueryWhere? _where; @override @@ -130,7 +132,15 @@ class UnorthodoxQuery extends Query { @override List get fields { - return const ['name']; + const _fields = ['name']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + UnorthodoxQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -143,11 +153,12 @@ class UnorthodoxQuery extends Query { return UnorthodoxQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } - var model = Unorthodox(name: (row[0] as String?)); + var model = + Unorthodox(name: fields.contains('name') ? (row[0] as String?) : null); return Optional.of(model); } @@ -221,6 +232,8 @@ class WeirdJoinQuery extends Query { @override final WeirdJoinQueryValues values = WeirdJoinQueryValues(); + List _selectedFields = []; + WeirdJoinQueryWhere? _where; late UnorthodoxQuery _unorthodox; @@ -241,7 +254,15 @@ class WeirdJoinQuery extends Query { @override List get fields { - return const ['id', 'join_name']; + const _fields = ['id', 'join_name']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + WeirdJoinQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -254,31 +275,31 @@ class WeirdJoinQuery extends Query { return WeirdJoinQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } - var model = WeirdJoin(id: (row[0] as int?)); + var model = WeirdJoin(id: fields.contains('id') ? (row[0] as int?) : null); if (row.length > 2) { - var modelOpt = UnorthodoxQuery.parseRow(row.skip(2).take(1).toList()); + var modelOpt = UnorthodoxQuery().parseRow(row.skip(2).take(1).toList()); modelOpt.ifPresent((m) { model = model.copyWith(unorthodox: m); }); } if (row.length > 3) { - var modelOpt = SongQuery.parseRow(row.skip(3).take(5).toList()); + var modelOpt = SongQuery().parseRow(row.skip(3).take(5).toList()); modelOpt.ifPresent((m) { model = model.copyWith(song: m); }); } if (row.length > 8) { - var modelOpt = NumbaQuery.parseRow(row.skip(8).take(2).toList()); + var modelOpt = NumbaQuery().parseRow(row.skip(8).take(2).toList()); modelOpt.ifPresent((m) { model = model.copyWith(numbas: [m]); }); } if (row.length > 10) { - var modelOpt = FooQuery.parseRow(row.skip(10).take(1).toList()); + var modelOpt = FooQuery().parseRow(row.skip(10).take(1).toList()); modelOpt.ifPresent((m) { model = model.copyWith(foos: [m]); }); @@ -416,6 +437,8 @@ class SongQuery extends Query { @override final SongQueryValues values = SongQueryValues(); + List _selectedFields = []; + SongQueryWhere? _where; @override @@ -430,7 +453,21 @@ class SongQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'weird_join_id', 'title']; + const _fields = [ + 'id', + 'created_at', + 'updated_at', + 'weird_join_id', + 'title' + ]; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + SongQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -443,16 +480,16 @@ class SongQuery extends Query { return SongQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Song( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - weirdJoinId: (row[3] as int?), - title: (row[4] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + weirdJoinId: fields.contains('weird_join_id') ? (row[3] as int?) : null, + title: fields.contains('title') ? (row[4] as String?) : null); return Optional.of(model); } @@ -535,6 +572,8 @@ class NumbaQuery extends Query { @override final NumbaQueryValues values = NumbaQueryValues(); + List _selectedFields = []; + NumbaQueryWhere? _where; @override @@ -549,7 +588,15 @@ class NumbaQuery extends Query { @override List get fields { - return const ['i', 'parent']; + const _fields = ['i', 'parent']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + NumbaQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -562,11 +609,13 @@ class NumbaQuery extends Query { return NumbaQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } - var model = Numba(i: (row[0] as int?), parent: (row[1] as int?)); + var model = Numba( + i: fields.contains('i') ? (row[0] as int?) : null, + parent: fields.contains('parent') ? (row[1] as int?) : null); return Optional.of(model); } @@ -629,6 +678,8 @@ class FooQuery extends Query { @override final FooQueryValues values = FooQueryValues(); + List _selectedFields = []; + FooQueryWhere? _where; @override @@ -643,7 +694,15 @@ class FooQuery extends Query { @override List get fields { - return const ['bar']; + const _fields = ['bar']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + FooQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -656,13 +715,13 @@ class FooQuery extends Query { return FooQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } - var model = Foo(bar: (row[0] as String?)); + var model = Foo(bar: fields.contains('bar') ? (row[0] as String?) : null); if (row.length > 1) { - var modelOpt = WeirdJoinQuery.parseRow(row.skip(1).take(2).toList()); + var modelOpt = WeirdJoinQuery().parseRow(row.skip(1).take(2).toList()); modelOpt.ifPresent((m) { model = model.copyWith(weirdJoins: [m]); }); @@ -784,6 +843,8 @@ class FooPivotQuery extends Query { @override final FooPivotQueryValues values = FooPivotQueryValues(); + List _selectedFields = []; + FooPivotQueryWhere? _where; late WeirdJoinQuery _weirdJoin; @@ -802,7 +863,15 @@ class FooPivotQuery extends Query { @override List get fields { - return const ['weird_join_id', 'foo_bar']; + const _fields = ['weird_join_id', 'foo_bar']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + FooPivotQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -815,19 +884,19 @@ class FooPivotQuery extends Query { return FooPivotQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = FooPivot(); if (row.length > 2) { - var modelOpt = WeirdJoinQuery.parseRow(row.skip(2).take(2).toList()); + var modelOpt = WeirdJoinQuery().parseRow(row.skip(2).take(2).toList()); modelOpt.ifPresent((m) { model = model.copyWith(weirdJoin: m); }); } if (row.length > 4) { - var modelOpt = FooQuery.parseRow(row.skip(4).take(1).toList()); + var modelOpt = FooQuery().parseRow(row.skip(4).take(1).toList()); modelOpt.ifPresent((m) { model = model.copyWith(foo: m); }); diff --git a/packages/orm/angel_orm_test/lib/src/models/user.g.dart b/packages/orm/angel_orm_test/lib/src/models/user.g.dart index 63ba4437..4c883a9b 100644 --- a/packages/orm/angel_orm_test/lib/src/models/user.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/user.g.dart @@ -77,6 +77,8 @@ class UserQuery extends Query { @override final UserQueryValues values = UserQueryValues(); + List _selectedFields = []; + UserQueryWhere? _where; @override @@ -91,7 +93,7 @@ class UserQuery extends Query { @override List get fields { - return const [ + const _fields = [ 'id', 'created_at', 'updated_at', @@ -99,6 +101,14 @@ class UserQuery extends Query { 'password', 'email' ]; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + UserQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -111,19 +121,19 @@ class UserQuery extends Query { return UserQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = User( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - username: (row[3] as String?), - password: (row[4] as String?), - email: (row[5] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + username: fields.contains('username') ? (row[3] as String?) : null, + password: fields.contains('password') ? (row[4] as String?) : null, + email: fields.contains('email') ? (row[5] as String?) : null); if (row.length > 6) { - var modelOpt = RoleQuery.parseRow(row.skip(6).take(4).toList()); + var modelOpt = RoleQuery().parseRow(row.skip(6).take(4).toList()); modelOpt.ifPresent((m) { model = model.copyWith(roles: [m]); }); @@ -295,6 +305,8 @@ class RoleUserQuery extends Query { @override final RoleUserQueryValues values = RoleUserQueryValues(); + List _selectedFields = []; + RoleUserQueryWhere? _where; late RoleQuery _role; @@ -313,7 +325,15 @@ class RoleUserQuery extends Query { @override List get fields { - return const ['role_id', 'user_id']; + const _fields = ['role_id', 'user_id']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + RoleUserQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -326,19 +346,19 @@ class RoleUserQuery extends Query { return RoleUserQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = RoleUser(); if (row.length > 2) { - var modelOpt = RoleQuery.parseRow(row.skip(2).take(4).toList()); + var modelOpt = RoleQuery().parseRow(row.skip(2).take(4).toList()); modelOpt.ifPresent((m) { model = model.copyWith(role: m); }); } if (row.length > 6) { - var modelOpt = UserQuery.parseRow(row.skip(6).take(6).toList()); + var modelOpt = UserQuery().parseRow(row.skip(6).take(6).toList()); modelOpt.ifPresent((m) { model = model.copyWith(user: m); }); @@ -424,6 +444,8 @@ class RoleQuery extends Query { @override final RoleQueryValues values = RoleQueryValues(); + List _selectedFields = []; + RoleQueryWhere? _where; @override @@ -438,7 +460,15 @@ class RoleQuery extends Query { @override List get fields { - return const ['id', 'created_at', 'updated_at', 'name']; + const _fields = ['id', 'created_at', 'updated_at', 'name']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + RoleQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -451,17 +481,17 @@ class RoleQuery extends Query { return RoleQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } var model = Role( - id: row[0].toString(), - createdAt: (row[1] as DateTime?), - updatedAt: (row[2] as DateTime?), - name: (row[3] as String?)); + id: fields.contains('id') ? row[0].toString() : null, + createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null, + updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null, + name: fields.contains('name') ? (row[3] as String?) : null); if (row.length > 4) { - var modelOpt = UserQuery.parseRow(row.skip(4).take(6).toList()); + var modelOpt = UserQuery().parseRow(row.skip(4).take(6).toList()); modelOpt.ifPresent((m) { model = model.copyWith(users: [m]); }); diff --git a/packages/orm/angel_orm_test/lib/src/models/world.g.dart b/packages/orm/angel_orm_test/lib/src/models/world.g.dart index 674c883d..da72ec89 100644 --- a/packages/orm/angel_orm_test/lib/src/models/world.g.dart +++ b/packages/orm/angel_orm_test/lib/src/models/world.g.dart @@ -35,6 +35,8 @@ class WorldQuery extends Query { @override final WorldQueryValues values = WorldQueryValues(); + List _selectedFields = []; + WorldQueryWhere? _where; @override @@ -49,7 +51,15 @@ class WorldQuery extends Query { @override List get fields { - return const ['id', 'random_number']; + const _fields = ['id', 'random_number']; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + WorldQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -62,11 +72,14 @@ class WorldQuery extends Query { return WorldQueryWhere(this); } - static Optional parseRow(List row) { + Optional parseRow(List row) { if (row.every((x) => x == null)) { return Optional.empty(); } - var model = World(id: (row[0] as int?), randomNumber: (row[1] as int?)); + var model = World( + id: fields.contains('id') ? (row[0] as int?) : null, + randomNumber: + fields.contains('random_number') ? (row[1] as int?) : null); return Optional.of(model); } diff --git a/packages/orm/angel_orm_test/pubspec.yaml b/packages/orm/angel_orm_test/pubspec.yaml index 5cfcc518..1ed9f2ce 100644 --- a/packages/orm/angel_orm_test/pubspec.yaml +++ b/packages/orm/angel_orm_test/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_orm_test -version: 3.0.4 +version: 3.0.5 description: Common tests for Angel3 ORM. Reference implmentation of the generated ORM files. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_test