diff --git a/angel_orm_generator/lib/src/orm_generator.dart b/angel_orm_generator/lib/src/orm_generator.dart index a0ba59ba..f72e1191 100644 --- a/angel_orm_generator/lib/src/orm_generator.dart +++ b/angel_orm_generator/lib/src/orm_generator.dart @@ -116,7 +116,8 @@ class OrmGenerator extends GeneratorForAnnotation { // Next, add method stubs. // * getAll // * getById - // * update + // * updateX() + // * createX() // * query() // getAll @@ -135,13 +136,25 @@ class OrmGenerator extends GeneratorForAnnotation { m ..name = 'getById' ..returns = futureOf(ctx.buildContext.modelClassName) - ..requiredParameters.add(new Parameter((b) => b..name = 'id')); + ..requiredParameters.add(new Parameter((b) => b + ..name = 'id' + ..type = refer('String'))); })); - // update + // createX() clazz.methods.add(new Method((m) { m - ..name = 'update' + ..name = 'create${ctx.buildContext.modelClassName}' + ..returns = futureOf(ctx.buildContext.modelClassName) + ..requiredParameters.add(new Parameter((b) => b + ..name = 'model' + ..type = ctx.buildContext.modelClassType)); + })); + + // updateX() + clazz.methods.add(new Method((m) { + m + ..name = 'update${ctx.buildContext.modelClassName}' ..returns = futureOf(ctx.buildContext.modelClassName) ..requiredParameters.add(new Parameter((b) => b ..name = 'model' diff --git a/angel_orm_generator/lib/src/postgresql_orm_generator.dart b/angel_orm_generator/lib/src/postgresql_orm_generator.dart index c355f287..29204b42 100644 --- a/angel_orm_generator/lib/src/postgresql_orm_generator.dart +++ b/angel_orm_generator/lib/src/postgresql_orm_generator.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:analyzer/dart/element/element.dart'; import 'package:angel_orm/angel_orm.dart'; import 'package:angel_serialize_generator/angel_serialize_generator.dart'; +import 'package:angel_serialize_generator/build_context.dart'; import 'package:build/build.dart'; import 'package:code_builder/code_builder.dart' hide LibraryBuilder; import 'package:path/path.dart' as p; @@ -69,7 +70,10 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation { ..toThis = true)); })) ..methods.add(buildParseRowMethod(ctx)) - ..methods.add(buildGetById(ctx)); + ..methods.add(buildGetById(ctx)) + ..methods.add(buildGetAll(ctx)) + ..methods.add(buildCreate(ctx)) + ..methods.add(buildUpdate(ctx)); }); } @@ -99,6 +103,83 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation { }); } + String buildFieldString(OrmBuildContext ctx) { + var queryString = new StringBuffer(); + int i = 0; + + for (var field in ctx.buildContext.fields) { + if (i++ > 0) queryString.write(','); + queryString.write(' ' + ctx.buildContext.resolveFieldName(field.name)); + } + return queryString.toString(); + } + + String buildQuotedFieldString(OrmBuildContext ctx) { + var queryString = new StringBuffer(); + int i = 0; + + for (var field in ctx.buildContext.fields) { + if (i++ > 0) queryString.write(','); + queryString + .write(' "' + ctx.buildContext.resolveFieldName(field.name) + '"'); + } + return queryString.toString(); + } + + String buildInsertionValueString(OrmBuildContext ctx) { + var buf = new StringBuffer('('); + + int i = 0; + + for (var field in ctx.buildContext.fields) { + if (i++ > 0) buf.write(','); + if (dateTimeTypeChecker.isAssignableFromType(field.type)) + buf.write( + 'CAST (@${field.name} AS ${ctx.columns[field.name].type.name})'); + else + buf.write('@${field.name}'); + } + + return buf.toString() + ')'; + } + + Expression buildSubstitutionValues(OrmBuildContext ctx) { + var values = {}; + + for (var field in ctx.buildContext.fields) { + values[literalString(field.name)] = refer('model').property(field.name); + } + + return literalMap(values); + } + + void applyQuery( + BlockBuilder b, String queryString, Expression substitutionValues) { + b.statements.add(refer('connection') + .property('query') + .call( + [literalString(queryString)], + substitutionValues == null + ? {} + : {'substitutionValues': substitutionValues}) + .awaited + .assignVar('r') + .statement); + } + + void applyQueryAndReturnOne( + BlockBuilder b, String queryString, Expression substitutionValues) { + applyQuery(b, queryString, substitutionValues); + b.addExpression( + (refer('parseRow').call([refer('r').property('first')])).returned); + } + + void applyQueryAndReturnList( + BlockBuilder b, String queryString, Expression substitutionValues) { + applyQuery(b, queryString, substitutionValues); + b.statements.add(new Code('return r.map(parseRow).toList();')); + } + Method buildGetById(OrmBuildContext ctx) { /* @override @@ -112,32 +193,111 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation { ..name = 'getById' ..annotations.add(refer('override')) ..modifier = MethodModifier.async + ..requiredParameters.add(new Parameter((b) => b + ..name = 'id' + ..type = refer('String'))) ..returns = new TypeReference((b) => b ..symbol = 'Future' ..types.add(ctx.buildContext.modelClassType)) ..body = new Block((b) { - var queryString = new StringBuffer('SELECT'); - int i = 0; + var fields = buildFieldString(ctx); + var queryString = + 'SELECT $fields FROM "${ctx.tableName}" WHERE id = @id LIMIT 1;'; + applyQueryAndReturnOne( + b, + queryString, + literalMap({ + 'id': refer('int').property('parse').call([refer('id')]) + })); + }); + }); + } - for (var field in ctx.buildContext.fields) { - if (i > 0) queryString.write(', '); - queryString.write(ctx.buildContext.resolveFieldName(field.name)); + Method buildGetAll(OrmBuildContext ctx) { + /* + @override + Future> getAll() async { + var r = await connection + .query('SELECT id, name, created_at, updated_at FROM "authors";'); + return r.map(parseRow).toList(); + } + */ + return new Method((method) { + method + ..name = 'getAll' + ..modifier = MethodModifier.async + ..returns = refer('Future>') + ..annotations.add(refer('override')) + ..body = new Block((block) { + var fields = buildFieldString(ctx); + var queryString = 'SELECT $fields FROM "${ctx.tableName}";'; + applyQueryAndReturnList(block, queryString, null); + }); + }); + } + + Method buildCreate(OrmBuildContext ctx) { + /* + @override + Future createAuthor(Author model) async { + // ... + } + */ + return new Method((method) { + method + ..name = 'create${ctx.buildContext.modelClassName}' + ..modifier = MethodModifier.async + ..annotations.add(refer('override')) + ..returns = refer('Future<${ctx.buildContext.modelClassName}>') + ..requiredParameters.add(new Parameter((b) => b + ..name = 'model' + ..type = ctx.buildContext.modelClassType)) + ..body = new Block((block) { + if (ctx.buildContext.autoIdAndDateFields != false) { + // If we are auto-managing created+updated at, do so now + block.statements.add(new Code( + 'model = model.copyWith(createdAt: new DateTime.now(), updatedAt: new DateTime.now());')); } - queryString.write(' FROM "${ctx.tableName}" id = @id;'); - b.statements.add(refer('connection') - .property('query') - .call([ - literalString(queryString.toString()) - ], { - 'substitutionValues': literalMap({'id': refer('id')}) - }) - .awaited - .assignVar('r') - .statement); - b.addExpression( - (refer('parseRow').call([refer('r').property('first')])) - .returned); + var fields = buildQuotedFieldString(ctx); + var fieldSet = buildInsertionValueString(ctx); + var queryString = + 'INSERT INTO "${ctx.tableName}" ($fields) VALUES $fieldSet RETURNING $fields;'; + applyQueryAndReturnOne( + block, queryString, buildSubstitutionValues(ctx)); + }); + }); + } + + Method buildUpdate(OrmBuildContext ctx) { + /* + @override + Future updateAuthor(Author model) async { + // ... + } + */ + return new Method((method) { + method + ..name = 'update${ctx.buildContext.modelClassName}' + ..modifier = MethodModifier.async + ..annotations.add(refer('override')) + ..returns = refer('Future<${ctx.buildContext.modelClassName}>') + ..requiredParameters.add(new Parameter((b) => b + ..name = 'model' + ..type = ctx.buildContext.modelClassType)) + ..body = new Block((block) { + if (ctx.buildContext.autoIdAndDateFields != false) { + // If we are auto-managing created+updated at, do so now + block.statements.add(new Code( + 'model = model.copyWith(updatedAt: new DateTime.now());')); + } + + var fields = buildQuotedFieldString(ctx); + var fieldSet = buildInsertionValueString(ctx); + var queryString = + 'UPDATE "${ctx.tableName}" SET ($fields) = $fieldSet RETURNING $fields;'; + applyQueryAndReturnOne( + block, queryString, buildSubstitutionValues(ctx)); }); }); } diff --git a/angel_orm_generator/test/models/author.orm.g.dart b/angel_orm_generator/test/models/author.orm.g.dart index ce84f481..e7c30d86 100644 --- a/angel_orm_generator/test/models/author.orm.g.dart +++ b/angel_orm_generator/test/models/author.orm.g.dart @@ -14,8 +14,9 @@ abstract class AuthorOrm { _PostgreSqlAuthorOrmImpl; Future> getAll(); - Future getById(id); - Future update(Author model); + Future getById(String id); + Future createAuthor(Author model); + Future updateAuthor(Author model); AuthorQuery query(); } diff --git a/angel_orm_generator/test/models/author.postgresql.orm.g.dart b/angel_orm_generator/test/models/author.postgresql.orm.g.dart index 6bdf8e2d..e11d30ec 100644 --- a/angel_orm_generator/test/models/author.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/author.postgresql.orm.g.dart @@ -20,10 +20,46 @@ class _PostgreSqlAuthorOrmImpl implements AuthorOrm { } @override - Future getById(id) async { + Future getById(String id) async { var r = await connection.query( - 'SELECTidnamecreated_atupdated_at FROM "authors" id = @id;', - substitutionValues: {'id': id}); - parseRow(r.first); + 'SELECT id, name, created_at, updated_at FROM "authors" WHERE id = @id LIMIT 1;', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + + @override + Future> getAll() async { + var r = await connection + .query('SELECT id, name, created_at, updated_at FROM "authors";'); + return r.map(parseRow).toList(); + } + + @override + Future createAuthor(Author model) async { + model = model.copyWith( + createdAt: new DateTime.now(), updatedAt: new DateTime.now()); + var r = await connection.query( + 'INSERT INTO "authors" ( "id", "name", "created_at", "updated_at") VALUES (@id,@name,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "name", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'name': model.name, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); + } + + @override + Future updateAuthor(Author model) async { + model = model.copyWith(updatedAt: new DateTime.now()); + var r = await connection.query( + 'UPDATE "authors" SET ( "id", "name", "created_at", "updated_at") = (@id,@name,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "name", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'name': model.name, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); } } diff --git a/angel_orm_generator/test/models/car.orm.g.dart b/angel_orm_generator/test/models/car.orm.g.dart index 04d65f2a..8e7a44b1 100644 --- a/angel_orm_generator/test/models/car.orm.g.dart +++ b/angel_orm_generator/test/models/car.orm.g.dart @@ -14,8 +14,9 @@ abstract class CarOrm { _PostgreSqlCarOrmImpl; Future> getAll(); - Future getById(id); - Future update(Car model); + Future getById(String id); + Future createCar(Car model); + Future updateCar(Car model); CarQuery query(); } diff --git a/angel_orm_generator/test/models/car.postgresql.orm.g.dart b/angel_orm_generator/test/models/car.postgresql.orm.g.dart index fc972b08..d48c2207 100644 --- a/angel_orm_generator/test/models/car.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/car.postgresql.orm.g.dart @@ -23,10 +23,52 @@ class _PostgreSqlCarOrmImpl implements CarOrm { } @override - Future getById() async { + Future getById(String id) async { var r = await connection.query( - 'SELECTidmakedescriptionfamily_friendlyrecalled_atcreated_atupdated_at FROM "cars" id = @id;', - substitutionValues: {'id': id}); - parseRow(r.first); + 'SELECT id, make, description, family_friendly, recalled_at, created_at, updated_at FROM "cars" WHERE id = @id LIMIT 1;', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + + @override + Future> getAll() async { + var r = await connection.query( + 'SELECT id, make, description, family_friendly, recalled_at, created_at, updated_at FROM "cars";'); + return r.map(parseRow).toList(); + } + + @override + Future createCar(Car model) async { + model = model.copyWith( + createdAt: new DateTime.now(), updatedAt: new DateTime.now()); + var r = await connection.query( + 'INSERT INTO "cars" ( "id", "make", "description", "family_friendly", "recalled_at", "created_at", "updated_at") VALUES (@id,@make,@description,@familyFriendly,CAST (@recalledAt AS timestamp),CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "make", "description", "family_friendly", "recalled_at", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'make': model.make, + 'description': model.description, + 'familyFriendly': model.familyFriendly, + 'recalledAt': model.recalledAt, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); + } + + @override + Future updateCar(Car model) async { + model = model.copyWith(updatedAt: new DateTime.now()); + var r = await connection.query( + 'UPDATE "cars" SET ( "id", "make", "description", "family_friendly", "recalled_at", "created_at", "updated_at") = (@id,@make,@description,@familyFriendly,CAST (@recalledAt AS timestamp),CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "make", "description", "family_friendly", "recalled_at", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'make': model.make, + 'description': model.description, + 'familyFriendly': model.familyFriendly, + 'recalledAt': model.recalledAt, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); } } diff --git a/angel_orm_generator/test/models/customer.orm.g.dart b/angel_orm_generator/test/models/customer.orm.g.dart index 920ac895..3afef6b8 100644 --- a/angel_orm_generator/test/models/customer.orm.g.dart +++ b/angel_orm_generator/test/models/customer.orm.g.dart @@ -14,8 +14,9 @@ abstract class CustomerOrm { _PostgreSqlCustomerOrmImpl; Future> getAll(); - Future getById(id); - Future update(Customer model); + Future getById(String id); + Future createCustomer(Customer model); + Future updateCustomer(Customer model); CustomerQuery query(); } diff --git a/angel_orm_generator/test/models/customer.postgresql.orm.g.dart b/angel_orm_generator/test/models/customer.postgresql.orm.g.dart index 054fff06..2ca1fe76 100644 --- a/angel_orm_generator/test/models/customer.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/customer.postgresql.orm.g.dart @@ -19,10 +19,44 @@ class _PostgreSqlCustomerOrmImpl implements CustomerOrm { } @override - Future getById() async { + Future getById(String id) async { var r = await connection.query( - 'SELECTidcreated_atupdated_at FROM "customers" id = @id;', - substitutionValues: {'id': id}); - parseRow(r.first); + 'SELECT id, created_at, updated_at FROM "customers" WHERE id = @id LIMIT 1;', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + + @override + Future> getAll() async { + var r = await connection + .query('SELECT id, created_at, updated_at FROM "customers";'); + return r.map(parseRow).toList(); + } + + @override + Future createCustomer(Customer model) async { + model = model.copyWith( + createdAt: new DateTime.now(), updatedAt: new DateTime.now()); + var r = await connection.query( + 'INSERT INTO "customers" ( "id", "created_at", "updated_at") VALUES (@id,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); + } + + @override + Future updateCustomer(Customer model) async { + model = model.copyWith(updatedAt: new DateTime.now()); + var r = await connection.query( + 'UPDATE "customers" SET ( "id", "created_at", "updated_at") = (@id,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); } } diff --git a/angel_orm_generator/test/models/foot.orm.g.dart b/angel_orm_generator/test/models/foot.orm.g.dart index 5db6a72f..99295919 100644 --- a/angel_orm_generator/test/models/foot.orm.g.dart +++ b/angel_orm_generator/test/models/foot.orm.g.dart @@ -14,8 +14,9 @@ abstract class FootOrm { _PostgreSqlFootOrmImpl; Future> getAll(); - Future getById(id); - Future update(Foot model); + Future getById(String id); + Future createFoot(Foot model); + Future updateFoot(Foot model); FootQuery query(); } diff --git a/angel_orm_generator/test/models/foot.postgresql.orm.g.dart b/angel_orm_generator/test/models/foot.postgresql.orm.g.dart index 0e498256..7442a506 100644 --- a/angel_orm_generator/test/models/foot.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/foot.postgresql.orm.g.dart @@ -21,10 +21,48 @@ class _PostgreSqlFootOrmImpl implements FootOrm { } @override - Future getById() async { + Future getById(String id) async { var r = await connection.query( - 'SELECTidleg_idn_toescreated_atupdated_at FROM "foots" id = @id;', - substitutionValues: {'id': id}); - parseRow(r.first); + 'SELECT id, leg_id, n_toes, created_at, updated_at FROM "foots" WHERE id = @id LIMIT 1;', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + + @override + Future> getAll() async { + var r = await connection.query( + 'SELECT id, leg_id, n_toes, created_at, updated_at FROM "foots";'); + return r.map(parseRow).toList(); + } + + @override + Future createFoot(Foot model) async { + model = model.copyWith( + createdAt: new DateTime.now(), updatedAt: new DateTime.now()); + var r = await connection.query( + 'INSERT INTO "foots" ( "id", "leg_id", "n_toes", "created_at", "updated_at") VALUES (@id,@legId,@nToes,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "leg_id", "n_toes", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'legId': model.legId, + 'nToes': model.nToes, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); + } + + @override + Future updateFoot(Foot model) async { + model = model.copyWith(updatedAt: new DateTime.now()); + var r = await connection.query( + 'UPDATE "foots" SET ( "id", "leg_id", "n_toes", "created_at", "updated_at") = (@id,@legId,@nToes,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "leg_id", "n_toes", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'legId': model.legId, + 'nToes': model.nToes, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); } } diff --git a/angel_orm_generator/test/models/fruit.orm.g.dart b/angel_orm_generator/test/models/fruit.orm.g.dart index a94c0f08..b4e17442 100644 --- a/angel_orm_generator/test/models/fruit.orm.g.dart +++ b/angel_orm_generator/test/models/fruit.orm.g.dart @@ -14,8 +14,9 @@ abstract class FruitOrm { _PostgreSqlFruitOrmImpl; Future> getAll(); - Future getById(id); - Future update(Fruit model); + Future getById(String id); + Future createFruit(Fruit model); + Future updateFruit(Fruit model); FruitQuery query(); } diff --git a/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart b/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart index eef902b0..54b91920 100644 --- a/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart @@ -21,10 +21,48 @@ class _PostgreSqlFruitOrmImpl implements FruitOrm { } @override - Future getById() async { + Future getById(String id) async { var r = await connection.query( - 'SELECTidtree_idcommon_namecreated_atupdated_at FROM "fruits" id = @id;', - substitutionValues: {'id': id}); - parseRow(r.first); + 'SELECT id, tree_id, common_name, created_at, updated_at FROM "fruits" WHERE id = @id LIMIT 1;', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + + @override + Future> getAll() async { + var r = await connection.query( + 'SELECT id, tree_id, common_name, created_at, updated_at FROM "fruits";'); + return r.map(parseRow).toList(); + } + + @override + Future createFruit(Fruit model) async { + model = model.copyWith( + createdAt: new DateTime.now(), updatedAt: new DateTime.now()); + var r = await connection.query( + 'INSERT INTO "fruits" ( "id", "tree_id", "common_name", "created_at", "updated_at") VALUES (@id,@treeId,@commonName,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "tree_id", "common_name", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'treeId': model.treeId, + 'commonName': model.commonName, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); + } + + @override + Future updateFruit(Fruit model) async { + model = model.copyWith(updatedAt: new DateTime.now()); + var r = await connection.query( + 'UPDATE "fruits" SET ( "id", "tree_id", "common_name", "created_at", "updated_at") = (@id,@treeId,@commonName,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "tree_id", "common_name", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'treeId': model.treeId, + 'commonName': model.commonName, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); } } diff --git a/angel_orm_generator/test/models/order.orm.g.dart b/angel_orm_generator/test/models/order.orm.g.dart index 655bd757..24258464 100644 --- a/angel_orm_generator/test/models/order.orm.g.dart +++ b/angel_orm_generator/test/models/order.orm.g.dart @@ -14,8 +14,9 @@ abstract class OrderOrm { _PostgreSqlOrderOrmImpl; Future> getAll(); - Future getById(id); - Future update(Order model); + Future getById(String id); + Future createOrder(Order model); + Future updateOrder(Order model); OrderQuery query(); } diff --git a/angel_orm_generator/test/models/order.postgresql.orm.g.dart b/angel_orm_generator/test/models/order.postgresql.orm.g.dart index 333d8606..bb1f2ed2 100644 --- a/angel_orm_generator/test/models/order.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/order.postgresql.orm.g.dart @@ -23,10 +23,52 @@ class _PostgreSqlOrderOrmImpl implements OrderOrm { } @override - Future getById() async { + Future getById(String id) async { var r = await connection.query( - 'SELECTidcustomer_idemployee_idorder_dateshipper_idcreated_atupdated_at FROM "orders" id = @id;', - substitutionValues: {'id': id}); - parseRow(r.first); + 'SELECT id, customer_id, employee_id, order_date, shipper_id, created_at, updated_at FROM "orders" WHERE id = @id LIMIT 1;', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + + @override + Future> getAll() async { + var r = await connection.query( + 'SELECT id, customer_id, employee_id, order_date, shipper_id, created_at, updated_at FROM "orders";'); + return r.map(parseRow).toList(); + } + + @override + Future createOrder(Order model) async { + model = model.copyWith( + createdAt: new DateTime.now(), updatedAt: new DateTime.now()); + var r = await connection.query( + 'INSERT INTO "orders" ( "id", "customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at") VALUES (@id,@customerId,@employeeId,CAST (@orderDate AS timestamp),@shipperId,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'customerId': model.customerId, + 'employeeId': model.employeeId, + 'orderDate': model.orderDate, + 'shipperId': model.shipperId, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); + } + + @override + Future updateOrder(Order model) async { + model = model.copyWith(updatedAt: new DateTime.now()); + var r = await connection.query( + 'UPDATE "orders" SET ( "id", "customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at") = (@id,@customerId,@employeeId,CAST (@orderDate AS timestamp),@shipperId,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'customerId': model.customerId, + 'employeeId': model.employeeId, + 'orderDate': model.orderDate, + 'shipperId': model.shipperId, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); } } diff --git a/angel_orm_generator/test/models/role.orm.g.dart b/angel_orm_generator/test/models/role.orm.g.dart index 0e5ddfd3..66a4c339 100644 --- a/angel_orm_generator/test/models/role.orm.g.dart +++ b/angel_orm_generator/test/models/role.orm.g.dart @@ -14,8 +14,9 @@ abstract class RoleOrm { _PostgreSqlRoleOrmImpl; Future> getAll(); - Future getById(id); - Future update(Role model); + Future getById(String id); + Future createRole(Role model); + Future updateRole(Role model); RoleQuery query(); } diff --git a/angel_orm_generator/test/models/role.postgresql.orm.g.dart b/angel_orm_generator/test/models/role.postgresql.orm.g.dart index a431fdf0..ea65367f 100644 --- a/angel_orm_generator/test/models/role.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/role.postgresql.orm.g.dart @@ -20,10 +20,46 @@ class _PostgreSqlRoleOrmImpl implements RoleOrm { } @override - Future getById() async { + Future getById(String id) async { var r = await connection.query( - 'SELECTidnamecreated_atupdated_at FROM "roles" id = @id;', - substitutionValues: {'id': id}); - parseRow(r.first); + 'SELECT id, name, created_at, updated_at FROM "roles" WHERE id = @id LIMIT 1;', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + + @override + Future> getAll() async { + var r = await connection + .query('SELECT id, name, created_at, updated_at FROM "roles";'); + return r.map(parseRow).toList(); + } + + @override + Future createRole(Role model) async { + model = model.copyWith( + createdAt: new DateTime.now(), updatedAt: new DateTime.now()); + var r = await connection.query( + 'INSERT INTO "roles" ( "id", "name", "created_at", "updated_at") VALUES (@id,@name,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "name", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'name': model.name, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); + } + + @override + Future updateRole(Role model) async { + model = model.copyWith(updatedAt: new DateTime.now()); + var r = await connection.query( + 'UPDATE "roles" SET ( "id", "name", "created_at", "updated_at") = (@id,@name,CAST (@createdAt AS timestamp),CAST (@updatedAt AS timestamp)) RETURNING "id", "name", "created_at", "updated_at";', + substitutionValues: { + 'id': model.id, + 'name': model.name, + 'createdAt': model.createdAt, + 'updatedAt': model.updatedAt + }); + return parseRow(r.first); } }