diff --git a/angel_orm_generator/lib/src/orm_generator.dart b/angel_orm_generator/lib/src/orm_generator.dart index a501698f..87096780 100644 --- a/angel_orm_generator/lib/src/orm_generator.dart +++ b/angel_orm_generator/lib/src/orm_generator.dart @@ -268,6 +268,7 @@ class OrmGenerator extends GeneratorForAnnotation { ..type = TypeReference((b) => b ..symbol = 'Set' ..types.add(refer('String'))))) + ..initializers.add(Code('super(parent: parent)')) ..body = Block((b) { b.statements.addAll([ Code('trampoline ??= Set();'), diff --git a/angel_orm_test/lib/src/models/book.g.dart b/angel_orm_test/lib/src/models/book.g.dart new file mode 100644 index 00000000..b72cdd3f --- /dev/null +++ b/angel_orm_test/lib/src/models/book.g.dart @@ -0,0 +1,606 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of angel_orm.generator.models.book; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class BookMigration extends Migration { + @override + up(Schema schema) { + schema.create('books', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.varChar('name'); + table + .declare('author_id', ColumnType('serial')) + .references('authors', 'id'); + table + .declare('partner_author_id', ColumnType('serial')) + .references('authors', 'id'); + }); + } + + @override + down(Schema schema) { + schema.drop('books'); + } +} + +class AuthorMigration extends Migration { + @override + up(Schema schema) { + schema.create('authors', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.varChar('name', length: 255)..defaultsTo('Tobe Osakwe'); + }); + } + + @override + down(Schema schema) { + schema.drop('authors'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class BookQuery extends Query { + BookQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = BookQueryWhere(this); + leftJoin(_author = AuthorQuery(trampoline: trampoline, parent: this), + 'author_id', 'id', + additionalFields: const ['id', 'created_at', 'updated_at', 'name'], + trampoline: trampoline); + leftJoin(_partnerAuthor = AuthorQuery(trampoline: trampoline, parent: this), + 'partner_author_id', 'id', + additionalFields: const ['id', 'created_at', 'updated_at', 'name'], + trampoline: trampoline); + } + + @override + final BookQueryValues values = BookQueryValues(); + + BookQueryWhere _where; + + AuthorQuery _author; + + AuthorQuery _partnerAuthor; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'books'; + } + + @override + get fields { + return const [ + 'id', + 'created_at', + 'updated_at', + 'author_id', + 'partner_author_id', + 'name' + ]; + } + + @override + BookQueryWhere get where { + return _where; + } + + @override + BookQueryWhere newWhereClause() { + return BookQueryWhere(this); + } + + static Book parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Book( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + name: (row[5] as String)); + if (row.length > 6) { + model = model.copyWith( + author: AuthorQuery.parseRow(row.skip(6).take(4).toList())); + } + if (row.length > 10) { + model = model.copyWith( + partnerAuthor: AuthorQuery.parseRow(row.skip(10).take(4).toList())); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + AuthorQuery get author { + return _author; + } + + AuthorQuery get partnerAuthor { + return _partnerAuthor; + } +} + +class BookQueryWhere extends QueryWhere { + BookQueryWhere(BookQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + authorId = NumericSqlExpressionBuilder(query, 'author_id'), + partnerAuthorId = + NumericSqlExpressionBuilder(query, 'partner_author_id'), + name = StringSqlExpressionBuilder(query, 'name'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final NumericSqlExpressionBuilder authorId; + + final NumericSqlExpressionBuilder partnerAuthorId; + + final StringSqlExpressionBuilder name; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, authorId, partnerAuthorId, name]; + } +} + +class BookQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + int get authorId { + return (values['author_id'] as int); + } + + set authorId(int value) => values['author_id'] = value; + int get partnerAuthorId { + return (values['partner_author_id'] as int); + } + + set partnerAuthorId(int value) => values['partner_author_id'] = value; + String get name { + return (values['name'] as String); + } + + set name(String value) => values['name'] = value; + void copyFrom(Book model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + name = model.name; + if (model.author != null) { + values['author_id'] = model.author.id; + } + if (model.partnerAuthor != null) { + values['partner_author_id'] = model.partnerAuthor.id; + } + } +} + +class AuthorQuery extends Query { + AuthorQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = AuthorQueryWhere(this); + } + + @override + final AuthorQueryValues values = AuthorQueryValues(); + + AuthorQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'authors'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'name']; + } + + @override + AuthorQueryWhere get where { + return _where; + } + + @override + AuthorQueryWhere newWhereClause() { + return AuthorQueryWhere(this); + } + + static Author parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Author( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + name: (row[3] as String)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class AuthorQueryWhere extends QueryWhere { + AuthorQueryWhere(AuthorQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + name = StringSqlExpressionBuilder(query, 'name'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final StringSqlExpressionBuilder name; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, name]; + } +} + +class AuthorQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + String get name { + return (values['name'] as String); + } + + set name(String value) => values['name'] = value; + void copyFrom(Author model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + name = model.name; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Book extends _Book { + Book( + {this.id, + this.createdAt, + this.updatedAt, + this.author, + this.partnerAuthor, + this.name}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + _Author author; + + @override + _Author partnerAuthor; + + @override + String name; + + Book copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + _Author author, + _Author partnerAuthor, + String name}) { + return Book( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + author: author ?? this.author, + partnerAuthor: partnerAuthor ?? this.partnerAuthor, + name: name ?? this.name); + } + + bool operator ==(other) { + return other is _Book && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.author == author && + other.partnerAuthor == partnerAuthor && + other.name == name; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, author, partnerAuthor, name]); + } + + @override + String toString() { + return "Book(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, author=$author, partnerAuthor=$partnerAuthor, name=$name)"; + } + + Map toJson() { + return BookSerializer.toMap(this); + } +} + +@generatedSerializable +class Author extends _Author { + Author({this.id, this.createdAt, this.updatedAt, this.name = 'Tobe Osakwe'}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + final String name; + + Author copyWith( + {String id, DateTime createdAt, DateTime updatedAt, String name}) { + return Author( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + name: name ?? this.name); + } + + bool operator ==(other) { + return other is _Author && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.name == name; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, name]); + } + + @override + String toString() { + return "Author(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, name=$name)"; + } + + Map toJson() { + return AuthorSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const BookSerializer bookSerializer = BookSerializer(); + +class BookEncoder extends Converter { + const BookEncoder(); + + @override + Map convert(Book model) => BookSerializer.toMap(model); +} + +class BookDecoder extends Converter { + const BookDecoder(); + + @override + Book convert(Map map) => BookSerializer.fromMap(map); +} + +class BookSerializer extends Codec { + const BookSerializer(); + + @override + get encoder => const BookEncoder(); + @override + get decoder => const BookDecoder(); + static Book fromMap(Map map) { + return Book( + id: map['id'] as String, + 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, + author: map['author'] != null + ? AuthorSerializer.fromMap(map['author'] as Map) + : null, + partnerAuthor: map['partner_author'] != null + ? AuthorSerializer.fromMap(map['partner_author'] as Map) + : null, + name: map['name'] as String); + } + + static Map toMap(_Book model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'author': AuthorSerializer.toMap(model.author), + 'partner_author': AuthorSerializer.toMap(model.partnerAuthor), + 'name': model.name + }; + } +} + +abstract class BookFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + author, + partnerAuthor, + name + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String author = 'author'; + + static const String partnerAuthor = 'partner_author'; + + static const String name = 'name'; +} + +const AuthorSerializer authorSerializer = AuthorSerializer(); + +class AuthorEncoder extends Converter { + const AuthorEncoder(); + + @override + Map convert(Author model) => AuthorSerializer.toMap(model); +} + +class AuthorDecoder extends Converter { + const AuthorDecoder(); + + @override + Author convert(Map map) => AuthorSerializer.fromMap(map); +} + +class AuthorSerializer extends Codec { + const AuthorSerializer(); + + @override + get encoder => const AuthorEncoder(); + @override + get decoder => const AuthorDecoder(); + static Author fromMap(Map map) { + return Author( + id: map['id'] as String, + 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, + name: map['name'] as String ?? 'Tobe Osakwe'); + } + + static Map toMap(_Author model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'name': model.name + }; + } +} + +abstract class AuthorFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + name + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String name = 'name'; +} diff --git a/angel_orm_test/lib/src/models/car.g.dart b/angel_orm_test/lib/src/models/car.g.dart new file mode 100644 index 00000000..9de60a6b --- /dev/null +++ b/angel_orm_test/lib/src/models/car.g.dart @@ -0,0 +1,367 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of angel_orm.generator.models.car; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class CarMigration extends Migration { + @override + up(Schema schema) { + schema.create('cars', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.varChar('make'); + table.varChar('description'); + table.boolean('family_friendly'); + table.timeStamp('recalled_at'); + }); + } + + @override + down(Schema schema) { + schema.drop('cars'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class CarQuery extends Query { + CarQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = CarQueryWhere(this); + } + + @override + final CarQueryValues values = CarQueryValues(); + + CarQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'cars'; + } + + @override + get fields { + return const [ + 'id', + 'created_at', + 'updated_at', + 'make', + 'description', + 'family_friendly', + 'recalled_at' + ]; + } + + @override + CarQueryWhere get where { + return _where; + } + + @override + CarQueryWhere newWhereClause() { + return CarQueryWhere(this); + } + + static Car parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Car( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + make: (row[3] as String), + description: (row[4] as String), + familyFriendly: (row[5] as bool), + recalledAt: (row[6] as DateTime)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class CarQueryWhere extends QueryWhere { + CarQueryWhere(CarQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + make = StringSqlExpressionBuilder(query, 'make'), + description = StringSqlExpressionBuilder(query, 'description'), + familyFriendly = BooleanSqlExpressionBuilder(query, 'family_friendly'), + recalledAt = DateTimeSqlExpressionBuilder(query, 'recalled_at'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final StringSqlExpressionBuilder make; + + final StringSqlExpressionBuilder description; + + final BooleanSqlExpressionBuilder familyFriendly; + + final DateTimeSqlExpressionBuilder recalledAt; + + @override + get expressionBuilders { + return [ + id, + createdAt, + updatedAt, + make, + description, + familyFriendly, + recalledAt + ]; + } +} + +class CarQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + String get make { + return (values['make'] as String); + } + + set make(String value) => values['make'] = value; + String get description { + return (values['description'] as String); + } + + set description(String value) => values['description'] = value; + bool get familyFriendly { + return (values['family_friendly'] as bool); + } + + set familyFriendly(bool value) => values['family_friendly'] = value; + DateTime get recalledAt { + return (values['recalled_at'] as DateTime); + } + + set recalledAt(DateTime value) => values['recalled_at'] = value; + void copyFrom(Car model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + make = model.make; + description = model.description; + familyFriendly = model.familyFriendly; + recalledAt = model.recalledAt; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Car extends _Car { + Car( + {this.id, + this.createdAt, + this.updatedAt, + this.make, + this.description, + this.familyFriendly, + this.recalledAt}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + String make; + + @override + String description; + + @override + bool familyFriendly; + + @override + DateTime recalledAt; + + Car copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + String make, + String description, + bool familyFriendly, + DateTime recalledAt}) { + return Car( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + make: make ?? this.make, + description: description ?? this.description, + familyFriendly: familyFriendly ?? this.familyFriendly, + recalledAt: recalledAt ?? this.recalledAt); + } + + bool operator ==(other) { + return other is _Car && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.make == make && + other.description == description && + other.familyFriendly == familyFriendly && + other.recalledAt == recalledAt; + } + + @override + int get hashCode { + return hashObjects([ + id, + createdAt, + updatedAt, + make, + description, + familyFriendly, + recalledAt + ]); + } + + @override + String toString() { + return "Car(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, make=$make, description=$description, familyFriendly=$familyFriendly, recalledAt=$recalledAt)"; + } + + Map toJson() { + return CarSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const CarSerializer carSerializer = CarSerializer(); + +class CarEncoder extends Converter { + const CarEncoder(); + + @override + Map convert(Car model) => CarSerializer.toMap(model); +} + +class CarDecoder extends Converter { + const CarDecoder(); + + @override + Car convert(Map map) => CarSerializer.fromMap(map); +} + +class CarSerializer extends Codec { + const CarSerializer(); + + @override + get encoder => const CarEncoder(); + @override + get decoder => const CarDecoder(); + static Car fromMap(Map map) { + return Car( + id: map['id'] as String, + 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, + make: map['make'] as String, + description: map['description'] as String, + familyFriendly: map['family_friendly'] as bool, + recalledAt: map['recalled_at'] != null + ? (map['recalled_at'] is DateTime + ? (map['recalled_at'] as DateTime) + : DateTime.parse(map['recalled_at'].toString())) + : null); + } + + static Map toMap(_Car model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'make': model.make, + 'description': model.description, + 'family_friendly': model.familyFriendly, + 'recalled_at': model.recalledAt?.toIso8601String() + }; + } +} + +abstract class CarFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + make, + description, + familyFriendly, + recalledAt + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String make = 'make'; + + static const String description = 'description'; + + static const String familyFriendly = 'family_friendly'; + + static const String recalledAt = 'recalled_at'; +} diff --git a/angel_orm_test/lib/src/models/email_indexed.g.dart b/angel_orm_test/lib/src/models/email_indexed.g.dart new file mode 100644 index 00000000..85fffb87 --- /dev/null +++ b/angel_orm_test/lib/src/models/email_indexed.g.dart @@ -0,0 +1,798 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'email_indexed.dart'; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class RoleMigration extends Migration { + @override + up(Schema schema) { + schema.create('roles', (table) { + table.declare('role', ColumnType('varchar'))..primaryKey(); + }); + } + + @override + down(Schema schema) { + schema.drop('roles', cascade: true); + } +} + +class RoleUserMigration extends Migration { + @override + up(Schema schema) { + schema.create('role_users', (table) { + table + .declare('role_role', ColumnType('varchar')) + .references('roles', 'role'); + table + .declare('user_email', ColumnType('varchar')) + .references('users', 'email'); + }); + } + + @override + down(Schema schema) { + schema.drop('role_users'); + } +} + +class UserMigration extends Migration { + @override + up(Schema schema) { + schema.create('users', (table) { + table.varChar('email')..primaryKey(); + table.varChar('name'); + table.varChar('password'); + }); + } + + @override + down(Schema schema) { + schema.drop('users', cascade: true); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class RoleQuery extends Query { + RoleQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = RoleQueryWhere(this); + leftJoin(_users = RoleUserQuery(trampoline: trampoline, parent: this), + 'role', 'role_role', + additionalFields: const ['email', 'name', 'password'], + trampoline: trampoline); + } + + @override + final RoleQueryValues values = RoleQueryValues(); + + RoleQueryWhere _where; + + RoleUserQuery _users; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'roles'; + } + + @override + get fields { + return const ['role']; + } + + @override + RoleQueryWhere get where { + return _where; + } + + @override + RoleQueryWhere newWhereClause() { + return RoleQueryWhere(this); + } + + static Role parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Role(role: (row[0] as String)); + if (row.length > 1) { + model = model.copyWith( + users: [UserQuery.parseRow(row.skip(1).take(3).toList())] + .where((x) => x != null) + .toList()); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + RoleUserQuery get users { + return _users; + } + + @override + bool canCompile(trampoline) { + return (!(trampoline.contains('roles') && + trampoline.contains('role_users'))); + } + + @override + get(QueryExecutor executor) { + return super.get(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.role == model.role); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + users: List<_User>.from(l.users ?? []) + ..addAll(model.users ?? [])); + } + }); + }); + } + + @override + update(QueryExecutor executor) { + return super.update(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.role == model.role); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + users: List<_User>.from(l.users ?? []) + ..addAll(model.users ?? [])); + } + }); + }); + } + + @override + delete(QueryExecutor executor) { + return super.delete(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.role == model.role); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + users: List<_User>.from(l.users ?? []) + ..addAll(model.users ?? [])); + } + }); + }); + } +} + +class RoleQueryWhere extends QueryWhere { + RoleQueryWhere(RoleQuery query) + : role = StringSqlExpressionBuilder(query, 'role'); + + final StringSqlExpressionBuilder role; + + @override + get expressionBuilders { + return [role]; + } +} + +class RoleQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get role { + return (values['role'] as String); + } + + set role(String value) => values['role'] = value; + void copyFrom(Role model) { + role = model.role; + } +} + +class RoleUserQuery extends Query { + RoleUserQuery({Query parent, Set trampoline}) + : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = RoleUserQueryWhere(this); + leftJoin(_role = RoleQuery(trampoline: trampoline, parent: this), + 'role_role', 'role', + additionalFields: const ['role'], trampoline: trampoline); + leftJoin(_user = UserQuery(trampoline: trampoline, parent: this), + 'user_email', 'email', + additionalFields: const ['email', 'name', 'password'], + trampoline: trampoline); + } + + @override + final RoleUserQueryValues values = RoleUserQueryValues(); + + RoleUserQueryWhere _where; + + RoleQuery _role; + + UserQuery _user; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'role_users'; + } + + @override + get fields { + return const ['role_role', 'user_email']; + } + + @override + RoleUserQueryWhere get where { + return _where; + } + + @override + RoleUserQueryWhere newWhereClause() { + return RoleUserQueryWhere(this); + } + + static RoleUser parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = RoleUser(); + if (row.length > 2) { + model = model.copyWith( + role: RoleQuery.parseRow(row.skip(2).take(1).toList())); + } + if (row.length > 3) { + model = model.copyWith( + user: UserQuery.parseRow(row.skip(3).take(3).toList())); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + RoleQuery get role { + return _role; + } + + UserQuery get user { + return _user; + } +} + +class RoleUserQueryWhere extends QueryWhere { + RoleUserQueryWhere(RoleUserQuery query) + : roleRole = StringSqlExpressionBuilder(query, 'role_role'), + userEmail = StringSqlExpressionBuilder(query, 'user_email'); + + final StringSqlExpressionBuilder roleRole; + + final StringSqlExpressionBuilder userEmail; + + @override + get expressionBuilders { + return [roleRole, userEmail]; + } +} + +class RoleUserQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get roleRole { + return (values['role_role'] as String); + } + + set roleRole(String value) => values['role_role'] = value; + String get userEmail { + return (values['user_email'] as String); + } + + set userEmail(String value) => values['user_email'] = value; + void copyFrom(RoleUser model) { + if (model.role != null) { + values['role_role'] = model.role.role; + } + if (model.user != null) { + values['user_email'] = model.user.email; + } + } +} + +class UserQuery extends Query { + UserQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = UserQueryWhere(this); + leftJoin(_roles = RoleUserQuery(trampoline: trampoline, parent: this), + 'email', 'user_email', + additionalFields: const ['role'], trampoline: trampoline); + } + + @override + final UserQueryValues values = UserQueryValues(); + + UserQueryWhere _where; + + RoleUserQuery _roles; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'users'; + } + + @override + get fields { + return const ['email', 'name', 'password']; + } + + @override + UserQueryWhere get where { + return _where; + } + + @override + UserQueryWhere newWhereClause() { + return UserQueryWhere(this); + } + + static User parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = User( + email: (row[0] as String), + name: (row[1] as String), + password: (row[2] as String)); + if (row.length > 3) { + model = model.copyWith( + roles: [RoleQuery.parseRow(row.skip(3).take(1).toList())] + .where((x) => x != null) + .toList()); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + RoleUserQuery get roles { + return _roles; + } + + @override + bool canCompile(trampoline) { + return (!(trampoline.contains('users') && + trampoline.contains('role_users'))); + } + + @override + get(QueryExecutor executor) { + return super.get(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.email == model.email); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + roles: List<_Role>.from(l.roles ?? []) + ..addAll(model.roles ?? [])); + } + }); + }); + } + + @override + update(QueryExecutor executor) { + return super.update(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.email == model.email); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + roles: List<_Role>.from(l.roles ?? []) + ..addAll(model.roles ?? [])); + } + }); + }); + } + + @override + delete(QueryExecutor executor) { + return super.delete(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.email == model.email); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + roles: List<_Role>.from(l.roles ?? []) + ..addAll(model.roles ?? [])); + } + }); + }); + } +} + +class UserQueryWhere extends QueryWhere { + UserQueryWhere(UserQuery query) + : email = StringSqlExpressionBuilder(query, 'email'), + name = StringSqlExpressionBuilder(query, 'name'), + password = StringSqlExpressionBuilder(query, 'password'); + + final StringSqlExpressionBuilder email; + + final StringSqlExpressionBuilder name; + + final StringSqlExpressionBuilder password; + + @override + get expressionBuilders { + return [email, name, password]; + } +} + +class UserQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get email { + return (values['email'] as String); + } + + set email(String value) => values['email'] = value; + String get name { + return (values['name'] as String); + } + + set name(String value) => values['name'] = value; + String get password { + return (values['password'] as String); + } + + set password(String value) => values['password'] = value; + void copyFrom(User model) { + email = model.email; + name = model.name; + password = model.password; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Role implements _Role { + const Role({this.role, this.users}); + + @override + final String role; + + @override + final List<_User> users; + + Role copyWith({String role, List<_User> users}) { + return Role(role: role ?? this.role, users: users ?? this.users); + } + + bool operator ==(other) { + return other is _Role && + other.role == role && + ListEquality<_User>(DefaultEquality<_User>()) + .equals(other.users, users); + } + + @override + int get hashCode { + return hashObjects([role, users]); + } + + @override + String toString() { + return "Role(role=$role, users=$users)"; + } + + Map toJson() { + return RoleSerializer.toMap(this); + } +} + +@generatedSerializable +class RoleUser implements _RoleUser { + const RoleUser({this.role, this.user}); + + @override + final _Role role; + + @override + final _User user; + + RoleUser copyWith({_Role role, _User user}) { + return RoleUser(role: role ?? this.role, user: user ?? this.user); + } + + bool operator ==(other) { + return other is _RoleUser && other.role == role && other.user == user; + } + + @override + int get hashCode { + return hashObjects([role, user]); + } + + @override + String toString() { + return "RoleUser(role=$role, user=$user)"; + } + + Map toJson() { + return RoleUserSerializer.toMap(this); + } +} + +@generatedSerializable +class User implements _User { + const User({this.email, this.name, this.password, this.roles}); + + @override + final String email; + + @override + final String name; + + @override + final String password; + + @override + final List<_Role> roles; + + User copyWith( + {String email, String name, String password, List<_Role> roles}) { + return User( + email: email ?? this.email, + name: name ?? this.name, + password: password ?? this.password, + roles: roles ?? this.roles); + } + + bool operator ==(other) { + return other is _User && + other.email == email && + other.name == name && + other.password == password && + ListEquality<_Role>(DefaultEquality<_Role>()) + .equals(other.roles, roles); + } + + @override + int get hashCode { + return hashObjects([email, name, password, roles]); + } + + @override + String toString() { + return "User(email=$email, name=$name, password=$password, roles=$roles)"; + } + + Map toJson() { + return UserSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const RoleSerializer roleSerializer = RoleSerializer(); + +class RoleEncoder extends Converter { + const RoleEncoder(); + + @override + Map convert(Role model) => RoleSerializer.toMap(model); +} + +class RoleDecoder extends Converter { + const RoleDecoder(); + + @override + Role convert(Map map) => RoleSerializer.fromMap(map); +} + +class RoleSerializer extends Codec { + const RoleSerializer(); + + @override + get encoder => const RoleEncoder(); + @override + get decoder => const RoleDecoder(); + static Role fromMap(Map map) { + return Role( + role: map['role'] as String, + users: map['users'] is Iterable + ? List.unmodifiable(((map['users'] as Iterable).whereType()) + .map(UserSerializer.fromMap)) + : null); + } + + static Map toMap(_Role model) { + if (model == null) { + return null; + } + return { + 'role': model.role, + 'users': model.users?.map((m) => UserSerializer.toMap(m))?.toList() + }; + } +} + +abstract class RoleFields { + static const List allFields = [role, users]; + + static const String role = 'role'; + + static const String users = 'users'; +} + +const RoleUserSerializer roleUserSerializer = RoleUserSerializer(); + +class RoleUserEncoder extends Converter { + const RoleUserEncoder(); + + @override + Map convert(RoleUser model) => RoleUserSerializer.toMap(model); +} + +class RoleUserDecoder extends Converter { + const RoleUserDecoder(); + + @override + RoleUser convert(Map map) => RoleUserSerializer.fromMap(map); +} + +class RoleUserSerializer extends Codec { + const RoleUserSerializer(); + + @override + get encoder => const RoleUserEncoder(); + @override + get decoder => const RoleUserDecoder(); + static RoleUser fromMap(Map map) { + return RoleUser( + role: map['role'] != null + ? RoleSerializer.fromMap(map['role'] as Map) + : null, + user: map['user'] != null + ? UserSerializer.fromMap(map['user'] as Map) + : null); + } + + static Map toMap(_RoleUser model) { + if (model == null) { + return null; + } + return { + 'role': RoleSerializer.toMap(model.role), + 'user': UserSerializer.toMap(model.user) + }; + } +} + +abstract class RoleUserFields { + static const List allFields = [role, user]; + + static const String role = 'role'; + + static const String user = 'user'; +} + +const UserSerializer userSerializer = UserSerializer(); + +class UserEncoder extends Converter { + const UserEncoder(); + + @override + Map convert(User model) => UserSerializer.toMap(model); +} + +class UserDecoder extends Converter { + const UserDecoder(); + + @override + User convert(Map map) => UserSerializer.fromMap(map); +} + +class UserSerializer extends Codec { + const UserSerializer(); + + @override + get encoder => const UserEncoder(); + @override + get decoder => const UserDecoder(); + static User fromMap(Map map) { + return User( + email: map['email'] as String, + name: map['name'] as String, + password: map['password'] as String, + roles: map['roles'] is Iterable + ? List.unmodifiable(((map['roles'] as Iterable).whereType()) + .map(RoleSerializer.fromMap)) + : null); + } + + static Map toMap(_User model) { + if (model == null) { + return null; + } + return { + 'email': model.email, + 'name': model.name, + 'password': model.password, + 'roles': model.roles?.map((m) => RoleSerializer.toMap(m))?.toList() + }; + } +} + +abstract class UserFields { + static const List allFields = [email, name, password, roles]; + + static const String email = 'email'; + + static const String name = 'name'; + + static const String password = 'password'; + + static const String roles = 'roles'; +} diff --git a/angel_orm_test/lib/src/models/has_car.g.dart b/angel_orm_test/lib/src/models/has_car.g.dart new file mode 100644 index 00000000..b8adf3d5 --- /dev/null +++ b/angel_orm_test/lib/src/models/has_car.g.dart @@ -0,0 +1,275 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'has_car.dart'; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class HasCarMigration extends Migration { + @override + up(Schema schema) { + schema.create('has_cars', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.integer('type'); + }); + } + + @override + down(Schema schema) { + schema.drop('has_cars'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class HasCarQuery extends Query { + HasCarQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = HasCarQueryWhere(this); + } + + @override + final HasCarQueryValues values = HasCarQueryValues(); + + HasCarQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'has_cars'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'type']; + } + + @override + HasCarQueryWhere get where { + return _where; + } + + @override + HasCarQueryWhere newWhereClause() { + return HasCarQueryWhere(this); + } + + static HasCar parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = HasCar( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + type: row[3] == null ? null : CarType.values[(row[3] as int)]); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class HasCarQueryWhere extends QueryWhere { + HasCarQueryWhere(HasCarQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + type = EnumSqlExpressionBuilder(query, 'type', (v) => v.index); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final EnumSqlExpressionBuilder type; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, type]; + } +} + +class HasCarQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + CarType get type { + return CarType.values[(values['type'] as int)]; + } + + set type(CarType value) => values['type'] = value?.index; + void copyFrom(HasCar model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + type = model.type; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class HasCar extends _HasCar { + HasCar({this.id, this.createdAt, this.updatedAt, this.type = CarType.sedan}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + final CarType type; + + HasCar copyWith( + {String id, DateTime createdAt, DateTime updatedAt, CarType type}) { + return HasCar( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + type: type ?? this.type); + } + + bool operator ==(other) { + return other is _HasCar && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.type == type; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, type]); + } + + @override + String toString() { + return "HasCar(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, type=$type)"; + } + + Map toJson() { + return HasCarSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const HasCarSerializer hasCarSerializer = HasCarSerializer(); + +class HasCarEncoder extends Converter { + const HasCarEncoder(); + + @override + Map convert(HasCar model) => HasCarSerializer.toMap(model); +} + +class HasCarDecoder extends Converter { + const HasCarDecoder(); + + @override + HasCar convert(Map map) => HasCarSerializer.fromMap(map); +} + +class HasCarSerializer extends Codec { + const HasCarSerializer(); + + @override + get encoder => const HasCarEncoder(); + @override + get decoder => const HasCarDecoder(); + static HasCar fromMap(Map map) { + if (map['type'] == null) { + throw FormatException("Missing required field 'type' on HasCar."); + } + + return HasCar( + id: map['id'] as String, + 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, + type: map['type'] is CarType + ? (map['type'] as CarType) + : (map['type'] is int + ? CarType.values[map['type'] as int] + : CarType.sedan)); + } + + static Map toMap(_HasCar model) { + if (model == null) { + return null; + } + if (model.type == null) { + throw FormatException("Missing required field 'type' on HasCar."); + } + + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'type': model.type == null ? null : CarType.values.indexOf(model.type) + }; + } +} + +abstract class HasCarFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + type + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String type = 'type'; +} diff --git a/angel_orm_test/lib/src/models/has_map.g.dart b/angel_orm_test/lib/src/models/has_map.g.dart new file mode 100644 index 00000000..71a612f7 --- /dev/null +++ b/angel_orm_test/lib/src/models/has_map.g.dart @@ -0,0 +1,208 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'has_map.dart'; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class HasMapMigration extends Migration { + @override + up(Schema schema) { + schema.create('has_maps', (table) { + table.declare('value', ColumnType('jsonb')); + table.declare('list', ColumnType('jsonb')); + }); + } + + @override + down(Schema schema) { + schema.drop('has_maps'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class HasMapQuery extends Query { + HasMapQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = HasMapQueryWhere(this); + } + + @override + final HasMapQueryValues values = HasMapQueryValues(); + + HasMapQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'has_maps'; + } + + @override + get fields { + return const ['value', 'list']; + } + + @override + HasMapQueryWhere get where { + return _where; + } + + @override + HasMapQueryWhere newWhereClause() { + return HasMapQueryWhere(this); + } + + static HasMap parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = HasMap( + value: (row[0] as Map), + list: (row[1] as List)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class HasMapQueryWhere extends QueryWhere { + HasMapQueryWhere(HasMapQuery query) + : value = MapSqlExpressionBuilder(query, 'value'), + list = ListSqlExpressionBuilder(query, 'list'); + + final MapSqlExpressionBuilder value; + + final ListSqlExpressionBuilder list; + + @override + get expressionBuilders { + return [value, list]; + } +} + +class HasMapQueryValues extends MapQueryValues { + @override + get casts { + return {'list': 'jsonb'}; + } + + Map get value { + return (values['value'] as Map); + } + + set value(Map value) => values['value'] = value; + List get list { + return (json.decode((values['list'] as String)) as List); + } + + set list(List value) => values['list'] = json.encode(value); + void copyFrom(HasMap model) { + value = model.value; + list = model.list; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class HasMap implements _HasMap { + const HasMap({this.value, this.list}); + + @override + final Map value; + + @override + final List list; + + HasMap copyWith({Map value, List list}) { + return HasMap(value: value ?? this.value, list: list ?? this.list); + } + + bool operator ==(other) { + return other is _HasMap && + MapEquality( + keys: DefaultEquality(), values: DefaultEquality()) + .equals(other.value, value) && + ListEquality(DefaultEquality()).equals(other.list, list); + } + + @override + int get hashCode { + return hashObjects([value, list]); + } + + @override + String toString() { + return "HasMap(value=$value, list=$list)"; + } + + Map toJson() { + return HasMapSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const HasMapSerializer hasMapSerializer = HasMapSerializer(); + +class HasMapEncoder extends Converter { + const HasMapEncoder(); + + @override + Map convert(HasMap model) => HasMapSerializer.toMap(model); +} + +class HasMapDecoder extends Converter { + const HasMapDecoder(); + + @override + HasMap convert(Map map) => HasMapSerializer.fromMap(map); +} + +class HasMapSerializer extends Codec { + const HasMapSerializer(); + + @override + get encoder => const HasMapEncoder(); + @override + get decoder => const HasMapDecoder(); + static HasMap fromMap(Map map) { + return HasMap( + value: map['value'] is Map + ? (map['value'] as Map).cast() + : null, + list: map['list'] is Iterable + ? (map['list'] as Iterable).cast().toList() + : null); + } + + static Map toMap(_HasMap model) { + if (model == null) { + return null; + } + return {'value': model.value, 'list': model.list}; + } +} + +abstract class HasMapFields { + static const List allFields = [value, list]; + + static const String value = 'value'; + + static const String list = 'list'; +} diff --git a/angel_orm_test/lib/src/models/leg.g.dart b/angel_orm_test/lib/src/models/leg.g.dart new file mode 100644 index 00000000..1575ddfb --- /dev/null +++ b/angel_orm_test/lib/src/models/leg.g.dart @@ -0,0 +1,568 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of angel_orm_generator.test.models.leg; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class LegMigration extends Migration { + @override + up(Schema schema) { + schema.create('legs', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.varChar('name'); + }); + } + + @override + down(Schema schema) { + schema.drop('legs', cascade: true); + } +} + +class FootMigration extends Migration { + @override + up(Schema schema) { + schema.create('feet', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.integer('leg_id'); + table.declare('n_toes', ColumnType('decimal')); + }); + } + + @override + down(Schema schema) { + schema.drop('feet'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class LegQuery extends Query { + LegQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = LegQueryWhere(this); + leftJoin( + _foot = FootQuery(trampoline: trampoline, parent: this), 'id', 'leg_id', + additionalFields: const [ + 'id', + 'created_at', + 'updated_at', + 'leg_id', + 'n_toes' + ], + trampoline: trampoline); + } + + @override + final LegQueryValues values = LegQueryValues(); + + LegQueryWhere _where; + + FootQuery _foot; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'legs'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'name']; + } + + @override + LegQueryWhere get where { + return _where; + } + + @override + LegQueryWhere newWhereClause() { + return LegQueryWhere(this); + } + + static Leg parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Leg( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + name: (row[3] as String)); + if (row.length > 4) { + model = model.copyWith( + foot: FootQuery.parseRow(row.skip(4).take(5).toList())); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + FootQuery get foot { + return _foot; + } +} + +class LegQueryWhere extends QueryWhere { + LegQueryWhere(LegQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + name = StringSqlExpressionBuilder(query, 'name'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final StringSqlExpressionBuilder name; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, name]; + } +} + +class LegQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + String get name { + return (values['name'] as String); + } + + set name(String value) => values['name'] = value; + void copyFrom(Leg model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + name = model.name; + } +} + +class FootQuery extends Query { + FootQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = FootQueryWhere(this); + } + + @override + final FootQueryValues values = FootQueryValues(); + + FootQueryWhere _where; + + @override + get casts { + return {'n_toes': 'text'}; + } + + @override + get tableName { + return 'feet'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'leg_id', 'n_toes']; + } + + @override + FootQueryWhere get where { + return _where; + } + + @override + FootQueryWhere newWhereClause() { + return FootQueryWhere(this); + } + + static Foot parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Foot( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + legId: (row[3] as int), + nToes: double.tryParse(row[4].toString())); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class FootQueryWhere extends QueryWhere { + FootQueryWhere(FootQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + legId = NumericSqlExpressionBuilder(query, 'leg_id'), + nToes = NumericSqlExpressionBuilder(query, 'n_toes'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final NumericSqlExpressionBuilder legId; + + final NumericSqlExpressionBuilder nToes; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, legId, nToes]; + } +} + +class FootQueryValues extends MapQueryValues { + @override + get casts { + return {'n_toes': 'decimal'}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + int get legId { + return (values['leg_id'] as int); + } + + set legId(int value) => values['leg_id'] = value; + double get nToes { + return double.tryParse((values['n_toes'] as String)); + } + + set nToes(double value) => values['n_toes'] = value.toString(); + void copyFrom(Foot model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + legId = model.legId; + nToes = model.nToes; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Leg extends _Leg { + Leg({this.id, this.createdAt, this.updatedAt, this.foot, this.name}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + _Foot foot; + + @override + String name; + + Leg copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + _Foot foot, + String name}) { + return Leg( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + foot: foot ?? this.foot, + name: name ?? this.name); + } + + bool operator ==(other) { + return other is _Leg && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.foot == foot && + other.name == name; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, foot, name]); + } + + @override + String toString() { + return "Leg(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, foot=$foot, name=$name)"; + } + + Map toJson() { + return LegSerializer.toMap(this); + } +} + +@generatedSerializable +class Foot extends _Foot { + Foot({this.id, this.createdAt, this.updatedAt, this.legId, this.nToes}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + int legId; + + @override + double nToes; + + Foot copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + int legId, + double nToes}) { + return Foot( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + legId: legId ?? this.legId, + nToes: nToes ?? this.nToes); + } + + bool operator ==(other) { + return other is _Foot && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.legId == legId && + other.nToes == nToes; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, legId, nToes]); + } + + @override + String toString() { + return "Foot(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, legId=$legId, nToes=$nToes)"; + } + + Map toJson() { + return FootSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const LegSerializer legSerializer = LegSerializer(); + +class LegEncoder extends Converter { + const LegEncoder(); + + @override + Map convert(Leg model) => LegSerializer.toMap(model); +} + +class LegDecoder extends Converter { + const LegDecoder(); + + @override + Leg convert(Map map) => LegSerializer.fromMap(map); +} + +class LegSerializer extends Codec { + const LegSerializer(); + + @override + get encoder => const LegEncoder(); + @override + get decoder => const LegDecoder(); + static Leg fromMap(Map map) { + return Leg( + id: map['id'] as String, + 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, + foot: map['foot'] != null + ? FootSerializer.fromMap(map['foot'] as Map) + : null, + name: map['name'] as String); + } + + static Map toMap(_Leg model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'foot': FootSerializer.toMap(model.foot), + 'name': model.name + }; + } +} + +abstract class LegFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + foot, + name + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String foot = 'foot'; + + static const String name = 'name'; +} + +const FootSerializer footSerializer = FootSerializer(); + +class FootEncoder extends Converter { + const FootEncoder(); + + @override + Map convert(Foot model) => FootSerializer.toMap(model); +} + +class FootDecoder extends Converter { + const FootDecoder(); + + @override + Foot convert(Map map) => FootSerializer.fromMap(map); +} + +class FootSerializer extends Codec { + const FootSerializer(); + + @override + get encoder => const FootEncoder(); + @override + get decoder => const FootDecoder(); + static Foot fromMap(Map map) { + return Foot( + id: map['id'] as String, + 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, + legId: map['leg_id'] as int, + nToes: map['n_toes'] as double); + } + + static Map toMap(_Foot model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'leg_id': model.legId, + 'n_toes': model.nToes + }; + } +} + +abstract class FootFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + legId, + nToes + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String legId = 'leg_id'; + + static const String nToes = 'n_toes'; +} diff --git a/angel_orm_test/lib/src/models/order.g.dart b/angel_orm_test/lib/src/models/order.g.dart new file mode 100644 index 00000000..edff71ca --- /dev/null +++ b/angel_orm_test/lib/src/models/order.g.dart @@ -0,0 +1,598 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of angel_orm_generator.test.models.order; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class OrderMigration extends Migration { + @override + up(Schema schema) { + schema.create('orders', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.integer('employee_id'); + table.timeStamp('order_date'); + table.integer('shipper_id'); + table + .declare('customer_id', ColumnType('serial')) + .references('customers', 'id'); + }); + } + + @override + down(Schema schema) { + schema.drop('orders'); + } +} + +class CustomerMigration extends Migration { + @override + up(Schema schema) { + schema.create('customers', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + }); + } + + @override + down(Schema schema) { + schema.drop('customers'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class OrderQuery extends Query { + OrderQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = OrderQueryWhere(this); + leftJoin(_customer = CustomerQuery(trampoline: trampoline, parent: this), + 'customer_id', 'id', + additionalFields: const ['id', 'created_at', 'updated_at'], + trampoline: trampoline); + } + + @override + final OrderQueryValues values = OrderQueryValues(); + + OrderQueryWhere _where; + + CustomerQuery _customer; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'orders'; + } + + @override + get fields { + return const [ + 'id', + 'created_at', + 'updated_at', + 'customer_id', + 'employee_id', + 'order_date', + 'shipper_id' + ]; + } + + @override + OrderQueryWhere get where { + return _where; + } + + @override + OrderQueryWhere newWhereClause() { + return OrderQueryWhere(this); + } + + static Order parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Order( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + employeeId: (row[4] as int), + orderDate: (row[5] as DateTime), + shipperId: (row[6] as int)); + if (row.length > 7) { + model = model.copyWith( + customer: CustomerQuery.parseRow(row.skip(7).take(3).toList())); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + CustomerQuery get customer { + return _customer; + } +} + +class OrderQueryWhere extends QueryWhere { + OrderQueryWhere(OrderQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + customerId = NumericSqlExpressionBuilder(query, 'customer_id'), + employeeId = NumericSqlExpressionBuilder(query, 'employee_id'), + orderDate = DateTimeSqlExpressionBuilder(query, 'order_date'), + shipperId = NumericSqlExpressionBuilder(query, 'shipper_id'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final NumericSqlExpressionBuilder customerId; + + final NumericSqlExpressionBuilder employeeId; + + final DateTimeSqlExpressionBuilder orderDate; + + final NumericSqlExpressionBuilder shipperId; + + @override + get expressionBuilders { + return [ + id, + createdAt, + updatedAt, + customerId, + employeeId, + orderDate, + shipperId + ]; + } +} + +class OrderQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + int get customerId { + return (values['customer_id'] as int); + } + + set customerId(int value) => values['customer_id'] = value; + int get employeeId { + return (values['employee_id'] as int); + } + + set employeeId(int value) => values['employee_id'] = value; + DateTime get orderDate { + return (values['order_date'] as DateTime); + } + + set orderDate(DateTime value) => values['order_date'] = value; + int get shipperId { + return (values['shipper_id'] as int); + } + + set shipperId(int value) => values['shipper_id'] = value; + void copyFrom(Order model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + employeeId = model.employeeId; + orderDate = model.orderDate; + shipperId = model.shipperId; + if (model.customer != null) { + values['customer_id'] = model.customer.id; + } + } +} + +class CustomerQuery extends Query { + CustomerQuery({Query parent, Set trampoline}) + : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = CustomerQueryWhere(this); + } + + @override + final CustomerQueryValues values = CustomerQueryValues(); + + CustomerQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'customers'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at']; + } + + @override + CustomerQueryWhere get where { + return _where; + } + + @override + CustomerQueryWhere newWhereClause() { + return CustomerQueryWhere(this); + } + + static Customer parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Customer( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class CustomerQueryWhere extends QueryWhere { + CustomerQueryWhere(CustomerQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt]; + } +} + +class CustomerQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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(Customer model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Order extends _Order { + Order( + {this.id, + this.createdAt, + this.updatedAt, + this.customer, + this.employeeId, + this.orderDate, + this.shipperId}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + final _Customer customer; + + @override + final int employeeId; + + @override + final DateTime orderDate; + + @override + final int shipperId; + + Order copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + _Customer customer, + int employeeId, + DateTime orderDate, + int shipperId}) { + return Order( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + customer: customer ?? this.customer, + employeeId: employeeId ?? this.employeeId, + orderDate: orderDate ?? this.orderDate, + shipperId: shipperId ?? this.shipperId); + } + + bool operator ==(other) { + return other is _Order && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.customer == customer && + other.employeeId == employeeId && + other.orderDate == orderDate && + other.shipperId == shipperId; + } + + @override + int get hashCode { + return hashObjects( + [id, createdAt, updatedAt, customer, employeeId, orderDate, shipperId]); + } + + @override + String toString() { + return "Order(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, customer=$customer, employeeId=$employeeId, orderDate=$orderDate, shipperId=$shipperId)"; + } + + Map toJson() { + return OrderSerializer.toMap(this); + } +} + +@generatedSerializable +class Customer extends _Customer { + Customer({this.id, this.createdAt, this.updatedAt}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + Customer copyWith({String id, DateTime createdAt, DateTime updatedAt}) { + return Customer( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt); + } + + bool operator ==(other) { + return other is _Customer && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt]); + } + + @override + String toString() { + return "Customer(id=$id, createdAt=$createdAt, updatedAt=$updatedAt)"; + } + + Map toJson() { + return CustomerSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const OrderSerializer orderSerializer = OrderSerializer(); + +class OrderEncoder extends Converter { + const OrderEncoder(); + + @override + Map convert(Order model) => OrderSerializer.toMap(model); +} + +class OrderDecoder extends Converter { + const OrderDecoder(); + + @override + Order convert(Map map) => OrderSerializer.fromMap(map); +} + +class OrderSerializer extends Codec { + const OrderSerializer(); + + @override + get encoder => const OrderEncoder(); + @override + get decoder => const OrderDecoder(); + static Order fromMap(Map map) { + return Order( + id: map['id'] as String, + 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, + customer: map['customer'] != null + ? CustomerSerializer.fromMap(map['customer'] as Map) + : null, + employeeId: map['employee_id'] as int, + orderDate: map['order_date'] != null + ? (map['order_date'] is DateTime + ? (map['order_date'] as DateTime) + : DateTime.parse(map['order_date'].toString())) + : null, + shipperId: map['shipper_id'] as int); + } + + static Map toMap(_Order model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'customer': CustomerSerializer.toMap(model.customer), + 'employee_id': model.employeeId, + 'order_date': model.orderDate?.toIso8601String(), + 'shipper_id': model.shipperId + }; + } +} + +abstract class OrderFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + customer, + employeeId, + orderDate, + shipperId + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String customer = 'customer'; + + static const String employeeId = 'employee_id'; + + static const String orderDate = 'order_date'; + + static const String shipperId = 'shipper_id'; +} + +const CustomerSerializer customerSerializer = CustomerSerializer(); + +class CustomerEncoder extends Converter { + const CustomerEncoder(); + + @override + Map convert(Customer model) => CustomerSerializer.toMap(model); +} + +class CustomerDecoder extends Converter { + const CustomerDecoder(); + + @override + Customer convert(Map map) => CustomerSerializer.fromMap(map); +} + +class CustomerSerializer extends Codec { + const CustomerSerializer(); + + @override + get encoder => const CustomerEncoder(); + @override + get decoder => const CustomerDecoder(); + static Customer fromMap(Map map) { + return Customer( + id: map['id'] as String, + 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(_Customer model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String() + }; + } +} + +abstract class CustomerFields { + static const List allFields = [id, createdAt, updatedAt]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; +} diff --git a/angel_orm_test/lib/src/models/tree.g.dart b/angel_orm_test/lib/src/models/tree.g.dart new file mode 100644 index 00000000..b20ca724 --- /dev/null +++ b/angel_orm_test/lib/src/models/tree.g.dart @@ -0,0 +1,636 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of angel_orm_generator.test.models.tree; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class TreeMigration extends Migration { + @override + up(Schema schema) { + schema.create('trees', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.declare('rings', ColumnType('smallint')); + }); + } + + @override + down(Schema schema) { + schema.drop('trees', cascade: true); + } +} + +class FruitMigration extends Migration { + @override + up(Schema schema) { + schema.create('fruits', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.integer('tree_id'); + table.varChar('common_name'); + }); + } + + @override + down(Schema schema) { + schema.drop('fruits'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class TreeQuery extends Query { + TreeQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = TreeQueryWhere(this); + leftJoin(_fruits = FruitQuery(trampoline: trampoline, parent: this), 'id', + 'tree_id', + additionalFields: const [ + 'id', + 'created_at', + 'updated_at', + 'tree_id', + 'common_name' + ], + trampoline: trampoline); + } + + @override + final TreeQueryValues values = TreeQueryValues(); + + TreeQueryWhere _where; + + FruitQuery _fruits; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'trees'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'rings']; + } + + @override + TreeQueryWhere get where { + return _where; + } + + @override + TreeQueryWhere newWhereClause() { + return TreeQueryWhere(this); + } + + static Tree parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Tree( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + rings: (row[3] as int)); + if (row.length > 4) { + model = model.copyWith( + fruits: [FruitQuery.parseRow(row.skip(4).take(5).toList())] + .where((x) => x != null) + .toList()); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + FruitQuery get fruits { + return _fruits; + } + + @override + get(QueryExecutor executor) { + return super.get(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + fruits: List<_Fruit>.from(l.fruits ?? []) + ..addAll(model.fruits ?? [])); + } + }); + }); + } + + @override + update(QueryExecutor executor) { + return super.update(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + fruits: List<_Fruit>.from(l.fruits ?? []) + ..addAll(model.fruits ?? [])); + } + }); + }); + } + + @override + delete(QueryExecutor executor) { + return super.delete(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + fruits: List<_Fruit>.from(l.fruits ?? []) + ..addAll(model.fruits ?? [])); + } + }); + }); + } +} + +class TreeQueryWhere extends QueryWhere { + TreeQueryWhere(TreeQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + rings = NumericSqlExpressionBuilder(query, 'rings'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final NumericSqlExpressionBuilder rings; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, rings]; + } +} + +class TreeQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + int get rings { + return (values['rings'] as int); + } + + set rings(int value) => values['rings'] = value; + void copyFrom(Tree model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + rings = model.rings; + } +} + +class FruitQuery extends Query { + FruitQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = FruitQueryWhere(this); + } + + @override + final FruitQueryValues values = FruitQueryValues(); + + FruitQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'fruits'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'tree_id', 'common_name']; + } + + @override + FruitQueryWhere get where { + return _where; + } + + @override + FruitQueryWhere newWhereClause() { + return FruitQueryWhere(this); + } + + static Fruit parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Fruit( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + treeId: (row[3] as int), + commonName: (row[4] as String)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class FruitQueryWhere extends QueryWhere { + FruitQueryWhere(FruitQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + treeId = NumericSqlExpressionBuilder(query, 'tree_id'), + commonName = StringSqlExpressionBuilder(query, 'common_name'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final NumericSqlExpressionBuilder treeId; + + final StringSqlExpressionBuilder commonName; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, treeId, commonName]; + } +} + +class FruitQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + int get treeId { + return (values['tree_id'] as int); + } + + set treeId(int value) => values['tree_id'] = value; + String get commonName { + return (values['common_name'] as String); + } + + set commonName(String value) => values['common_name'] = value; + void copyFrom(Fruit model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + treeId = model.treeId; + commonName = model.commonName; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Tree extends _Tree { + Tree( + {this.id, + this.createdAt, + this.updatedAt, + this.rings, + List<_Fruit> fruits}) + : this.fruits = List.unmodifiable(fruits ?? []); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + int rings; + + @override + List<_Fruit> fruits; + + Tree copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + int rings, + List<_Fruit> fruits}) { + return Tree( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + rings: rings ?? this.rings, + fruits: fruits ?? this.fruits); + } + + bool operator ==(other) { + return other is _Tree && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.rings == rings && + ListEquality<_Fruit>(DefaultEquality<_Fruit>()) + .equals(other.fruits, fruits); + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, rings, fruits]); + } + + @override + String toString() { + return "Tree(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, rings=$rings, fruits=$fruits)"; + } + + Map toJson() { + return TreeSerializer.toMap(this); + } +} + +@generatedSerializable +class Fruit extends _Fruit { + Fruit( + {this.id, this.createdAt, this.updatedAt, this.treeId, this.commonName}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + int treeId; + + @override + String commonName; + + Fruit copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + int treeId, + String commonName}) { + return Fruit( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + treeId: treeId ?? this.treeId, + commonName: commonName ?? this.commonName); + } + + bool operator ==(other) { + return other is _Fruit && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.treeId == treeId && + other.commonName == commonName; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, treeId, commonName]); + } + + @override + String toString() { + return "Fruit(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, treeId=$treeId, commonName=$commonName)"; + } + + Map toJson() { + return FruitSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const TreeSerializer treeSerializer = TreeSerializer(); + +class TreeEncoder extends Converter { + const TreeEncoder(); + + @override + Map convert(Tree model) => TreeSerializer.toMap(model); +} + +class TreeDecoder extends Converter { + const TreeDecoder(); + + @override + Tree convert(Map map) => TreeSerializer.fromMap(map); +} + +class TreeSerializer extends Codec { + const TreeSerializer(); + + @override + get encoder => const TreeEncoder(); + @override + get decoder => const TreeDecoder(); + static Tree fromMap(Map map) { + return Tree( + id: map['id'] as String, + 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, + rings: map['rings'] as int, + fruits: map['fruits'] is Iterable + ? List.unmodifiable(((map['fruits'] as Iterable).whereType()) + .map(FruitSerializer.fromMap)) + : null); + } + + static Map toMap(_Tree model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'rings': model.rings, + 'fruits': model.fruits?.map((m) => FruitSerializer.toMap(m))?.toList() + }; + } +} + +abstract class TreeFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + rings, + fruits + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String rings = 'rings'; + + static const String fruits = 'fruits'; +} + +const FruitSerializer fruitSerializer = FruitSerializer(); + +class FruitEncoder extends Converter { + const FruitEncoder(); + + @override + Map convert(Fruit model) => FruitSerializer.toMap(model); +} + +class FruitDecoder extends Converter { + const FruitDecoder(); + + @override + Fruit convert(Map map) => FruitSerializer.fromMap(map); +} + +class FruitSerializer extends Codec { + const FruitSerializer(); + + @override + get encoder => const FruitEncoder(); + @override + get decoder => const FruitDecoder(); + static Fruit fromMap(Map map) { + return Fruit( + id: map['id'] as String, + 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, + treeId: map['tree_id'] as int, + commonName: map['common_name'] as String); + } + + static Map toMap(_Fruit model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'tree_id': model.treeId, + 'common_name': model.commonName + }; + } +} + +abstract class FruitFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + treeId, + commonName + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String treeId = 'tree_id'; + + static const String commonName = 'common_name'; +} diff --git a/angel_orm_test/lib/src/models/unorthodox.g.dart b/angel_orm_test/lib/src/models/unorthodox.g.dart new file mode 100644 index 00000000..558674e0 --- /dev/null +++ b/angel_orm_test/lib/src/models/unorthodox.g.dart @@ -0,0 +1,1481 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'unorthodox.dart'; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class UnorthodoxMigration extends Migration { + @override + up(Schema schema) { + schema.create('unorthodoxes', (table) { + table.varChar('name'); + }); + } + + @override + down(Schema schema) { + schema.drop('unorthodoxes'); + } +} + +class WeirdJoinMigration extends Migration { + @override + up(Schema schema) { + schema.create('weird_joins', (table) { + table.integer('id')..primaryKey(); + table + .declare('join_name', ColumnType('varchar')) + .references('unorthodoxes', 'name'); + }); + } + + @override + down(Schema schema) { + schema.drop('weird_joins', cascade: true); + } +} + +class SongMigration extends Migration { + @override + up(Schema schema) { + schema.create('songs', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.integer('weird_join_id'); + table.varChar('title'); + }); + } + + @override + down(Schema schema) { + schema.drop('songs'); + } +} + +class NumbaMigration extends Migration { + @override + up(Schema schema) { + schema.create('numbas', (table) { + table.integer('i')..primaryKey(); + table.integer('parent'); + }); + } + + @override + down(Schema schema) { + schema.drop('numbas'); + } +} + +class FooMigration extends Migration { + @override + up(Schema schema) { + schema.create('foos', (table) { + table.varChar('bar')..primaryKey(); + }); + } + + @override + down(Schema schema) { + schema.drop('foos', cascade: true); + } +} + +class FooPivotMigration extends Migration { + @override + up(Schema schema) { + schema.create('foo_pivots', (table) { + table + .declare('weird_join_id', ColumnType('int')) + .references('weird_joins', 'id'); + table.declare('foo_bar', ColumnType('varchar')).references('foos', 'bar'); + }); + } + + @override + down(Schema schema) { + schema.drop('foo_pivots'); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class UnorthodoxQuery extends Query { + UnorthodoxQuery({Query parent, Set trampoline}) + : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = UnorthodoxQueryWhere(this); + } + + @override + final UnorthodoxQueryValues values = UnorthodoxQueryValues(); + + UnorthodoxQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'unorthodoxes'; + } + + @override + get fields { + return const ['name']; + } + + @override + UnorthodoxQueryWhere get where { + return _where; + } + + @override + UnorthodoxQueryWhere newWhereClause() { + return UnorthodoxQueryWhere(this); + } + + static Unorthodox parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Unorthodox(name: (row[0] as String)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class UnorthodoxQueryWhere extends QueryWhere { + UnorthodoxQueryWhere(UnorthodoxQuery query) + : name = StringSqlExpressionBuilder(query, 'name'); + + final StringSqlExpressionBuilder name; + + @override + get expressionBuilders { + return [name]; + } +} + +class UnorthodoxQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get name { + return (values['name'] as String); + } + + set name(String value) => values['name'] = value; + void copyFrom(Unorthodox model) { + name = model.name; + } +} + +class WeirdJoinQuery extends Query { + WeirdJoinQuery({Query parent, Set trampoline}) + : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = WeirdJoinQueryWhere(this); + leftJoin( + _unorthodox = UnorthodoxQuery(trampoline: trampoline, parent: this), + 'join_name', + 'name', + additionalFields: const ['name'], + trampoline: trampoline); + leftJoin(_song = SongQuery(trampoline: trampoline, parent: this), 'id', + 'weird_join_id', + additionalFields: const [ + 'id', + 'created_at', + 'updated_at', + 'weird_join_id', + 'title' + ], + trampoline: trampoline); + leftJoin(_numbas = NumbaQuery(trampoline: trampoline, parent: this), 'id', + 'parent', + additionalFields: const ['i', 'parent'], trampoline: trampoline); + leftJoin(_foos = FooPivotQuery(trampoline: trampoline, parent: this), 'id', + 'weird_join_id', + additionalFields: const ['bar'], trampoline: trampoline); + } + + @override + final WeirdJoinQueryValues values = WeirdJoinQueryValues(); + + WeirdJoinQueryWhere _where; + + UnorthodoxQuery _unorthodox; + + SongQuery _song; + + NumbaQuery _numbas; + + FooPivotQuery _foos; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'weird_joins'; + } + + @override + get fields { + return const ['id', 'join_name']; + } + + @override + WeirdJoinQueryWhere get where { + return _where; + } + + @override + WeirdJoinQueryWhere newWhereClause() { + return WeirdJoinQueryWhere(this); + } + + static WeirdJoin parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = WeirdJoin(id: (row[0] as int)); + if (row.length > 2) { + model = model.copyWith( + unorthodox: UnorthodoxQuery.parseRow(row.skip(2).take(1).toList())); + } + if (row.length > 3) { + model = model.copyWith( + song: SongQuery.parseRow(row.skip(3).take(5).toList())); + } + if (row.length > 8) { + model = model.copyWith( + numbas: [NumbaQuery.parseRow(row.skip(8).take(2).toList())] + .where((x) => x != null) + .toList()); + } + if (row.length > 10) { + model = model.copyWith( + foos: [FooQuery.parseRow(row.skip(10).take(1).toList())] + .where((x) => x != null) + .toList()); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + UnorthodoxQuery get unorthodox { + return _unorthodox; + } + + SongQuery get song { + return _song; + } + + NumbaQuery get numbas { + return _numbas; + } + + FooPivotQuery get foos { + return _foos; + } + + @override + bool canCompile(trampoline) { + return (!(trampoline.contains('weird_joins') && + trampoline.contains('foo_pivots'))); + } + + @override + get(QueryExecutor executor) { + return super.get(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + numbas: List<_Numba>.from(l.numbas ?? []) + ..addAll(model.numbas ?? []), + foos: List<_Foo>.from(l.foos ?? [])..addAll(model.foos ?? [])); + } + }); + }); + } + + @override + update(QueryExecutor executor) { + return super.update(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + numbas: List<_Numba>.from(l.numbas ?? []) + ..addAll(model.numbas ?? []), + foos: List<_Foo>.from(l.foos ?? [])..addAll(model.foos ?? [])); + } + }); + }); + } + + @override + delete(QueryExecutor executor) { + return super.delete(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + numbas: List<_Numba>.from(l.numbas ?? []) + ..addAll(model.numbas ?? []), + foos: List<_Foo>.from(l.foos ?? [])..addAll(model.foos ?? [])); + } + }); + }); + } +} + +class WeirdJoinQueryWhere extends QueryWhere { + WeirdJoinQueryWhere(WeirdJoinQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + joinName = StringSqlExpressionBuilder(query, 'join_name'); + + final NumericSqlExpressionBuilder id; + + final StringSqlExpressionBuilder joinName; + + @override + get expressionBuilders { + return [id, joinName]; + } +} + +class WeirdJoinQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + int get id { + return (values['id'] as int); + } + + set id(int value) => values['id'] = value; + String get joinName { + return (values['join_name'] as String); + } + + set joinName(String value) => values['join_name'] = value; + void copyFrom(WeirdJoin model) { + id = model.id; + if (model.unorthodox != null) { + values['join_name'] = model.unorthodox.name; + } + } +} + +class SongQuery extends Query { + SongQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = SongQueryWhere(this); + } + + @override + final SongQueryValues values = SongQueryValues(); + + SongQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'songs'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'weird_join_id', 'title']; + } + + @override + SongQueryWhere get where { + return _where; + } + + @override + SongQueryWhere newWhereClause() { + return SongQueryWhere(this); + } + + static Song parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Song( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + weirdJoinId: (row[3] as int), + title: (row[4] as String)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class SongQueryWhere extends QueryWhere { + SongQueryWhere(SongQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + weirdJoinId = NumericSqlExpressionBuilder(query, 'weird_join_id'), + title = StringSqlExpressionBuilder(query, 'title'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final NumericSqlExpressionBuilder weirdJoinId; + + final StringSqlExpressionBuilder title; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, weirdJoinId, title]; + } +} + +class SongQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + int get weirdJoinId { + return (values['weird_join_id'] as int); + } + + set weirdJoinId(int value) => values['weird_join_id'] = value; + String get title { + return (values['title'] as String); + } + + set title(String value) => values['title'] = value; + void copyFrom(Song model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + weirdJoinId = model.weirdJoinId; + title = model.title; + } +} + +class NumbaQuery extends Query { + NumbaQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = NumbaQueryWhere(this); + } + + @override + final NumbaQueryValues values = NumbaQueryValues(); + + NumbaQueryWhere _where; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'numbas'; + } + + @override + get fields { + return const ['i', 'parent']; + } + + @override + NumbaQueryWhere get where { + return _where; + } + + @override + NumbaQueryWhere newWhereClause() { + return NumbaQueryWhere(this); + } + + static Numba parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Numba(i: (row[0] as int), parent: (row[1] as int)); + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } +} + +class NumbaQueryWhere extends QueryWhere { + NumbaQueryWhere(NumbaQuery query) + : i = NumericSqlExpressionBuilder(query, 'i'), + parent = NumericSqlExpressionBuilder(query, 'parent'); + + final NumericSqlExpressionBuilder i; + + final NumericSqlExpressionBuilder parent; + + @override + get expressionBuilders { + return [i, parent]; + } +} + +class NumbaQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + int get i { + return (values['i'] as int); + } + + set i(int value) => values['i'] = value; + int get parent { + return (values['parent'] as int); + } + + set parent(int value) => values['parent'] = value; + void copyFrom(Numba model) { + i = model.i; + parent = model.parent; + } +} + +class FooQuery extends Query { + FooQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = FooQueryWhere(this); + leftJoin(_weirdJoins = FooPivotQuery(trampoline: trampoline, parent: this), + 'bar', 'foo_bar', + additionalFields: const ['id', 'join_name'], trampoline: trampoline); + } + + @override + final FooQueryValues values = FooQueryValues(); + + FooQueryWhere _where; + + FooPivotQuery _weirdJoins; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'foos'; + } + + @override + get fields { + return const ['bar']; + } + + @override + FooQueryWhere get where { + return _where; + } + + @override + FooQueryWhere newWhereClause() { + return FooQueryWhere(this); + } + + static Foo parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Foo(bar: (row[0] as String)); + if (row.length > 1) { + model = model.copyWith( + weirdJoins: [WeirdJoinQuery.parseRow(row.skip(1).take(2).toList())] + .where((x) => x != null) + .toList()); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + FooPivotQuery get weirdJoins { + return _weirdJoins; + } + + @override + bool canCompile(trampoline) { + return (!(trampoline.contains('foos') && + trampoline.contains('foo_pivots'))); + } + + @override + get(QueryExecutor executor) { + return super.get(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.bar == model.bar); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + weirdJoins: List<_WeirdJoin>.from(l.weirdJoins ?? []) + ..addAll(model.weirdJoins ?? [])); + } + }); + }); + } + + @override + update(QueryExecutor executor) { + return super.update(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.bar == model.bar); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + weirdJoins: List<_WeirdJoin>.from(l.weirdJoins ?? []) + ..addAll(model.weirdJoins ?? [])); + } + }); + }); + } + + @override + delete(QueryExecutor executor) { + return super.delete(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.bar == model.bar); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + weirdJoins: List<_WeirdJoin>.from(l.weirdJoins ?? []) + ..addAll(model.weirdJoins ?? [])); + } + }); + }); + } +} + +class FooQueryWhere extends QueryWhere { + FooQueryWhere(FooQuery query) + : bar = StringSqlExpressionBuilder(query, 'bar'); + + final StringSqlExpressionBuilder bar; + + @override + get expressionBuilders { + return [bar]; + } +} + +class FooQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get bar { + return (values['bar'] as String); + } + + set bar(String value) => values['bar'] = value; + void copyFrom(Foo model) { + bar = model.bar; + } +} + +class FooPivotQuery extends Query { + FooPivotQuery({Query parent, Set trampoline}) + : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = FooPivotQueryWhere(this); + leftJoin(_weirdJoin = WeirdJoinQuery(trampoline: trampoline, parent: this), + 'weird_join_id', 'id', + additionalFields: const ['id', 'join_name'], trampoline: trampoline); + leftJoin( + _foo = FooQuery(trampoline: trampoline, parent: this), 'foo_bar', 'bar', + additionalFields: const ['bar'], trampoline: trampoline); + } + + @override + final FooPivotQueryValues values = FooPivotQueryValues(); + + FooPivotQueryWhere _where; + + WeirdJoinQuery _weirdJoin; + + FooQuery _foo; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'foo_pivots'; + } + + @override + get fields { + return const ['weird_join_id', 'foo_bar']; + } + + @override + FooPivotQueryWhere get where { + return _where; + } + + @override + FooPivotQueryWhere newWhereClause() { + return FooPivotQueryWhere(this); + } + + static FooPivot parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = FooPivot(); + if (row.length > 2) { + model = model.copyWith( + weirdJoin: WeirdJoinQuery.parseRow(row.skip(2).take(2).toList())); + } + if (row.length > 4) { + model = + model.copyWith(foo: FooQuery.parseRow(row.skip(4).take(1).toList())); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + WeirdJoinQuery get weirdJoin { + return _weirdJoin; + } + + FooQuery get foo { + return _foo; + } +} + +class FooPivotQueryWhere extends QueryWhere { + FooPivotQueryWhere(FooPivotQuery query) + : weirdJoinId = NumericSqlExpressionBuilder(query, 'weird_join_id'), + fooBar = StringSqlExpressionBuilder(query, 'foo_bar'); + + final NumericSqlExpressionBuilder weirdJoinId; + + final StringSqlExpressionBuilder fooBar; + + @override + get expressionBuilders { + return [weirdJoinId, fooBar]; + } +} + +class FooPivotQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + int get weirdJoinId { + return (values['weird_join_id'] as int); + } + + set weirdJoinId(int value) => values['weird_join_id'] = value; + String get fooBar { + return (values['foo_bar'] as String); + } + + set fooBar(String value) => values['foo_bar'] = value; + void copyFrom(FooPivot model) { + if (model.weirdJoin != null) { + values['weird_join_id'] = model.weirdJoin.id; + } + if (model.foo != null) { + values['foo_bar'] = model.foo.bar; + } + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Unorthodox implements _Unorthodox { + const Unorthodox({this.name}); + + @override + final String name; + + Unorthodox copyWith({String name}) { + return Unorthodox(name: name ?? this.name); + } + + bool operator ==(other) { + return other is _Unorthodox && other.name == name; + } + + @override + int get hashCode { + return hashObjects([name]); + } + + @override + String toString() { + return "Unorthodox(name=$name)"; + } + + Map toJson() { + return UnorthodoxSerializer.toMap(this); + } +} + +@generatedSerializable +class WeirdJoin implements _WeirdJoin { + const WeirdJoin( + {this.id, this.unorthodox, this.song, this.numbas, this.foos}); + + @override + final int id; + + @override + final _Unorthodox unorthodox; + + @override + final _Song song; + + @override + final List<_Numba> numbas; + + @override + final List<_Foo> foos; + + WeirdJoin copyWith( + {int id, + _Unorthodox unorthodox, + _Song song, + List<_Numba> numbas, + List<_Foo> foos}) { + return WeirdJoin( + id: id ?? this.id, + unorthodox: unorthodox ?? this.unorthodox, + song: song ?? this.song, + numbas: numbas ?? this.numbas, + foos: foos ?? this.foos); + } + + bool operator ==(other) { + return other is _WeirdJoin && + other.id == id && + other.unorthodox == unorthodox && + other.song == song && + ListEquality<_Numba>(DefaultEquality<_Numba>()) + .equals(other.numbas, numbas) && + ListEquality<_Foo>(DefaultEquality<_Foo>()).equals(other.foos, foos); + } + + @override + int get hashCode { + return hashObjects([id, unorthodox, song, numbas, foos]); + } + + @override + String toString() { + return "WeirdJoin(id=$id, unorthodox=$unorthodox, song=$song, numbas=$numbas, foos=$foos)"; + } + + Map toJson() { + return WeirdJoinSerializer.toMap(this); + } +} + +@generatedSerializable +class Song extends _Song { + Song({this.id, this.createdAt, this.updatedAt, this.weirdJoinId, this.title}); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + final int weirdJoinId; + + @override + final String title; + + Song copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + int weirdJoinId, + String title}) { + return Song( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + weirdJoinId: weirdJoinId ?? this.weirdJoinId, + title: title ?? this.title); + } + + bool operator ==(other) { + return other is _Song && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.weirdJoinId == weirdJoinId && + other.title == title; + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, weirdJoinId, title]); + } + + @override + String toString() { + return "Song(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, weirdJoinId=$weirdJoinId, title=$title)"; + } + + Map toJson() { + return SongSerializer.toMap(this); + } +} + +@generatedSerializable +class Numba extends _Numba { + Numba({this.i, this.parent}); + + @override + int i; + + @override + int parent; + + Numba copyWith({int i, int parent}) { + return Numba(i: i ?? this.i, parent: parent ?? this.parent); + } + + bool operator ==(other) { + return other is _Numba && other.i == i && other.parent == parent; + } + + @override + int get hashCode { + return hashObjects([i, parent]); + } + + @override + String toString() { + return "Numba(i=$i, parent=$parent)"; + } + + Map toJson() { + return NumbaSerializer.toMap(this); + } +} + +@generatedSerializable +class Foo implements _Foo { + const Foo({this.bar, this.weirdJoins}); + + @override + final String bar; + + @override + final List<_WeirdJoin> weirdJoins; + + Foo copyWith({String bar, List<_WeirdJoin> weirdJoins}) { + return Foo(bar: bar ?? this.bar, weirdJoins: weirdJoins ?? this.weirdJoins); + } + + bool operator ==(other) { + return other is _Foo && + other.bar == bar && + ListEquality<_WeirdJoin>(DefaultEquality<_WeirdJoin>()) + .equals(other.weirdJoins, weirdJoins); + } + + @override + int get hashCode { + return hashObjects([bar, weirdJoins]); + } + + @override + String toString() { + return "Foo(bar=$bar, weirdJoins=$weirdJoins)"; + } + + Map toJson() { + return FooSerializer.toMap(this); + } +} + +@generatedSerializable +class FooPivot implements _FooPivot { + const FooPivot({this.weirdJoin, this.foo}); + + @override + final _WeirdJoin weirdJoin; + + @override + final _Foo foo; + + FooPivot copyWith({_WeirdJoin weirdJoin, _Foo foo}) { + return FooPivot( + weirdJoin: weirdJoin ?? this.weirdJoin, foo: foo ?? this.foo); + } + + bool operator ==(other) { + return other is _FooPivot && + other.weirdJoin == weirdJoin && + other.foo == foo; + } + + @override + int get hashCode { + return hashObjects([weirdJoin, foo]); + } + + @override + String toString() { + return "FooPivot(weirdJoin=$weirdJoin, foo=$foo)"; + } + + Map toJson() { + return FooPivotSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const UnorthodoxSerializer unorthodoxSerializer = UnorthodoxSerializer(); + +class UnorthodoxEncoder extends Converter { + const UnorthodoxEncoder(); + + @override + Map convert(Unorthodox model) => UnorthodoxSerializer.toMap(model); +} + +class UnorthodoxDecoder extends Converter { + const UnorthodoxDecoder(); + + @override + Unorthodox convert(Map map) => UnorthodoxSerializer.fromMap(map); +} + +class UnorthodoxSerializer extends Codec { + const UnorthodoxSerializer(); + + @override + get encoder => const UnorthodoxEncoder(); + @override + get decoder => const UnorthodoxDecoder(); + static Unorthodox fromMap(Map map) { + return Unorthodox(name: map['name'] as String); + } + + static Map toMap(_Unorthodox model) { + if (model == null) { + return null; + } + return {'name': model.name}; + } +} + +abstract class UnorthodoxFields { + static const List allFields = [name]; + + static const String name = 'name'; +} + +const WeirdJoinSerializer weirdJoinSerializer = WeirdJoinSerializer(); + +class WeirdJoinEncoder extends Converter { + const WeirdJoinEncoder(); + + @override + Map convert(WeirdJoin model) => WeirdJoinSerializer.toMap(model); +} + +class WeirdJoinDecoder extends Converter { + const WeirdJoinDecoder(); + + @override + WeirdJoin convert(Map map) => WeirdJoinSerializer.fromMap(map); +} + +class WeirdJoinSerializer extends Codec { + const WeirdJoinSerializer(); + + @override + get encoder => const WeirdJoinEncoder(); + @override + get decoder => const WeirdJoinDecoder(); + static WeirdJoin fromMap(Map map) { + return WeirdJoin( + id: map['id'] as int, + unorthodox: map['unorthodox'] != null + ? UnorthodoxSerializer.fromMap(map['unorthodox'] as Map) + : null, + song: map['song'] != null + ? SongSerializer.fromMap(map['song'] as Map) + : null, + numbas: map['numbas'] is Iterable + ? List.unmodifiable(((map['numbas'] as Iterable).whereType()) + .map(NumbaSerializer.fromMap)) + : null, + foos: map['foos'] is Iterable + ? List.unmodifiable(((map['foos'] as Iterable).whereType()) + .map(FooSerializer.fromMap)) + : null); + } + + static Map toMap(_WeirdJoin model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'unorthodox': UnorthodoxSerializer.toMap(model.unorthodox), + 'song': SongSerializer.toMap(model.song), + 'numbas': model.numbas?.map((m) => NumbaSerializer.toMap(m))?.toList(), + 'foos': model.foos?.map((m) => FooSerializer.toMap(m))?.toList() + }; + } +} + +abstract class WeirdJoinFields { + static const List allFields = [ + id, + unorthodox, + song, + numbas, + foos + ]; + + static const String id = 'id'; + + static const String unorthodox = 'unorthodox'; + + static const String song = 'song'; + + static const String numbas = 'numbas'; + + static const String foos = 'foos'; +} + +const SongSerializer songSerializer = SongSerializer(); + +class SongEncoder extends Converter { + const SongEncoder(); + + @override + Map convert(Song model) => SongSerializer.toMap(model); +} + +class SongDecoder extends Converter { + const SongDecoder(); + + @override + Song convert(Map map) => SongSerializer.fromMap(map); +} + +class SongSerializer extends Codec { + const SongSerializer(); + + @override + get encoder => const SongEncoder(); + @override + get decoder => const SongDecoder(); + static Song fromMap(Map map) { + return Song( + id: map['id'] as String, + 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, + weirdJoinId: map['weird_join_id'] as int, + title: map['title'] as String); + } + + static Map toMap(_Song model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'weird_join_id': model.weirdJoinId, + 'title': model.title + }; + } +} + +abstract class SongFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + weirdJoinId, + title + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String weirdJoinId = 'weird_join_id'; + + static const String title = 'title'; +} + +const NumbaSerializer numbaSerializer = NumbaSerializer(); + +class NumbaEncoder extends Converter { + const NumbaEncoder(); + + @override + Map convert(Numba model) => NumbaSerializer.toMap(model); +} + +class NumbaDecoder extends Converter { + const NumbaDecoder(); + + @override + Numba convert(Map map) => NumbaSerializer.fromMap(map); +} + +class NumbaSerializer extends Codec { + const NumbaSerializer(); + + @override + get encoder => const NumbaEncoder(); + @override + get decoder => const NumbaDecoder(); + static Numba fromMap(Map map) { + return Numba(i: map['i'] as int, parent: map['parent'] as int); + } + + static Map toMap(_Numba model) { + if (model == null) { + return null; + } + return {'i': model.i, 'parent': model.parent}; + } +} + +abstract class NumbaFields { + static const List allFields = [i, parent]; + + static const String i = 'i'; + + static const String parent = 'parent'; +} + +const FooSerializer fooSerializer = FooSerializer(); + +class FooEncoder extends Converter { + const FooEncoder(); + + @override + Map convert(Foo model) => FooSerializer.toMap(model); +} + +class FooDecoder extends Converter { + const FooDecoder(); + + @override + Foo convert(Map map) => FooSerializer.fromMap(map); +} + +class FooSerializer extends Codec { + const FooSerializer(); + + @override + get encoder => const FooEncoder(); + @override + get decoder => const FooDecoder(); + static Foo fromMap(Map map) { + return Foo( + bar: map['bar'] as String, + weirdJoins: map['weird_joins'] is Iterable + ? List.unmodifiable( + ((map['weird_joins'] as Iterable).whereType()) + .map(WeirdJoinSerializer.fromMap)) + : null); + } + + static Map toMap(_Foo model) { + if (model == null) { + return null; + } + return { + 'bar': model.bar, + 'weird_joins': + model.weirdJoins?.map((m) => WeirdJoinSerializer.toMap(m))?.toList() + }; + } +} + +abstract class FooFields { + static const List allFields = [bar, weirdJoins]; + + static const String bar = 'bar'; + + static const String weirdJoins = 'weird_joins'; +} + +const FooPivotSerializer fooPivotSerializer = FooPivotSerializer(); + +class FooPivotEncoder extends Converter { + const FooPivotEncoder(); + + @override + Map convert(FooPivot model) => FooPivotSerializer.toMap(model); +} + +class FooPivotDecoder extends Converter { + const FooPivotDecoder(); + + @override + FooPivot convert(Map map) => FooPivotSerializer.fromMap(map); +} + +class FooPivotSerializer extends Codec { + const FooPivotSerializer(); + + @override + get encoder => const FooPivotEncoder(); + @override + get decoder => const FooPivotDecoder(); + static FooPivot fromMap(Map map) { + return FooPivot( + weirdJoin: map['weird_join'] != null + ? WeirdJoinSerializer.fromMap(map['weird_join'] as Map) + : null, + foo: map['foo'] != null + ? FooSerializer.fromMap(map['foo'] as Map) + : null); + } + + static Map toMap(_FooPivot model) { + if (model == null) { + return null; + } + return { + 'weird_join': WeirdJoinSerializer.toMap(model.weirdJoin), + 'foo': FooSerializer.toMap(model.foo) + }; + } +} + +abstract class FooPivotFields { + static const List allFields = [weirdJoin, foo]; + + static const String weirdJoin = 'weird_join'; + + static const String foo = 'foo'; +} diff --git a/angel_orm_test/lib/src/models/user.g.dart b/angel_orm_test/lib/src/models/user.g.dart new file mode 100644 index 00000000..5bcad71f --- /dev/null +++ b/angel_orm_test/lib/src/models/user.g.dart @@ -0,0 +1,995 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of angel_orm_generator.test.models.user; + +// ************************************************************************** +// MigrationGenerator +// ************************************************************************** + +class UserMigration extends Migration { + @override + up(Schema schema) { + schema.create('users', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.varChar('username'); + table.varChar('password'); + table.varChar('email'); + }); + } + + @override + down(Schema schema) { + schema.drop('users', cascade: true); + } +} + +class RoleUserMigration extends Migration { + @override + up(Schema schema) { + schema.create('role_users', (table) { + table.declare('role_id', ColumnType('serial')).references('roles', 'id'); + table.declare('user_id', ColumnType('serial')).references('users', 'id'); + }); + } + + @override + down(Schema schema) { + schema.drop('role_users'); + } +} + +class RoleMigration extends Migration { + @override + up(Schema schema) { + schema.create('roles', (table) { + table.serial('id')..primaryKey(); + table.timeStamp('created_at'); + table.timeStamp('updated_at'); + table.varChar('name'); + }); + } + + @override + down(Schema schema) { + schema.drop('roles', cascade: true); + } +} + +// ************************************************************************** +// OrmGenerator +// ************************************************************************** + +class UserQuery extends Query { + UserQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = UserQueryWhere(this); + leftJoin(_roles = RoleUserQuery(trampoline: trampoline, parent: this), 'id', + 'user_id', + additionalFields: const ['id', 'created_at', 'updated_at', 'name'], + trampoline: trampoline); + } + + @override + final UserQueryValues values = UserQueryValues(); + + UserQueryWhere _where; + + RoleUserQuery _roles; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'users'; + } + + @override + get fields { + return const [ + 'id', + 'created_at', + 'updated_at', + 'username', + 'password', + 'email' + ]; + } + + @override + UserQueryWhere get where { + return _where; + } + + @override + UserQueryWhere newWhereClause() { + return UserQueryWhere(this); + } + + static User parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = User( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + username: (row[3] as String), + password: (row[4] as String), + email: (row[5] as String)); + if (row.length > 6) { + model = model.copyWith( + roles: [RoleQuery.parseRow(row.skip(6).take(4).toList())] + .where((x) => x != null) + .toList()); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + RoleUserQuery get roles { + return _roles; + } + + @override + bool canCompile(trampoline) { + return (!(trampoline.contains('users') && + trampoline.contains('role_users'))); + } + + @override + get(QueryExecutor executor) { + return super.get(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + roles: List<_Role>.from(l.roles ?? []) + ..addAll(model.roles ?? [])); + } + }); + }); + } + + @override + update(QueryExecutor executor) { + return super.update(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + roles: List<_Role>.from(l.roles ?? []) + ..addAll(model.roles ?? [])); + } + }); + }); + } + + @override + delete(QueryExecutor executor) { + return super.delete(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + roles: List<_Role>.from(l.roles ?? []) + ..addAll(model.roles ?? [])); + } + }); + }); + } +} + +class UserQueryWhere extends QueryWhere { + UserQueryWhere(UserQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + username = StringSqlExpressionBuilder(query, 'username'), + password = StringSqlExpressionBuilder(query, 'password'), + email = StringSqlExpressionBuilder(query, 'email'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final StringSqlExpressionBuilder username; + + final StringSqlExpressionBuilder password; + + final StringSqlExpressionBuilder email; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, username, password, email]; + } +} + +class UserQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + String get username { + return (values['username'] as String); + } + + set username(String value) => values['username'] = value; + String get password { + return (values['password'] as String); + } + + set password(String value) => values['password'] = value; + String get email { + return (values['email'] as String); + } + + set email(String value) => values['email'] = value; + void copyFrom(User model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + username = model.username; + password = model.password; + email = model.email; + } +} + +class RoleUserQuery extends Query { + RoleUserQuery({Query parent, Set trampoline}) + : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = RoleUserQueryWhere(this); + leftJoin(_role = RoleQuery(trampoline: trampoline, parent: this), 'role_id', + 'id', + additionalFields: const ['id', 'created_at', 'updated_at', 'name'], + trampoline: trampoline); + leftJoin(_user = UserQuery(trampoline: trampoline, parent: this), 'user_id', + 'id', + additionalFields: const [ + 'id', + 'created_at', + 'updated_at', + 'username', + 'password', + 'email' + ], + trampoline: trampoline); + } + + @override + final RoleUserQueryValues values = RoleUserQueryValues(); + + RoleUserQueryWhere _where; + + RoleQuery _role; + + UserQuery _user; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'role_users'; + } + + @override + get fields { + return const ['role_id', 'user_id']; + } + + @override + RoleUserQueryWhere get where { + return _where; + } + + @override + RoleUserQueryWhere newWhereClause() { + return RoleUserQueryWhere(this); + } + + static RoleUser parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = RoleUser(); + if (row.length > 2) { + model = model.copyWith( + role: RoleQuery.parseRow(row.skip(2).take(4).toList())); + } + if (row.length > 6) { + model = model.copyWith( + user: UserQuery.parseRow(row.skip(6).take(6).toList())); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + RoleQuery get role { + return _role; + } + + UserQuery get user { + return _user; + } +} + +class RoleUserQueryWhere extends QueryWhere { + RoleUserQueryWhere(RoleUserQuery query) + : roleId = NumericSqlExpressionBuilder(query, 'role_id'), + userId = NumericSqlExpressionBuilder(query, 'user_id'); + + final NumericSqlExpressionBuilder roleId; + + final NumericSqlExpressionBuilder userId; + + @override + get expressionBuilders { + return [roleId, userId]; + } +} + +class RoleUserQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + int get roleId { + return (values['role_id'] as int); + } + + set roleId(int value) => values['role_id'] = value; + int get userId { + return (values['user_id'] as int); + } + + set userId(int value) => values['user_id'] = value; + void copyFrom(RoleUser model) { + if (model.role != null) { + values['role_id'] = model.role.id; + } + if (model.user != null) { + values['user_id'] = model.user.id; + } + } +} + +class RoleQuery extends Query { + RoleQuery({Query parent, Set trampoline}) : super(parent: parent) { + trampoline ??= Set(); + trampoline.add(tableName); + _where = RoleQueryWhere(this); + leftJoin(_users = RoleUserQuery(trampoline: trampoline, parent: this), 'id', + 'role_id', + additionalFields: const [ + 'id', + 'created_at', + 'updated_at', + 'username', + 'password', + 'email' + ], + trampoline: trampoline); + } + + @override + final RoleQueryValues values = RoleQueryValues(); + + RoleQueryWhere _where; + + RoleUserQuery _users; + + @override + get casts { + return {}; + } + + @override + get tableName { + return 'roles'; + } + + @override + get fields { + return const ['id', 'created_at', 'updated_at', 'name']; + } + + @override + RoleQueryWhere get where { + return _where; + } + + @override + RoleQueryWhere newWhereClause() { + return RoleQueryWhere(this); + } + + static Role parseRow(List row) { + if (row.every((x) => x == null)) return null; + var model = Role( + id: row[0].toString(), + createdAt: (row[1] as DateTime), + updatedAt: (row[2] as DateTime), + name: (row[3] as String)); + if (row.length > 4) { + model = model.copyWith( + users: [UserQuery.parseRow(row.skip(4).take(6).toList())] + .where((x) => x != null) + .toList()); + } + return model; + } + + @override + deserialize(List row) { + return parseRow(row); + } + + RoleUserQuery get users { + return _users; + } + + @override + bool canCompile(trampoline) { + return (!(trampoline.contains('roles') && + trampoline.contains('role_users'))); + } + + @override + get(QueryExecutor executor) { + return super.get(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + users: List<_User>.from(l.users ?? []) + ..addAll(model.users ?? [])); + } + }); + }); + } + + @override + update(QueryExecutor executor) { + return super.update(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + users: List<_User>.from(l.users ?? []) + ..addAll(model.users ?? [])); + } + }); + }); + } + + @override + delete(QueryExecutor executor) { + return super.delete(executor).then((result) { + return result.fold>([], (out, model) { + var idx = out.indexWhere((m) => m.id == model.id); + + if (idx == -1) { + return out..add(model); + } else { + var l = out[idx]; + return out + ..[idx] = l.copyWith( + users: List<_User>.from(l.users ?? []) + ..addAll(model.users ?? [])); + } + }); + }); + } +} + +class RoleQueryWhere extends QueryWhere { + RoleQueryWhere(RoleQuery query) + : id = NumericSqlExpressionBuilder(query, 'id'), + createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), + updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'), + name = StringSqlExpressionBuilder(query, 'name'); + + final NumericSqlExpressionBuilder id; + + final DateTimeSqlExpressionBuilder createdAt; + + final DateTimeSqlExpressionBuilder updatedAt; + + final StringSqlExpressionBuilder name; + + @override + get expressionBuilders { + return [id, createdAt, updatedAt, name]; + } +} + +class RoleQueryValues extends MapQueryValues { + @override + get casts { + return {}; + } + + String get id { + return (values['id'] as String); + } + + set id(String value) => values['id'] = value; + 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; + String get name { + return (values['name'] as String); + } + + set name(String value) => values['name'] = value; + void copyFrom(Role model) { + createdAt = model.createdAt; + updatedAt = model.updatedAt; + name = model.name; + } +} + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class User extends _User { + User( + {this.id, + this.createdAt, + this.updatedAt, + this.username, + this.password, + this.email, + List<_Role> roles}) + : this.roles = List.unmodifiable(roles ?? []); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + final String username; + + @override + final String password; + + @override + final String email; + + @override + final List<_Role> roles; + + User copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + String username, + String password, + String email, + List<_Role> roles}) { + return User( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + username: username ?? this.username, + password: password ?? this.password, + email: email ?? this.email, + roles: roles ?? this.roles); + } + + bool operator ==(other) { + return other is _User && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.username == username && + other.password == password && + other.email == email && + ListEquality<_Role>(DefaultEquality<_Role>()) + .equals(other.roles, roles); + } + + @override + int get hashCode { + return hashObjects( + [id, createdAt, updatedAt, username, password, email, roles]); + } + + @override + String toString() { + return "User(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, username=$username, password=$password, email=$email, roles=$roles)"; + } + + Map toJson() { + return UserSerializer.toMap(this); + } +} + +@generatedSerializable +class RoleUser implements _RoleUser { + const RoleUser({this.role, this.user}); + + @override + final _Role role; + + @override + final _User user; + + RoleUser copyWith({_Role role, _User user}) { + return RoleUser(role: role ?? this.role, user: user ?? this.user); + } + + bool operator ==(other) { + return other is _RoleUser && other.role == role && other.user == user; + } + + @override + int get hashCode { + return hashObjects([role, user]); + } + + @override + String toString() { + return "RoleUser(role=$role, user=$user)"; + } + + Map toJson() { + return RoleUserSerializer.toMap(this); + } +} + +@generatedSerializable +class Role extends _Role { + Role({this.id, this.createdAt, this.updatedAt, this.name, List<_User> users}) + : this.users = List.unmodifiable(users ?? []); + + /// A unique identifier corresponding to this item. + @override + String id; + + /// The time at which this item was created. + @override + DateTime createdAt; + + /// The last time at which this item was updated. + @override + DateTime updatedAt; + + @override + String name; + + @override + final List<_User> users; + + Role copyWith( + {String id, + DateTime createdAt, + DateTime updatedAt, + String name, + List<_User> users}) { + return Role( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + name: name ?? this.name, + users: users ?? this.users); + } + + bool operator ==(other) { + return other is _Role && + other.id == id && + other.createdAt == createdAt && + other.updatedAt == updatedAt && + other.name == name && + ListEquality<_User>(DefaultEquality<_User>()) + .equals(other.users, users); + } + + @override + int get hashCode { + return hashObjects([id, createdAt, updatedAt, name, users]); + } + + @override + String toString() { + return "Role(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, name=$name, users=$users)"; + } + + Map toJson() { + return RoleSerializer.toMap(this); + } +} + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +const UserSerializer userSerializer = UserSerializer(); + +class UserEncoder extends Converter { + const UserEncoder(); + + @override + Map convert(User model) => UserSerializer.toMap(model); +} + +class UserDecoder extends Converter { + const UserDecoder(); + + @override + User convert(Map map) => UserSerializer.fromMap(map); +} + +class UserSerializer extends Codec { + const UserSerializer(); + + @override + get encoder => const UserEncoder(); + @override + get decoder => const UserDecoder(); + static User fromMap(Map map) { + return User( + id: map['id'] as String, + 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, + username: map['username'] as String, + password: map['password'] as String, + email: map['email'] as String, + roles: map['roles'] is Iterable + ? List.unmodifiable(((map['roles'] as Iterable).whereType()) + .map(RoleSerializer.fromMap)) + : null); + } + + static Map toMap(_User model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'username': model.username, + 'password': model.password, + 'email': model.email, + 'roles': model.roles?.map((m) => RoleSerializer.toMap(m))?.toList() + }; + } +} + +abstract class UserFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + username, + password, + email, + roles + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String username = 'username'; + + static const String password = 'password'; + + static const String email = 'email'; + + static const String roles = 'roles'; +} + +const RoleUserSerializer roleUserSerializer = RoleUserSerializer(); + +class RoleUserEncoder extends Converter { + const RoleUserEncoder(); + + @override + Map convert(RoleUser model) => RoleUserSerializer.toMap(model); +} + +class RoleUserDecoder extends Converter { + const RoleUserDecoder(); + + @override + RoleUser convert(Map map) => RoleUserSerializer.fromMap(map); +} + +class RoleUserSerializer extends Codec { + const RoleUserSerializer(); + + @override + get encoder => const RoleUserEncoder(); + @override + get decoder => const RoleUserDecoder(); + static RoleUser fromMap(Map map) { + return RoleUser( + role: map['role'] != null + ? RoleSerializer.fromMap(map['role'] as Map) + : null, + user: map['user'] != null + ? UserSerializer.fromMap(map['user'] as Map) + : null); + } + + static Map toMap(_RoleUser model) { + if (model == null) { + return null; + } + return { + 'role': RoleSerializer.toMap(model.role), + 'user': UserSerializer.toMap(model.user) + }; + } +} + +abstract class RoleUserFields { + static const List allFields = [role, user]; + + static const String role = 'role'; + + static const String user = 'user'; +} + +const RoleSerializer roleSerializer = RoleSerializer(); + +class RoleEncoder extends Converter { + const RoleEncoder(); + + @override + Map convert(Role model) => RoleSerializer.toMap(model); +} + +class RoleDecoder extends Converter { + const RoleDecoder(); + + @override + Role convert(Map map) => RoleSerializer.fromMap(map); +} + +class RoleSerializer extends Codec { + const RoleSerializer(); + + @override + get encoder => const RoleEncoder(); + @override + get decoder => const RoleDecoder(); + static Role fromMap(Map map) { + return Role( + id: map['id'] as String, + 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, + name: map['name'] as String, + users: map['users'] is Iterable + ? List.unmodifiable(((map['users'] as Iterable).whereType()) + .map(UserSerializer.fromMap)) + : null); + } + + static Map toMap(_Role model) { + if (model == null) { + return null; + } + return { + 'id': model.id, + 'created_at': model.createdAt?.toIso8601String(), + 'updated_at': model.updatedAt?.toIso8601String(), + 'name': model.name, + 'users': model.users?.map((m) => UserSerializer.toMap(m))?.toList() + }; + } +} + +abstract class RoleFields { + static const List allFields = [ + id, + createdAt, + updatedAt, + name, + users + ]; + + static const String id = 'id'; + + static const String createdAt = 'created_at'; + + static const String updatedAt = 'updated_at'; + + static const String name = 'name'; + + static const String users = 'users'; +}