Merge pull request #44 from dukefirehawk/fix-bug/pr42

Updated ORM
This commit is contained in:
Thomas Hii 2022-01-06 21:16:29 +08:00 committed by GitHub
commit 7f82d8b941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 479 additions and 161 deletions

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 4.2.0
* Added `select(List fields)`
## 4.1.3 ## 4.1.3
* Fixed issue #24, incorrect class casting * Fixed issue #24, incorrect class casting

View file

@ -248,8 +248,13 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
type = refer('int'); type = refer('int');
} }
literalNum(i++); // Reverted since as `continue` for `RelationshipFieldImp` will
var expr = (refer('row').index(CodeExpression(Code('_index++')))); // 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)) { if (isSpecialId(ctx, field)) {
expr = expr.property('toString').call([]); expr = expr.property('toString').call([]);
} else if (field is RelationFieldImpl) { } else if (field is RelationFieldImpl) {
@ -277,7 +282,7 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
b.statements.add(Code( b.statements.add(Code(
'if (row.every((x) => x == null)) { return Optional.empty(); }')); '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 b.addExpression(ctx.buildContext.modelClassType
.newInstance([], args).assignVar('model')); .newInstance([], args).assignVar('model'));
@ -310,6 +315,7 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
var parsed = refer( var parsed = refer(
'${foreign.buildContext.modelClassNameRecase.pascalCase}Query') '${foreign.buildContext.modelClassNameRecase.pascalCase}Query')
.newInstance([])
.property('parseRow') .property('parseRow')
.call([skipToList]); .call([skipToList]);

View file

@ -1,5 +1,5 @@
name: angel3_orm_generator name: angel3_orm_generator
version: 4.1.3 version: 4.2.0
description: Code generators for Angel3 ORM. Generates query builder classes. description: Code generators for Angel3 ORM. Generates query builder classes.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_generator repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_generator

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 3.0.5
* Updated test cases
## 3.0.4 ## 3.0.4
* Updated test cases * Updated test cases

View file

@ -68,6 +68,8 @@ class BookQuery extends Query<Book, BookQueryWhere> {
@override @override
final BookQueryValues values = BookQueryValues(); final BookQueryValues values = BookQueryValues();
List<String> _selectedFields = [];
BookQueryWhere? _where; BookQueryWhere? _where;
late AuthorQuery _author; late AuthorQuery _author;
@ -86,7 +88,7 @@ class BookQuery extends Query<Book, BookQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const [ const _fields = [
'id', 'id',
'created_at', 'created_at',
'updated_at', 'updated_at',
@ -94,6 +96,14 @@ class BookQuery extends Query<Book, BookQueryWhere> {
'partner_author_id', 'partner_author_id',
'name' 'name'
]; ];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
BookQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -106,23 +116,23 @@ class BookQuery extends Query<Book, BookQueryWhere> {
return BookQueryWhere(this); return BookQueryWhere(this);
} }
static Optional<Book> parseRow(List row) { Optional<Book> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Book( var model = Book(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
name: (row[5] as String?)); name: fields.contains('name') ? (row[5] as String?) : null);
if (row.length > 6) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(author: m); model = model.copyWith(author: m);
}); });
} }
if (row.length > 10) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(partnerAuthor: m); model = model.copyWith(partnerAuthor: m);
}); });
@ -232,6 +242,8 @@ class AuthorQuery extends Query<Author, AuthorQueryWhere> {
@override @override
final AuthorQueryValues values = AuthorQueryValues(); final AuthorQueryValues values = AuthorQueryValues();
List<String> _selectedFields = [];
AuthorQueryWhere? _where; AuthorQueryWhere? _where;
@override @override
@ -246,7 +258,15 @@ class AuthorQuery extends Query<Author, AuthorQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -259,15 +279,15 @@ class AuthorQuery extends Query<Author, AuthorQueryWhere> {
return AuthorQueryWhere(this); return AuthorQueryWhere(this);
} }
static Optional<Author> parseRow(List row) { Optional<Author> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Author( var model = Author(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
name: (row[3] as String?)); name: fields.contains('name') ? (row[3] as String?) : null);
return Optional.of(model); return Optional.of(model);
} }

View file

