platform/angel_orm/example/main.dart

88 lines
2.3 KiB
Dart
Raw Normal View History

import 'package:angel_model/angel_model.dart';
import 'package:angel_orm/angel_orm.dart';
2018-12-01 17:21:34 +00:00
import 'package:angel_serialize/angel_serialize.dart';
part 'main.g.dart';
part 'main.serializer.g.dart';
2018-12-01 17:21:34 +00:00
main() async {
2018-12-01 22:39:10 +00:00
var query = new EmployeeQuery()
..where.firstName.equals('Rich')
..where.lastName.equals('Person')
..orWhere((w) => w.salary.greaterThanOrEqualTo(75000))
..join('companies', 'company_id', 'id');
2018-12-01 18:23:50 +00:00
2018-12-01 17:21:34 +00:00
var richPerson = await query.getOne(new _FakeExecutor());
print(richPerson.toJson());
}
2018-12-01 17:21:34 +00:00
class _FakeExecutor extends QueryExecutor {
const _FakeExecutor();
2018-08-24 12:30:38 +00:00
2018-12-01 17:21:34 +00:00
@override
2018-12-01 22:39:10 +00:00
Future<List<List>> query(String query, returningFields) async {
2018-12-01 17:21:34 +00:00
var now = new DateTime.now();
print('_FakeExecutor received query: $query');
return [
[1, 'Rich', 'Person', 100000.0, now, now]
];
}
}
2018-12-01 17:21:34 +00:00
@orm
@serializable
2018-08-24 12:30:38 +00:00
abstract class _Employee extends Model {
String get firstName;
2018-08-24 12:30:38 +00:00
String get lastName;
2018-08-24 12:30:38 +00:00
double get salary;
2018-12-01 17:21:34 +00:00
}
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
@override
final EmployeeQueryWhere where = new EmployeeQueryWhere();
@override
String get tableName => 'employees';
@override
List<String> get fields =>
['id', 'first_name', 'last_name', 'salary', 'created_at', 'updated_at'];
@override
Employee deserialize(List row) {
return new Employee(
id: row[0].toString(),
firstName: row[1] as String,
lastName: row[2] as String,
salary: row[3] as double,
createdAt: row[4] as DateTime,
updatedAt: row[5] as DateTime);
}
}
class EmployeeQueryWhere extends QueryWhere {
@override
2018-12-01 18:23:50 +00:00
Iterable<SqlExpressionBuilder> get expressionBuilders {
return [id, firstName, lastName, salary, createdAt, updatedAt];
2018-12-01 17:21:34 +00:00
}
final NumericSqlExpressionBuilder<int> id =
2018-12-01 18:23:50 +00:00
new NumericSqlExpressionBuilder<int>('id');
2018-12-01 17:21:34 +00:00
2018-12-01 18:23:50 +00:00
final StringSqlExpressionBuilder firstName =
new StringSqlExpressionBuilder('first_name');
2018-12-01 17:21:34 +00:00
2018-12-01 18:23:50 +00:00
final StringSqlExpressionBuilder lastName =
new StringSqlExpressionBuilder('last_name');
2018-12-01 17:21:34 +00:00
final NumericSqlExpressionBuilder<double> salary =
2018-12-01 18:23:50 +00:00
new NumericSqlExpressionBuilder<double>('salary');
2018-12-01 17:21:34 +00:00
final DateTimeSqlExpressionBuilder createdAt =
new DateTimeSqlExpressionBuilder('created_at');
2018-12-01 17:21:34 +00:00
final DateTimeSqlExpressionBuilder updatedAt =
new DateTimeSqlExpressionBuilder('updated_at');
}