diff --git a/angel_orm_generator/CHANGELOG.md b/angel_orm_generator/CHANGELOG.md index b987f5b6..33aab2bf 100644 --- a/angel_orm_generator/CHANGELOG.md +++ b/angel_orm_generator/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.0.0-alpha+6 +* `DateTime` is now `CAST` on insertion and update operations. + # 1.0.0-alpha+3 Implemented `@hasOne`, with tests. Still missing `@hasMany`. `belongsToMany` will likely be scrapped. diff --git a/angel_orm_generator/lib/src/builder/orm/postgres.dart b/angel_orm_generator/lib/src/builder/orm/postgres.dart index f4efeeea..860e5bd6 100644 --- a/angel_orm_generator/lib/src/builder/orm/postgres.dart +++ b/angel_orm_generator/lib/src/builder/orm/postgres.dart @@ -546,6 +546,14 @@ class PostgresOrmGenerator extends GeneratorForAnnotation { namedArguments: {'substitutionValues': map(substitutionValues)}); } + void printField( + PostgresBuildContext ctx, FieldElement field, StringBuffer buf) { + if (dateTimeTypeChecker.isAssignableFromType(field.type)) + buf.write('CAST (@${field.name} AS ${ctx.columnInfo[field.name].type.name})'); + else + buf.write('@${field.name}'); + } + MethodBuilder buildUpdateMethod(PostgresBuildContext ctx) { var meth = new MethodBuilder('update', returnType: @@ -572,7 +580,7 @@ class PostgresOrmGenerator extends GeneratorForAnnotation { return; else { if (i++ > 0) buf.write(', '); - buf.write('@${field.name}'); + printField(ctx, field, buf); } }); buf.write(') '); @@ -678,7 +686,7 @@ class PostgresOrmGenerator extends GeneratorForAnnotation { return; else { if (i++ > 0) buf.write(', '); - buf.write('@${field.name}'); + printField(ctx, field, buf); } }); diff --git a/angel_orm_generator/pubspec.yaml b/angel_orm_generator/pubspec.yaml index 57fb9a16..6dbc8a60 100644 --- a/angel_orm_generator/pubspec.yaml +++ b/angel_orm_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_orm_generator -version: 1.0.0-alpha+5 +version: 1.0.0-alpha+6 description: Code generators for Angel's ORM. author: Tobe O homepage: https://github.com/angel-dart/orm diff --git a/angel_orm_generator/test/models/author.orm.g.dart b/angel_orm_generator/test/models/author.orm.g.dart index 844ddd90..686525d2 100644 --- a/angel_orm_generator/test/models/author.orm.g.dart +++ b/angel_orm_generator/test/models/author.orm.g.dart @@ -122,7 +122,7 @@ class AuthorQuery { Stream update(PostgreSQLConnection connection, {String name: 'Tobe Osakwe', DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "authors" SET ("name", "created_at", "updated_at") = (@name, @createdAt, @updatedAt) '); + 'UPDATE "authors" SET ("name", "created_at", "updated_at") = (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -176,7 +176,7 @@ class AuthorQuery { DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "authors" ("name", "created_at", "updated_at") VALUES (@name, @createdAt, @updatedAt) RETURNING "id", "name", "created_at", "updated_at";', + 'INSERT INTO "authors" ("name", "created_at", "updated_at") VALUES (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "name", "created_at", "updated_at";', substitutionValues: { 'name': name, 'createdAt': createdAt != null ? createdAt : __ormNow__, diff --git a/angel_orm_generator/test/models/book.orm.g.dart b/angel_orm_generator/test/models/book.orm.g.dart index c2b19115..0c0cc41c 100644 --- a/angel_orm_generator/test/models/book.orm.g.dart +++ b/angel_orm_generator/test/models/book.orm.g.dart @@ -130,7 +130,7 @@ class BookQuery { Stream update(PostgreSQLConnection connection, {String name, DateTime createdAt, DateTime updatedAt, int authorId}) { var buf = new StringBuffer( - 'UPDATE "books" SET ("name", "created_at", "updated_at", "author_id") = (@name, @createdAt, @updatedAt, @authorId) '); + 'UPDATE "books" SET ("name", "created_at", "updated_at", "author_id") = (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp), @authorId) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -189,7 +189,7 @@ class BookQuery { int authorId}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "books" ("name", "created_at", "updated_at", "author_id") VALUES (@name, @createdAt, @updatedAt, @authorId) RETURNING "id", "name", "created_at", "updated_at", "author_id";', + 'INSERT INTO "books" ("name", "created_at", "updated_at", "author_id") VALUES (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp), @authorId) RETURNING "id", "name", "created_at", "updated_at", "author_id";', substitutionValues: { 'name': name, 'createdAt': createdAt != null ? createdAt : __ormNow__, diff --git a/angel_orm_generator/test/models/car.orm.g.dart b/angel_orm_generator/test/models/car.orm.g.dart index 4b61e0ac..074df007 100644 --- a/angel_orm_generator/test/models/car.orm.g.dart +++ b/angel_orm_generator/test/models/car.orm.g.dart @@ -130,7 +130,7 @@ class CarQuery { DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "cars" SET ("make", "description", "family_friendly", "recalled_at", "created_at", "updated_at") = (@make, @description, @familyFriendly, @recalledAt, @createdAt, @updatedAt) '); + 'UPDATE "cars" SET ("make", "description", "family_friendly", "recalled_at", "created_at", "updated_at") = (@make, @description, @familyFriendly, CAST (@recalledAt AS timestamp), CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -191,7 +191,7 @@ class CarQuery { DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "cars" ("make", "description", "family_friendly", "recalled_at", "created_at", "updated_at") VALUES (@make, @description, @familyFriendly, @recalledAt, @createdAt, @updatedAt) RETURNING "id", "make", "description", "family_friendly", "recalled_at", "created_at", "updated_at";', + 'INSERT INTO "cars" ("make", "description", "family_friendly", "recalled_at", "created_at", "updated_at") VALUES (@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: { 'make': make, 'description': description, diff --git a/angel_orm_generator/test/models/customer.orm.g.dart b/angel_orm_generator/test/models/customer.orm.g.dart index 1a9437d5..9678e282 100644 --- a/angel_orm_generator/test/models/customer.orm.g.dart +++ b/angel_orm_generator/test/models/customer.orm.g.dart @@ -118,7 +118,7 @@ class CustomerQuery { Stream update(PostgreSQLConnection connection, {DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "customers" SET ("created_at", "updated_at") = (@createdAt, @updatedAt) '); + 'UPDATE "customers" SET ("created_at", "updated_at") = (CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -169,7 +169,7 @@ class CustomerQuery { {DateTime createdAt, DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "customers" ("created_at", "updated_at") VALUES (@createdAt, @updatedAt) RETURNING "id", "created_at", "updated_at";', + 'INSERT INTO "customers" ("created_at", "updated_at") VALUES (CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "created_at", "updated_at";', substitutionValues: { 'createdAt': createdAt != null ? createdAt : __ormNow__, 'updatedAt': updatedAt != null ? updatedAt : __ormNow__ diff --git a/angel_orm_generator/test/models/foot.orm.g.dart b/angel_orm_generator/test/models/foot.orm.g.dart index c2869ff2..62ef4f39 100644 --- a/angel_orm_generator/test/models/foot.orm.g.dart +++ b/angel_orm_generator/test/models/foot.orm.g.dart @@ -123,7 +123,7 @@ class FootQuery { Stream update(PostgreSQLConnection connection, {int legId, int nToes, DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "foots" SET ("leg_id", "n_toes", "created_at", "updated_at") = (@legId, @nToes, @createdAt, @updatedAt) '); + 'UPDATE "foots" SET ("leg_id", "n_toes", "created_at", "updated_at") = (@legId, @nToes, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -177,7 +177,7 @@ class FootQuery { {int legId, int nToes, DateTime createdAt, DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "foots" ("leg_id", "n_toes", "created_at", "updated_at") VALUES (@legId, @nToes, @createdAt, @updatedAt) RETURNING "id", "leg_id", "n_toes", "created_at", "updated_at";', + 'INSERT INTO "foots" ("leg_id", "n_toes", "created_at", "updated_at") VALUES (@legId, @nToes, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "leg_id", "n_toes", "created_at", "updated_at";', substitutionValues: { 'legId': legId, 'nToes': nToes, diff --git a/angel_orm_generator/test/models/fruit.orm.g.dart b/angel_orm_generator/test/models/fruit.orm.g.dart index a0a613b5..43ade1ea 100644 --- a/angel_orm_generator/test/models/fruit.orm.g.dart +++ b/angel_orm_generator/test/models/fruit.orm.g.dart @@ -123,7 +123,7 @@ class FruitQuery { Stream update(PostgreSQLConnection connection, {int treeId, String commonName, DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "fruits" SET ("tree_id", "common_name", "created_at", "updated_at") = (@treeId, @commonName, @createdAt, @updatedAt) '); + 'UPDATE "fruits" SET ("tree_id", "common_name", "created_at", "updated_at") = (@treeId, @commonName, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -180,7 +180,7 @@ class FruitQuery { DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "fruits" ("tree_id", "common_name", "created_at", "updated_at") VALUES (@treeId, @commonName, @createdAt, @updatedAt) RETURNING "id", "tree_id", "common_name", "created_at", "updated_at";', + 'INSERT INTO "fruits" ("tree_id", "common_name", "created_at", "updated_at") VALUES (@treeId, @commonName, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "tree_id", "common_name", "created_at", "updated_at";', substitutionValues: { 'treeId': treeId, 'commonName': commonName, diff --git a/angel_orm_generator/test/models/leg.orm.g.dart b/angel_orm_generator/test/models/leg.orm.g.dart index 492f73d2..5b6c4865 100644 --- a/angel_orm_generator/test/models/leg.orm.g.dart +++ b/angel_orm_generator/test/models/leg.orm.g.dart @@ -133,7 +133,7 @@ class LegQuery { Stream update(PostgreSQLConnection connection, {String name, DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "legs" SET ("name", "created_at", "updated_at") = (@name, @createdAt, @updatedAt) '); + 'UPDATE "legs" SET ("name", "created_at", "updated_at") = (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -193,7 +193,7 @@ class LegQuery { {String name, DateTime createdAt, DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "legs" ("name", "created_at", "updated_at") VALUES (@name, @createdAt, @updatedAt) RETURNING "id", "name", "created_at", "updated_at";', + 'INSERT INTO "legs" ("name", "created_at", "updated_at") VALUES (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "name", "created_at", "updated_at";', substitutionValues: { 'name': name, 'createdAt': createdAt != null ? createdAt : __ormNow__, diff --git a/angel_orm_generator/test/models/order.orm.g.dart b/angel_orm_generator/test/models/order.orm.g.dart index 8b7afd26..476c800e 100644 --- a/angel_orm_generator/test/models/order.orm.g.dart +++ b/angel_orm_generator/test/models/order.orm.g.dart @@ -130,7 +130,7 @@ class OrderQuery { DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "orders" SET ("customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at") = (@customerId, @employeeId, @orderDate, @shipperId, @createdAt, @updatedAt) '); + 'UPDATE "orders" SET ("customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at") = (@customerId, @employeeId, CAST (@orderDate AS timestamp), @shipperId, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -191,7 +191,7 @@ class OrderQuery { DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "orders" ("customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at") VALUES (@customerId, @employeeId, @orderDate, @shipperId, @createdAt, @updatedAt) RETURNING "id", "customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at";', + 'INSERT INTO "orders" ("customer_id", "employee_id", "order_date", "shipper_id", "created_at", "updated_at") VALUES (@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: { 'customerId': customerId, 'employeeId': employeeId, @@ -233,8 +233,7 @@ class OrderQuery { static Stream getAll(PostgreSQLConnection connection) => new OrderQuery().get(connection); - static joinCustomers(PostgreSQLConnection connection) { - } + static joinCustomers(PostgreSQLConnection connection) {} } class OrderQueryWhere { diff --git a/angel_orm_generator/test/models/role.orm.g.dart b/angel_orm_generator/test/models/role.orm.g.dart index 7e0e9b43..9a0486ec 100644 --- a/angel_orm_generator/test/models/role.orm.g.dart +++ b/angel_orm_generator/test/models/role.orm.g.dart @@ -122,7 +122,7 @@ class RoleQuery { Stream update(PostgreSQLConnection connection, {String name, DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "roles" SET ("name", "created_at", "updated_at") = (@name, @createdAt, @updatedAt) '); + 'UPDATE "roles" SET ("name", "created_at", "updated_at") = (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -174,7 +174,7 @@ class RoleQuery { {String name, DateTime createdAt, DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "roles" ("name", "created_at", "updated_at") VALUES (@name, @createdAt, @updatedAt) RETURNING "id", "name", "created_at", "updated_at";', + 'INSERT INTO "roles" ("name", "created_at", "updated_at") VALUES (@name, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "name", "created_at", "updated_at";', substitutionValues: { 'name': name, 'createdAt': createdAt != null ? createdAt : __ormNow__, diff --git a/angel_orm_generator/test/models/tree.orm.g.dart b/angel_orm_generator/test/models/tree.orm.g.dart index ccae50c2..b21951cd 100644 --- a/angel_orm_generator/test/models/tree.orm.g.dart +++ b/angel_orm_generator/test/models/tree.orm.g.dart @@ -131,7 +131,7 @@ class TreeQuery { Stream update(PostgreSQLConnection connection, {int rings, DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "trees" SET ("rings", "created_at", "updated_at") = (@rings, @createdAt, @updatedAt) '); + 'UPDATE "trees" SET ("rings", "created_at", "updated_at") = (@rings, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -192,7 +192,7 @@ class TreeQuery { {int rings, DateTime createdAt, DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "trees" ("rings", "created_at", "updated_at") VALUES (@rings, @createdAt, @updatedAt) RETURNING "id", "rings", "created_at", "updated_at";', + 'INSERT INTO "trees" ("rings", "created_at", "updated_at") VALUES (@rings, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "rings", "created_at", "updated_at";', substitutionValues: { 'rings': rings, 'createdAt': createdAt != null ? createdAt : __ormNow__, diff --git a/angel_orm_generator/test/models/user.orm.g.dart b/angel_orm_generator/test/models/user.orm.g.dart index c4dc3913..b074722d 100644 --- a/angel_orm_generator/test/models/user.orm.g.dart +++ b/angel_orm_generator/test/models/user.orm.g.dart @@ -135,7 +135,7 @@ class UserQuery { DateTime createdAt, DateTime updatedAt}) { var buf = new StringBuffer( - 'UPDATE "users" SET ("username", "password", "email", "created_at", "updated_at") = (@username, @password, @email, @createdAt, @updatedAt) '); + 'UPDATE "users" SET ("username", "password", "email", "created_at", "updated_at") = (@username, @password, @email, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) '); var whereClause = where.toWhereClause(); if (whereClause != null) { buf.write(whereClause); @@ -200,7 +200,7 @@ class UserQuery { DateTime updatedAt}) async { var __ormNow__ = new DateTime.now(); var result = await connection.query( - 'INSERT INTO "users" ("username", "password", "email", "created_at", "updated_at") VALUES (@username, @password, @email, @createdAt, @updatedAt) RETURNING "id", "username", "password", "email", "created_at", "updated_at";', + 'INSERT INTO "users" ("username", "password", "email", "created_at", "updated_at") VALUES (@username, @password, @email, CAST (@createdAt AS timestamp), CAST (@updatedAt AS timestamp)) RETURNING "id", "username", "password", "email", "created_at", "updated_at";', substitutionValues: { 'username': username, 'password': password,