Added default to Column

This commit is contained in:
thomashii 2022-06-14 11:19:24 +08:00
parent 224d2be026
commit 6c51e7a231
5 changed files with 43 additions and 15 deletions

View file

@ -12,6 +12,7 @@ class MigrationColumn extends Column {
@override @override
IndexType get indexType => _index; IndexType get indexType => _index;
@override
dynamic get defaultValue => _defaultValue; dynamic get defaultValue => _defaultValue;
List<MigrationColumnReference> get externalReferences => List<MigrationColumnReference> get externalReferences =>
@ -21,8 +22,12 @@ class MigrationColumn extends Column {
{bool isNullable = true, {bool isNullable = true,
int length = 255, int length = 255,
IndexType indexType = IndexType.standardIndex, IndexType indexType = IndexType.standardIndex,
defaultValue}) dynamic defaultValue})
: super(type: type, length: length) { : super(
type: type,
length: length,
isNullable: isNullable,
defaultValue: defaultValue) {
_nullable = isNullable; _nullable = isNullable;
_index = indexType; _index = indexType;
_defaultValue = defaultValue; _defaultValue = defaultValue;

View file

@ -3,6 +3,7 @@
## 6.0.2 ## 6.0.2
* Fixed issue #68: Support for non-nullable type * Fixed issue #68: Support for non-nullable type
* Added `defaultValue` to Column
* Upgraded to `lints` 2.x.x * Upgraded to `lints` 2.x.x
## 6.0.1 ## 6.0.1

View file

@ -18,7 +18,7 @@ class Column {
final bool isNullable; final bool isNullable;
/// Specifies this column name. /// Specifies this column name.
final String name; final String? name;
/// Specifies the length of a `VARCHAR`. /// Specifies the length of a `VARCHAR`.
final int length; final int length;
@ -29,8 +29,8 @@ class Column {
/// Specifies the scale of a `NUMERIC` or `DECIMAL`. /// Specifies the scale of a `NUMERIC` or `DECIMAL`.
final int scale; final int scale;
/// Specifies the timezone of a temporal field. /// Specifies the timezone for temporal field.
final String timezone; 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;
@ -41,16 +41,20 @@ class Column {
/// A custom SQL expression to execute, instead of a named column. /// A custom SQL expression to execute, instead of a named column.
final String? expression; final String? expression;
/// Specifies the default values.
final dynamic defaultValue;
const Column( const Column(
{this.isNullable = true, {this.isNullable = true,
this.length = 255, this.length = 255,
this.precision = 17, this.precision = 17,
this.scale = 3, this.scale = 3,
this.name = "", this.name,
this.timezone = "", this.timezone,
this.type = ColumnType.varChar, this.type = ColumnType.varChar,
this.indexType = IndexType.none, this.indexType = IndexType.none,
this.expression}); this.expression,
this.defaultValue});
/// Returns `true` if [expression] is not `null`. /// Returns `true` if [expression] is not `null`.
bool get hasExpression => expression != null; bool get hasExpression => expression != null;

View file

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/dart/element/type.dart';
import 'package:angel3_model/angel3_model.dart'; import 'package:angel3_model/angel3_model.dart';
@ -198,7 +199,20 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
var defaultValue = ctx.buildContext.defaults[name]; var defaultValue = ctx.buildContext.defaults[name];
if (defaultValue != null && !defaultValue.isNull) { // Handle 'defaultValue' on Column annotation
if (col.defaultValue != null) {
var defaultCode =
dartObjectToString(col.defaultValue as DartObject);
if (defaultCode != null) {
Expression defaultExpr = CodeExpression(
Code(defaultCode),
);
cascade.add(refer('defaultsTo').call([defaultExpr]));
}
// Handle 'defaultValue' on SerializableField annotation
} else if (defaultValue != null && !defaultValue.isNull) {
var type = defaultValue.type; var type = defaultValue.type;
Expression? defaultExpr; Expression? defaultExpr;
@ -219,10 +233,13 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
// Definitely an analyzer issue. // Definitely an analyzer issue.
} }
} else { } else {
var defaultCode = dartObjectToString(defaultValue);
if (defaultCode != null) {
defaultExpr = CodeExpression( defaultExpr = CodeExpression(
Code(dartObjectToString(defaultValue)!), Code(defaultCode),
); );
} }
}
if (defaultExpr != null) { if (defaultExpr != null) {
cascade.add(refer('defaultsTo').call([defaultExpr])); cascade.add(refer('defaultsTo').call([defaultExpr]));

View file

@ -105,12 +105,10 @@ Future<OrmBuildContext?> buildOrmContext(
// Check for column annotation... // Check for column annotation...
var element = _findElement(field); var element = _findElement(field);
var columnAnnotation = columnTypeChecker.firstAnnotationOf(element); var columnAnnotation = columnTypeChecker.firstAnnotationOf(element);
if (columnAnnotation != null) {
print('[ORM_BUILD_CONTEXT] ${element.name} => $columnAnnotation');
}
Column? column; Column? column;
if (columnAnnotation != null) { if (columnAnnotation != null) {
// print('[ORM_BUILD_CONTEXT] ${element.name} => $columnAnnotation');
column = reviveColumn(ConstantReader(columnAnnotation)); column = reviveColumn(ConstantReader(columnAnnotation));
} }
@ -132,6 +130,7 @@ Future<OrmBuildContext?> buildOrmContext(
length: column.length, length: column.length,
indexType: column.indexType, indexType: column.indexType,
type: inferColumnType(field.type), type: inferColumnType(field.type),
defaultValue: column.defaultValue,
); );
// Try to find a relationship // Try to find a relationship
@ -321,6 +320,7 @@ Future<OrmBuildContext?> buildOrmContext(
length: column.length, length: column.length,
type: column.type, type: column.type,
indexType: column.indexType, indexType: column.indexType,
defaultValue: column.defaultValue,
expression: expression:
ConstantReader(columnAnnotation).peek('expression')?.stringValue, ConstantReader(columnAnnotation).peek('expression')?.stringValue,
); );
@ -396,6 +396,7 @@ Column reviveColumn(ConstantReader cr) {
return Column( return Column(
isNullable: cr.peek('isNullable')?.boolValue ?? false, isNullable: cr.peek('isNullable')?.boolValue ?? false,
length: cr.peek('length')?.intValue ?? 255, length: cr.peek('length')?.intValue ?? 255,
defaultValue: cr.peek('defaultValue')?.objectValue,
type: columnType, type: columnType,
indexType: indexType, indexType: indexType,
); );