From dd1e6e4d5cd77d5ae5145725859a2436bdfcbab2 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 22 Oct 2018 16:54:51 -0400 Subject: [PATCH] begin postgres --- .../lib/src/postgresql_orm_generator.dart | 45 ++++++++++++++++++- .../test/models/author.postgresql.orm.g.dart | 8 ++++ .../test/models/car.postgresql.orm.g.dart | 8 ++++ .../models/customer.postgresql.orm.g.dart | 8 ++++ .../test/models/foot.postgresql.orm.g.dart | 8 ++++ .../test/models/fruit.postgresql.orm.g.dart | 8 ++++ .../test/models/order.postgresql.orm.g.dart | 8 ++++ .../test/models/role.postgresql.orm.g.dart | 8 ++++ 8 files changed, 100 insertions(+), 1 deletion(-) diff --git a/angel_orm_generator/lib/src/postgresql_orm_generator.dart b/angel_orm_generator/lib/src/postgresql_orm_generator.dart index 7004f946..91a5c003 100644 --- a/angel_orm_generator/lib/src/postgresql_orm_generator.dart +++ b/angel_orm_generator/lib/src/postgresql_orm_generator.dart @@ -68,7 +68,8 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation { ..name = 'connection' ..toThis = true)); })) - ..methods.add(buildParseRowMethod(ctx)); + ..methods.add(buildParseRowMethod(ctx)) + ..methods.add(buildGetById(ctx)); }); } @@ -97,4 +98,46 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation { }); }); } + + Method buildGetById(OrmBuildContext ctx) { + /* + @override + Future getById(id) async { + var r = await connection.query(''); + return parseRow(r.first); + } + */ + return new Method((m) { + m + ..name = 'getById' + ..annotations.add(refer('override')) + ..modifier = MethodModifier.async + ..returns = new TypeReference((b) => b + ..symbol = 'Future' + ..types.add(ctx.buildContext.modelClassType)) + ..body = new Block((b) { + var queryString = new StringBuffer('SELECT'); + int i = 0; + + for (var field in ctx.buildContext.fields) { + if (i > 0) queryString.write(', '); + queryString.write(ctx.buildContext.resolveFieldName(field.name)); + } + + 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')])); + }); + }); + } } 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 9c6ae65d..6bdf8e2d 100644 --- a/angel_orm_generator/test/models/author.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/author.postgresql.orm.g.dart @@ -18,4 +18,12 @@ class _PostgreSqlAuthorOrmImpl implements AuthorOrm { createdAt: (row[2] as DateTime), updatedAt: (row[3] as DateTime)); } + + @override + Future getById(id) async { + var r = await connection.query( + 'SELECTidnamecreated_atupdated_at FROM "authors" id = @id;', + substitutionValues: {'id': id}); + parseRow(r.first); + } } 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 2df1892e..fc972b08 100644 --- a/angel_orm_generator/test/models/car.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/car.postgresql.orm.g.dart @@ -21,4 +21,12 @@ class _PostgreSqlCarOrmImpl implements CarOrm { createdAt: (row[5] as DateTime), updatedAt: (row[6] as DateTime)); } + + @override + Future getById() async { + var r = await connection.query( + 'SELECTidmakedescriptionfamily_friendlyrecalled_atcreated_atupdated_at FROM "cars" id = @id;', + substitutionValues: {'id': id}); + parseRow(r.first); + } } 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 7e942c4e..054fff06 100644 --- a/angel_orm_generator/test/models/customer.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/customer.postgresql.orm.g.dart @@ -17,4 +17,12 @@ class _PostgreSqlCustomerOrmImpl implements CustomerOrm { createdAt: (row[1] as DateTime), updatedAt: (row[2] as DateTime)); } + + @override + Future getById() async { + var r = await connection.query( + 'SELECTidcreated_atupdated_at FROM "customers" id = @id;', + substitutionValues: {'id': id}); + parseRow(r.first); + } } 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 a066378f..0e498256 100644 --- a/angel_orm_generator/test/models/foot.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/foot.postgresql.orm.g.dart @@ -19,4 +19,12 @@ class _PostgreSqlFootOrmImpl implements FootOrm { createdAt: (row[3] as DateTime), updatedAt: (row[4] as DateTime)); } + + @override + Future getById() async { + var r = await connection.query( + 'SELECTidleg_idn_toescreated_atupdated_at FROM "foots" id = @id;', + substitutionValues: {'id': id}); + parseRow(r.first); + } } 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 5caafe91..eef902b0 100644 --- a/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/fruit.postgresql.orm.g.dart @@ -19,4 +19,12 @@ class _PostgreSqlFruitOrmImpl implements FruitOrm { createdAt: (row[3] as DateTime), updatedAt: (row[4] as DateTime)); } + + @override + Future getById() async { + var r = await connection.query( + 'SELECTidtree_idcommon_namecreated_atupdated_at FROM "fruits" id = @id;', + substitutionValues: {'id': id}); + parseRow(r.first); + } } 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 cb08e80b..333d8606 100644 --- a/angel_orm_generator/test/models/order.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/order.postgresql.orm.g.dart @@ -21,4 +21,12 @@ class _PostgreSqlOrderOrmImpl implements OrderOrm { createdAt: (row[5] as DateTime), updatedAt: (row[6] as DateTime)); } + + @override + Future getById() async { + var r = await connection.query( + 'SELECTidcustomer_idemployee_idorder_dateshipper_idcreated_atupdated_at FROM "orders" id = @id;', + substitutionValues: {'id': id}); + parseRow(r.first); + } } 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 2b3aaddc..a431fdf0 100644 --- a/angel_orm_generator/test/models/role.postgresql.orm.g.dart +++ b/angel_orm_generator/test/models/role.postgresql.orm.g.dart @@ -18,4 +18,12 @@ class _PostgreSqlRoleOrmImpl implements RoleOrm { createdAt: (row[2] as DateTime), updatedAt: (row[3] as DateTime)); } + + @override + Future getById() async { + var r = await connection.query( + 'SELECTidnamecreated_atupdated_at FROM "roles" id = @id;', + substitutionValues: {'id': id}); + parseRow(r.first); + } }