From f2dce7810481e230f3b15bf3cf829cb5b0db9b12 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 31 Dec 2018 07:24:21 -0500 Subject: [PATCH] Add examples for gen --- angel_orm_generator/example/main.dart | 112 ++++++++++++++++++ angel_orm_generator/example/main.g.dart | 71 +++++++++++ .../example/main.serializer.g.dart | 64 ++++++++++ 3 files changed, 247 insertions(+) create mode 100644 angel_orm_generator/example/main.dart create mode 100644 angel_orm_generator/example/main.g.dart create mode 100644 angel_orm_generator/example/main.serializer.g.dart diff --git a/angel_orm_generator/example/main.dart b/angel_orm_generator/example/main.dart new file mode 100644 index 00000000..7831bb03 --- /dev/null +++ b/angel_orm_generator/example/main.dart @@ -0,0 +1,112 @@ +import 'dart:async'; + +import 'package:angel_model/angel_model.dart'; +import 'package:angel_orm/angel_orm.dart'; +import 'package:angel_orm/src/query.dart'; +import 'package:angel_serialize/angel_serialize.dart'; +part 'main.g.dart'; +part 'main.serializer.g.dart'; + +main() async { + var query = new EmployeeQuery() + ..where.firstName.equals('Rich') + ..where.lastName.equals('Person') + ..orWhere((w) => w.salary.greaterThanOrEqualTo(75000)) + ..join('companies', 'company_id', 'id'); + + var richPerson = await query.getOne(new _FakeExecutor()); + print(richPerson.toJson()); +} + +class _FakeExecutor extends QueryExecutor { + const _FakeExecutor(); + + @override + Future> query( + String query, Map substitutionValues, + [returningFields]) async { + var now = new DateTime.now(); + print( + '_FakeExecutor received query: $query and values: $substitutionValues'); + return [ + [1, 'Rich', 'Person', 100000.0, now, now] + ]; + } + + @override + Future transaction(FutureOr Function() f) { + throw new UnsupportedError('Transactions are not supported.'); + } +} + +@orm +@serializable +abstract class _Employee extends Model { + String get firstName; + + String get lastName; + + double get salary; +} + +class EmployeeQuery extends Query { + @override + final QueryValues values = new MapQueryValues(); + + EmployeeQueryWhere _where; + + EmployeeQuery() { + _where = new EmployeeQueryWhere(this); + } + + @override + EmployeeQueryWhere get where => _where; + + @override + String get tableName => 'employees'; + + @override + List get fields => + ['id', 'first_name', 'last_name', 'salary', 'created_at', 'updated_at']; + + @override + EmployeeQueryWhere newWhereClause() => new EmployeeQueryWhere(this); + + @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 { + EmployeeQueryWhere(EmployeeQuery query) + : id = new NumericSqlExpressionBuilder(query, 'id'), + firstName = new StringSqlExpressionBuilder(query, 'first_name'), + lastName = new StringSqlExpressionBuilder(query, 'last_name'), + salary = new NumericSqlExpressionBuilder(query, 'salary'), + createdAt = new DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = new DateTimeSqlExpressionBuilder(query, 'updated_at'); + + @override + Iterable get expressionBuilders { + return [id, firstName, lastName, salary, createdAt, updatedAt]; + } + + final NumericSqlExpressionBuilder id; + + final StringSqlExpressionBuilder firstName; + + final StringSqlExpressionBuilder lastName; + + final NumericSqlExpressionBuilder salary; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; +} diff --git a/angel_orm_generator/example/main.g.dart b/angel_orm_generator/example/main.g.dart new file mode 100644 index 00000000..950cbd12 --- /dev/null +++ b/angel_orm_generator/example/main.g.dart @@ -0,0 +1,71 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'main.dart'; + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Employee extends _Employee { + Employee( + {this.id, + this.firstName, + this.lastName, + this.salary, + this.createdAt, + this.updatedAt}); + + @override + final String id; + + @override + final String firstName; + + @override + final String lastName; + + @override + final double salary; + + @override + final DateTime createdAt; + + @override + final DateTime updatedAt; + + Employee copyWith( + {String id, + String firstName, + String lastName, + double salary, + DateTime createdAt, + DateTime updatedAt}) { + return new Employee( + id: id ?? this.id, + firstName: firstName ?? this.firstName, + lastName: lastName ?? this.lastName, + salary: salary ?? this.salary, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt); + } + + bool operator ==(other) { + return other is _Employee && + other.id == id && + other.firstName == firstName && + other.lastName == lastName && + other.salary == salary && + other.createdAt == createdAt && + other.updatedAt == updatedAt; + } + + @override + int get hashCode { + return hashObjects([id, firstName, lastName, salary, createdAt, updatedAt]); + } + + Map toJson() { + return EmployeeSerializer.toMap(this); + } +} diff --git a/angel_orm_generator/example/main.serializer.g.dart b/angel_orm_generator/example/main.serializer.g.dart new file mode 100644 index 00000000..5e160b3b --- /dev/null +++ b/angel_orm_generator/example/main.serializer.g.dart @@ -0,0 +1,64 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'main.dart'; + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +abstract class EmployeeSerializer { + static Employee fromMap(Map map) { + return new Employee( + id: map['id'] as String, + firstName: map['first_name'] as String, + lastName: map['last_name'] as String, + salary: map['salary'] as double, + createdAt: map['created_at'] != null + ? (map['created_at'] is DateTime + ? (map['created_at'] as DateTime) + : DateTime.parse(map['created_at'].toString())) + : null, + updatedAt: map['updated_at'] != null + ? (map['updated_at'] is DateTime + ? (map['updated_at'] as DateTime) + : DateTime.parse(map['updated_at'].toString())) + : null); + } + + static Map toMap(Employee model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'first_name': model.firstName, + 'last_name': model.lastName, + 'salary': model.salary, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String() + }; + } +} + +abstract class EmployeeFields { + static const List allFields = const [ + id, + firstName, + lastName, + salary, + createdAt, + updatedAt + ]; + + static const String id = 'id'; + + static const String firstName = 'first_name'; + + static const String lastName = 'last_name'; + + static const String salary = 'salary'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; +}