create + update

This commit is contained in:
Tobe O 2018-10-22 18:28:32 -04:00
parent 43648c8daa
commit 6073d0ec29
16 changed files with 512 additions and 66 deletions

View file

@ -116,7 +116,8 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
// Next, add method stubs. // Next, add method stubs.
// * getAll // * getAll
// * getById // * getById
// * update // * updateX()
// * createX()
// * query() // * query()
// getAll // getAll
@ -135,13 +136,25 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
m m
..name = 'getById' ..name = 'getById'
..returns = futureOf(ctx.buildContext.modelClassName) ..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) { clazz.methods.add(new Method((m) {
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) ..returns = futureOf(ctx.buildContext.modelClassName)
..requiredParameters.add(new Parameter((b) => b ..requiredParameters.add(new Parameter((b) => b
..name = 'model' ..name = 'model'

View file

@ -3,6 +3,7 @@ import 'dart:async';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:angel_orm/angel_orm.dart'; import 'package:angel_orm/angel_orm.dart';
import 'package:angel_serialize_generator/angel_serialize_generator.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:build/build.dart';
import 'package:code_builder/code_builder.dart' hide LibraryBuilder; import 'package:code_builder/code_builder.dart' hide LibraryBuilder;
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
@ -69,7 +70,10 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation<Orm> {
..toThis = true)); ..toThis = true));
})) }))
..methods.add(buildParseRowMethod(ctx)) ..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<Orm> {
}); });
} }
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 = <Expression, Expression>{};
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) { Method buildGetById(OrmBuildContext ctx) {
/* /*
@override @override
@ -112,32 +193,111 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation<Orm> {
..name = 'getById' ..name = 'getById'
..annotations.add(refer('override')) ..annotations.add(refer('override'))
..modifier = MethodModifier.async ..modifier = MethodModifier.async
..requiredParameters.add(new Parameter((b) => b
..name = 'id'
..type = refer('String')))
..returns = new TypeReference((b) => b ..returns = new TypeReference((b) => b
..symbol = 'Future' ..symbol = 'Future'
..types.add(ctx.buildContext.modelClassType)) ..types.add(ctx.buildContext.modelClassType))
..body = new Block((b) { ..body = new Block((b) {
var queryString = new StringBuffer('SELECT'); var fields = buildFieldString(ctx);
int i = 0; 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) { Method buildGetAll(OrmBuildContext ctx) {
if (i > 0) queryString.write(', '); /*
queryString.write(ctx.buildContext.resolveFieldName(field.name)); @override
Future<List<Author>> 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<List<${ctx.buildContext.modelClassName}>>')
..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<Author> 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;'); var fields = buildQuotedFieldString(ctx);
b.statements.add(refer('connection') var fieldSet = buildInsertionValueString(ctx);
.property('query') var queryString =
.call([ 'INSERT INTO "${ctx.tableName}" ($fields) VALUES $fieldSet RETURNING $fields;';
literalString(queryString.toString()) applyQueryAndReturnOne(
], { block, queryString, buildSubstitutionValues(ctx));
'substitutionValues': literalMap({'id': refer('id')}) });
}) });
.awaited }
.assignVar('r')
.statement); Method buildUpdate(OrmBuildContext ctx) {
b.addExpression( /*
(refer('parseRow').call([refer('r').property('first')])) @override
.returned); Future<Author> 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));
}); });
}); });
} }

View file

@ -14,8 +14,9 @@ abstract class AuthorOrm {
_PostgreSqlAuthorOrmImpl; _PostgreSqlAuthorOrmImpl;
Future<List<Author>> getAll(); Future<List<Author>> getAll();
Future<Author> getById(id); Future<Author> getById(String id);
Future<Author> update(Author model); Future<Author> createAuthor(Author model);
Future<Author> updateAuthor(Author model);
AuthorQuery query(); AuthorQuery query();
} }

View file

@ -20,10 +20,46 @@ class _PostgreSqlAuthorOrmImpl implements AuthorOrm {
} }
@override @override
Future<Author> getById(id) async { Future<Author> getById(String id) async {
var r = await connection.query( var r = await connection.query(
'SELECTidnamecreated_atupdated_at FROM "authors" id = @id;', 'SELECT id, name, created_at, updated_at FROM "authors" WHERE id = @id LIMIT 1;',
substitutionValues: {'id': id}); substitutionValues: {'id': int.parse(id)});
parseRow(r.first); return parseRow(r.first);
}
@override
Future<List<Author>> getAll() async {
var r = await connection
.query('SELECT id, name, created_at, updated_at FROM "authors";');
return r.map(parseRow).toList();
}
@override
Future<Author> 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<Author> 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);
} }
} }

View file

@ -14,8 +14,9 @@ abstract class CarOrm {
_PostgreSqlCarOrmImpl; _PostgreSqlCarOrmImpl;
Future<List<Car>> getAll(); Future<List<Car>> getAll();
Future<Car> getById(id); Future<Car> getById(String id);
Future<Car> update(Car model); Future<Car> createCar(Car model);
Future<Car> updateCar(Car model);
CarQuery query(); CarQuery query();
} }

View file

@ -23,10 +23,52 @@ class _PostgreSqlCarOrmImpl implements CarOrm {
} }
@override @override
Future<Car> getById() async { Future<Car> getById(String id) async {
var r = await connection.query( var r = await connection.query(
'SELECTidmakedescriptionfamily_friendlyrecalled_atcreated_atupdated_at FROM "cars" id = @id;', 'SELECT id, make, description, family_friendly, recalled_at, created_at, updated_at FROM "cars" WHERE id = @id LIMIT 1;',
substitutionValues: {'id': id}); substitutionValues: {'id': int.parse(id)});
parseRow(r.first); return parseRow(r.first);
}
@override
Future<List<Car>> 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<Car> 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<Car> 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);
} }
} }

View file

@ -14,8 +14,9 @@ abstract class CustomerOrm {
_PostgreSqlCustomerOrmImpl; _PostgreSqlCustomerOrmImpl;
Future<List<Customer>> getAll(); Future<List<Customer>> getAll();
Future<Customer> getById(id); Future<Customer> getById(String id);
Future<Customer> update(Customer model); Future<Customer> createCustomer(Customer model);
Future<Customer> updateCustomer(Customer model);
CustomerQuery query(); CustomerQuery query();
} }

View file

@ -19,10 +19,44 @@ class _PostgreSqlCustomerOrmImpl implements CustomerOrm {
} }
@override @override
Future<Customer> getById() async { Future<Customer> getById(String id) async {
var r = await connection.query( var r = await connection.query(
'SELECTidcreated_atupdated_at FROM "customers" id = @id;', 'SELECT id, created_at, updated_at FROM "customers" WHERE id = @id LIMIT 1;',
substitutionValues: {'id': id}); substitutionValues: {'id': int.parse(id)});
parseRow(r.first); return parseRow(r.first);
}
@override
Future<List<Customer>> getAll() async {
var r = await connection
.query('SELECT id, created_at, updated_at FROM "customers";');
return r.map(parseRow).toList();
}
@override
Future<Customer> 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<Customer> 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);
} }
} }

View file

@ -14,8 +14,9 @@ abstract class FootOrm {
_PostgreSqlFootOrmImpl; _PostgreSqlFootOrmImpl;
Future<List<Foot>> getAll(); Future<List<Foot>> getAll();
Future<Foot> getById(id); Future<Foot> getById(String id);
Future<Foot> update(Foot model); Future<Foot> createFoot(Foot model);
Future<Foot> updateFoot(Foot model);
FootQuery query(); FootQuery query();
} }

View file

@ -21,10 +21,48 @@ class _PostgreSqlFootOrmImpl implements FootOrm {
} }
@override @override
Future<Foot> getById() async { Future<Foot> getById(String id) async {
var r = await connection.query( var r = await connection.query(
'SELECTidleg_idn_toescreated_atupdated_at FROM "foots" id = @id;', 'SELECT id, leg_id, n_toes, created_at, updated_at FROM "foots" WHERE id = @id LIMIT 1;',
substitutionValues: {'id': id}); substitutionValues: {'id': int.parse(id)});
parseRow(r.first); return parseRow(r.first);
}
@override
Future<List<Foot>> 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<Foot> 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<Foot> 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);
} }
} }

View file

@ -14,8 +14,9 @@ abstract class FruitOrm {
_PostgreSqlFruitOrmImpl; _PostgreSqlFruitOrmImpl;
Future<List<Fruit>> getAll(); Future<List<Fruit>> getAll();
Future<Fruit> getById(id); Future<Fruit> getById(String id);
Future<Fruit> update(Fruit model); Future<Fruit> createFruit(Fruit model);
Future<Fruit> updateFruit(Fruit model);
FruitQuery query(); FruitQuery query();
} }

View file

@ -21,10 +21,48 @@ class _PostgreSqlFruitOrmImpl implements FruitOrm {
} }
@override @override
Future<Fruit> getById() async { Future<Fruit> getById(String id) async {
var r = await connection.query( var r = await connection.query(
'SELECTidtree_idcommon_namecreated_atupdated_at FROM "fruits" id = @id;', 'SELECT id, tree_id, common_name, created_at, updated_at FROM "fruits" WHERE id = @id LIMIT 1;',
substitutionValues: {'id': id}); substitutionValues: {'id': int.parse(id)});
parseRow(r.first); return parseRow(r.first);
}
@override
Future<List<Fruit>> 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<Fruit> 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<Fruit> 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);
} }
} }

View file

@ -14,8 +14,9 @@ abstract class OrderOrm {
_PostgreSqlOrderOrmImpl; _PostgreSqlOrderOrmImpl;
Future<List<Order>> getAll(); Future<List<Order>> getAll();
Future<Order> getById(id); Future<Order> getById(String id);
Future<Order> update(Order model); Future<Order> createOrder(Order model);
Future<Order> updateOrder(Order model);
OrderQuery query(); OrderQuery query();
} }

View file

@ -23,10 +23,52 @@ class _PostgreSqlOrderOrmImpl implements OrderOrm {
} }
@override @override
Future<Order> getById() async { Future<Order> getById(String id) async {
var r = await connection.query( var r = await connection.query(
'SELECTidcustomer_idemployee_idorder_dateshipper_idcreated_atupdated_at FROM "orders" id = @id;', 'SELECT id, customer_id, employee_id, order_date, shipper_id, created_at, updated_at FROM "orders" WHERE id = @id LIMIT 1;',
substitutionValues: {'id': id}); substitutionValues: {'id': int.parse(id)});
parseRow(r.first); return parseRow(r.first);
}
@override
Future<List<Order>> 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<Order> 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<Order> 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);
} }
} }

View file

@ -14,8 +14,9 @@ abstract class RoleOrm {
_PostgreSqlRoleOrmImpl; _PostgreSqlRoleOrmImpl;
Future<List<Role>> getAll(); Future<List<Role>> getAll();
Future<Role> getById(id); Future<Role> getById(String id);
Future<Role> update(Role model); Future<Role> createRole(Role model);
Future<Role> updateRole(Role model);
RoleQuery query(); RoleQuery query();
} }

View file

@ -20,10 +20,46 @@ class _PostgreSqlRoleOrmImpl implements RoleOrm {
} }
@override @override
Future<Role> getById() async { Future<Role> getById(String id) async {
var r = await connection.query( var r = await connection.query(
'SELECTidnamecreated_atupdated_at FROM "roles" id = @id;', 'SELECT id, name, created_at, updated_at FROM "roles" WHERE id = @id LIMIT 1;',
substitutionValues: {'id': id}); substitutionValues: {'id': int.parse(id)});
parseRow(r.first); return parseRow(r.first);
}
@override
Future<List<Role>> getAll() async {
var r = await connection
.query('SELECT id, name, created_at, updated_at FROM "roles";');
return r.map(parseRow).toList();
}
@override
Future<Role> 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<Role> 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);
} }
} }