@ -40,6 +40,8 @@ class CarQuery extends Query<Car, CarQueryWhere> {
@override @override
final CarQueryValues values = CarQueryValues(); final CarQueryValues values = CarQueryValues();
List<String> _selectedFields = [];
CarQueryWhere? _where; CarQueryWhere? _where;
@override @override
@ -54,7 +56,7 @@ class CarQuery extends Query<Car, CarQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const [ const _fields = [
'id', 'id',
'created_at', 'created_at',
'updated_at', 'updated_at',
@ -63,6 +65,14 @@ class CarQuery extends Query<Car, CarQueryWhere> {
'family_friendly', 'family_friendly',
'recalled_at' 'recalled_at'
]; ];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
CarQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -75,18 +85,21 @@ class CarQuery extends Query<Car, CarQueryWhere> {
return CarQueryWhere(this); return CarQueryWhere(this);
} }
static Optional<Car> parseRow(List row) { Optional<Car> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Car( var model = Car(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
make: (row[3] as String?), make: fields.contains('make') ? (row[3] as String?) : null,
description: (row[4] as String?), description:
familyFriendly: (row[5] as bool?), fields.contains('description') ? (row[4] as String?) : null,
recalledAt: (row[6] as DateTime?)); familyFriendly:
fields.contains('family_friendly') ? (row[5] as bool?) : null,
recalledAt:
fields.contains('recalled_at') ? (row[6] as DateTime?) : null);
return Optional.of(model); return Optional.of(model);
} }

View file

@ -58,6 +58,8 @@ class NumbersQuery extends Query<Numbers, NumbersQueryWhere> {
@override @override
final NumbersQueryValues values = NumbersQueryValues(); final NumbersQueryValues values = NumbersQueryValues();
List<String> _selectedFields = [];
NumbersQueryWhere? _where; NumbersQueryWhere? _where;
@override @override
@ -72,7 +74,15 @@ class NumbersQuery extends Query<Numbers, NumbersQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -85,15 +95,15 @@ class NumbersQuery extends Query<Numbers, NumbersQueryWhere> {
return NumbersQueryWhere(this); return NumbersQueryWhere(this);
} }
static Optional<Numbers> parseRow(List row) { Optional<Numbers> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Numbers( var model = Numbers(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
two: (row[3] as int?)); two: fields.contains('two') ? (row[3] as int?) : null);
return Optional.of(model); return Optional.of(model);
} }
@ -163,6 +173,8 @@ class AlphabetQuery extends Query<Alphabet, AlphabetQueryWhere> {
@override @override
final AlphabetQueryValues values = AlphabetQueryValues(); final AlphabetQueryValues values = AlphabetQueryValues();
List<String> _selectedFields = [];
AlphabetQueryWhere? _where; AlphabetQueryWhere? _where;
late NumbersQuery _numbers; late NumbersQuery _numbers;
@ -179,7 +191,15 @@ class AlphabetQuery extends Query<Alphabet, AlphabetQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -192,17 +212,17 @@ class AlphabetQuery extends Query<Alphabet, AlphabetQueryWhere> {
return AlphabetQueryWhere(this); return AlphabetQueryWhere(this);
} }
static Optional<Alphabet> parseRow(List row) { Optional<Alphabet> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Alphabet( var model = Alphabet(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
value: (row[3] as String?)); value: fields.contains('value') ? (row[3] as String?) : null);
if (row.length > 5) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(numbers: m); model = model.copyWith(numbers: m);
}); });

View file

