2.0.0-dev.6
This commit is contained in:
parent
19fd1ed46b
commit
a6eeeb32e8
58 changed files with 314 additions and 932 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
# 2.0.0-dev.6
|
||||||
|
* Add `delete`, `insert` and `update` methods to `Query`.
|
||||||
|
|
||||||
# 2.0.0-dev.4
|
# 2.0.0-dev.4
|
||||||
* Add more querying methods.
|
* Add more querying methods.
|
||||||
* Add preamble to `Query.compile`.
|
* Add preamble to `Query.compile`.
|
||||||
|
|
|
@ -39,6 +39,9 @@ abstract class _Employee extends Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
|
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
|
||||||
|
@override
|
||||||
|
final QueryValues values = new MapQueryValues();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final EmployeeQueryWhere where = new EmployeeQueryWhere();
|
final EmployeeQueryWhere where = new EmployeeQueryWhere();
|
||||||
|
|
||||||
|
|
|
@ -2,4 +2,4 @@ export 'src/annotations.dart';
|
||||||
export 'src/builder.dart';
|
export 'src/builder.dart';
|
||||||
export 'src/migration.dart';
|
export 'src/migration.dart';
|
||||||
export 'src/relations.dart';
|
export 'src/relations.dart';
|
||||||
export 'src/query.dart';
|
export 'src/query.dart';
|
||||||
|
|
|
@ -40,7 +40,8 @@ class Column {
|
||||||
class PrimaryKey extends Column {
|
class PrimaryKey extends Column {
|
||||||
const PrimaryKey({ColumnType columnType})
|
const PrimaryKey({ColumnType columnType})
|
||||||
: super(
|
: super(
|
||||||
type: columnType ?? ColumnType.serial, indexType: IndexType.primaryKey);
|
type: columnType ?? ColumnType.serial,
|
||||||
|
indexType: IndexType.primaryKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Column primaryKey = const PrimaryKey();
|
const Column primaryKey = const PrimaryKey();
|
||||||
|
|
|
@ -9,6 +9,9 @@ abstract class QueryBase<T> {
|
||||||
/// If it's `null`, then this query will perform a `SELECT *`.
|
/// If it's `null`, then this query will perform a `SELECT *`.
|
||||||
List<String> get fields;
|
List<String> get fields;
|
||||||
|
|
||||||
|
/// A String of all [fields], joined by a comma (`,`).
|
||||||
|
String get fieldSet => fields.join(', ');
|
||||||
|
|
||||||
String compile({bool includeTableName: false, String preamble});
|
String compile({bool includeTableName: false, String preamble});
|
||||||
|
|
||||||
T deserialize(List row);
|
T deserialize(List row);
|
||||||
|
@ -42,6 +45,21 @@ class OrderBy {
|
||||||
String compile() => descending ? '$key DESC' : '$key ASC';
|
String compile() => descending ? '$key DESC' : '$key ASC';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String toSql(Object obj) {
|
||||||
|
if (obj is DateTime) {
|
||||||
|
return dateYmdHms.format(obj);
|
||||||
|
} else if (obj is bool) {
|
||||||
|
return obj ? 'TRUE' : 'FALSE';
|
||||||
|
} else if (obj == null) {
|
||||||
|
return 'NULL';
|
||||||
|
} else if (obj is String) {
|
||||||
|
// TODO: Proper escapes
|
||||||
|
return obj;
|
||||||
|
} else {
|
||||||
|
return obj.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A SQL `SELECT` query builder.
|
/// A SQL `SELECT` query builder.
|
||||||
abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
final List<OrderBy> _orderBy = [];
|
final List<OrderBy> _orderBy = [];
|
||||||
|
@ -54,9 +72,14 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
|
|
||||||
/// A reference to an abstract query builder.
|
/// A reference to an abstract query builder.
|
||||||
///
|
///
|
||||||
/// This is often a generated class.
|
/// This is usually a generated class.
|
||||||
Where get where;
|
Where get where;
|
||||||
|
|
||||||
|
/// A set of values, for an insertion or update.
|
||||||
|
///
|
||||||
|
/// This is usually a generated class.
|
||||||
|
QueryValues get values;
|
||||||
|
|
||||||
/// Makes a new [Where] clause.
|
/// Makes a new [Where] clause.
|
||||||
Where newWhereClause() {
|
Where newWhereClause() {
|
||||||
throw new UnsupportedError(
|
throw new UnsupportedError(
|
||||||
|
@ -168,6 +191,12 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
return b.toString();
|
return b.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<T> getOne(QueryExecutor executor) {
|
||||||
|
limit(1);
|
||||||
|
return super.getOne(executor);
|
||||||
|
}
|
||||||
|
|
||||||
Future<List<T>> delete(QueryExecutor executor) async {
|
Future<List<T>> delete(QueryExecutor executor) async {
|
||||||
var sql = compile(preamble: 'DELETE FROM $tableName');
|
var sql = compile(preamble: 'DELETE FROM $tableName');
|
||||||
return executor
|
return executor
|
||||||
|
@ -176,8 +205,88 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<T> deleteOne(QueryExecutor executor) {
|
Future<T> deleteOne(QueryExecutor executor) {
|
||||||
|
limit(1);
|
||||||
return delete(executor).then((it) => it.isEmpty ? null : it.first);
|
return delete(executor).then((it) => it.isEmpty ? null : it.first);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<T> insert(QueryExecutor executor) {
|
||||||
|
var sql = new StringBuffer('INSERT INTO $tableName ($fieldSet)');
|
||||||
|
var valuesClause = values.compileForInsert();
|
||||||
|
|
||||||
|
if (valuesClause == null) {
|
||||||
|
throw new StateError('No values have been specified for update.');
|
||||||
|
} else {
|
||||||
|
sql.write(' $valuesClause');
|
||||||
|
return executor
|
||||||
|
.query(sql.toString(), fields)
|
||||||
|
.then((it) => it.isEmpty ? null : deserialize(it.first));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<T>> update(QueryExecutor executor) async {
|
||||||
|
var sql = new StringBuffer('UPDATE $tableName');
|
||||||
|
var valuesClause = values.compileForUpdate();
|
||||||
|
|
||||||
|
if (valuesClause == null) {
|
||||||
|
throw new StateError('No values have been specified for update.');
|
||||||
|
} else {
|
||||||
|
sql.write(' $valuesClause');
|
||||||
|
var whereClause = where.compile();
|
||||||
|
if (whereClause.isNotEmpty) sql.write(' WHERE $whereClause');
|
||||||
|
if (_limit != null) sql.write(' LIMIT $_limit');
|
||||||
|
return executor
|
||||||
|
.query(sql.toString(), fields)
|
||||||
|
.then((it) => it.map(deserialize).toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<T> updateOne(QueryExecutor executor) {
|
||||||
|
limit(1);
|
||||||
|
return update(executor).then((it) => it.isEmpty ? null : it.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class QueryValues {
|
||||||
|
Map<String, dynamic> toMap();
|
||||||
|
|
||||||
|
String compileForInsert() {
|
||||||
|
var data = toMap();
|
||||||
|
if (data.isEmpty) return null;
|
||||||
|
var b = new StringBuffer('VALUES (');
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for (var entry in data.entries) {
|
||||||
|
if (i++ > 0) b.write(', ');
|
||||||
|
b.write(toSql(entry.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
b.write(')');
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
String compileForUpdate() {
|
||||||
|
var data = toMap();
|
||||||
|
if (data.isEmpty) return null;
|
||||||
|
var b = new StringBuffer('SET');
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for (var entry in data.entries) {
|
||||||
|
if (i++ > 0) b.write(',');
|
||||||
|
b.write(' ');
|
||||||
|
b.write(entry.key);
|
||||||
|
b.write('=');
|
||||||
|
b.write(toSql(entry.value));
|
||||||
|
}
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A [QueryValues] implementation that simply writes to a [Map].
|
||||||
|
class MapQueryValues extends QueryValues {
|
||||||
|
final Map<String, dynamic> values = {};
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, dynamic> toMap() => values;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds a SQL `WHERE` clause.
|
/// Builds a SQL `WHERE` clause.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_orm
|
name: angel_orm
|
||||||
version: 2.0.0-dev.5
|
version: 2.0.0-dev.6
|
||||||
description: Runtime support for Angel's ORM.
|
description: Runtime support for Angel's ORM.
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
homepage: https://github.com/angel-dart/orm
|
homepage: https://github.com/angel-dart/orm
|
||||||
|
|
|
@ -10,3 +10,21 @@ builders:
|
||||||
- ".angel_orm.g.part"
|
- ".angel_orm.g.part"
|
||||||
applies_builders:
|
applies_builders:
|
||||||
["source_gen|combining_builder", "source_gen|part_cleanup"]
|
["source_gen|combining_builder", "source_gen|part_cleanup"]
|
||||||
|
targets:
|
||||||
|
_standalone:
|
||||||
|
sources:
|
||||||
|
- test/models/author.dart
|
||||||
|
- test/models/car.dart
|
||||||
|
- test/models/customer.dart
|
||||||
|
- test/models/foot.dart
|
||||||
|
- test/models/fruit.dart
|
||||||
|
- test/models/role.dart
|
||||||
|
$default:
|
||||||
|
dependencies:
|
||||||
|
- :_standalone
|
||||||
|
sources:
|
||||||
|
- test/models/book.dart
|
||||||
|
- test/models/leg.dart
|
||||||
|
- test/models/order.dart
|
||||||
|
- test/models/tree.dart
|
||||||
|
- test/models/user.dart
|
|
@ -1,4 +1,4 @@
|
||||||
//export 'src/mongodb_orm_generator.dart';
|
//export 'src/mongodb_orm_generator.dart';
|
||||||
export 'src/orm_build_context.dart';
|
export 'src/orm_build_context.dart';
|
||||||
export 'src/orm_generator.dart';
|
export 'src/orm_generator.dart';
|
||||||
export 'src/readers.dart';
|
export 'src/readers.dart';
|
||||||
|
|
|
@ -86,10 +86,8 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
..annotations.add(refer('override'))
|
..annotations.add(refer('override'))
|
||||||
..type = MethodType.getter
|
..type = MethodType.getter
|
||||||
..body = new Block((b) {
|
..body = new Block((b) {
|
||||||
var names = ctx.buildContext.fields
|
b.addExpression(
|
||||||
.map((f) => literalString(f.name))
|
refer('${rc.pascalCase}Fields').property('allFields').returned);
|
||||||
.toList();
|
|
||||||
b.addExpression(literalConstList(names).returned);
|
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -116,8 +114,16 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
var args = <String, Expression>{};
|
var args = <String, Expression>{};
|
||||||
|
|
||||||
for (var field in ctx.buildContext.fields) {
|
for (var field in ctx.buildContext.fields) {
|
||||||
var type = convertTypeReference(field.type);
|
Reference type = convertTypeReference(field.type);
|
||||||
args[field.name] = (refer('row').index(literalNum(i))).asA(type);
|
if (isSpecialId(field)) type = refer('int');
|
||||||
|
|
||||||
|
var expr = (refer('row').index(literalNum(i++)));
|
||||||
|
if (isSpecialId(field))
|
||||||
|
expr = expr.property('toString').call([]);
|
||||||
|
else
|
||||||
|
expr = expr.asA(type);
|
||||||
|
|
||||||
|
args[field.name] = expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
b.addExpression(
|
b.addExpression(
|
||||||
|
@ -127,6 +133,10 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isSpecialId(FieldElement field) {
|
||||||
|
return (field.name == 'id' && autoIdAndDateFields);
|
||||||
|
}
|
||||||
|
|
||||||
Class buildWhereClass(OrmBuildContext ctx) {
|
Class buildWhereClass(OrmBuildContext ctx) {
|
||||||
return new Class((clazz) {
|
return new Class((clazz) {
|
||||||
var rc = ctx.buildContext.modelClassNameRecase;
|
var rc = ctx.buildContext.modelClassNameRecase;
|
||||||
|
@ -151,7 +161,14 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
// TODO: Handle fields with relations
|
// TODO: Handle fields with relations
|
||||||
Reference builderType;
|
Reference builderType;
|
||||||
|
|
||||||
if (const TypeChecker.fromRuntime(String).isExactlyType(field.type)) {
|
if (const TypeChecker.fromRuntime(int).isExactlyType(field.type) ||
|
||||||
|
const TypeChecker.fromRuntime(double).isExactlyType(field.type) ||
|
||||||
|
isSpecialId(field)) {
|
||||||
|
builderType = new TypeReference((b) => b
|
||||||
|
..symbol = 'NumericSqlExpressionBuilder'
|
||||||
|
..types.add(refer(isSpecialId(field) ? 'int' : field.type.name)));
|
||||||
|
} else if (const TypeChecker.fromRuntime(String)
|
||||||
|
.isExactlyType(field.type)) {
|
||||||
builderType = refer('StringSqlExpressionBuilder');
|
builderType = refer('StringSqlExpressionBuilder');
|
||||||
} else if (const TypeChecker.fromRuntime(bool)
|
} else if (const TypeChecker.fromRuntime(bool)
|
||||||
.isExactlyType(field.type)) {
|
.isExactlyType(field.type)) {
|
||||||
|
@ -159,12 +176,6 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
} else if (const TypeChecker.fromRuntime(DateTime)
|
} else if (const TypeChecker.fromRuntime(DateTime)
|
||||||
.isExactlyType(field.type)) {
|
.isExactlyType(field.type)) {
|
||||||
builderType = refer('DateTimeSqlExpressionBuilder');
|
builderType = refer('DateTimeSqlExpressionBuilder');
|
||||||
} else if (const TypeChecker.fromRuntime(int)
|
|
||||||
.isExactlyType(field.type) ||
|
|
||||||
const TypeChecker.fromRuntime(double).isExactlyType(field.type)) {
|
|
||||||
builderType = new TypeReference((b) => b
|
|
||||||
..symbol = 'NumericSqlExpressionBuilder'
|
|
||||||
..types.add(refer(field.type.name)));
|
|
||||||
} else {
|
} else {
|
||||||
throw new UnsupportedError(
|
throw new UnsupportedError(
|
||||||
'Cannot generate ORM code for field of type ${field.type.name}.');
|
'Cannot generate ORM code for field of type ${field.type.name}.');
|
||||||
|
|
|
@ -24,5 +24,6 @@ dev_dependencies:
|
||||||
#angel_migration: ^1.0.0-alpha
|
#angel_migration: ^1.0.0-alpha
|
||||||
#angel_test: ^1.0.0
|
#angel_test: ^1.0.0
|
||||||
build_runner: ^1.0.0
|
build_runner: ^1.0.0
|
||||||
|
collection: ^1.0.0
|
||||||
postgres: ^1.0.0
|
postgres: ^1.0.0
|
||||||
test: ^1.0.0
|
test: ^1.0.0
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
/// Tests for @belongsTo...
|
/// Tests for @belongsTo...
|
||||||
library angel_orm_generator.test.book_test;
|
library angel_orm_generator.test.book_test;
|
||||||
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/author.dart';
|
import 'models/author.dart';
|
||||||
import 'models/author.orm.g.dart';
|
|
||||||
import 'models/book.dart';
|
import 'models/book.dart';
|
||||||
import 'models/book.orm.g.dart';
|
|
||||||
import 'common.dart';
|
import 'common.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
PostgreSQLConnection connection;
|
PostgresExecutor connection;
|
||||||
Author rowling;
|
Author rowling;
|
||||||
Author jameson;
|
Author jameson;
|
||||||
Book deathlyHallows;
|
Book deathlyHallows;
|
||||||
|
@ -24,13 +21,14 @@ main() {
|
||||||
|
|
||||||
// And a book
|
// And a book
|
||||||
deathlyHallows = await BookQuery.insert(connection,
|
deathlyHallows = await BookQuery.insert(connection,
|
||||||
authorId: int.parse(rowling.id), name: 'Deathly Hallows', partnerAuthorId: int.parse(jameson.id));
|
authorId: int.parse(rowling.id),
|
||||||
|
name: 'Deathly Hallows',
|
||||||
|
partnerAuthorId: int.parse(jameson.id));
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() => connection.close());
|
tearDown(() => connection.close());
|
||||||
|
|
||||||
group('selects', ()
|
group('selects', () {
|
||||||
{
|
|
||||||
test('select all', () async {
|
test('select all', () async {
|
||||||
var query = new BookQuery();
|
var query = new BookQuery();
|
||||||
var books = await query.get(connection).toList();
|
var books = await query.get(connection).toList();
|
||||||
|
@ -52,8 +50,8 @@ main() {
|
||||||
query.where.id.equals(int.parse(deathlyHallows.id));
|
query.where.id.equals(int.parse(deathlyHallows.id));
|
||||||
print(query.toSql());
|
print(query.toSql());
|
||||||
|
|
||||||
var book = await BookQuery.getOne(
|
var book =
|
||||||
int.parse(deathlyHallows.id), connection);
|
await BookQuery.getOne(int.parse(deathlyHallows.id), connection);
|
||||||
print(book.toJson());
|
print(book.toJson());
|
||||||
expect(book.id, deathlyHallows.id);
|
expect(book.id, deathlyHallows.id);
|
||||||
expect(book.name, deathlyHallows.name);
|
expect(book.name, deathlyHallows.name);
|
||||||
|
@ -85,10 +83,8 @@ main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('union', () async {
|
test('union', () async {
|
||||||
var query1 = new BookQuery()
|
var query1 = new BookQuery()..where.name.like('Deathly%');
|
||||||
..where.name.like('Deathly%');
|
var query2 = new BookQuery()..where.authorId.equals(-1);
|
||||||
var query2 = new BookQuery()
|
|
||||||
..where.authorId.equals(-1);
|
|
||||||
var query3 = new BookQuery()
|
var query3 = new BookQuery()
|
||||||
..where.name.isIn(['Goblet of Fire', 'Order of the Phoenix']);
|
..where.name.isIn(['Goblet of Fire', 'Order of the Phoenix']);
|
||||||
query1
|
query1
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:postgres/postgres.dart';
|
import 'package:postgres/postgres.dart';
|
||||||
|
|
||||||
Future<PostgreSQLConnection> connectToPostgres(Iterable<String> schemas) async {
|
Future<PostgresExecutor> connectToPostgres(Iterable<String> schemas) async {
|
||||||
var conn = new PostgreSQLConnection('127.0.0.1', 5432, 'angel_orm_test',
|
var conn = new PostgreSQLConnection('127.0.0.1', 5432, 'angel_orm_test',
|
||||||
username: Platform.environment['POSTGRES_USERNAME'] ?? 'postgres',
|
username: Platform.environment['POSTGRES_USERNAME'] ?? 'postgres',
|
||||||
password: Platform.environment['POSTGRES_PASSWORD'] ?? 'password');
|
password: Platform.environment['POSTGRES_PASSWORD'] ?? 'password');
|
||||||
|
@ -12,5 +13,20 @@ Future<PostgreSQLConnection> connectToPostgres(Iterable<String> schemas) async {
|
||||||
await conn
|
await conn
|
||||||
.execute(await new File('test/models/$s.up.g.sql').readAsString());
|
.execute(await new File('test/models/$s.up.g.sql').readAsString());
|
||||||
|
|
||||||
return conn;
|
return new PostgresExecutor(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
class PostgresExecutor extends QueryExecutor {
|
||||||
|
final PostgreSQLConnection connection;
|
||||||
|
|
||||||
|
PostgresExecutor(this.connection);
|
||||||
|
|
||||||
|
Future close() => connection.close();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<List>> query(String query, List<String> returningFields) {
|
||||||
|
var fields = returningFields.join(', ');
|
||||||
|
var returning = 'RETURNING ($fields)';
|
||||||
|
return connection.query('$query $returning');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:postgres/postgres.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import '../models/car.dart';
|
import '../models/car.dart';
|
||||||
import '../models/car.orm.g.dart';
|
|
||||||
|
|
||||||
@Expose('/api/cars')
|
@Expose('/api/cars')
|
||||||
class CarController extends Controller {
|
class CarController extends Controller {
|
||||||
@Expose('/luxury')
|
@Expose('/luxury')
|
||||||
Stream<Car> getLuxuryCars(PostgreSQLConnection connection) {
|
Future<List<Car>> getLuxuryCars(QueryExecutor connection) {
|
||||||
var query = new CarQuery();
|
var query = new CarQuery();
|
||||||
query.where
|
query.where
|
||||||
..familyFriendly.equals(false)
|
..familyFriendly.equals(false)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/// Tests for @hasOne...
|
/// Tests for @hasOne...
|
||||||
library angel_orm_generator.test.has_one_test;
|
library angel_orm_generator.test.has_one_test;
|
||||||
|
|
||||||
import 'package:postgres/postgres.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/foot.orm.g.dart';
|
import 'models/foot.orm.g.dart';
|
||||||
import 'models/leg.dart';
|
import 'models/leg.dart';
|
||||||
|
@ -9,7 +9,7 @@ import 'models/leg.orm.g.dart';
|
||||||
import 'common.dart';
|
import 'common.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
PostgreSQLConnection connection;
|
QueryExecutor connection;
|
||||||
Leg originalLeg;
|
Leg originalLeg;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
|
|
|
@ -17,21 +17,22 @@ class AuthorQuery extends Query<Author, AuthorQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get fields {
|
get fields {
|
||||||
return const ['id', 'name', 'createdAt', 'updatedAt'];
|
return AuthorFields.allFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
deserialize(List row) {
|
deserialize(List row) {
|
||||||
return new Author(
|
return new Author(
|
||||||
id: (row[0] as String),
|
id: row[0].toString(),
|
||||||
name: (row[0] as String),
|
name: (row[1] as String),
|
||||||
createdAt: (row[0] as DateTime),
|
createdAt: (row[2] as DateTime),
|
||||||
updatedAt: (row[0] as DateTime));
|
updatedAt: (row[3] as DateTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AuthorQueryWhere extends QueryWhere {
|
class AuthorQueryWhere extends QueryWhere {
|
||||||
final StringSqlExpressionBuilder id = new StringSqlExpressionBuilder('id');
|
final NumericSqlExpressionBuilder<int> id =
|
||||||
|
new NumericSqlExpressionBuilder<int>('id');
|
||||||
|
|
||||||
final StringSqlExpressionBuilder name =
|
final StringSqlExpressionBuilder name =
|
||||||
new StringSqlExpressionBuilder('name');
|
new StringSqlExpressionBuilder('name');
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
import 'author.dart';
|
|
||||||
import 'dart:async';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
part 'author.postgresql.orm.g.dart';
|
|
||||||
|
|
||||||
abstract class AuthorOrm {
|
|
||||||
factory AuthorOrm.postgreSql(PostgreSQLConnection connection) =
|
|
||||||
PostgreSqlAuthorOrm;
|
|
||||||
|
|
||||||
Future<List<Author>> getAll();
|
|
||||||
Future<Author> getById(String id);
|
|
||||||
Future<Author> deleteById(String id);
|
|
||||||
Future<Author> createAuthor(Author model);
|
|
||||||
Future<Author> updateAuthor(Author model);
|
|
||||||
AuthorQuery query();
|
|
||||||
}
|
|
||||||
|
|
||||||
class AuthorQuery {}
|
|
|
@ -1,77 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
part of 'author.orm.g.dart';
|
|
||||||
|
|
||||||
class PostgreSqlAuthorOrm implements AuthorOrm {
|
|
||||||
PostgreSqlAuthorOrm(this.connection);
|
|
||||||
|
|
||||||
final PostgreSQLConnection connection;
|
|
||||||
|
|
||||||
static Author parseRow(List row) {
|
|
||||||
return new Author(
|
|
||||||
id: (row[0] as String),
|
|
||||||
name: (row[1] as String),
|
|
||||||
createdAt: (row[2] as DateTime),
|
|
||||||
updatedAt: (row[3] as DateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Author> getById(String id) async {
|
|
||||||
var r = await connection.query(
|
|
||||||
'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<Author> 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<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);
|
|
||||||
}
|
|
||||||
|
|
||||||
AuthorQuery query() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -21,10 +21,10 @@ class Book extends _Book {
|
||||||
final String id;
|
final String id;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final dynamic author;
|
final Author author;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final dynamic partnerAuthor;
|
final Author partnerAuthor;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final int authorId;
|
final int authorId;
|
||||||
|
@ -40,8 +40,8 @@ class Book extends _Book {
|
||||||
|
|
||||||
Book copyWith(
|
Book copyWith(
|
||||||
{String id,
|
{String id,
|
||||||
dynamic author,
|
Author author,
|
||||||
dynamic partnerAuthor,
|
Author partnerAuthor,
|
||||||
int authorId,
|
int authorId,
|
||||||
String name,
|
String name,
|
||||||
DateTime createdAt,
|
DateTime createdAt,
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "author" with type "Author".
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "author" with type "Author".
|
|
|
@ -10,8 +10,12 @@ abstract class BookSerializer {
|
||||||
static Book fromMap(Map map) {
|
static Book fromMap(Map map) {
|
||||||
return new Book(
|
return new Book(
|
||||||
id: map['id'] as String,
|
id: map['id'] as String,
|
||||||
author: map['author'] as dynamic,
|
author: map['author'] != null
|
||||||
partnerAuthor: map['partner_author'] as dynamic,
|
? AuthorSerializer.fromMap(map['author'] as Map)
|
||||||
|
: null,
|
||||||
|
partnerAuthor: map['partner_author'] != null
|
||||||
|
? AuthorSerializer.fromMap(map['partner_author'] as Map)
|
||||||
|
: null,
|
||||||
authorId: map['author_id'] as int,
|
authorId: map['author_id'] as int,
|
||||||
name: map['name'] as String,
|
name: map['name'] as String,
|
||||||
createdAt: map['created_at'] != null
|
createdAt: map['created_at'] != null
|
||||||
|
@ -32,8 +36,8 @@ abstract class BookSerializer {
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
'id': model.id,
|
'id': model.id,
|
||||||
'author': model.author,
|
'author': AuthorSerializer.toMap(model.author),
|
||||||
'partner_author': model.partnerAuthor,
|
'partner_author': AuthorSerializer.toMap(model.partnerAuthor),
|
||||||
'author_id': model.authorId,
|
'author_id': model.authorId,
|
||||||
'name': model.name,
|
'name': model.name,
|
||||||
'created_at': model.createdAt?.toIso8601String(),
|
'created_at': model.createdAt?.toIso8601String(),
|
||||||
|
|
|
@ -17,32 +17,25 @@ class CarQuery extends Query<Car, CarQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get fields {
|
get fields {
|
||||||
return const [
|
return CarFields.allFields;
|
||||||
'id',
|
|
||||||
'make',
|
|
||||||
'description',
|
|
||||||
'familyFriendly',
|
|
||||||
'recalledAt',
|
|
||||||
'createdAt',
|
|
||||||
'updatedAt'
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
deserialize(List row) {
|
deserialize(List row) {
|
||||||
return new Car(
|
return new Car(
|
||||||
id: (row[0] as String),
|
id: row[0].toString(),
|
||||||
make: (row[0] as String),
|
make: (row[1] as String),
|
||||||
description: (row[0] as String),
|
description: (row[2] as String),
|
||||||
familyFriendly: (row[0] as bool),
|
familyFriendly: (row[3] as bool),
|
||||||
recalledAt: (row[0] as DateTime),
|
recalledAt: (row[4] as DateTime),
|
||||||
createdAt: (row[0] as DateTime),
|
createdAt: (row[5] as DateTime),
|
||||||
updatedAt: (row[0] as DateTime));
|
updatedAt: (row[6] as DateTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CarQueryWhere extends QueryWhere {
|
class CarQueryWhere extends QueryWhere {
|
||||||
final StringSqlExpressionBuilder id = new StringSqlExpressionBuilder('id');
|
final NumericSqlExpressionBuilder<int> id =
|
||||||
|
new NumericSqlExpressionBuilder<int>('id');
|
||||||
|
|
||||||
final StringSqlExpressionBuilder make =
|
final StringSqlExpressionBuilder make =
|
||||||
new StringSqlExpressionBuilder('make');
|
new StringSqlExpressionBuilder('make');
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
import 'car.dart';
|
|
||||||
import 'dart:async';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
part 'car.postgresql.orm.g.dart';
|
|
||||||
|
|
||||||
abstract class CarOrm {
|
|
||||||
factory CarOrm.postgreSql(PostgreSQLConnection connection) = PostgreSqlCarOrm;
|
|
||||||
|
|
||||||
Future<List<Car>> getAll();
|
|
||||||
Future<Car> getById(String id);
|
|
||||||
Future<Car> deleteById(String id);
|
|
||||||
Future<Car> createCar(Car model);
|
|
||||||
Future<Car> updateCar(Car model);
|
|
||||||
CarQuery query();
|
|
||||||
}
|
|
||||||
|
|
||||||
class CarQuery {}
|
|
|
@ -1,86 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
part of 'car.orm.g.dart';
|
|
||||||
|
|
||||||
class PostgreSqlCarOrm implements CarOrm {
|
|
||||||
PostgreSqlCarOrm(this.connection);
|
|
||||||
|
|
||||||
final PostgreSQLConnection connection;
|
|
||||||
|
|
||||||
static Car parseRow(List row) {
|
|
||||||
return new Car(
|
|
||||||
id: (row[0] as String),
|
|
||||||
make: (row[1] as String),
|
|
||||||
description: (row[2] as String),
|
|
||||||
familyFriendly: (row[3] as bool),
|
|
||||||
recalledAt: (row[4] as DateTime),
|
|
||||||
createdAt: (row[5] as DateTime),
|
|
||||||
updatedAt: (row[6] as DateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Car> getById(String id) async {
|
|
||||||
var r = await connection.query(
|
|
||||||
'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<Car> 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<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);
|
|
||||||
}
|
|
||||||
|
|
||||||
CarQuery query() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,5 +8,4 @@ part 'customer.serializer.g.dart';
|
||||||
|
|
||||||
@orm
|
@orm
|
||||||
@serializable
|
@serializable
|
||||||
class _Customer extends Model {
|
class _Customer extends Model {}
|
||||||
}
|
|
||||||
|
|
|
@ -17,20 +17,21 @@ class CustomerQuery extends Query<Customer, CustomerQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get fields {
|
get fields {
|
||||||
return const ['id', 'createdAt', 'updatedAt'];
|
return CustomerFields.allFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
deserialize(List row) {
|
deserialize(List row) {
|
||||||
return new Customer(
|
return new Customer(
|
||||||
id: (row[0] as String),
|
id: row[0].toString(),
|
||||||
createdAt: (row[0] as DateTime),
|
createdAt: (row[1] as DateTime),
|
||||||
updatedAt: (row[0] as DateTime));
|
updatedAt: (row[2] as DateTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CustomerQueryWhere extends QueryWhere {
|
class CustomerQueryWhere extends QueryWhere {
|
||||||
final StringSqlExpressionBuilder id = new StringSqlExpressionBuilder('id');
|
final NumericSqlExpressionBuilder<int> id =
|
||||||
|
new NumericSqlExpressionBuilder<int>('id');
|
||||||
|
|
||||||
final DateTimeSqlExpressionBuilder createdAt =
|
final DateTimeSqlExpressionBuilder createdAt =
|
||||||
new DateTimeSqlExpressionBuilder('created_at');
|
new DateTimeSqlExpressionBuilder('created_at');
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
import 'customer.dart';
|
|
||||||
import 'dart:async';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
part 'customer.postgresql.orm.g.dart';
|
|
||||||
|
|
||||||
abstract class CustomerOrm {
|
|
||||||
factory CustomerOrm.postgreSql(PostgreSQLConnection connection) =
|
|
||||||
PostgreSqlCustomerOrm;
|
|
||||||
|
|
||||||
Future<List<Customer>> getAll();
|
|
||||||
Future<Customer> getById(String id);
|
|
||||||
Future<Customer> deleteById(String id);
|
|
||||||
Future<Customer> createCustomer(Customer model);
|
|
||||||
Future<Customer> updateCustomer(Customer model);
|
|
||||||
CustomerQuery query();
|
|
||||||
}
|
|
||||||
|
|
||||||
class CustomerQuery {}
|
|
|
@ -1,74 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
part of 'customer.orm.g.dart';
|
|
||||||
|
|
||||||
class PostgreSqlCustomerOrm implements CustomerOrm {
|
|
||||||
PostgreSqlCustomerOrm(this.connection);
|
|
||||||
|
|
||||||
final PostgreSQLConnection connection;
|
|
||||||
|
|
||||||
static Customer parseRow(List row) {
|
|
||||||
return new Customer(
|
|
||||||
id: (row[0] as String),
|
|
||||||
createdAt: (row[1] as DateTime),
|
|
||||||
updatedAt: (row[2] as DateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Customer> getById(String id) async {
|
|
||||||
var r = await connection.query(
|
|
||||||
'SELECT id, created_at, updated_at FROM "customers" WHERE id = @id LIMIT 1;',
|
|
||||||
substitutionValues: {'id': int.parse(id)});
|
|
||||||
return parseRow(r.first);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Customer> 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<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);
|
|
||||||
}
|
|
||||||
|
|
||||||
CustomerQuery query() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,4 +10,4 @@ part 'foot.serializer.g.dart';
|
||||||
@orm
|
@orm
|
||||||
class _Foot extends Model {
|
class _Foot extends Model {
|
||||||
int legId, nToes;
|
int legId, nToes;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,22 +17,23 @@ class FootQuery extends Query<Foot, FootQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get fields {
|
get fields {
|
||||||
return const ['id', 'legId', 'nToes', 'createdAt', 'updatedAt'];
|
return FootFields.allFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
deserialize(List row) {
|
deserialize(List row) {
|
||||||
return new Foot(
|
return new Foot(
|
||||||
id: (row[0] as String),
|
id: row[0].toString(),
|
||||||
legId: (row[0] as int),
|
legId: (row[1] as int),
|
||||||
nToes: (row[0] as int),
|
nToes: (row[2] as int),
|
||||||
createdAt: (row[0] as DateTime),
|
createdAt: (row[3] as DateTime),
|
||||||
updatedAt: (row[0] as DateTime));
|
updatedAt: (row[4] as DateTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FootQueryWhere extends QueryWhere {
|
class FootQueryWhere extends QueryWhere {
|
||||||
final StringSqlExpressionBuilder id = new StringSqlExpressionBuilder('id');
|
final NumericSqlExpressionBuilder<int> id =
|
||||||
|
new NumericSqlExpressionBuilder<int>('id');
|
||||||
|
|
||||||
final NumericSqlExpressionBuilder<int> legId =
|
final NumericSqlExpressionBuilder<int> legId =
|
||||||
new NumericSqlExpressionBuilder<int>('leg_id');
|
new NumericSqlExpressionBuilder<int>('leg_id');
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
import 'dart:async';
|
|
||||||
import 'foot.dart';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
part 'foot.postgresql.orm.g.dart';
|
|
||||||
|
|
||||||
abstract class FootOrm {
|
|
||||||
factory FootOrm.postgreSql(PostgreSQLConnection connection) =
|
|
||||||
PostgreSqlFootOrm;
|
|
||||||
|
|
||||||
Future<List<Foot>> getAll();
|
|
||||||
Future<Foot> getById(String id);
|
|
||||||
Future<Foot> deleteById(String id);
|
|
||||||
Future<Foot> createFoot(Foot model);
|
|
||||||
Future<Foot> updateFoot(Foot model);
|
|
||||||
FootQuery query();
|
|
||||||
}
|
|
||||||
|
|
||||||
class FootQuery {}
|
|
|
@ -1,80 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
part of 'foot.orm.g.dart';
|
|
||||||
|
|
||||||
class PostgreSqlFootOrm implements FootOrm {
|
|
||||||
PostgreSqlFootOrm(this.connection);
|
|
||||||
|
|
||||||
final PostgreSQLConnection connection;
|
|
||||||
|
|
||||||
static Foot parseRow(List row) {
|
|
||||||
return new Foot(
|
|
||||||
id: (row[0] as String),
|
|
||||||
legId: (row[1] as int),
|
|
||||||
nToes: (row[2] as int),
|
|
||||||
createdAt: (row[3] as DateTime),
|
|
||||||
updatedAt: (row[4] as DateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Foot> getById(String id) async {
|
|
||||||
var r = await connection.query(
|
|
||||||
'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<Foot> 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<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);
|
|
||||||
}
|
|
||||||
|
|
||||||
FootQuery query() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,4 +11,4 @@ part 'fruit.serializer.g.dart';
|
||||||
class _Fruit extends Model {
|
class _Fruit extends Model {
|
||||||
int treeId;
|
int treeId;
|
||||||
String commonName;
|
String commonName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,22 +17,23 @@ class FruitQuery extends Query<Fruit, FruitQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get fields {
|
get fields {
|
||||||
return const ['id', 'treeId', 'commonName', 'createdAt', 'updatedAt'];
|
return FruitFields.allFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
deserialize(List row) {
|
deserialize(List row) {
|
||||||
return new Fruit(
|
return new Fruit(
|
||||||
id: (row[0] as String),
|
id: row[0].toString(),
|
||||||
treeId: (row[0] as int),
|
treeId: (row[1] as int),
|
||||||
commonName: (row[0] as String),
|
commonName: (row[2] as String),
|
||||||
createdAt: (row[0] as DateTime),
|
createdAt: (row[3] as DateTime),
|
||||||
updatedAt: (row[0] as DateTime));
|
updatedAt: (row[4] as DateTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FruitQueryWhere extends QueryWhere {
|
class FruitQueryWhere extends QueryWhere {
|
||||||
final StringSqlExpressionBuilder id = new StringSqlExpressionBuilder('id');
|
final NumericSqlExpressionBuilder<int> id =
|
||||||
|
new NumericSqlExpressionBuilder<int>('id');
|
||||||
|
|
||||||
final NumericSqlExpressionBuilder<int> treeId =
|
final NumericSqlExpressionBuilder<int> treeId =
|
||||||
new NumericSqlExpressionBuilder<int>('tree_id');
|
new NumericSqlExpressionBuilder<int>('tree_id');
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
import 'dart:async';
|
|
||||||
import 'fruit.dart';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
part 'fruit.postgresql.orm.g.dart';
|
|
||||||
|
|
||||||
abstract class FruitOrm {
|
|
||||||
factory FruitOrm.postgreSql(PostgreSQLConnection connection) =
|
|
||||||
PostgreSqlFruitOrm;
|
|
||||||
|
|
||||||
Future<List<Fruit>> getAll();
|
|
||||||
Future<Fruit> getById(String id);
|
|
||||||
Future<Fruit> deleteById(String id);
|
|
||||||
Future<Fruit> createFruit(Fruit model);
|
|
||||||
Future<Fruit> updateFruit(Fruit model);
|
|
||||||
FruitQuery query();
|
|
||||||
}
|
|
||||||
|
|
||||||
class FruitQuery {}
|
|
|
@ -1,80 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
part of 'fruit.orm.g.dart';
|
|
||||||
|
|
||||||
class PostgreSqlFruitOrm implements FruitOrm {
|
|
||||||
PostgreSqlFruitOrm(this.connection);
|
|
||||||
|
|
||||||
final PostgreSQLConnection connection;
|
|
||||||
|
|
||||||
static Fruit parseRow(List row) {
|
|
||||||
return new Fruit(
|
|
||||||
id: (row[0] as String),
|
|
||||||
treeId: (row[1] as int),
|
|
||||||
commonName: (row[2] as String),
|
|
||||||
createdAt: (row[3] as DateTime),
|
|
||||||
updatedAt: (row[4] as DateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Fruit> getById(String id) async {
|
|
||||||
var r = await connection.query(
|
|
||||||
'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<Fruit> 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<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);
|
|
||||||
}
|
|
||||||
|
|
||||||
FruitQuery query() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -14,4 +14,4 @@ class _Leg extends Model {
|
||||||
Foot foot;
|
Foot foot;
|
||||||
|
|
||||||
String name;
|
String name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ class Leg extends _Leg {
|
||||||
final String id;
|
final String id;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final dynamic foot;
|
final Foot foot;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final String name;
|
final String name;
|
||||||
|
@ -27,7 +27,7 @@ class Leg extends _Leg {
|
||||||
|
|
||||||
Leg copyWith(
|
Leg copyWith(
|
||||||
{String id,
|
{String id,
|
||||||
dynamic foot,
|
Foot foot,
|
||||||
String name,
|
String name,
|
||||||
DateTime createdAt,
|
DateTime createdAt,
|
||||||
DateTime updatedAt}) {
|
DateTime updatedAt}) {
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "foot" with type "Foot".
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "foot" with type "Foot".
|
|
|
@ -10,7 +10,9 @@ abstract class LegSerializer {
|
||||||
static Leg fromMap(Map map) {
|
static Leg fromMap(Map map) {
|
||||||
return new Leg(
|
return new Leg(
|
||||||
id: map['id'] as String,
|
id: map['id'] as String,
|
||||||
foot: map['foot'] as dynamic,
|
foot: map['foot'] != null
|
||||||
|
? FootSerializer.fromMap(map['foot'] as Map)
|
||||||
|
: null,
|
||||||
name: map['name'] as String,
|
name: map['name'] as String,
|
||||||
createdAt: map['created_at'] != null
|
createdAt: map['created_at'] != null
|
||||||
? (map['created_at'] is DateTime
|
? (map['created_at'] is DateTime
|
||||||
|
@ -30,7 +32,7 @@ abstract class LegSerializer {
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
'id': model.id,
|
'id': model.id,
|
||||||
'foot': model.foot,
|
'foot': FootSerializer.toMap(model.foot),
|
||||||
'name': model.name,
|
'name': model.name,
|
||||||
'created_at': model.createdAt?.toIso8601String(),
|
'created_at': model.createdAt?.toIso8601String(),
|
||||||
'updated_at': model.updatedAt?.toIso8601String()
|
'updated_at': model.updatedAt?.toIso8601String()
|
||||||
|
|
|
@ -17,32 +17,25 @@ class OrderQuery extends Query<Order, OrderQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get fields {
|
get fields {
|
||||||
return const [
|
return OrderFields.allFields;
|
||||||
'id',
|
|
||||||
'customerId',
|
|
||||||
'employeeId',
|
|
||||||
'orderDate',
|
|
||||||
'shipperId',
|
|
||||||
'createdAt',
|
|
||||||
'updatedAt'
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
deserialize(List row) {
|
deserialize(List row) {
|
||||||
return new Order(
|
return new Order(
|
||||||
id: (row[0] as String),
|
id: row[0].toString(),
|
||||||
customerId: (row[0] as int),
|
customerId: (row[1] as int),
|
||||||
employeeId: (row[0] as int),
|
employeeId: (row[2] as int),
|
||||||
orderDate: (row[0] as DateTime),
|
orderDate: (row[3] as DateTime),
|
||||||
shipperId: (row[0] as int),
|
shipperId: (row[4] as int),
|
||||||
createdAt: (row[0] as DateTime),
|
createdAt: (row[5] as DateTime),
|
||||||
updatedAt: (row[0] as DateTime));
|
updatedAt: (row[6] as DateTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class OrderQueryWhere extends QueryWhere {
|
class OrderQueryWhere extends QueryWhere {
|
||||||
final StringSqlExpressionBuilder id = new StringSqlExpressionBuilder('id');
|
final NumericSqlExpressionBuilder<int> id =
|
||||||
|
new NumericSqlExpressionBuilder<int>('id');
|
||||||
|
|
||||||
final NumericSqlExpressionBuilder<int> customerId =
|
final NumericSqlExpressionBuilder<int> customerId =
|
||||||
new NumericSqlExpressionBuilder<int>('customer_id');
|
new NumericSqlExpressionBuilder<int>('customer_id');
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
import 'dart:async';
|
|
||||||
import 'order.dart';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
part 'order.postgresql.orm.g.dart';
|
|
||||||
|
|
||||||
abstract class OrderOrm {
|
|
||||||
factory OrderOrm.postgreSql(PostgreSQLConnection connection) =
|
|
||||||
PostgreSqlOrderOrm;
|
|
||||||
|
|
||||||
Future<List<Order>> getAll();
|
|
||||||
Future<Order> getById(String id);
|
|
||||||
Future<Order> deleteById(String id);
|
|
||||||
Future<Order> createOrder(Order model);
|
|
||||||
Future<Order> updateOrder(Order model);
|
|
||||||
OrderQuery query();
|
|
||||||
}
|
|
||||||
|
|
||||||
class OrderQuery {}
|
|
|
@ -1,86 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
part of 'order.orm.g.dart';
|
|
||||||
|
|
||||||
class PostgreSqlOrderOrm implements OrderOrm {
|
|
||||||
PostgreSqlOrderOrm(this.connection);
|
|
||||||
|
|
||||||
final PostgreSQLConnection connection;
|
|
||||||
|
|
||||||
static Order parseRow(List row) {
|
|
||||||
return new Order(
|
|
||||||
id: (row[0] as String),
|
|
||||||
customerId: (row[1] as int),
|
|
||||||
employeeId: (row[2] as int),
|
|
||||||
orderDate: (row[3] as DateTime),
|
|
||||||
shipperId: (row[4] as int),
|
|
||||||
createdAt: (row[5] as DateTime),
|
|
||||||
updatedAt: (row[6] as DateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Order> getById(String id) async {
|
|
||||||
var r = await connection.query(
|
|
||||||
'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<Order> 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<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);
|
|
||||||
}
|
|
||||||
|
|
||||||
OrderQuery query() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,4 +10,4 @@ part 'role.serializer.g.dart';
|
||||||
@orm
|
@orm
|
||||||
class _Role extends Model {
|
class _Role extends Model {
|
||||||
String name;
|
String name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,21 +17,22 @@ class RoleQuery extends Query<Role, RoleQueryWhere> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
get fields {
|
get fields {
|
||||||
return const ['id', 'name', 'createdAt', 'updatedAt'];
|
return RoleFields.allFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
deserialize(List row) {
|
deserialize(List row) {
|
||||||
return new Role(
|
return new Role(
|
||||||
id: (row[0] as String),
|
id: row[0].toString(),
|
||||||
name: (row[0] as String),
|
name: (row[1] as String),
|
||||||
createdAt: (row[0] as DateTime),
|
createdAt: (row[2] as DateTime),
|
||||||
updatedAt: (row[0] as DateTime));
|
updatedAt: (row[3] as DateTime));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RoleQueryWhere extends QueryWhere {
|
class RoleQueryWhere extends QueryWhere {
|
||||||
final StringSqlExpressionBuilder id = new StringSqlExpressionBuilder('id');
|
final NumericSqlExpressionBuilder<int> id =
|
||||||
|
new NumericSqlExpressionBuilder<int>('id');
|
||||||
|
|
||||||
final StringSqlExpressionBuilder name =
|
final StringSqlExpressionBuilder name =
|
||||||
new StringSqlExpressionBuilder('name');
|
new StringSqlExpressionBuilder('name');
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
import 'dart:async';
|
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
import 'role.dart';
|
|
||||||
part 'role.postgresql.orm.g.dart';
|
|
||||||
|
|
||||||
abstract class RoleOrm {
|
|
||||||
factory RoleOrm.postgreSql(PostgreSQLConnection connection) =
|
|
||||||
PostgreSqlRoleOrm;
|
|
||||||
|
|
||||||
Future<List<Role>> getAll();
|
|
||||||
Future<Role> getById(String id);
|
|
||||||
Future<Role> deleteById(String id);
|
|
||||||
Future<Role> createRole(Role model);
|
|
||||||
Future<Role> updateRole(Role model);
|
|
||||||
RoleQuery query();
|
|
||||||
}
|
|
||||||
|
|
||||||
class RoleQuery {}
|
|
|
@ -1,77 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
part of 'role.orm.g.dart';
|
|
||||||
|
|
||||||
class PostgreSqlRoleOrm implements RoleOrm {
|
|
||||||
PostgreSqlRoleOrm(this.connection);
|
|
||||||
|
|
||||||
final PostgreSQLConnection connection;
|
|
||||||
|
|
||||||
static Role parseRow(List row) {
|
|
||||||
return new Role(
|
|
||||||
id: (row[0] as String),
|
|
||||||
name: (row[1] as String),
|
|
||||||
createdAt: (row[2] as DateTime),
|
|
||||||
updatedAt: (row[3] as DateTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<Role> getById(String id) async {
|
|
||||||
var r = await connection.query(
|
|
||||||
'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<Role> 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<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);
|
|
||||||
}
|
|
||||||
|
|
||||||
RoleQuery query() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,11 +9,7 @@ part of angel_orm_generator.test.models.tree;
|
||||||
@generatedSerializable
|
@generatedSerializable
|
||||||
class Tree extends _Tree {
|
class Tree extends _Tree {
|
||||||
Tree(
|
Tree(
|
||||||
{this.id,
|
{this.id, this.rings, List<Fruit> fruits, this.createdAt, this.updatedAt})
|
||||||
this.rings,
|
|
||||||
List<dynamic> fruits,
|
|
||||||
this.createdAt,
|
|
||||||
this.updatedAt})
|
|
||||||
: this.fruits = new List.unmodifiable(fruits ?? []);
|
: this.fruits = new List.unmodifiable(fruits ?? []);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -23,7 +19,7 @@ class Tree extends _Tree {
|
||||||
final int rings;
|
final int rings;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final List<dynamic> fruits;
|
final List<Fruit> fruits;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
|
@ -34,7 +30,7 @@ class Tree extends _Tree {
|
||||||
Tree copyWith(
|
Tree copyWith(
|
||||||
{String id,
|
{String id,
|
||||||
int rings,
|
int rings,
|
||||||
List<dynamic> fruits,
|
List<Fruit> fruits,
|
||||||
DateTime createdAt,
|
DateTime createdAt,
|
||||||
DateTime updatedAt}) {
|
DateTime updatedAt}) {
|
||||||
return new Tree(
|
return new Tree(
|
||||||
|
@ -49,7 +45,7 @@ class Tree extends _Tree {
|
||||||
return other is _Tree &&
|
return other is _Tree &&
|
||||||
other.id == id &&
|
other.id == id &&
|
||||||
other.rings == rings &&
|
other.rings == rings &&
|
||||||
const ListEquality<dynamic>(const DefaultEquality())
|
const ListEquality<Fruit>(const DefaultEquality<Fruit>())
|
||||||
.equals(other.fruits, fruits) &&
|
.equals(other.fruits, fruits) &&
|
||||||
other.createdAt == createdAt &&
|
other.createdAt == createdAt &&
|
||||||
other.updatedAt == updatedAt;
|
other.updatedAt == updatedAt;
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "fruits" with type "List".
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "fruits" with type "List".
|
|
|
@ -11,7 +11,11 @@ abstract class TreeSerializer {
|
||||||
return new Tree(
|
return new Tree(
|
||||||
id: map['id'] as String,
|
id: map['id'] as String,
|
||||||
rings: map['rings'] as int,
|
rings: map['rings'] as int,
|
||||||
fruits: map['fruits'] as List<dynamic>,
|
fruits: map['fruits'] is Iterable
|
||||||
|
? new List.unmodifiable(((map['fruits'] as Iterable)
|
||||||
|
.where((x) => x is Map) as Iterable<Map>)
|
||||||
|
.map(FruitSerializer.fromMap))
|
||||||
|
: null,
|
||||||
createdAt: map['created_at'] != null
|
createdAt: map['created_at'] != null
|
||||||
? (map['created_at'] is DateTime
|
? (map['created_at'] is DateTime
|
||||||
? (map['created_at'] as DateTime)
|
? (map['created_at'] as DateTime)
|
||||||
|
@ -31,7 +35,7 @@ abstract class TreeSerializer {
|
||||||
return {
|
return {
|
||||||
'id': model.id,
|
'id': model.id,
|
||||||
'rings': model.rings,
|
'rings': model.rings,
|
||||||
'fruits': model.fruits,
|
'fruits': model.fruits?.map((m) => m.toJson())?.toList(),
|
||||||
'created_at': model.createdAt?.toIso8601String(),
|
'created_at': model.createdAt?.toIso8601String(),
|
||||||
'updated_at': model.updatedAt?.toIso8601String()
|
'updated_at': model.updatedAt?.toIso8601String()
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,6 +3,7 @@ library angel_orm_generator.test.models.user;
|
||||||
import 'package:angel_model/angel_model.dart';
|
import 'package:angel_model/angel_model.dart';
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:angel_serialize/angel_serialize.dart';
|
import 'package:angel_serialize/angel_serialize.dart';
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
import 'role.dart';
|
import 'role.dart';
|
||||||
part 'user.g.dart';
|
part 'user.g.dart';
|
||||||
part 'user.serializer.g.dart';
|
part 'user.serializer.g.dart';
|
||||||
|
@ -14,4 +15,4 @@ class _User extends Model {
|
||||||
|
|
||||||
@belongsToMany
|
@belongsToMany
|
||||||
List<Role> roles;
|
List<Role> roles;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ class User extends _User {
|
||||||
this.username,
|
this.username,
|
||||||
this.password,
|
this.password,
|
||||||
this.email,
|
this.email,
|
||||||
List<dynamic> roles,
|
List<Role> roles,
|
||||||
this.createdAt,
|
this.createdAt,
|
||||||
this.updatedAt})
|
this.updatedAt})
|
||||||
: this.roles = new List.unmodifiable(roles ?? []);
|
: this.roles = new List.unmodifiable(roles ?? []);
|
||||||
|
@ -31,7 +31,7 @@ class User extends _User {
|
||||||
final String email;
|
final String email;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final List<dynamic> roles;
|
final List<Role> roles;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
|
@ -44,7 +44,7 @@ class User extends _User {
|
||||||
String username,
|
String username,
|
||||||
String password,
|
String password,
|
||||||
String email,
|
String email,
|
||||||
List<dynamic> roles,
|
List<Role> roles,
|
||||||
DateTime createdAt,
|
DateTime createdAt,
|
||||||
DateTime updatedAt}) {
|
DateTime updatedAt}) {
|
||||||
return new User(
|
return new User(
|
||||||
|
@ -63,7 +63,7 @@ class User extends _User {
|
||||||
other.username == username &&
|
other.username == username &&
|
||||||
other.password == password &&
|
other.password == password &&
|
||||||
other.email == email &&
|
other.email == email &&
|
||||||
const ListEquality<dynamic>(const DefaultEquality())
|
const ListEquality<Role>(const DefaultEquality<Role>())
|
||||||
.equals(other.roles, roles) &&
|
.equals(other.roles, roles) &&
|
||||||
other.createdAt == createdAt &&
|
other.createdAt == createdAt &&
|
||||||
other.updatedAt == updatedAt;
|
other.updatedAt == updatedAt;
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// OrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "roles" with type "List".
|
|
|
@ -1,7 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// PostgreSqlOrmGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
// Error: Cannot infer SQL column type for field "roles" with type "List".
|
|
|
@ -13,7 +13,11 @@ abstract class UserSerializer {
|
||||||
username: map['username'] as String,
|
username: map['username'] as String,
|
||||||
password: map['password'] as String,
|
password: map['password'] as String,
|
||||||
email: map['email'] as String,
|
email: map['email'] as String,
|
||||||
roles: map['roles'] as List<dynamic>,
|
roles: map['roles'] is Iterable
|
||||||
|
? new List.unmodifiable(((map['roles'] as Iterable)
|
||||||
|
.where((x) => x is Map) as Iterable<Map>)
|
||||||
|
.map(RoleSerializer.fromMap))
|
||||||
|
: null,
|
||||||
createdAt: map['created_at'] != null
|
createdAt: map['created_at'] != null
|
||||||
? (map['created_at'] is DateTime
|
? (map['created_at'] is DateTime
|
||||||
? (map['created_at'] as DateTime)
|
? (map['created_at'] as DateTime)
|
||||||
|
@ -35,7 +39,7 @@ abstract class UserSerializer {
|
||||||
'username': model.username,
|
'username': model.username,
|
||||||
'password': model.password,
|
'password': model.password,
|
||||||
'email': model.email,
|
'email': model.email,
|
||||||
'roles': model.roles,
|
'roles': model.roles?.map((m) => m.toJson())?.toList(),
|
||||||
'created_at': model.createdAt?.toIso8601String(),
|
'created_at': model.createdAt?.toIso8601String(),
|
||||||
'updated_at': model.updatedAt?.toIso8601String()
|
'updated_at': model.updatedAt?.toIso8601String()
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
library angel_orm_generator.test.car_test;
|
library angel_orm_generator.test.car_test;
|
||||||
|
|
||||||
import 'package:angel_orm/angel_orm.dart';
|
import 'package:angel_orm/angel_orm.dart';
|
||||||
import 'package:postgres/postgres.dart';
|
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'models/car.dart';
|
import 'models/car.dart';
|
||||||
import 'common.dart';
|
import 'common.dart';
|
||||||
|
@ -44,7 +43,7 @@ main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
group('queries', () {
|
group('queries', () {
|
||||||
PostgreSQLConnection connection;
|
PostgresExecutor connection;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
connection = await connectToPostgres(['car']);
|
connection = await connectToPostgres(['car']);
|
||||||
|
@ -52,7 +51,7 @@ main() {
|
||||||
|
|
||||||
group('selects', () {
|
group('selects', () {
|
||||||
test('select all', () async {
|
test('select all', () async {
|
||||||
var cars = await CarQuery.getAll(connection);
|
var cars = await new CarQuery().get(connection);
|
||||||
expect(cars, []);
|
expect(cars, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -66,6 +65,8 @@ main() {
|
||||||
familyFriendly: false);
|
familyFriendly: false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tearDown(() => connection.close());
|
||||||
|
|
||||||
test('where clause is applied', () async {
|
test('where clause is applied', () async {
|
||||||
var query = new CarQuery()..where.familyFriendly.equals(true);
|
var query = new CarQuery()..where.familyFriendly.equals(true);
|
||||||
var cars = await query.get(connection);
|
var cars = await query.get(connection);
|
||||||
|
@ -113,23 +114,29 @@ main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get one', () async {
|
test('get one', () async {
|
||||||
var car = await CarQuery.getOne(int.parse(ferrari.id), connection);
|
var id = int.parse(ferrari.id);
|
||||||
expect(car.toJson(), ferrari.toJson());
|
var query = new CarQuery()..where.id.equals(id);
|
||||||
|
var car = await query.getOne(connection);
|
||||||
|
expect(car, ferrari);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('delete one', () async {
|
test('delete one', () async {
|
||||||
var car = await CarQuery.deleteOne(int.parse(ferrari.id), connection);
|
var id = int.parse(ferrari.id);
|
||||||
|
var query = new CarQuery()..where.id.equals(id);
|
||||||
|
var car = await query.deleteOne(connection);
|
||||||
expect(car.toJson(), ferrari.toJson());
|
expect(car.toJson(), ferrari.toJson());
|
||||||
|
|
||||||
var cars = await CarQuery.getAll(connection);
|
var cars = await new CarQuery().get(connection);
|
||||||
expect(cars, isEmpty);
|
expect(cars, isEmpty);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('delete stream', () async {
|
test('delete stream', () async {
|
||||||
var query = new CarQuery()..where.make.equals('Ferrari');
|
var query = new CarQuery()
|
||||||
query.or(new CarQueryWhere()..familyFriendly.equals(true));
|
..where.make.equals('Ferrari')
|
||||||
print(query.compile('DELETE FROM "cars"'));
|
..orWhere((w) => w.familyFriendly.equals(true));
|
||||||
var cars = await query.delete(connection);
|
|
||||||
|
print(query.compile(preamble: 'DELETE FROM "cars"'));
|
||||||
|
var cars = await query.get(connection);
|
||||||
expect(cars, hasLength(1));
|
expect(cars, hasLength(1));
|
||||||
expect(cars.first.toJson(), ferrari.toJson());
|
expect(cars.first.toJson(), ferrari.toJson());
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue