Finish custom constraints implementation

This commit is contained in:
Simon Binder 2019-03-27 18:37:29 +01:00
parent f3a0f5f230
commit 9490cd22fe
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
5 changed files with 20 additions and 8 deletions

View File

@ -27,8 +27,15 @@ abstract class GeneratedColumn<T, S extends SqlType<T>> extends Column<T, S> {
/// [here](https://www.sqlite.org/syntax/column-def.html), into the given
/// buffer.
void writeColumnDefinition(StringBuffer into) {
into..write('${$name} $typeName ')..write($nullable ? 'NULL' : 'NOT NULL');
writeCustomConstraints(into);
into.write('${$name} $typeName ');
if ($customConstraints == null) {
into.write($nullable ? 'NULL' : 'NOT NULL');
// these custom constraints refer to builtin constraints from moor
writeCustomConstraints(into);
} else {
into.write($customConstraints);
}
}
@visibleForOverriding

View File

@ -26,7 +26,8 @@ class Users extends Table {
@DataClassName('Category')
class Categories extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get description => text().named('desc')();
TextColumn get description =>
text().named('desc').customConstraint('NOT NULL UNIQUE')();
}
class SharedTodos extends Table {

View File

@ -217,10 +217,9 @@ class $CategoriesTable extends Categories
_id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
GeneratedTextColumn _description;
@override
GeneratedTextColumn get description => _description ??= GeneratedTextColumn(
'`desc`',
false,
);
GeneratedTextColumn get description =>
_description ??= GeneratedTextColumn('`desc`', false,
$customConstraints: 'NOT NULL UNIQUE');
@override
List<GeneratedColumn> get $columns => [id, description];
@override

View File

@ -24,7 +24,7 @@ void main() {
'category INTEGER NULL);'));
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS categories '
'(id INTEGER PRIMARY KEY AUTOINCREMENT, `desc` VARCHAR NOT NULL);'));
'(id INTEGER PRIMARY KEY AUTOINCREMENT, `desc` VARCHAR NOT NULL UNIQUE);'));
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS users '
'(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, '

View File

@ -98,6 +98,11 @@ class TableWriter {
}
}
if (column.customConstraints != null) {
// todo should we worry about escaping this string?
additionalParams['\$customConstraints'] = "'${column.customConstraints}'";
}
// GeneratedIntColumn('sql_name', isNullable, additionalField: true)
final expressionBuffer = StringBuffer()
..write(column.implColumnTypeName)