@ -75,6 +75,8 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
@override @override
final RoleQueryValues values = RoleQueryValues(); final RoleQueryValues values = RoleQueryValues();
List<String> _selectedFields = [];
RoleQueryWhere? _where; RoleQueryWhere? _where;
@override @override
@ -89,7 +91,15 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const ['role']; const _fields = ['role'];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
RoleQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -102,13 +112,14 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
return RoleQueryWhere(this); return RoleQueryWhere(this);
} }
static Optional<Role> parseRow(List row) { Optional<Role> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); 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) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(users: [m]); model = model.copyWith(users: [m]);
}); });
@ -228,6 +239,8 @@ class RoleUserQuery extends Query<RoleUser, RoleUserQueryWhere> {
@override @override
final RoleUserQueryValues values = RoleUserQueryValues(); final RoleUserQueryValues values = RoleUserQueryValues();
List<String> _selectedFields = [];
RoleUserQueryWhere? _where; RoleUserQueryWhere? _where;
late RoleQuery _role; late RoleQuery _role;
@ -246,7 +259,15 @@ class RoleUserQuery extends Query<RoleUser, RoleUserQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -259,19 +280,19 @@ class RoleUserQuery extends Query<RoleUser, RoleUserQueryWhere> {
return RoleUserQueryWhere(this); return RoleUserQueryWhere(this);
} }
static Optional<RoleUser> parseRow(List row) { Optional<RoleUser> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = RoleUser(); var model = RoleUser();
if (row.length > 2) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(role: m); model = model.copyWith(role: m);
}); });
} }
if (row.length > 3) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(user: m); model = model.copyWith(user: m);
}); });
@ -350,6 +371,8 @@ class UserQuery extends Query<User, UserQueryWhere> {
@override @override
final UserQueryValues values = UserQueryValues(); final UserQueryValues values = UserQueryValues();
List<String> _selectedFields = [];
UserQueryWhere? _where; UserQueryWhere? _where;
@override @override
@ -364,7 +387,15 @@ class UserQuery extends Query<User, UserQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -377,16 +408,16 @@ class UserQuery extends Query<User, UserQueryWhere> {
return UserQueryWhere(this); return UserQueryWhere(this);
} }
static Optional<User> parseRow(List row) { Optional<User> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = User( var model = User(
email: (row[0] as String?), email: fields.contains('email') ? (row[0] as String?) : null,
name: (row[1] as String?), name: fields.contains('name') ? (row[1] as String?) : null,
password: (row[2] as String?)); password: fields.contains('password') ? (row[2] as String?) : null);
if (row.length > 3) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(roles: [m]); model = model.copyWith(roles: [m]);
}); });

View file

@ -36,6 +36,8 @@ class FortuneQuery extends Query<Fortune, FortuneQueryWhere> {
@override @override
final FortuneQueryValues values = FortuneQueryValues(); final FortuneQueryValues values = FortuneQueryValues();
List<String> _selectedFields = [];
FortuneQueryWhere? _where; FortuneQueryWhere? _where;
@override @override
@ -50,7 +52,15 @@ class FortuneQuery extends Query<Fortune, FortuneQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const ['id', 'message']; const _fields = ['id', 'message'];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
FortuneQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -63,11 +73,13 @@ class FortuneQuery extends Query<Fortune, FortuneQueryWhere> {
return FortuneQueryWhere(this); return FortuneQueryWhere(this);
} }
static Optional<Fortune> parseRow(List row) { Optional<Fortune> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); 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); return Optional.of(model);
} }

View file

@ -38,6 +38,8 @@ class HasCarQuery extends Query<HasCar, HasCarQueryWhere> {
@override @override
final HasCarQueryValues values = HasCarQueryValues(); final HasCarQueryValues values = HasCarQueryValues();
List<String> _selectedFields = [];
HasCarQueryWhere? _where; HasCarQueryWhere? _where;
@override @override
@ -52,7 +54,15 @@ class HasCarQuery extends Query<HasCar, HasCarQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -65,15 +75,19 @@ class HasCarQuery extends Query<HasCar, HasCarQueryWhere> {
return HasCarQueryWhere(this); return HasCarQueryWhere(this);
} }
static Optional<HasCar> parseRow(List row) { Optional<HasCar> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = HasCar( var model = HasCar(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
type: row[3] == null ? null : CarType?.values[(row[3] as int)]); type: fields.contains('type')
? row[3] == null
? null
: CarType?.values[(row[3] as int)]
: null);
return Optional.of(model); return Optional.of(model);
} }

View file

@ -38,6 +38,8 @@ class HasMapQuery extends Query<HasMap, HasMapQueryWhere> {
@override @override
final HasMapQueryValues values = HasMapQueryValues(); final HasMapQueryValues values = HasMapQueryValues();
List<String> _selectedFields = [];
HasMapQueryWhere? _where; HasMapQueryWhere? _where;
@override @override
@ -52,7 +54,15 @@ class HasMapQuery extends Query<HasMap, HasMapQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const ['value', 'list']; const _fields = ['value', 'list'];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
HasMapQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -65,13 +75,15 @@ class HasMapQuery extends Query<HasMap, HasMapQueryWhere> {
return HasMapQueryWhere(this); return HasMapQueryWhere(this);
} }
static Optional<HasMap> parseRow(List row) { Optional<HasMap> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = HasMap( var model = HasMap(
value: (row[0] as Map<dynamic, dynamic>?), value: fields.contains('value')
list: (row[1] as List<dynamic>?)); ? (row[0] as Map<dynamic, dynamic>?)
: null,
list: fields.contains('list') ? (row[1] as List<dynamic>?) : null);
return Optional.of(model); return Optional.of(model);
} }

