diff --git a/angel_orm_generator/lib/src/orm_generator.dart b/angel_orm_generator/lib/src/orm_generator.dart index f72e1191..359eb985 100644 --- a/angel_orm_generator/lib/src/orm_generator.dart +++ b/angel_orm_generator/lib/src/orm_generator.dart @@ -116,6 +116,7 @@ class OrmGenerator extends GeneratorForAnnotation { // Next, add method stubs. // * getAll // * getById + // * deleteById // * updateX() // * createX() // * query() @@ -141,6 +142,16 @@ class OrmGenerator extends GeneratorForAnnotation { ..type = refer('String'))); })); + // deleteById + clazz.methods.add(new Method((m) { + m + ..name = 'deleteById' + ..returns = futureOf(ctx.buildContext.modelClassName) + ..requiredParameters.add(new Parameter((b) => b + ..name = 'id' + ..type = refer('String'))); + })); + // createX() clazz.methods.add(new Method((m) { m diff --git a/angel_orm_generator/lib/src/postgresql_orm_generator.dart b/angel_orm_generator/lib/src/postgresql_orm_generator.dart index 29204b42..caac4456 100644 --- a/angel_orm_generator/lib/src/postgresql_orm_generator.dart +++ b/angel_orm_generator/lib/src/postgresql_orm_generator.dart @@ -71,6 +71,7 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation { })) ..methods.add(buildParseRowMethod(ctx)) ..methods.add(buildGetById(ctx)) + ..methods.add(buildDeleteById(ctx)) ..methods.add(buildGetAll(ctx)) ..methods.add(buildCreate(ctx)) ..methods.add(buildUpdate(ctx)); @@ -213,6 +214,39 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation { }); } + Method buildDeleteById(OrmBuildContext ctx) { + /* + @override + Future getById(id) async { + var r = await connection.query(''); + return parseRow(r.first); + } + */ + return new Method((m) { + m + ..name = 'deleteById' + ..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 fields = buildQuotedFieldString(ctx); + var queryString = + 'DELETE FROM "${ctx.tableName}" WHERE id = @id RETURNING $fields;'; + applyQueryAndReturnOne( + b, + queryString, + literalMap({ + 'id': refer('int').property('parse').call([refer('id')]) + })); + }); + }); + } + Method buildGetAll(OrmBuildContext ctx) { /* @override diff --git a/angel_orm_generator/test/models/author.orm.g.dart b/angel_orm_generator/test/models/author.orm.g.dart index e7c30d86..43d3befd 100644 --- a/angel_orm_generator/test/models/author.orm.g.dart +++ b/angel_orm_generator/test/models/author.orm.g.dart @@ -15,6 +15,7 @@ abstract class AuthorOrm { Future> getAll(); Future getById(String id); + Future deleteById(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 e11d30ec..767b44f9 100644 --- a/angel_orm_generator/test/models/author.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/author.postgresql.orm.g.dart @@ -27,6 +27,14 @@ class _PostgreSqlAuthorOrmImpl implements AuthorOrm { return parseRow(r.first); } + @override + Future deleteById(String id) async { + var r = await connection.query( + 'DELETE FROM "authors" WHERE id = @id RETURNING "id", "name", "created_at", "updated_at";', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + @override Future> getAll() async { var r = await connection diff --git a/angel_orm_generator/test/models/car.orm.g.dart b/angel_orm_generator/test/models/car.orm.g.dart index 8e7a44b1..9595ca52 100644 --- a/angel_orm_generator/test/models/car.orm.g.dart +++ b/angel_orm_generator/test/models/car.orm.g.dart @@ -15,6 +15,7 @@ abstract class CarOrm { Future> getAll(); Future getById(String id); + Future deleteById(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 d48c2207..68270a27 100644 --- a/angel_orm_generator/test/models/car.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/car.postgresql.orm.g.dart @@ -30,6 +30,14 @@ class _PostgreSqlCarOrmImpl implements CarOrm { return parseRow(r.first); } + @override + Future deleteById(String id) async { + var r = await connection.query( + 'DELETE FROM "cars" WHERE id = @id RETURNING "id", "make", "description", "family_friendly", "recalled_at", "created_at", "updated_at";', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + @override Future> getAll() async { var r = await connection.query( diff --git a/angel_orm_generator/test/models/customer.orm.g.dart b/angel_orm_generator/test/models/customer.orm.g.dart index 3afef6b8..da67eb6e 100644 --- a/angel_orm_generator/test/models/customer.orm.g.dart +++ b/angel_orm_generator/test/models/customer.orm.g.dart @@ -15,6 +15,7 @@ abstract class CustomerOrm { Future> getAll(); Future getById(String id); + Future deleteById(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 2ca1fe76..61e75a2a 100644 --- a/angel_orm_generator/test/models/customer.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/customer.postgresql.orm.g.dart @@ -26,6 +26,14 @@ class _PostgreSqlCustomerOrmImpl implements CustomerOrm { return parseRow(r.first); } + @override + Future deleteById(String id) async { + var r = await connection.query( + 'DELETE FROM "customers" WHERE id = @id RETURNING "id", "created_at", "updated_at";', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + @override Future> getAll() async { var r = await connection diff --git a/angel_orm_generator/test/models/foot.orm.g.dart b/angel_orm_generator/test/models/foot.orm.g.dart index 99295919..a71aa11c 100644 --- a/angel_orm_generator/test/models/foot.orm.g.dart +++ b/angel_orm_generator/test/models/foot.orm.g.dart @@ -15,6 +15,7 @@ abstract class FootOrm { Future> getAll(); Future getById(String id); + Future deleteById(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 7442a506..e495a82c 100644 --- a/angel_orm_generator/test/models/foot.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/foot.postgresql.orm.g.dart @@ -28,6 +28,14 @@ class _PostgreSqlFootOrmImpl implements FootOrm { return parseRow(r.first); } + @override + Future deleteById(String id) async { + var r = await connection.query( + 'DELETE FROM "foots" WHERE id = @id RETURNING "id", "leg_id", "n_toes", "created_at", "updated_at";', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + @override Future> getAll() async { var r = await connection.query( diff --git a/angel_orm_generator/test/models/fruit.orm.g.dart b/angel_orm_generator/test/models/fruit.orm.g.dart index b4e17442..b48b91d4 100644 --- a/angel_orm_generator/test/models/fruit.orm.g.dart +++ b/angel_orm_generator/test/models/fruit.orm.g.dart @@ -15,6 +15,7 @@ abstract class FruitOrm { Future> getAll(); Future getById(String id); + Future deleteById(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 54b91920..275b2c22 100644 --- a/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart @@ -28,6 +28,14 @@ class _PostgreSqlFruitOrmImpl implements FruitOrm { return parseRow(r.first); } + @override + Future deleteById(String id) async { + var r = await connection.query( + 'DELETE FROM "fruits" WHERE id = @id RETURNING "id", "tree_id", "common_name", "created_at", "updated_at";', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + @override Future> getAll() async { var r = await connection.query( diff --git a/angel_orm_generator/test/models/order.orm.g.dart b/angel_orm_generator/test/models/order.orm.g.dart index 24258464..694cd615 100644 --- a/angel_orm_generator/test/models/order.orm.g.dart +++ b/angel_orm_generator/test/models/order.orm.g.dart @@ -15,6 +15,7 @@ abstract class OrderOrm { Future> getAll(); Future getById(String id); + Future deleteById(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 bb1f2ed2..759ca7c3 100644 --- a/angel_orm_generator/test/models/order.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/order.postgresql.orm.g.dart @@ -30,6 +30,14 @@ class _PostgreSqlOrderOrmImpl implements OrderOrm { return parseRow(r.first); } + @override + Future deleteById(String id) async { + var r = await connection.query( + 'DELETE FROM "orders" WHERE id = @id RETURNING "id", "customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at";', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + @override Future> getAll() async { var r = await connection.query( diff --git a/angel_orm_generator/test/models/role.orm.g.dart b/angel_orm_generator/test/models/role.orm.g.dart index 66a4c339..993e4c60 100644 --- a/angel_orm_generator/test/models/role.orm.g.dart +++ b/angel_orm_generator/test/models/role.orm.g.dart @@ -15,6 +15,7 @@ abstract class RoleOrm { Future> getAll(); Future getById(String id); + Future deleteById(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 ea65367f..ba7af45c 100644 --- a/angel_orm_generator/test/models/role.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/role.postgresql.orm.g.dart @@ -27,6 +27,14 @@ class _PostgreSqlRoleOrmImpl implements RoleOrm { return parseRow(r.first); } + @override + Future deleteById(String id) async { + var r = await connection.query( + 'DELETE FROM "roles" WHERE id = @id RETURNING "id", "name", "created_at", "updated_at";', + substitutionValues: {'id': int.parse(id)}); + return parseRow(r.first); + } + @override Future> getAll() async { var r = await connection