Updated ORM

This commit is contained in:
thomashii 2022-06-11 00:44:09 +08:00
parent a714059cb9
commit da5b87043d
21 changed files with 117 additions and 68 deletions

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 6.0.1
* Upgraded to `lints` 2.x.x
## 6.0.0 ## 6.0.0
* Updated to SDK 2.16.x * Updated to SDK 2.16.x

View file

@ -13,7 +13,11 @@ abstract class Table {
MigrationColumn float(String name) => declare(name, ColumnType.float); MigrationColumn float(String name) => declare(name, ColumnType.float);
MigrationColumn numeric(String name) => declare(name, ColumnType.numeric); MigrationColumn double(String name) => declare(name, ColumnType.double);
MigrationColumn numeric(String name, {int precision = 17, int scale = 3}) {
return declare(name, ColumnType.numeric);
}
MigrationColumn boolean(String name) => declare(name, ColumnType.boolean); MigrationColumn boolean(String name) => declare(name, ColumnType.boolean);
@ -23,7 +27,9 @@ abstract class Table {
//MigrationColumn dateTime(String name) => timeStamp(name, timezone: true); //MigrationColumn dateTime(String name) => timeStamp(name, timezone: true);
MigrationColumn timeStamp(String name, {bool timezone = false}) { MigrationColumn timeStamp(String name, {bool timezone = false}) {
if (timezone != true) return declare(name, ColumnType.timeStamp); if (!timezone) {
return declare(name, ColumnType.timeStamp);
}
return declare(name, ColumnType.timeStampWithTimeZone); return declare(name, ColumnType.timeStampWithTimeZone);
} }

View file

@ -1,5 +1,5 @@
name: angel3_migration name: angel3_migration
version: 6.0.0 version: 6.0.1
description: Database migration runtime for Angel3 ORM. Use this package to define schemas. description: Database migration runtime for Angel3 ORM. Use this package to define schemas.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_migration repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_migration
@ -8,7 +8,7 @@ environment:
dependencies: dependencies:
angel3_orm: ^6.0.0 angel3_orm: ^6.0.0
dev_dependencies: dev_dependencies:
lints: ^1.0.0 lints: ^2.0.0
# dependency_overrides: dependency_overrides:
# angel3_orm: angel3_orm:
# path: ../angel_orm path: ../angel_orm

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 6.0.2
* Upgraded to `lints` 2.x.x
## 6.0.1 ## 6.0.1
* Added `MariaDbMigrationRunner` to support MariaDB migration with `mysql1` driver * Added `MariaDbMigrationRunner` to support MariaDB migration with `mysql1` driver

View file

@ -1,5 +1,5 @@
name: angel3_migration_runner name: angel3_migration_runner
version: 6.0.1 version: 6.0.2
description: Command-line based database migration runner for Angel3's ORM. description: Command-line based database migration runner for Angel3's ORM.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_migration_runner repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_migration_runner
@ -15,7 +15,7 @@ dependencies:
mysql1: ^0.19.0 mysql1: ^0.19.0
logging: ^1.0.0 logging: ^1.0.0
dev_dependencies: dev_dependencies:
lints: ^1.0.0 lints: ^2.0.0
# dependency_overrides: # dependency_overrides:
# angel3_orm: # angel3_orm:
# path: ../angel_orm # path: ../angel_orm

View file

@ -23,6 +23,15 @@ class Column {
/// Specifies the length of a `VARCHAR`. /// Specifies the length of a `VARCHAR`.
final int length; final int length;
/// Specifies the precision of a `NUMERIC` or `DECIMAL`.
final int precision;
/// Specifies the scale of a `NUMERIC` or `DECIMAL`.
final int scale;
/// Specifies the timezone of a temporal field.
final String timezone;
/// Explicitly defines a SQL type for this column. /// Explicitly defines a SQL type for this column.
final ColumnType type; final ColumnType type;
@ -35,7 +44,10 @@ class Column {
const Column( const Column(
{this.isNullable = true, {this.isNullable = true,
this.length = 255, this.length = 255,
this.precision = 17,
this.scale = 3,
this.name = "", this.name = "",
this.timezone = "",
this.type = ColumnType.varChar, this.type = ColumnType.varChar,
this.indexType = IndexType.none, this.indexType = IndexType.none,
this.expression}); this.expression});
@ -71,9 +83,16 @@ enum IndexType {
class ColumnType { class ColumnType {
/// The name of this data type. /// The name of this data type.
final String name; final String name;
final bool hasSize; final bool hasLength;
final bool hasPrecision;
final bool hasScale;
final bool hasTimezone;
const ColumnType(this.name, [this.hasSize = false]); const ColumnType(this.name,
{this.hasLength = false,
this.hasPrecision = false,
this.hasScale = false,
this.hasTimezone = false});
static const ColumnType boolean = ColumnType('boolean'); static const ColumnType boolean = ColumnType('boolean');
@ -87,13 +106,15 @@ class ColumnType {
static const ColumnType smallInt = ColumnType('smallint'); static const ColumnType smallInt = ColumnType('smallint');
static const ColumnType tinyInt = ColumnType('tinyint'); static const ColumnType tinyInt = ColumnType('tinyint');
static const ColumnType bit = ColumnType('bit'); static const ColumnType bit = ColumnType('bit');
static const ColumnType decimal = ColumnType('decimal', true); static const ColumnType decimal =
static const ColumnType numeric = ColumnType('numeric', true); ColumnType('decimal', hasPrecision: true, hasScale: true);
static const ColumnType numeric =
ColumnType('numeric', hasPrecision: true, hasScale: true);
static const ColumnType money = ColumnType('money'); static const ColumnType money = ColumnType('money');
static const ColumnType smallMoney = ColumnType('smallmoney'); static const ColumnType smallMoney = ColumnType('smallmoney');
static const ColumnType float = ColumnType('float'); static const ColumnType float = ColumnType('float');
static const ColumnType real = ColumnType('real'); static const ColumnType real = ColumnType('real');
static const ColumnType double = ColumnType('double'); static const ColumnType double = ColumnType('double precision');
// Dates and times // Dates and times
static const ColumnType dateTime = ColumnType('datetime'); static const ColumnType dateTime = ColumnType('datetime');
@ -102,35 +123,38 @@ class ColumnType {
static const ColumnType time = ColumnType('time'); static const ColumnType time = ColumnType('time');
static const ColumnType timeStamp = ColumnType('timestamp'); static const ColumnType timeStamp = ColumnType('timestamp');
static const ColumnType timeStampWithTimeZone = static const ColumnType timeStampWithTimeZone =
ColumnType('timestamp with time zone'); ColumnType('timestamptz', hasTimezone: true);
// Strings // Strings
static const ColumnType char = ColumnType('char', true); static const ColumnType char = ColumnType('char', hasLength: true);
static const ColumnType varChar = ColumnType('varchar', true); static const ColumnType varChar = ColumnType('varchar', hasLength: true);
static const ColumnType varCharMax = ColumnType('varchar(max)'); static const ColumnType varCharMax = ColumnType('varchar(max)');
static const ColumnType text = ColumnType('text', true); static const ColumnType text = ColumnType('text', hasLength: true);
// Unicode strings // Unicode strings
static const ColumnType nChar = ColumnType('nchar', true); static const ColumnType nChar = ColumnType('nchar', hasLength: true);
static const ColumnType nVarChar = ColumnType('nvarchar', true); static const ColumnType nVarChar = ColumnType('nvarchar', hasLength: true);
static const ColumnType nVarCharMax = ColumnType('nvarchar(max)', true); static const ColumnType nVarCharMax =
static const ColumnType nText = ColumnType('ntext', true); ColumnType('nvarchar(max)', hasLength: true);
static const ColumnType nText = ColumnType('ntext', hasLength: true);
// Binary // Binary
static const ColumnType binary = ColumnType('binary', true); static const ColumnType binary = ColumnType('binary', hasLength: true);
static const ColumnType varBinary = ColumnType('varbinary', true); static const ColumnType varBinary = ColumnType('varbinary', hasLength: true);
static const ColumnType varBinaryMax = ColumnType('varbinary(max)', true); static const ColumnType varBinaryMax =
static const ColumnType image = ColumnType('image', true); ColumnType('varbinary(max)', hasLength: true);
static const ColumnType image = ColumnType('image', hasLength: true);
// JSON. // JSON.
static const ColumnType json = ColumnType('json', true); static const ColumnType json = ColumnType('json', hasLength: true);
static const ColumnType jsonb = ColumnType('jsonb', true); static const ColumnType jsonb = ColumnType('jsonb', hasLength: true);
// Misc. // Misc.
static const ColumnType sqlVariant = ColumnType('sql_variant', true); static const ColumnType sqlVariant =
ColumnType('sql_variant', hasLength: true);
static const ColumnType uniqueIdentifier = static const ColumnType uniqueIdentifier =
ColumnType('uniqueidentifier', true); ColumnType('uniqueidentifier', hasLength: true);
static const ColumnType xml = ColumnType('xml', true); static const ColumnType xml = ColumnType('xml', hasLength: true);
static const ColumnType cursor = ColumnType('cursor', true); static const ColumnType cursor = ColumnType('cursor', hasLength: true);
static const ColumnType table = ColumnType('table', true); static const ColumnType table = ColumnType('table', hasLength: true);
} }

View file

@ -377,7 +377,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
var returningSql = ''; var returningSql = '';
if (executor.dialect is PostgreSQLDialect) { if (executor.dialect is PostgreSQLDialect) {
var returning = fields.map(adornWithTableName).join(', '); var returning = fields.map(adornWithTableName).join(', ');
sql = 'WITH $tableName as ($insertion RETURNING $returning) ' + sql; sql = 'WITH $tableName as ($insertion RETURNING $returning) $sql';
} else if (executor.dialect is MySQLDialect) { } else if (executor.dialect is MySQLDialect) {
// Default to using 'id' as primary key in model // Default to using 'id' as primary key in model
if (fields.contains("id")) { if (fields.contains("id")) {
@ -422,7 +422,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
var sql = compile({}); var sql = compile({});
var returningSql = ''; var returningSql = '';
if (executor.dialect is PostgreSQLDialect) { if (executor.dialect is PostgreSQLDialect) {
sql = 'WITH $tableName as ($updateSql RETURNING $returning) ' + sql; sql = 'WITH $tableName as ($updateSql RETURNING $returning) $sql';
} else if (executor.dialect is MySQLDialect) { } else if (executor.dialect is MySQLDialect) {
returningSql = sql; returningSql = sql;
sql = '$updateSql'; sql = '$updateSql';

View file

@ -135,7 +135,9 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
switch (col.type) { switch (col.type) {
case ColumnType.varChar: case ColumnType.varChar:
methodName = 'varChar'; methodName = 'varChar';
named['length'] = literal(col.length); if (col.type.hasLength) {
named['length'] = literal(col.length);
}
break; break;
case ColumnType.serial: case ColumnType.serial:
methodName = 'serial'; methodName = 'serial';
@ -148,9 +150,7 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
break; break;
case ColumnType.numeric: case ColumnType.numeric:
methodName = 'numeric'; methodName = 'numeric';
break; if (col.type.hasPrecision) {}
case ColumnType.double:
methodName = 'float';
break; break;
case ColumnType.boolean: case ColumnType.boolean:
methodName = 'boolean'; methodName = 'boolean';

View file

@ -342,10 +342,10 @@ ColumnType inferColumnType(DartType type) {
return ColumnType.int; return ColumnType.int;
} }
if (const TypeChecker.fromRuntime(double).isAssignableFromType(type)) { if (const TypeChecker.fromRuntime(double).isAssignableFromType(type)) {
return ColumnType.double; return ColumnType.float;
} }
if (const TypeChecker.fromRuntime(num).isAssignableFromType(type)) { if (const TypeChecker.fromRuntime(num).isAssignableFromType(type)) {
return ColumnType.double; return ColumnType.float;
} }
if (const TypeChecker.fromRuntime(bool).isAssignableFromType(type)) { if (const TypeChecker.fromRuntime(bool).isAssignableFromType(type)) {
return ColumnType.boolean; return ColumnType.boolean;
@ -426,7 +426,16 @@ class _ColumnType implements ColumnType {
final String name; final String name;
@override @override
final bool hasSize = false; bool hasLength = false;
@override
bool hasPrecision = false;
@override
bool hasScale = false;
@override
bool hasTimezone = false;
//_ColumnType(this.name, [this.hasSize = false]); //_ColumnType(this.name, [this.hasSize = false]);
_ColumnType(this.name); _ColumnType(this.name);

View file

@ -14,7 +14,6 @@ var floatTypes = [
ColumnType.float, ColumnType.float,
ColumnType.numeric, ColumnType.numeric,
ColumnType.real, ColumnType.real,
ColumnType.double,
const ColumnType('double precision'), const ColumnType('double precision'),
]; ];
@ -498,7 +497,7 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
// , <user_fields> // , <user_fields>
b.write(foreignFields.isEmpty b.write(foreignFields.isEmpty
? '' ? ''
: ', ' + foreignFields.join(', ')); : ', ${foreignFields.join(', ')}');
// FROM users // FROM users
b.write(' FROM '); b.write(' FROM ');
b.write(relationForeign.tableName); b.write(relationForeign.tableName);
@ -552,9 +551,8 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
// There'll be a private `_field`, and then a getter, named `field`, // There'll be a private `_field`, and then a getter, named `field`,
// that returns the subquery object. // that returns the subquery object.
var foreignQueryType = refer(relationForeign var foreignQueryType = refer(
.buildContext.modelClassNameRecase.pascalCase + '${relationForeign.buildContext.modelClassNameRecase.pascalCase}Query');
'Query');
clazz clazz
..fields.add(Field((b) => b ..fields.add(Field((b) => b

View file

@ -49,6 +49,6 @@ dependency_overrides:
path: ../../serialize/angel_serialize_generator path: ../../serialize/angel_serialize_generator
angel3_orm: angel3_orm:
path: ../angel_orm path: ../angel_orm
# angel3_migration: angel3_migration:
# path: ../angel_migration path: ../angel_migration

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 6.0.1
* Upgraded to `lints` 2.x.x
## 6.0.0 ## 6.0.0
* Updated to SDK 2.16.x * Updated to SDK 2.16.x

View file

@ -1,5 +1,5 @@
name: angel3_orm_postgres name: angel3_orm_postgres
version: 6.0.0 version: 6.0.1
description: PostgreSQL support for Angel3 ORM. Includes functionality for querying and transactions. description: PostgreSQL support for Angel3 ORM. Includes functionality for querying and transactions.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_postgres repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_orm_postgres
@ -15,7 +15,7 @@ dev_dependencies:
belatuk_pretty_logging: ^4.0.0 belatuk_pretty_logging: ^4.0.0
angel3_orm_test: ^6.0.0 angel3_orm_test: ^6.0.0
test: ^1.21.0 test: ^1.21.0
lints: ^1.0.0 lints: ^2.0.0
dependency_overrides: dependency_overrides:
# angel3_serialize: # angel3_serialize:
# path: ../../serialize/angel_serialize # path: ../../serialize/angel_serialize
@ -25,5 +25,5 @@ dependency_overrides:
path: ../angel_orm_test path: ../angel_orm_test
angel3_orm: angel3_orm:
path: ../angel_orm path: ../angel_orm
# angel3_migration: angel3_migration:
# path: ../angel_migration path: ../angel_migration

View file

@ -2,7 +2,7 @@ CREATE TEMPORARY TABLE "person_orders" (
"id" serial PRIMARY KEY, "id" serial PRIMARY KEY,
"person_id" int not null, "person_id" int not null,
"name" varchar(255), "name" varchar(255),
"price" int, "price" float,
"deleted" bool not null default false, "deleted" bool not null default false,
"created_at" timestamp, "created_at" timestamp,
"updated_at" timestamp "updated_at" timestamp

View file

@ -164,7 +164,7 @@ class BikeQueryWhere extends QueryWhere {
class BikeQueryValues extends MapQueryValues { class BikeQueryValues extends MapQueryValues {
@override @override
Map<String, String> get casts { Map<String, String> get casts {
return {'price': 'double'}; return {'price': 'float'};
} }
String? get id { String? get id {

View file

@ -164,7 +164,7 @@ class BoatQueryWhere extends QueryWhere {
class BoatQueryValues extends MapQueryValues { class BoatQueryValues extends MapQueryValues {
@override @override
Map<String, String> get casts { Map<String, String> get casts {
return {'price': 'double'}; return {'price': 'float'};
} }
String? get id { String? get id {

View file

@ -279,7 +279,7 @@ class FootQueryWhere extends QueryWhere {
class FootQueryValues extends MapQueryValues { class FootQueryValues extends MapQueryValues {
@override @override
Map<String, String> get casts { Map<String, String> get casts {
return {'n_toes': 'double'}; return {'n_toes': 'float'};
} }
String? get id { String? get id {

View file

@ -22,5 +22,5 @@ class _PersonWithLastOrder {
String? lastOrderName; String? lastOrderName;
@Column(expression: 'po.price') @Column(expression: 'po.price')
int? lastOrderPrice; double? lastOrderPrice;
} }

View file

@ -179,7 +179,7 @@ class PersonWithLastOrderQuery
@override @override
Map<String, String> get casts { Map<String, String> get casts {
return {}; return {'last_order_price': 'char'};
} }
@override @override
@ -219,7 +219,7 @@ class PersonWithLastOrderQuery
lastOrderName: lastOrderName:
fields.contains('last_order_name') ? (row[1] as String?) : null, fields.contains('last_order_name') ? (row[1] as String?) : null,
lastOrderPrice: lastOrderPrice:
fields.contains('last_order_price') ? (row[2] as int?) : null); fields.contains('last_order_price') ? mapToDouble(row[2]) : null);
return Optional.of(model); return Optional.of(model);
} }
@ -244,7 +244,7 @@ class PersonWithLastOrderQueryWhere extends QueryWhere {
class PersonWithLastOrderQueryValues extends MapQueryValues { class PersonWithLastOrderQueryValues extends MapQueryValues {
@override @override
Map<String, String> get casts { Map<String, String> get casts {
return {}; return {'last_order_price': 'float'};
} }
String? get name { String? get name {
@ -333,10 +333,10 @@ class PersonWithLastOrder extends _PersonWithLastOrder {
String? lastOrderName; String? lastOrderName;
@override @override
int? lastOrderPrice; double? lastOrderPrice;
PersonWithLastOrder copyWith( PersonWithLastOrder copyWith(
{String? name, String? lastOrderName, int? lastOrderPrice}) { {String? name, String? lastOrderName, double? lastOrderPrice}) {
return PersonWithLastOrder( return PersonWithLastOrder(
name: name ?? this.name, name: name ?? this.name,
lastOrderName: lastOrderName ?? this.lastOrderName, lastOrderName: lastOrderName ?? this.lastOrderName,
@ -474,7 +474,7 @@ class PersonWithLastOrderSerializer extends Codec<PersonWithLastOrder, Map> {
return PersonWithLastOrder( return PersonWithLastOrder(
name: map['name'] as String?, name: map['name'] as String?,
lastOrderName: map['last_order_name'] as String?, lastOrderName: map['last_order_name'] as String?,
lastOrderPrice: map['last_order_price'] as int?); lastOrderPrice: map['last_order_price'] as double?);
} }
static Map<String, dynamic> toMap(_PersonWithLastOrder? model) { static Map<String, dynamic> toMap(_PersonWithLastOrder? model) {

View file

@ -140,7 +140,7 @@ class PersonOrderQueryWhere extends QueryWhere {
class PersonOrderQueryValues extends MapQueryValues { class PersonOrderQueryValues extends MapQueryValues {
@override @override
Map<String, String> get casts { Map<String, String> get casts {
return {'price': 'double'}; return {'price': 'float'};
} }
String? get id { String? get id {
@ -301,7 +301,7 @@ class OrderWithPersonInfoQueryWhere extends QueryWhere {
class OrderWithPersonInfoQueryValues extends MapQueryValues { class OrderWithPersonInfoQueryValues extends MapQueryValues {
@override @override
Map<String, String> get casts { Map<String, String> get casts {
return {'price': 'double'}; return {'price': 'float'};
} }
String? get id { String? get id {

View file

@ -38,7 +38,7 @@ dependency_overrides:
path: ../../serialize/angel_serialize_generator path: ../../serialize/angel_serialize_generator
angel3_orm: angel3_orm:
path: ../angel_orm path: ../angel_orm
# angel3_migration: angel3_migration:
# path: ../angel_migration path: ../angel_migration
angel3_orm_generator: angel3_orm_generator:
path: ../angel_orm_generator path: ../angel_orm_generator