View file

@ -66,6 +66,8 @@ class LegQuery extends Query<Leg, LegQueryWhere> {
@override @override
final LegQueryValues values = LegQueryValues(); final LegQueryValues values = LegQueryValues();
List<String> _selectedFields = [];
LegQueryWhere? _where; LegQueryWhere? _where;
late FootQuery _foot; late FootQuery _foot;
@ -82,7 +84,15 @@ class LegQuery extends Query<Leg, LegQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -95,17 +105,17 @@ class LegQuery extends Query<Leg, LegQueryWhere> {
return LegQueryWhere(this); return LegQueryWhere(this);
} }
static Optional<Leg> parseRow(List row) { Optional<Leg> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Leg( var model = Leg(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
name: (row[3] as String?)); name: fields.contains('name') ? (row[3] as String?) : null);
if (row.length > 4) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(foot: m); model = model.copyWith(foot: m);
}); });
@ -187,6 +197,8 @@ class FootQuery extends Query<Foot, FootQueryWhere> {
@override @override
final FootQueryValues values = FootQueryValues(); final FootQueryValues values = FootQueryValues();
List<String> _selectedFields = [];
FootQueryWhere? _where; FootQueryWhere? _where;
@override @override
@ -201,7 +213,15 @@ class FootQuery extends Query<Foot, FootQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -214,16 +234,18 @@ class FootQuery extends Query<Foot, FootQueryWhere> {
return FootQueryWhere(this); return FootQueryWhere(this);
} }
static Optional<Foot> parseRow(List row) { Optional<Foot> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Foot( var model = Foot(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
legId: (row[3] as int?), legId: fields.contains('leg_id') ? (row[3] as int?) : null,
nToes: double.tryParse(row[4].toString())); nToes: fields.contains('n_toes')
? double.tryParse(row[4].toString())
: null);
return Optional.of(model); return Optional.of(model);
} }

View file

@ -62,6 +62,8 @@ class OrderQuery extends Query<Order, OrderQueryWhere> {
@override @override
final OrderQueryValues values = OrderQueryValues(); final OrderQueryValues values = OrderQueryValues();
List<String> _selectedFields = [];
OrderQueryWhere? _where; OrderQueryWhere? _where;
late CustomerQuery _customer; late CustomerQuery _customer;
@ -78,7 +80,7 @@ class OrderQuery extends Query<Order, OrderQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const [ const _fields = [
'id', 'id',
'created_at', 'created_at',
'updated_at', 'updated_at',
@ -87,6 +89,14 @@ class OrderQuery extends Query<Order, OrderQueryWhere> {
'order_date', 'order_date',
'shipper_id' 'shipper_id'
]; ];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
OrderQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -99,19 +109,19 @@ class OrderQuery extends Query<Order, OrderQueryWhere> {
return OrderQueryWhere(this); return OrderQueryWhere(this);
} }
static Optional<Order> parseRow(List row) { Optional<Order> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Order( var model = Order(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
employeeId: (row[4] as int?), employeeId: fields.contains('employee_id') ? (row[4] as int?) : null,
orderDate: (row[5] as DateTime?), orderDate: fields.contains('order_date') ? (row[5] as DateTime?) : null,
shipperId: (row[6] as int?)); shipperId: fields.contains('shipper_id') ? (row[6] as int?) : null);
if (row.length > 7) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(customer: m); model = model.copyWith(customer: m);
}); });
@ -231,6 +241,8 @@ class CustomerQuery extends Query<Customer, CustomerQueryWhere> {
@override @override
final CustomerQueryValues values = CustomerQueryValues(); final CustomerQueryValues values = CustomerQueryValues();
List<String> _selectedFields = [];
CustomerQueryWhere? _where; CustomerQueryWhere? _where;
@override @override
@ -245,7 +257,15 @@ class CustomerQuery extends Query<Customer, CustomerQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -258,14 +278,15 @@ class CustomerQuery extends Query<Customer, CustomerQueryWhere> {
return CustomerQueryWhere(this); return CustomerQueryWhere(this);
} }
static Optional<Customer> parseRow(List row) { Optional<Customer> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Customer( var model = Customer(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?)); updatedAt:
fields.contains('updated_at') ? (row[2] as DateTime?) : null);
return Optional.of(model); return Optional.of(model);
} }

