diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index 6ad1fb87..7d869b7a 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -44,6 +44,7 @@ @@ -58,6 +59,7 @@ @@ -65,6 +67,7 @@ @@ -72,6 +75,7 @@ @@ -79,6 +83,7 @@ @@ -489,6 +494,7 @@ @@ -604,11 +610,16 @@ + + + + + @@ -664,6 +675,7 @@ + diff --git a/sally/lib/src/dsl/columns.dart b/sally/lib/src/dsl/columns.dart index 280ef6af..86230358 100644 --- a/sally/lib/src/dsl/columns.dart +++ b/sally/lib/src/dsl/columns.dart @@ -27,6 +27,10 @@ class ColumnBuilder { Builder named(String name) => null; Builder primaryKey() => null; + /// Marks this column as nullable. Nullable columns should not appear in a + /// primary key. + Builder nullable() => null; + ResultColumn call() => null; } diff --git a/sally/pubspec.yaml b/sally/pubspec.yaml index 8721f7aa..63f61e53 100644 --- a/sally/pubspec.yaml +++ b/sally/pubspec.yaml @@ -11,5 +11,8 @@ dependencies: meta: '>= 1.0.0 <2.0.0' dev_dependencies: + sally_generator: + path: ../sally_generator + build_runner: ^1.2.6 test: ^1.5.3 mockito: ^4.0.0 diff --git a/sally/test/tables/todos.dart b/sally/test/tables/todos.dart new file mode 100644 index 00000000..b9b0eb4c --- /dev/null +++ b/sally/test/tables/todos.dart @@ -0,0 +1,10 @@ +import 'package:sally/sally.dart'; + +@DataClassName('TodoEntry') +class TodosTable extends Table { + + IntColumn get id => integer().autoIncrement()(); + TextColumn get title => text().withLength(min: 4, max: 6)(); + TextColumn get content => text()(); + +} \ No newline at end of file diff --git a/sally_generator/lib/src/model/specified_column.dart b/sally_generator/lib/src/model/specified_column.dart index f7b326a7..24129069 100644 --- a/sally_generator/lib/src/model/specified_column.dart +++ b/sally_generator/lib/src/model/specified_column.dart @@ -47,6 +47,9 @@ class SpecifiedColumn { final ColumnType type; /// The name of this column, as chosen by the user final ColumnName name; + /// Whether the user has explicitly declared this column to be nullable, the + /// default is false + final bool nullable; /// Whether this column has auto increment. bool get hasAI => features.any((f) => f is AutoIncrement); @@ -96,6 +99,7 @@ class SpecifiedColumn { this.dartGetterName, this.name, this.declaredAsPrimaryKey = false, + this.nullable = false, this.features = const []}); } diff --git a/sally_generator/lib/src/parser/column_parser.dart b/sally_generator/lib/src/parser/column_parser.dart index 339b94a1..93543c5d 100644 --- a/sally_generator/lib/src/parser/column_parser.dart +++ b/sally_generator/lib/src/parser/column_parser.dart @@ -17,6 +17,7 @@ const String functionPrimaryKey = 'primaryKey'; const String functionReferences = 'references'; const String functionAutoIncrement = 'autoIncrement'; const String functionWithLength = 'withLength'; +const String functionNullable = 'nullable'; const String errorMessage = 'This getter does not create a valid column that ' 'can be parsed by sally. Please refer to the readme from sally to see how ' @@ -53,6 +54,7 @@ class ColumnParser extends ParserBase { String foundStartMethod; String foundExplicitName; var wasDeclaredAsPrimaryKey = false; + var nullable = false; // todo parse reference final foundFeatures = []; @@ -104,6 +106,8 @@ class ColumnParser extends ParserBase { wasDeclaredAsPrimaryKey = true; foundFeatures.add(AutoIncrement()); break; + case functionNullable: + nullable = true; } // We're not at a starting method yet, so we need to go deeper! @@ -123,6 +127,7 @@ class ColumnParser extends ParserBase { dartGetterName: getter.name.name, name: name.escapeIfSqlKeyword(), declaredAsPrimaryKey: wasDeclaredAsPrimaryKey, + nullable: nullable, features: foundFeatures); } diff --git a/sally_generator/lib/src/writer/table_writer.dart b/sally_generator/lib/src/writer/table_writer.dart index d230a797..e3990d00 100644 --- a/sally_generator/lib/src/writer/table_writer.dart +++ b/sally_generator/lib/src/writer/table_writer.dart @@ -115,7 +115,7 @@ class TableWriter { } void _writeColumnGetter(StringBuffer buffer, SpecifiedColumn column) { - final isNullable = false; // todo nullability for columns + final isNullable = column.nullable; final additionalParams = {}; if (column.hasAI) {