diff --git a/packages/orm/angel_orm_generator/example/main.g.dart b/packages/orm/angel_orm_generator/example/main.g.dart index 086f4569..cc7f924f 100644 --- a/packages/orm/angel_orm_generator/example/main.g.dart +++ b/packages/orm/angel_orm_generator/example/main.g.dart @@ -11,13 +11,13 @@ class EmployeeMigration extends Migration { void up(Schema schema) { schema.create('employees', (table) { table.serial('id').primaryKey(); - table.varChar('error', length: 256); table.timeStamp('created_at'); table.timeStamp('updated_at'); - table.varChar('unique_id', length: 256).unique(); - table.varChar('first_name', length: 256); - table.varChar('last_name', length: 256); - table.declareColumn('salary', Column(type: ColumnType('decimal'))); + table.varChar('unique_id', length: 255).unique(); + table.varChar('first_name', length: 255); + table.varChar('last_name', length: 255); + table.declareColumn( + 'salary', Column(type: ColumnType('decimal'), length: 255)); }); } @@ -42,6 +42,8 @@ class EmployeeQuery extends Query { @override final EmployeeQueryValues values = EmployeeQueryValues(); + List _selectedFields = []; + EmployeeQueryWhere? _where; @override @@ -56,9 +58,8 @@ class EmployeeQuery extends Query { @override List get fields { - return const [ + const _fields = [ 'id', - 'error', 'created_at', 'updated_at', 'unique_id', @@ -66,6 +67,14 @@ class EmployeeQuery extends Query { 'last_name', 'salary' ]; + return _selectedFields.isEmpty + ? _fields + : _fields.where((field) => _selectedFields.contains(field)).toList(); + } + + EmployeeQuery select(List selectedFields) { + _selectedFields = selectedFields; + return this; } @override @@ -78,29 +87,38 @@ class EmployeeQuery extends Query { return EmployeeQueryWhere(this); } - static Employee? parseRow(List row) { - if (row.every((x) => x == null)) return null; + Optional parseRow(List row) { + if (row.every((x) => x == null)) { + return Optional.empty(); + } + var _index = 0; var model = Employee( - id: row[0].toString(), - createdAt: (row[2] as DateTime?), - updatedAt: (row[3] as DateTime?), - uniqueId: (row[4] as String?), - firstName: (row[5] as String?), - lastName: (row[6] as String?), - salary: double.tryParse(row[7].toString())); - return model; + id: fields.contains('id') ? row[_index++].toString() : null, + createdAt: + fields.contains('created_at') ? (row[_index++] as DateTime?) : null, + updatedAt: + fields.contains('updated_at') ? (row[_index++] as DateTime?) : null, + uniqueId: + fields.contains('unique_id') ? (row[_index++] as String?) : null, + firstName: + fields.contains('first_name') ? (row[_index++] as String?) : null, + lastName: + fields.contains('last_name') ? (row[_index++] as String?) : null, + salary: fields.contains('salary') + ? double.tryParse(row[_index++].toString()) + : null); + return Optional.of(model); } @override Optional deserialize(List row) { - return Optional.ofNullable(parseRow(row)); + return parseRow(row); } } class EmployeeQueryWhere extends QueryWhere { EmployeeQueryWhere(EmployeeQuery query) : id = NumericSqlExpressionBuilder(query, 'id'), - error = StringSqlExpressionBuilder(query, 'error'), createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), uniqueId = StringSqlExpressionBuilder(query, 'unique_id'), @@ -110,8 +128,6 @@ class EmployeeQueryWhere extends QueryWhere { final NumericSqlExpressionBuilder id; - final StringSqlExpressionBuilder error; - final DateTimeSqlExpressionBuilder createdAt; final DateTimeSqlExpressionBuilder updatedAt; @@ -126,16 +142,7 @@ class EmployeeQueryWhere extends QueryWhere { @override List get expressionBuilders { - return [ - id, - error, - createdAt, - updatedAt, - uniqueId, - firstName, - lastName, - salary - ]; + return [id, createdAt, updatedAt, uniqueId, firstName, lastName, salary]; } } @@ -150,11 +157,6 @@ class EmployeeQueryValues extends MapQueryValues { } set id(String? value) => values['id'] = value; - String? get error { - return (values['error'] as String?); - } - - set error(String? value) => values['error'] = value; DateTime? get createdAt { return (values['created_at'] as DateTime?); } @@ -236,7 +238,6 @@ class Employee extends _Employee { Employee copyWith( {String? id, - String? error, DateTime? createdAt, DateTime? updatedAt, String? uniqueId, @@ -310,7 +311,7 @@ class EmployeeSerializer extends Codec { EmployeeDecoder get decoder => const EmployeeDecoder(); static Employee fromMap(Map map) { return Employee( - id: map['id'] as String, + id: map['id'] as String?, createdAt: map['created_at'] != null ? (map['created_at'] is DateTime ? (map['created_at'] as DateTime) @@ -321,13 +322,16 @@ class EmployeeSerializer extends Codec { ? (map['updated_at'] as DateTime) : DateTime.parse(map['updated_at'].toString())) : null, - uniqueId: map['unique_id'] as String, - firstName: map['first_name'] as String, - lastName: map['last_name'] as String, - salary: map['salary'] as double); + uniqueId: map['unique_id'] as String?, + firstName: map['first_name'] as String?, + lastName: map['last_name'] as String?, + salary: map['salary'] as double?); } - static Map toMap(_Employee model) { + static Map toMap(_Employee? model) { + if (model == null) { + return {}; + } return { 'id': model.id, 'created_at': model.createdAt?.toIso8601String(), @@ -343,7 +347,6 @@ class EmployeeSerializer extends Codec { abstract class EmployeeFields { static const List allFields = [ id, - error, createdAt, updatedAt, uniqueId, @@ -354,8 +357,6 @@ abstract class EmployeeFields { static const String id = 'id'; - static const String error = 'error'; - static const String createdAt = 'created_at'; static const String updatedAt = 'updated_at'; 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 a0ca7d20..b0acc9cf 100644 --- a/packages/orm/angel_orm_generator/lib/src/orm_generator.dart +++ b/packages/orm/angel_orm_generator/lib/src/orm_generator.dart @@ -156,7 +156,49 @@ class OrmGenerator extends GeneratorForAnnotation { .map((f) => literalString(ctx.buildContext.resolveFieldName(f.name)!)) .toList(); - b.addExpression(literalConstList(names).returned); + b.addExpression(literalConstList(names).assignConst('_fields')); + b.addExpression(refer('_selectedFields') + .property('isEmpty') + .conditional( + refer('_fields'), + refer('_fields') + .property('where') + .call([ + CodeExpression( + Code('(field) => _selectedFields.contains(field)')) + ]) + .property('toList') + .call([]), + ) + .returned); + }); + })); + + // Add _selectedFields member + clazz.fields.add(Field((b) { + b + ..name = '_selectedFields' + ..type = TypeReference((t) => t + ..symbol = 'List' + ..types.add(TypeReference((b) => b..symbol = 'String'))) + ..assignment = Code('[]'); + })); + + // Add select(List fields) + clazz.methods.add(Method((m) { + m + ..name = 'select' + ..returns = refer('${rc.pascalCase}Query') + ..requiredParameters.add(Parameter((b) => b + ..name = 'selectedFields' + ..type = TypeReference((t) => t + ..symbol = 'List' + ..types.add(TypeReference((b) => b..symbol = 'String'))))) + ..body = Block((b) { + b.addExpression( + refer('_selectedFields').assign(refer('selectedFields')), + ); + b.addExpression(refer('this').returned); }); })); @@ -191,7 +233,6 @@ class OrmGenerator extends GeneratorForAnnotation { clazz.methods.add(Method((m) { m ..name = 'parseRow' - ..static = true ..returns = refer('Optional<${rc.pascalCase}>') ..requiredParameters.add(Parameter((b) => b ..name = 'row' @@ -207,7 +248,8 @@ class OrmGenerator extends GeneratorForAnnotation { type = refer('int'); } - var expr = (refer('row').index(literalNum(i++))); + literalNum(i++); + var expr = (refer('row').index(CodeExpression(Code('_index++')))); if (isSpecialId(ctx, field)) { expr = expr.property('toString').call([]); } else if (field is RelationFieldImpl) { @@ -227,12 +269,15 @@ class OrmGenerator extends GeneratorForAnnotation { } else { expr = expr.asA(type); } - + expr = refer('fields').property('contains').call([ + literalString(ctx.buildContext.resolveFieldName(field.name)!) + ]).conditional(expr, refer('null')); args[field.name] = expr; } b.statements.add(Code( 'if (row.every((x) => x == null)) { return Optional.empty(); }')); + b.addExpression(refer('0').assignVar('_index')); b.addExpression(ctx.buildContext.modelClassType .newInstance([], args).assignVar('model'));