View file

@ -65,6 +65,8 @@ class TreeQuery extends Query<Tree, TreeQueryWhere> {
@override @override
final TreeQueryValues values = TreeQueryValues(); final TreeQueryValues values = TreeQueryValues();
List<String> _selectedFields = [];
TreeQueryWhere? _where; TreeQueryWhere? _where;
late FruitQuery _fruits; late FruitQuery _fruits;
@ -81,7 +83,15 @@ class TreeQuery extends Query<Tree, TreeQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -94,17 +104,17 @@ class TreeQuery extends Query<Tree, TreeQueryWhere> {
return TreeQueryWhere(this); return TreeQueryWhere(this);
} }
static Optional<Tree> parseRow(List row) { Optional<Tree> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Tree( var model = Tree(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
rings: (row[3] as int?)); rings: fields.contains('rings') ? (row[3] as int?) : null);
if (row.length > 4) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(fruits: [m]); model = model.copyWith(fruits: [m]);
}); });
@ -240,6 +250,8 @@ class FruitQuery extends Query<Fruit, FruitQueryWhere> {
@override @override
final FruitQueryValues values = FruitQueryValues(); final FruitQueryValues values = FruitQueryValues();
List<String> _selectedFields = [];
FruitQueryWhere? _where; FruitQueryWhere? _where;
@override @override
@ -254,7 +266,21 @@ class FruitQuery extends Query<Fruit, FruitQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -267,16 +293,17 @@ class FruitQuery extends Query<Fruit, FruitQueryWhere> {
return FruitQueryWhere(this); return FruitQueryWhere(this);
} }
static Optional<Fruit> parseRow(List row) { Optional<Fruit> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Fruit( var model = Fruit(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
treeId: (row[3] as int?), treeId: fields.contains('tree_id') ? (row[3] as int?) : null,
commonName: (row[4] as String?)); commonName:
fields.contains('common_name') ? (row[4] as String?) : null);
return Optional.of(model); return Optional.of(model);
} }

View file

