platform/packages/orm/angel_orm/example/main.dart

115 lines
3 KiB
Dart
Raw Normal View History

2018-12-08 02:03:03 +00:00
import 'dart:async';
import 'package:angel_model/angel_model.dart';
import 'package:angel_orm/angel_orm.dart';
2018-12-08 02:03:03 +00:00
import 'package:angel_orm/src/query.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';
2021-05-02 07:32:24 +00:00
void main() async {
2019-07-04 21:30:21 +00:00
var query = EmployeeQuery()
2021-05-02 07:32:24 +00:00
..where?.firstName.equals('Rich')
..where?.lastName.equals('Person')
..orWhere((w) => w?.salary.greaterThanOrEqualTo(75000))
2018-12-01 22:39:10 +00:00
..join('companies', 'company_id', 'id');
2018-12-01 18:23:50 +00:00
2019-07-04 21:30:21 +00:00
var richPerson = await query.getOne(_FakeExecutor());
2021-05-03 13:27:12 +00:00
if (richPerson.isPresent) {
print(richPerson.first.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
Future<List<List>> query(
2021-05-02 07:32:24 +00:00
String tableName, String? query, Map<String, dynamic> substitutionValues,
2021-05-03 13:27:12 +00:00
[returningFields = const []]) async {
2019-07-04 21:30:21 +00:00
var now = DateTime.now();
print(
'_FakeExecutor received query: $query and values: $substitutionValues');
2018-12-01 17:21:34 +00:00
return [
[1, 'Rich', 'Person', 100000.0, now, now]
];
}
2018-12-08 02:03:03 +00:00
@override
Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) {
2019-07-04 21:30:21 +00:00
throw UnsupportedError('Transactions are not supported.');
2018-12-08 02:03:03 +00:00
}
}
2018-12-01 17:21:34 +00:00
@orm
@serializable
2018-08-24 12:30:38 +00:00
abstract class _Employee extends Model {
2021-05-02 07:32:24 +00:00
String? get firstName;
2021-05-02 07:32:24 +00:00
String? get lastName;
2021-05-02 07:32:24 +00:00
double? get salary;
2018-12-01 17:21:34 +00:00
}
2021-05-02 07:32:24 +00:00
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere?> {
2018-12-03 13:41:14 +00:00
@override
2019-07-04 21:30:21 +00:00
final QueryValues values = MapQueryValues();
2018-12-03 13:41:14 +00:00
2021-05-02 07:32:24 +00:00
EmployeeQueryWhere? _where;
EmployeeQuery() {
2019-07-04 21:30:21 +00:00
_where = EmployeeQueryWhere(this);
}
2018-12-01 17:21:34 +00:00
@override
2021-05-02 07:32:24 +00:00
EmployeeQueryWhere? get where => _where;
2018-12-01 17:21:34 +00:00
@override
String get tableName => 'employees';
@override
List<String> get fields =>
['id', 'first_name', 'last_name', 'salary', 'created_at', 'updated_at'];
@override
2019-07-04 21:30:21 +00:00
EmployeeQueryWhere newWhereClause() => EmployeeQueryWhere(this);
2018-12-01 17:21:34 +00:00
@override
Employee deserialize(List row) {
2019-07-04 21:30:21 +00:00
return Employee(
2018-12-01 17:21:34 +00:00
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 {
EmployeeQueryWhere(EmployeeQuery query)
2019-07-04 21:30:21 +00:00
: id = NumericSqlExpressionBuilder(query, 'id'),
firstName = StringSqlExpressionBuilder(query, 'first_name'),
lastName = StringSqlExpressionBuilder(query, 'last_name'),
salary = NumericSqlExpressionBuilder(query, 'salary'),
createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'),
updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at');
2018-12-01 17:21:34 +00:00
@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 17:21:34 +00:00
final StringSqlExpressionBuilder firstName;
2018-12-01 17:21:34 +00:00
final StringSqlExpressionBuilder lastName;
2018-12-01 17:21:34 +00:00
final NumericSqlExpressionBuilder<double> salary;
2018-12-01 17:21:34 +00:00
final DateTimeSqlExpressionBuilder createdAt;
final DateTimeSqlExpressionBuilder updatedAt;
}