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
## 6.0.1
* Upgraded to `lints` 2.x.x
## 6.0.0
* Updated to SDK 2.16.x

View file

@ -13,7 +13,11 @@ abstract class Table {
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);
@ -23,7 +27,9 @@ abstract class Table {
//MigrationColumn dateTime(String name) => timeStamp(name, timezone: true);
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);
}

View file

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

View file

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

View file

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

View file

@ -23,6 +23,15 @@ class Column {
/// Specifies the length of a `VARCHAR`.
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.
final ColumnType type;
@ -35,7 +44,10 @@ class Column {
const Column(
{this.isNullable = true,
this.length = 255,
this.precision = 17,
this.scale = 3,
this.name = "",
this.timezone = "",
this.type = ColumnType.varChar,
this.indexType = IndexType.none,
this.expression});
@ -71,9 +83,16 @@ enum IndexType {
class ColumnType {
/// The name of this data type.
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');
@ -87,13 +106,15 @@ class ColumnType {
static const ColumnType smallInt = ColumnType('smallint');
static const ColumnType tinyInt = ColumnType('tinyint');
static const ColumnType bit = ColumnType('bit');
static const ColumnType decimal = ColumnType('decimal', true);
static const ColumnType numeric = ColumnType('numeric', true);
static const ColumnType decimal =
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 smallMoney = ColumnType('smallmoney');
static const ColumnType float = ColumnType('float');
static const ColumnType real = ColumnType('real');
static const ColumnType double = ColumnType('double');
static const ColumnType double = ColumnType('double precision');
// Dates and times
static const ColumnType dateTime = ColumnType('datetime');
@ -102,35 +123,38 @@ class ColumnType {
static const ColumnType time = ColumnType('time');
static const ColumnType timeStamp = ColumnType('timestamp');
static const ColumnType timeStampWithTimeZone =
ColumnType('timestamp with time zone');
ColumnType('timestamptz', hasTimezone: true);
// Strings
static const ColumnType char = ColumnType('char', true);
static const ColumnType varChar = ColumnType('varchar', true);
static const ColumnType char = ColumnType('char', hasLength: true);
static const ColumnType varChar = ColumnType('varchar', hasLength: true);
static const ColumnType varCharMax = ColumnType('varchar(max)');
static const ColumnType text = ColumnType('text', true);
static const ColumnType text = ColumnType('text', hasLength: true);
// Unicode strings
static const ColumnType nChar = ColumnType('nchar', true);
static const ColumnType nVarChar = ColumnType('nvarchar', true);
static const ColumnType nVarCharMax = ColumnType('nvarchar(max)', true);
static const ColumnType nText = ColumnType('ntext', true);
static const ColumnType nChar = ColumnType('nchar', hasLength: true);
static const ColumnType nVarChar = ColumnType('nvarchar', hasLength: true);
static const ColumnType nVarCharMax =
ColumnType('nvarchar(max)', hasLength: true);
static const ColumnType nText = ColumnType('ntext', hasLength: true);
// Binary
static const ColumnType binary = ColumnType('binary', true);
static const ColumnType varBinary = ColumnType('varbinary', true);
static const ColumnType varBinaryMax = ColumnType('varbinary(max)', true);
static const ColumnType image = ColumnType('image', true);
static const ColumnType binary = ColumnType('binary', hasLength: true);
static const ColumnType varBinary = ColumnType('varbinary', hasLength: true);
static const ColumnType varBinaryMax =
ColumnType('varbinary(max)', hasLength: true);
static const ColumnType image = ColumnType('image', hasLength: true);
// JSON.
static const ColumnType json = ColumnType('json', true);
static const ColumnType jsonb = ColumnType('jsonb', true);
static const ColumnType json = ColumnType('json', hasLength: true);
static const ColumnType jsonb = ColumnType('jsonb', hasLength: true);
// Misc.
static const ColumnType sqlVariant = ColumnType('sql_variant', true);
static const ColumnType sqlVariant =
ColumnType('sql_variant', hasLength: true);
static const ColumnType uniqueIdentifier =
ColumnType('uniqueidentifier', true);
static const ColumnType xml = ColumnType('xml', true);
static const ColumnType cursor = ColumnType('cursor', true);
static const ColumnType table = ColumnType('table', true);
ColumnType('uniqueidentifier', hasLength: true);
static const ColumnType xml = ColumnType('xml', hasLength: true);
static const ColumnType cursor = ColumnType('cursor', hasLength: 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 = '';
if (executor.dialect is PostgreSQLDialect) {
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) {
// Default to using 'id' as primary key in model
if (fields.contains("id")) {
@ -422,7 +422,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
var sql = compile({});
var returningSql = '';
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) {
returningSql = sql;
sql = '$updateSql';

View file

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

View file

@ -342,10 +342,10 @@ ColumnType inferColumnType(DartType type) {
return ColumnType.int;
}
if (const TypeChecker.fromRuntime(double).isAssignableFromType(type)) {
return ColumnType.double;
return ColumnType.float;
}
if (const TypeChecker.fromRuntime(num).isAssignableFromType(type)) {
return ColumnType.double;
return ColumnType.float;
}
if (const TypeChecker.fromRuntime(bool).isAssignableFromType(type)) {
return ColumnType.boolean;
@ -426,7 +426,16 @@ class _ColumnType implements ColumnType {
final String name;
@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);

View file

@ -14,7 +14,6 @@ var floatTypes = [
ColumnType.float,
ColumnType.numeric,
ColumnType.real,
ColumnType.double,
const ColumnType('double precision'),
];
@ -498,7 +497,7 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
// , <user_fields>
b.write(foreignFields.isEmpty
? ''
: ', ' + foreignFields.join(', '));
: ', ${foreignFields.join(', ')}');
// FROM users
b.write(' FROM ');
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`,
// that returns the subquery object.
var foreignQueryType = refer(relationForeign
.buildContext.modelClassNameRecase.pascalCase +
'Query');
var foreignQueryType = refer(
'${relationForeign.buildContext.modelClassNameRecase.pascalCase}Query');
clazz
..fields.add(Field((b) => b

View file

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

View file

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

View file

@ -1,5 +1,5 @@
name: angel3_orm_postgres
version: 6.0.0
version: 6.0.1
description: PostgreSQL support for Angel3 ORM. Includes functionality for querying and transactions.
homepage: https://angel3-framework.web.app/
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
angel3_orm_test: ^6.0.0
test: ^1.21.0
lints: ^1.0.0
lints: ^2.0.0
dependency_overrides:
# angel3_serialize:
# path: ../../serialize/angel_serialize
@ -25,5 +25,5 @@ dependency_overrides:
path: ../angel_orm_test
angel3_orm:
path: ../angel_orm
# angel3_migration:
# path: ../angel_migration
angel3_migration:
path: ../angel_migration

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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