@ -116,6 +116,8 @@ class UnorthodoxQuery extends Query<Unorthodox, UnorthodoxQueryWhere> {
@override @override
final UnorthodoxQueryValues values = UnorthodoxQueryValues(); final UnorthodoxQueryValues values = UnorthodoxQueryValues();
List<String> _selectedFields = [];
UnorthodoxQueryWhere? _where; UnorthodoxQueryWhere? _where;
@override @override
@ -130,7 +132,15 @@ class UnorthodoxQuery extends Query<Unorthodox, UnorthodoxQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const ['name']; const _fields = ['name'];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
UnorthodoxQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -143,11 +153,12 @@ class UnorthodoxQuery extends Query<Unorthodox, UnorthodoxQueryWhere> {
return UnorthodoxQueryWhere(this); return UnorthodoxQueryWhere(this);
} }
static Optional<Unorthodox> parseRow(List row) { Optional<Unorthodox> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); 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); return Optional.of(model);
} }
@ -221,6 +232,8 @@ class WeirdJoinQuery extends Query<WeirdJoin, WeirdJoinQueryWhere> {
@override @override
final WeirdJoinQueryValues values = WeirdJoinQueryValues(); final WeirdJoinQueryValues values = WeirdJoinQueryValues();
List<String> _selectedFields = [];
WeirdJoinQueryWhere? _where; WeirdJoinQueryWhere? _where;
late UnorthodoxQuery _unorthodox; late UnorthodoxQuery _unorthodox;
@ -241,7 +254,15 @@ class WeirdJoinQuery extends Query<WeirdJoin, WeirdJoinQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -254,31 +275,31 @@ class WeirdJoinQuery extends Query<WeirdJoin, WeirdJoinQueryWhere> {
return WeirdJoinQueryWhere(this); return WeirdJoinQueryWhere(this);
} }
static Optional<WeirdJoin> parseRow(List row) { Optional<WeirdJoin> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); 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) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(unorthodox: m); model = model.copyWith(unorthodox: m);
}); });
} }
if (row.length > 3) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(song: m); model = model.copyWith(song: m);
}); });
} }
if (row.length > 8) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(numbas: [m]); model = model.copyWith(numbas: [m]);
}); });
} }
if (row.length > 10) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(foos: [m]); model = model.copyWith(foos: [m]);
}); });
@ -416,6 +437,8 @@ class SongQuery extends Query<Song, SongQueryWhere> {
@override @override
final SongQueryValues values = SongQueryValues(); final SongQueryValues values = SongQueryValues();
List<String> _selectedFields = [];
SongQueryWhere? _where; SongQueryWhere? _where;
@override @override
@ -430,7 +453,21 @@ class SongQuery extends Query<Song, SongQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -443,16 +480,16 @@ class SongQuery extends Query<Song, SongQueryWhere> {
return SongQueryWhere(this); return SongQueryWhere(this);
} }
static Optional<Song> parseRow(List row) { Optional<Song> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Song( var model = Song(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
weirdJoinId: (row[3] as int?), weirdJoinId: fields.contains('weird_join_id') ? (row[3] as int?) : null,
title: (row[4] as String?)); title: fields.contains('title') ? (row[4] as String?) : null);
return Optional.of(model); return Optional.of(model);
} }
@ -535,6 +572,8 @@ class NumbaQuery extends Query<Numba, NumbaQueryWhere> {
@override @override
final NumbaQueryValues values = NumbaQueryValues(); final NumbaQueryValues values = NumbaQueryValues();
List<String> _selectedFields = [];
NumbaQueryWhere? _where; NumbaQueryWhere? _where;
@override @override
@ -549,7 +588,15 @@ class NumbaQuery extends Query<Numba, NumbaQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const ['i', 'parent']; const _fields = ['i', 'parent'];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
NumbaQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -562,11 +609,13 @@ class NumbaQuery extends Query<Numba, NumbaQueryWhere> {
return NumbaQueryWhere(this); return NumbaQueryWhere(this);
} }
static Optional<Numba> parseRow(List row) { Optional<Numba> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); 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); return Optional.of(model);
} }
@ -629,6 +678,8 @@ class FooQuery extends Query<Foo, FooQueryWhere> {
@override @override
final FooQueryValues values = FooQueryValues(); final FooQueryValues values = FooQueryValues();
List<String> _selectedFields = [];
FooQueryWhere? _where; FooQueryWhere? _where;
@override @override
@ -643,7 +694,15 @@ class FooQuery extends Query<Foo, FooQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const ['bar']; const _fields = ['bar'];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
FooQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -656,13 +715,13 @@ class FooQuery extends Query<Foo, FooQueryWhere> {
return FooQueryWhere(this); return FooQueryWhere(this);
} }
static Optional<Foo> parseRow(List row) { Optional<Foo> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); 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) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(weirdJoins: [m]); model = model.copyWith(weirdJoins: [m]);
}); });
@ -784,6 +843,8 @@ class FooPivotQuery extends Query<FooPivot, FooPivotQueryWhere> {
@override @override
final FooPivotQueryValues values = FooPivotQueryValues(); final FooPivotQueryValues values = FooPivotQueryValues();
List<String> _selectedFields = [];
FooPivotQueryWhere? _where; FooPivotQueryWhere? _where;
late WeirdJoinQuery _weirdJoin; late WeirdJoinQuery _weirdJoin;
@ -802,7 +863,15 @@ class FooPivotQuery extends Query<FooPivot, FooPivotQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -815,19 +884,19 @@ class FooPivotQuery extends Query<FooPivot, FooPivotQueryWhere> {
return FooPivotQueryWhere(this); return FooPivotQueryWhere(this);
} }
static Optional<FooPivot> parseRow(List row) { Optional<FooPivot> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = FooPivot(); var model = FooPivot();
if (row.length > 2) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(weirdJoin: m); model = model.copyWith(weirdJoin: m);
}); });
} }
if (row.length > 4) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(foo: m); model = model.copyWith(foo: m);
}); });

View file

