create + update
This commit is contained in:
parent
43648c8daa
commit
6073d0ec29
16 changed files with 512 additions and 66 deletions
|
@ -116,7 +116,8 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
|||
// Next, add method stubs.
|
||||
// * getAll
|
||||
// * getById
|
||||
// * update
|
||||
// * updateX()
|
||||
// * createX()
|
||||
// * query()
|
||||
|
||||
// getAll
|
||||
|
@ -135,13 +136,25 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
|||
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'
|
||||
|
|
|
@ -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<Orm> {
|
|||
..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<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) {
|
||||
/*
|
||||
@override
|
||||
|
@ -112,32 +193,111 @@ class PostgreSqlOrmGenerator extends GeneratorForAnnotation<Orm> {
|
|||
..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;
|
||||
|
||||
for (var field in ctx.buildContext.fields) {
|
||||
if (i > 0) queryString.write(', ');
|
||||
queryString.write(ctx.buildContext.resolveFieldName(field.name));
|
||||
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')])
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
Method buildGetAll(OrmBuildContext ctx) {
|
||||
/*
|
||||
@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());'));
|
||||
}
|
||||
|
||||
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<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));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ abstract class AuthorOrm {
|
|||
_PostgreSqlAuthorOrmImpl;
|
||||
|
||||
Future<List<Author>> getAll();
|
||||
Future<Author> getById(id);
|
||||
Future<Author> update(Author model);
|
||||
Future<Author> getById(String id);
|
||||
Future<Author> createAuthor(Author model);
|
||||
Future<Author> updateAuthor(Author model);
|
||||
AuthorQuery query();
|
||||
}
|
||||
|
||||
|
|
|
@ -20,10 +20,46 @@ class _PostgreSqlAuthorOrmImpl implements AuthorOrm {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<Author> getById(id) async {
|
||||
Future<Author> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ abstract class CarOrm {
|
|||
_PostgreSqlCarOrmImpl;
|
||||
|
||||
Future<List<Car>> getAll();
|
||||
Future<Car> getById(id);
|
||||
Future<Car> update(Car model);
|
||||
Future<Car> getById(String id);
|
||||
Future<Car> createCar(Car model);
|
||||
Future<Car> updateCar(Car model);
|
||||
CarQuery query();
|
||||
}
|
||||
|
||||
|
|
|
@ -23,10 +23,52 @@ class _PostgreSqlCarOrmImpl implements CarOrm {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<Car> getById() async {
|
||||
Future<Car> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ abstract class CustomerOrm {
|
|||
_PostgreSqlCustomerOrmImpl;
|
||||
|
||||
Future<List<Customer>> getAll();
|
||||
Future<Customer> getById(id);
|
||||
Future<Customer> update(Customer model);
|
||||
Future<Customer> getById(String id);
|
||||
Future<Customer> createCustomer(Customer model);
|
||||
Future<Customer> updateCustomer(Customer model);
|
||||
CustomerQuery query();
|
||||
}
|
||||
|
||||
|
|
|
@ -19,10 +19,44 @@ class _PostgreSqlCustomerOrmImpl implements CustomerOrm {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<Customer> getById() async {
|
||||
Future<Customer> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ abstract class FootOrm {
|
|||
_PostgreSqlFootOrmImpl;
|
||||
|
||||
Future<List<Foot>> getAll();
|
||||
Future<Foot> getById(id);
|
||||
Future<Foot> update(Foot model);
|
||||
Future<Foot> getById(String id);
|
||||
Future<Foot> createFoot(Foot model);
|
||||
Future<Foot> updateFoot(Foot model);
|
||||
FootQuery query();
|
||||
}
|
||||
|
||||
|
|
|
@ -21,10 +21,48 @@ class _PostgreSqlFootOrmImpl implements FootOrm {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<Foot> getById() async {
|
||||
Future<Foot> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ abstract class FruitOrm {
|
|||
_PostgreSqlFruitOrmImpl;
|
||||
|
||||
Future<List<Fruit>> getAll();
|
||||
Future<Fruit> getById(id);
|
||||
Future<Fruit> update(Fruit model);
|
||||
Future<Fruit> getById(String id);
|
||||
Future<Fruit> createFruit(Fruit model);
|
||||
Future<Fruit> updateFruit(Fruit model);
|
||||
FruitQuery query();
|
||||
}
|
||||
|
||||
|
|
|
@ -21,10 +21,48 @@ class _PostgreSqlFruitOrmImpl implements FruitOrm {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<Fruit> getById() async {
|
||||
Future<Fruit> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ abstract class OrderOrm {
|
|||
_PostgreSqlOrderOrmImpl;
|
||||
|
||||
Future<List<Order>> getAll();
|
||||
Future<Order> getById(id);
|
||||
Future<Order> update(Order model);
|
||||
Future<Order> getById(String id);
|
||||
Future<Order> createOrder(Order model);
|
||||
Future<Order> updateOrder(Order model);
|
||||
OrderQuery query();
|
||||
}
|
||||
|
||||
|
|
|
@ -23,10 +23,52 @@ class _PostgreSqlOrderOrmImpl implements OrderOrm {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<Order> getById() async {
|
||||
Future<Order> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ abstract class RoleOrm {
|
|||
_PostgreSqlRoleOrmImpl;
|
||||
|
||||
Future<List<Role>> getAll();
|
||||
Future<Role> getById(id);
|
||||
Future<Role> update(Role model);
|
||||
Future<Role> getById(String id);
|
||||
Future<Role> createRole(Role model);
|
||||
Future<Role> updateRole(Role model);
|
||||
RoleQuery query();
|
||||
}
|
||||
|
||||
|
|
|
@ -20,10 +20,46 @@ class _PostgreSqlRoleOrmImpl implements RoleOrm {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<Role> getById() async {
|
||||
Future<Role> 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<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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue