mirror of https://github.com/AMT-Cheif/drift.git
Generate code to match default values for CREATE TABLE
This commit is contained in:
parent
aa3706ae9d
commit
b1820ef5aa
|
@ -1,4 +1,5 @@
|
|||
export 'bools.dart' show and, or, not;
|
||||
export 'custom.dart';
|
||||
export 'datetimes.dart';
|
||||
export 'in.dart';
|
||||
export 'null_check.dart';
|
||||
|
|
|
@ -201,7 +201,9 @@ class WithDefaults extends Table with TableInfo<WithDefaults, WithDefault> {
|
|||
GeneratedTextColumn get a => _a ??= _constructA();
|
||||
GeneratedTextColumn _constructA() {
|
||||
return GeneratedTextColumn('a', $tableName, true,
|
||||
$customConstraints: 'DEFAULT \'something\'');
|
||||
$customConstraints: 'DEFAULT \'something\'',
|
||||
defaultValue:
|
||||
const CustomExpression<String, StringType>('\'something\''));
|
||||
}
|
||||
|
||||
final VerificationMeta _bMeta = const VerificationMeta('b');
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:built_value/built_value.dart';
|
||||
import 'package:moor_generator/src/model/used_type_converter.dart';
|
||||
|
||||
|
@ -61,6 +60,15 @@ const Map<ColumnType, String> createVariable = {
|
|||
ColumnType.real: 'Variable.withReal',
|
||||
};
|
||||
|
||||
const Map<ColumnType, String> sqlTypes = {
|
||||
ColumnType.boolean: 'BoolType',
|
||||
ColumnType.text: 'StringType',
|
||||
ColumnType.integer: 'IntType',
|
||||
ColumnType.datetime: 'DateTimeType',
|
||||
ColumnType.blob: 'BlobType',
|
||||
ColumnType.real: 'RealType',
|
||||
};
|
||||
|
||||
/// A column, as specified by a getter in a table.
|
||||
class SpecifiedColumn {
|
||||
/// The getter name of this column in the table class. It will also be used
|
||||
|
@ -95,10 +103,9 @@ class SpecifiedColumn {
|
|||
/// default ones.
|
||||
final String customConstraints;
|
||||
|
||||
/// If a default expression has been provided as the argument of
|
||||
/// ColumnBuilder.withDefault, contains the Dart code that references that
|
||||
/// expression.
|
||||
final Expression defaultArgument;
|
||||
/// Dart code that generates the default expression for this column, or null
|
||||
/// if there is no default expression.
|
||||
final String defaultArgument;
|
||||
|
||||
/// The [UsedTypeConverter], if one has been set on this column.
|
||||
final UsedTypeConverter typeConverter;
|
||||
|
@ -142,14 +149,7 @@ class SpecifiedColumn {
|
|||
|
||||
/// The class inside the moor library that represents the same sql type as
|
||||
/// this column.
|
||||
String get sqlTypeName => const {
|
||||
ColumnType.boolean: 'BoolType',
|
||||
ColumnType.text: 'StringType',
|
||||
ColumnType.integer: 'IntType',
|
||||
ColumnType.datetime: 'DateTimeType',
|
||||
ColumnType.blob: 'BlobType',
|
||||
ColumnType.real: 'RealType',
|
||||
}[type];
|
||||
String get sqlTypeName => sqlTypes[type];
|
||||
|
||||
const SpecifiedColumn({
|
||||
this.type,
|
||||
|
|
|
@ -199,7 +199,7 @@ class ColumnParser extends ParserBase {
|
|||
customConstraints: foundCustomConstraint,
|
||||
nullable: nullable,
|
||||
features: foundFeatures,
|
||||
defaultArgument: foundDefaultExpression,
|
||||
defaultArgument: foundDefaultExpression?.toSource(),
|
||||
typeConverter: converter);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:moor_generator/src/model/specified_column.dart';
|
|||
import 'package:moor_generator/src/model/specified_table.dart';
|
||||
import 'package:moor_generator/src/parser/sql/type_mapping.dart';
|
||||
import 'package:moor_generator/src/utils/names.dart';
|
||||
import 'package:moor_generator/src/utils/string_escaper.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
import 'package:sqlparser/sqlparser.dart';
|
||||
|
||||
|
@ -45,6 +46,8 @@ class CreateTable {
|
|||
final sqlName = column.name;
|
||||
final dartName = ReCase(sqlName).camelCase;
|
||||
final constraintWriter = StringBuffer();
|
||||
final moorType = mapper.resolvedToMoor(column.type);
|
||||
String defaultValue;
|
||||
|
||||
for (var constraint in column.constraints) {
|
||||
if (constraint is PrimaryKeyColumn) {
|
||||
|
@ -53,6 +56,13 @@ class CreateTable {
|
|||
features.add(AutoIncrement());
|
||||
}
|
||||
}
|
||||
if (constraint is Default) {
|
||||
final dartType = dartTypeNames[moorType];
|
||||
final sqlType = sqlTypes[moorType];
|
||||
final expressionName = 'const CustomExpression<$dartType, $sqlType>';
|
||||
final sqlDefault = constraint.expression.span.text;
|
||||
defaultValue = '$expressionName(${asDartLiteral(sqlDefault)})';
|
||||
}
|
||||
|
||||
if (constraintWriter.isNotEmpty) {
|
||||
constraintWriter.write(' ');
|
||||
|
@ -61,13 +71,14 @@ class CreateTable {
|
|||
}
|
||||
|
||||
final parsed = SpecifiedColumn(
|
||||
type: mapper.resolvedToMoor(column.type),
|
||||
type: moorType,
|
||||
nullable: column.type.nullable,
|
||||
dartGetterName: dartName,
|
||||
name: ColumnName.implicitly(sqlName),
|
||||
declaredAsPrimaryKey: isPrimaryKey,
|
||||
features: features,
|
||||
customConstraints: constraintWriter.toString(),
|
||||
defaultArgument: defaultValue,
|
||||
);
|
||||
|
||||
foundColumns[column.name] = parsed;
|
||||
|
|
|
@ -149,7 +149,7 @@ class TableWriter {
|
|||
}
|
||||
|
||||
if (column.defaultArgument != null) {
|
||||
additionalParams['defaultValue'] = column.defaultArgument.toSource();
|
||||
additionalParams['defaultValue'] = column.defaultArgument;
|
||||
}
|
||||
|
||||
expressionBuffer
|
||||
|
|
Loading…
Reference in New Issue