@ -77,6 +77,8 @@ class UserQuery extends Query<User, UserQueryWhere> {
@override @override
final UserQueryValues values = UserQueryValues(); final UserQueryValues values = UserQueryValues();
List<String> _selectedFields = [];
UserQueryWhere? _where; UserQueryWhere? _where;
@override @override
@ -91,7 +93,7 @@ class UserQuery extends Query<User, UserQueryWhere> {
@override @override
List<String> get fields { List<String> get fields {
return const [ const _fields = [
'id', 'id',
'created_at', 'created_at',
'updated_at', 'updated_at',
@ -99,6 +101,14 @@ class UserQuery extends Query<User, UserQueryWhere> {
'password', 'password',
'email' 'email'
]; ];
return _selectedFields.isEmpty
? _fields
: _fields.where((field) => _selectedFields.contains(field)).toList();
}
UserQuery select(List<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -111,19 +121,19 @@ class UserQuery extends Query<User, UserQueryWhere> {
return UserQueryWhere(this); return UserQueryWhere(this);
} }
static Optional<User> parseRow(List row) { Optional<User> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = User( var model = User(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
username: (row[3] as String?), username: fields.contains('username') ? (row[3] as String?) : null,
password: (row[4] as String?), password: fields.contains('password') ? (row[4] as String?) : null,
email: (row[5] as String?)); email: fields.contains('email') ? (row[5] as String?) : null);
if (row.length > 6) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(roles: [m]); model = model.copyWith(roles: [m]);
}); });
@ -295,6 +305,8 @@ class RoleUserQuery extends Query<RoleUser, RoleUserQueryWhere> {
@override @override
final RoleUserQueryValues values = RoleUserQueryValues(); final RoleUserQueryValues values = RoleUserQueryValues();
List<String> _selectedFields = [];
RoleUserQueryWhere? _where; RoleUserQueryWhere? _where;
late RoleQuery _role; late RoleQuery _role;
@ -313,7 +325,15 @@ class RoleUserQuery extends Query<RoleUser, RoleUserQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -326,19 +346,19 @@ class RoleUserQuery extends Query<RoleUser, RoleUserQueryWhere> {
return RoleUserQueryWhere(this); return RoleUserQueryWhere(this);
} }
static Optional<RoleUser> parseRow(List row) { Optional<RoleUser> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = RoleUser(); var model = RoleUser();
if (row.length > 2) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(role: m); model = model.copyWith(role: m);
}); });
} }
if (row.length > 6) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(user: m); model = model.copyWith(user: m);
}); });
@ -424,6 +444,8 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
@override @override
final RoleQueryValues values = RoleQueryValues(); final RoleQueryValues values = RoleQueryValues();
List<String> _selectedFields = [];
RoleQueryWhere? _where; RoleQueryWhere? _where;
@override @override
@ -438,7 +460,15 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -451,17 +481,17 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
return RoleQueryWhere(this); return RoleQueryWhere(this);
} }
static Optional<Role> parseRow(List row) { Optional<Role> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); return Optional.empty();
} }
var model = Role( var model = Role(
id: row[0].toString(), id: fields.contains('id') ? row[0].toString() : null,
createdAt: (row[1] as DateTime?), createdAt: fields.contains('created_at') ? (row[1] as DateTime?) : null,
updatedAt: (row[2] as DateTime?), updatedAt: fields.contains('updated_at') ? (row[2] as DateTime?) : null,
name: (row[3] as String?)); name: fields.contains('name') ? (row[3] as String?) : null);
if (row.length > 4) { 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) { modelOpt.ifPresent((m) {
model = model.copyWith(users: [m]); model = model.copyWith(users: [m]);
}); });

View file

@ -35,6 +35,8 @@ class WorldQuery extends Query<World, WorldQueryWhere> {
@override @override
final WorldQueryValues values = WorldQueryValues(); final WorldQueryValues values = WorldQueryValues();
List<String> _selectedFields = [];
WorldQueryWhere? _where; WorldQueryWhere? _where;
@override @override
@ -49,7 +51,15 @@ class WorldQuery extends Query<World, WorldQueryWhere> {
@override @override
List<String> get fields { List<String> 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<String> selectedFields) {
_selectedFields = selectedFields;
return this;
} }
@override @override
@ -62,11 +72,14 @@ class WorldQuery extends Query<World, WorldQueryWhere> {
return WorldQueryWhere(this); return WorldQueryWhere(this);
} }
static Optional<World> parseRow(List row) { Optional<World> parseRow(List row) {
if (row.every((x) => x == null)) { if (row.every((x) => x == null)) {
return Optional.empty(); 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); return Optional.of(model);
} }

View file

@ -1,5 +1,5 @@
name: angel3_orm_test 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. description: Common tests for Angel3 ORM. Reference implmentation of the generated ORM files.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_test repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_test