gen@2.0.4
This commit is contained in:
parent
13b7e40a44
commit
76016f2062
9 changed files with 298 additions and 160 deletions
|
@ -1,3 +1,6 @@
|
|||
# 2.0.4
|
||||
* Fix `reviveColumn` and element finding to properly detect all annotations now.
|
||||
|
||||
# 2.0.3
|
||||
* Remove `targets` in `build.yaml`.
|
||||
|
||||
|
|
|
@ -16,26 +16,4 @@ builders:
|
|||
applies_builders:
|
||||
- angel_serialize_generator|angel_serialize
|
||||
- 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/has_map.dart
|
||||
# - test/models/role.dart
|
||||
# - test/models/unorthodox.dart
|
||||
# $default:
|
||||
# dependencies:
|
||||
# - angel_serialize_generator
|
||||
# - :_standalone
|
||||
# sources:
|
||||
# - test/models/book.dart
|
||||
# - test/models/has_car.dart
|
||||
# - test/models/leg.dart
|
||||
# - test/models/order.dart
|
||||
# - test/models/tree.dart
|
||||
# - test/models/user.dart
|
||||
- source_gen|part_cleanup"
|
|
@ -1,11 +1,10 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:angel_migration/angel_migration.dart';
|
||||
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()
|
||||
|
@ -46,67 +45,8 @@ abstract class _Employee extends Model {
|
|||
|
||||
String get lastName;
|
||||
|
||||
@Column(indexType: IndexType.unique)
|
||||
String uniqueId;
|
||||
|
||||
double get salary;
|
||||
}
|
||||
|
||||
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
|
||||
@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<String> 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<SqlExpressionBuilder> get expressionBuilders {
|
||||
return [id, firstName, lastName, salary, createdAt, updatedAt];
|
||||
}
|
||||
|
||||
final NumericSqlExpressionBuilder<int> id;
|
||||
|
||||
final StringSqlExpressionBuilder firstName;
|
||||
|
||||
final StringSqlExpressionBuilder lastName;
|
||||
|
||||
final NumericSqlExpressionBuilder<double> salary;
|
||||
|
||||
final DateTimeSqlExpressionBuilder createdAt;
|
||||
|
||||
final DateTimeSqlExpressionBuilder updatedAt;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,179 @@
|
|||
|
||||
part of 'main.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// MigrationGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class EmployeeMigration extends Migration {
|
||||
@override
|
||||
up(Schema schema) {
|
||||
schema.create('employees', (table) {
|
||||
table.serial('id')..primaryKey();
|
||||
table.varChar('unique_id')..unique();
|
||||
table.varChar('first_name');
|
||||
table.varChar('last_name');
|
||||
table.declare('salary', ColumnType('decimal'));
|
||||
table.timeStamp('created_at');
|
||||
table.timeStamp('updated_at');
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
down(Schema schema) {
|
||||
schema.drop('employees');
|
||||
}
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// OrmGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class EmployeeQuery extends Query<Employee, EmployeeQueryWhere> {
|
||||
EmployeeQuery({Set<String> trampoline}) {
|
||||
trampoline ??= Set();
|
||||
trampoline.add(tableName);
|
||||
_where = EmployeeQueryWhere(this);
|
||||
}
|
||||
|
||||
@override
|
||||
final EmployeeQueryValues values = EmployeeQueryValues();
|
||||
|
||||
EmployeeQueryWhere _where;
|
||||
|
||||
@override
|
||||
get casts {
|
||||
return {'salary': 'text'};
|
||||
}
|
||||
|
||||
@override
|
||||
get tableName {
|
||||
return 'employees';
|
||||
}
|
||||
|
||||
@override
|
||||
get fields {
|
||||
return const [
|
||||
'id',
|
||||
'unique_id',
|
||||
'first_name',
|
||||
'last_name',
|
||||
'salary',
|
||||
'created_at',
|
||||
'updated_at'
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
EmployeeQueryWhere get where {
|
||||
return _where;
|
||||
}
|
||||
|
||||
@override
|
||||
EmployeeQueryWhere newWhereClause() {
|
||||
return EmployeeQueryWhere(this);
|
||||
}
|
||||
|
||||
static Employee parseRow(List row) {
|
||||
if (row.every((x) => x == null)) return null;
|
||||
var model = Employee(
|
||||
id: row[0].toString(),
|
||||
uniqueId: (row[1] as String),
|
||||
firstName: (row[2] as String),
|
||||
lastName: (row[3] as String),
|
||||
salary: double.tryParse(row[4].toString()),
|
||||
createdAt: (row[5] as DateTime),
|
||||
updatedAt: (row[6] as DateTime));
|
||||
return model;
|
||||
}
|
||||
|
||||
@override
|
||||
deserialize(List row) {
|
||||
return parseRow(row);
|
||||
}
|
||||
}
|
||||
|
||||
class EmployeeQueryWhere extends QueryWhere {
|
||||
EmployeeQueryWhere(EmployeeQuery query)
|
||||
: id = NumericSqlExpressionBuilder<int>(query, 'id'),
|
||||
uniqueId = StringSqlExpressionBuilder(query, 'unique_id'),
|
||||
firstName = StringSqlExpressionBuilder(query, 'first_name'),
|
||||
lastName = StringSqlExpressionBuilder(query, 'last_name'),
|
||||
salary = NumericSqlExpressionBuilder<double>(query, 'salary'),
|
||||
createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'),
|
||||
updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at');
|
||||
|
||||
final NumericSqlExpressionBuilder<int> id;
|
||||
|
||||
final StringSqlExpressionBuilder uniqueId;
|
||||
|
||||
final StringSqlExpressionBuilder firstName;
|
||||
|
||||
final StringSqlExpressionBuilder lastName;
|
||||
|
||||
final NumericSqlExpressionBuilder<double> salary;
|
||||
|
||||
final DateTimeSqlExpressionBuilder createdAt;
|
||||
|
||||
final DateTimeSqlExpressionBuilder updatedAt;
|
||||
|
||||
@override
|
||||
get expressionBuilders {
|
||||
return [id, uniqueId, firstName, lastName, salary, createdAt, updatedAt];
|
||||
}
|
||||
}
|
||||
|
||||
class EmployeeQueryValues extends MapQueryValues {
|
||||
@override
|
||||
get casts {
|
||||
return {'salary': 'decimal'};
|
||||
}
|
||||
|
||||
String get id {
|
||||
return (values['id'] as String);
|
||||
}
|
||||
|
||||
set id(String value) => values['id'] = value;
|
||||
String get uniqueId {
|
||||
return (values['unique_id'] as String);
|
||||
}
|
||||
|
||||
set uniqueId(String value) => values['unique_id'] = value;
|
||||
String get firstName {
|
||||
return (values['first_name'] as String);
|
||||
}
|
||||
|
||||
set firstName(String value) => values['first_name'] = value;
|
||||
String get lastName {
|
||||
return (values['last_name'] as String);
|
||||
}
|
||||
|
||||
set lastName(String value) => values['last_name'] = value;
|
||||
double get salary {
|
||||
return double.tryParse((values['salary'] as String));
|
||||
}
|
||||
|
||||
set salary(double value) => values['salary'] = value.toString();
|
||||
DateTime get createdAt {
|
||||
return (values['created_at'] as DateTime);
|
||||
}
|
||||
|
||||
set createdAt(DateTime value) => values['created_at'] = value;
|
||||
DateTime get updatedAt {
|
||||
return (values['updated_at'] as DateTime);
|
||||
}
|
||||
|
||||
set updatedAt(DateTime value) => values['updated_at'] = value;
|
||||
void copyFrom(Employee model) {
|
||||
uniqueId = model.uniqueId;
|
||||
firstName = model.firstName;
|
||||
lastName = model.lastName;
|
||||
salary = model.salary;
|
||||
createdAt = model.createdAt;
|
||||
updatedAt = model.updatedAt;
|
||||
}
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonModelGenerator
|
||||
// **************************************************************************
|
||||
|
@ -10,6 +183,7 @@ part of 'main.dart';
|
|||
class Employee extends _Employee {
|
||||
Employee(
|
||||
{this.id,
|
||||
this.uniqueId,
|
||||
this.firstName,
|
||||
this.lastName,
|
||||
this.salary,
|
||||
|
@ -19,6 +193,9 @@ class Employee extends _Employee {
|
|||
@override
|
||||
final String id;
|
||||
|
||||
@override
|
||||
final String uniqueId;
|
||||
|
||||
@override
|
||||
final String firstName;
|
||||
|
||||
|
@ -36,6 +213,7 @@ class Employee extends _Employee {
|
|||
|
||||
Employee copyWith(
|
||||
{String id,
|
||||
String uniqueId,
|
||||
String firstName,
|
||||
String lastName,
|
||||
double salary,
|
||||
|
@ -43,6 +221,7 @@ class Employee extends _Employee {
|
|||
DateTime updatedAt}) {
|
||||
return new Employee(
|
||||
id: id ?? this.id,
|
||||
uniqueId: uniqueId ?? this.uniqueId,
|
||||
firstName: firstName ?? this.firstName,
|
||||
lastName: lastName ?? this.lastName,
|
||||
salary: salary ?? this.salary,
|
||||
|
@ -53,6 +232,7 @@ class Employee extends _Employee {
|
|||
bool operator ==(other) {
|
||||
return other is _Employee &&
|
||||
other.id == id &&
|
||||
other.uniqueId == uniqueId &&
|
||||
other.firstName == firstName &&
|
||||
other.lastName == lastName &&
|
||||
other.salary == salary &&
|
||||
|
@ -62,10 +242,104 @@ class Employee extends _Employee {
|
|||
|
||||
@override
|
||||
int get hashCode {
|
||||
return hashObjects([id, firstName, lastName, salary, createdAt, updatedAt]);
|
||||
return hashObjects(
|
||||
[id, uniqueId, firstName, lastName, salary, createdAt, updatedAt]);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "Employee(id=$id, uniqueId=$uniqueId, firstName=$firstName, lastName=$lastName, salary=$salary, createdAt=$createdAt, updatedAt=$updatedAt)";
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return EmployeeSerializer.toMap(this);
|
||||
}
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// SerializerGenerator
|
||||
// **************************************************************************
|
||||
|
||||
const EmployeeSerializer employeeSerializer = const EmployeeSerializer();
|
||||
|
||||
class EmployeeEncoder extends Converter<Employee, Map> {
|
||||
const EmployeeEncoder();
|
||||
|
||||
@override
|
||||
Map convert(Employee model) => EmployeeSerializer.toMap(model);
|
||||
}
|
||||
|
||||
class EmployeeDecoder extends Converter<Map, Employee> {
|
||||
const EmployeeDecoder();
|
||||
|
||||
@override
|
||||
Employee convert(Map map) => EmployeeSerializer.fromMap(map);
|
||||
}
|
||||
|
||||
class EmployeeSerializer extends Codec<Employee, Map> {
|
||||
const EmployeeSerializer();
|
||||
|
||||
@override
|
||||
get encoder => const EmployeeEncoder();
|
||||
@override
|
||||
get decoder => const EmployeeDecoder();
|
||||
static Employee fromMap(Map map) {
|
||||
return new Employee(
|
||||
id: map['id'] as String,
|
||||
uniqueId: map['unique_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<String, dynamic> toMap(_Employee model) {
|
||||
if (model == null) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
'id': model.id,
|
||||
'unique_id': model.uniqueId,
|
||||
'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<String> allFields = <String>[
|
||||
id,
|
||||
uniqueId,
|
||||
firstName,
|
||||
lastName,
|
||||
salary,
|
||||
createdAt,
|
||||
updatedAt
|
||||
];
|
||||
|
||||
static const String id = 'id';
|
||||
|
||||
static const String uniqueId = 'unique_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';
|
||||
}
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
// 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<String, dynamic> 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<String> allFields = const <String>[
|
||||
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';
|
||||
}
|
|
@ -27,11 +27,15 @@ bool isSpecialId(OrmBuildContext ctx, FieldElement field) {
|
|||
.isAssignableFromType(ctx.buildContext.clazz.type));
|
||||
}
|
||||
|
||||
Element _findElement(FieldElement field) {
|
||||
return (field.setter == null ? field.getter : field) ?? field;
|
||||
}
|
||||
|
||||
FieldElement findPrimaryFieldInList(
|
||||
OrmBuildContext ctx, Iterable<FieldElement> fields) {
|
||||
for (var field_ in fields) {
|
||||
var field = field_ is RelationFieldImpl ? field_.originalField : field_;
|
||||
var element = field.getter ?? field;
|
||||
var element = _findElement(field);
|
||||
// print(
|
||||
// 'Searching in ${ctx.buildContext.originalClassName}=>${field?.name} (${field.runtimeType})');
|
||||
// Check for column annotation...
|
||||
|
@ -95,8 +99,9 @@ Future<OrmBuildContext> buildOrmContext(
|
|||
for (var field in buildCtx.fields) {
|
||||
// Check for column annotation...
|
||||
Column column;
|
||||
var element = field.getter ?? field;
|
||||
var element = _findElement(field);
|
||||
var columnAnnotation = columnTypeChecker.firstAnnotationOf(element);
|
||||
// print('${element.name} => $columnAnnotation');
|
||||
|
||||
if (columnAnnotation != null) {
|
||||
column = reviveColumn(new ConstantReader(columnAnnotation));
|
||||
|
@ -128,7 +133,7 @@ Future<OrmBuildContext> buildOrmContext(
|
|||
}
|
||||
|
||||
// Try to find a relationship
|
||||
var el = field.setter == null ? field.getter : field;
|
||||
var el = _findElement(field);
|
||||
el ??= field;
|
||||
var ann = relationshipTypeChecker.firstAnnotationOf(el);
|
||||
|
||||
|
@ -296,11 +301,13 @@ ColumnType inferColumnType(DartType type) {
|
|||
Column reviveColumn(ConstantReader cr) {
|
||||
ColumnType columnType;
|
||||
|
||||
var indexTypeObj = cr.peek('indexType')?.objectValue;
|
||||
indexTypeObj ??= cr.revive().namedArguments['indexType'];
|
||||
|
||||
var columnObj =
|
||||
cr.peek('type')?.objectValue?.getField('name')?.toStringValue();
|
||||
var indexType = IndexType.values[
|
||||
cr.peek('indexType')?.objectValue?.getField('index')?.toIntValue() ??
|
||||
IndexType.none.index];
|
||||
indexTypeObj?.getField('index')?.toIntValue() ?? IndexType.none.index];
|
||||
|
||||
if (const TypeChecker.fromRuntime(PrimaryKey)
|
||||
.isAssignableFromType(cr.objectValue.type)) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel_orm_generator
|
||||
version: 2.0.3
|
||||
version: 2.0.4
|
||||
description: Code generators for Angel's ORM. Generates query builder classes.
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/orm
|
||||
|
|
|
@ -11,7 +11,7 @@ class TreeMigration extends Migration {
|
|||
up(Schema schema) {
|
||||
schema.create('trees', (table) {
|
||||
table.serial('id')..primaryKey();
|
||||
table.integer('rings');
|
||||
table.declare('rings', ColumnType('smallint'));
|
||||
table.timeStamp('created_at');
|
||||
table.timeStamp('updated_at');
|
||||
});
|
||||
|
|
|
@ -56,7 +56,7 @@ class NumbaMigration extends Migration {
|
|||
@override
|
||||
up(Schema schema) {
|
||||
schema.create('numbas', (table) {
|
||||
table.integer('i');
|
||||
table.declare('i', ColumnType('serial'))..primaryKey();
|
||||
table.integer('parent');
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue