From f38d56842e9c99a80a4a48091fbb9d3882f16bf3 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sun, 23 Oct 2022 17:46:57 +0200 Subject: [PATCH] Fix nullability for drift -> sqlparser mapping --- drift/test/generated/custom_tables.g.dart | 136 +++++++++--------- drift/test/generated/todos.g.dart | 20 +-- .../resolver/drift/sqlparser/mapping.dart | 7 +- 3 files changed, 86 insertions(+), 77 deletions(-) diff --git a/drift/test/generated/custom_tables.g.dart b/drift/test/generated/custom_tables.g.dart index 770e45bb..28f19cc0 100644 --- a/drift/test/generated/custom_tables.g.dart +++ b/drift/test/generated/custom_tables.g.dart @@ -1381,23 +1381,23 @@ class $WeirdTableTable extends Table class MyViewData extends DataClass { final String configKey; - final String configValue; - final SyncType syncState; - final SyncType syncStateImplicit; + final String? configValue; + final SyncType? syncState; + final SyncType? syncStateImplicit; const MyViewData( {required this.configKey, - required this.configValue, - required this.syncState, - required this.syncStateImplicit}); + this.configValue, + this.syncState, + this.syncStateImplicit}); factory MyViewData.fromJson(Map json, {ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return MyViewData( configKey: serializer.fromJson(json['config_key']), - configValue: serializer.fromJson(json['config_value']), - syncState: serializer.fromJson(json['sync_state']), + configValue: serializer.fromJson(json['config_value']), + syncState: serializer.fromJson(json['sync_state']), syncStateImplicit: - serializer.fromJson(json['sync_state_implicit']), + serializer.fromJson(json['sync_state_implicit']), ); } factory MyViewData.fromJsonString(String encodedJson, @@ -1410,22 +1410,24 @@ class MyViewData extends DataClass { serializer ??= driftRuntimeOptions.defaultSerializer; return { 'config_key': serializer.toJson(configKey), - 'config_value': serializer.toJson(configValue), - 'sync_state': serializer.toJson(syncState), - 'sync_state_implicit': serializer.toJson(syncStateImplicit), + 'config_value': serializer.toJson(configValue), + 'sync_state': serializer.toJson(syncState), + 'sync_state_implicit': serializer.toJson(syncStateImplicit), }; } MyViewData copyWith( {String? configKey, - String? configValue, - SyncType? syncState, - SyncType? syncStateImplicit}) => + Value configValue = const Value.absent(), + Value syncState = const Value.absent(), + Value syncStateImplicit = const Value.absent()}) => MyViewData( configKey: configKey ?? this.configKey, - configValue: configValue ?? this.configValue, - syncState: syncState ?? this.syncState, - syncStateImplicit: syncStateImplicit ?? this.syncStateImplicit, + configValue: configValue.present ? configValue.value : this.configValue, + syncState: syncState.present ? syncState.value : this.syncState, + syncStateImplicit: syncStateImplicit.present + ? syncStateImplicit.value + : this.syncStateImplicit, ); @override String toString() { @@ -1475,13 +1477,13 @@ class MyView extends ViewInfo implements HasResultSet { configKey: attachedDatabase.options.types .read(DriftSqlType.string, data['${effectivePrefix}config_key'])!, configValue: attachedDatabase.options.types - .read(DriftSqlType.string, data['${effectivePrefix}config_value'])!, - syncState: MyView.$convertersyncStaten.fromSql(attachedDatabase + .read(DriftSqlType.string, data['${effectivePrefix}config_value']), + syncState: MyView.$convertersyncState.fromSql(attachedDatabase .options.types - .read(DriftSqlType.int, data['${effectivePrefix}sync_state'])!), - syncStateImplicit: MyView.$convertersyncStateImplicitn.fromSql( - attachedDatabase.options.types.read(DriftSqlType.int, - data['${effectivePrefix}sync_state_implicit'])!), + .read(DriftSqlType.int, data['${effectivePrefix}sync_state'])), + syncStateImplicit: MyView.$convertersyncStateImplicit.fromSql( + attachedDatabase.options.types.read( + DriftSqlType.int, data['${effectivePrefix}sync_state_implicit'])), ); } @@ -1489,16 +1491,17 @@ class MyView extends ViewInfo implements HasResultSet { 'config_key', aliasedName, false, type: DriftSqlType.string); late final GeneratedColumn configValue = GeneratedColumn( - 'config_value', aliasedName, false, + 'config_value', aliasedName, true, type: DriftSqlType.string); - late final GeneratedColumnWithTypeConverter syncState = - GeneratedColumn('sync_state', aliasedName, false, + late final GeneratedColumnWithTypeConverter syncState = + GeneratedColumn('sync_state', aliasedName, true, type: DriftSqlType.int) - .withConverter(MyView.$convertersyncStaten); - late final GeneratedColumnWithTypeConverter syncStateImplicit = - GeneratedColumn('sync_state_implicit', aliasedName, false, + .withConverter(MyView.$convertersyncState); + late final GeneratedColumnWithTypeConverter + syncStateImplicit = GeneratedColumn( + 'sync_state_implicit', aliasedName, true, type: DriftSqlType.int) - .withConverter(MyView.$convertersyncStateImplicitn); + .withConverter(MyView.$convertersyncStateImplicit); @override MyView createAlias(String alias) { return MyView(attachedDatabase, alias); @@ -1527,7 +1530,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { 'CREATE TRIGGER my_trigger AFTER INSERT ON config BEGIN INSERT INTO with_defaults VALUES (new.config_key, LENGTH(new.config_value));END', 'my_trigger'); late final MyView myView = MyView(this); - Future writeConfig({required String key, required String value}) { + Future writeConfig({required String key, String? value}) { return customInsert( 'REPLACE INTO config (config_key, config_value) VALUES (?1, ?2)', variables: [Variable(key), Variable(value)], @@ -1588,7 +1591,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { }).asyncMap(config.mapFromRow); } - Selectable typeConverterVar(SyncType var1, List var2, + Selectable typeConverterVar(SyncType? var1, List var2, {TypeConverterVar$pred? pred}) { var $arrayStartIndex = 2; final generatedpred = $write( @@ -1600,10 +1603,12 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { return customSelect( 'SELECT config_key FROM config WHERE ${generatedpred.sql} AND(sync_state = ?1 OR sync_state_implicit IN ($expandedvar2))', variables: [ - Variable($ConfigTable.$convertersyncStaten.toSql(var1)), + Variable(NullAwareTypeConverter.wrapToSql( + $ConfigTable.$convertersyncStaten, var1)), ...generatedpred.introducedVariables, for (var $ in var2) - Variable($ConfigTable.$convertersyncStateImplicitn.toSql($)) + Variable(NullAwareTypeConverter.wrapToSql( + $ConfigTable.$convertersyncStateImplicitn, $)) ], readsFrom: { config, @@ -1659,8 +1664,8 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { }).asyncMap((QueryRow row) async { return MultipleResult( row: row, - a: row.read('a'), - b: row.read('b'), + a: row.readNullable('a'), + b: row.readNullable('b'), c: await withConstraints.mapFromRowOrNull(row, tablePrefix: 'nested_0'), ); }); @@ -1695,11 +1700,13 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { row: row, rowid: row.read('rowid'), configKey: row.read('config_key'), - configValue: row.read('config_value'), - syncState: $ConfigTable.$convertersyncStaten - .fromSql(row.read('sync_state')), - syncStateImplicit: $ConfigTable.$convertersyncStateImplicitn - .fromSql(row.read('sync_state_implicit')), + configValue: row.readNullable('config_value'), + syncState: NullAwareTypeConverter.wrapFromSql( + $ConfigTable.$convertersyncStaten, + row.readNullable('sync_state')), + syncStateImplicit: NullAwareTypeConverter.wrapFromSql( + $ConfigTable.$convertersyncStateImplicitn, + row.readNullable('sync_state_implicit')), ); }); } @@ -1711,11 +1718,12 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { return ReadViewResult( row: row, configKey: row.read('config_key'), - configValue: row.read('config_value'), - syncState: - MyView.$convertersyncStaten.fromSql(row.read('sync_state')), - syncStateImplicit: MyView.$convertersyncStateImplicitn - .fromSql(row.read('sync_state_implicit')), + configValue: row.readNullable('config_value'), + syncState: NullAwareTypeConverter.wrapFromSql( + MyView.$convertersyncStaten, row.readNullable('sync_state')), + syncStateImplicit: NullAwareTypeConverter.wrapFromSql( + MyView.$convertersyncStateImplicitn, + row.readNullable('sync_state_implicit')), ); }); } @@ -1747,7 +1755,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { .then((rows) => Future.wait(rows.map(config.mapFromRow))); } - Selectable nested(String var1) { + Selectable nested(String? var1) { return customSelect( 'SELECT"defaults"."a" AS "nested_0.a", "defaults"."b" AS "nested_0.b", defaults.b AS "\$n_0" FROM with_defaults AS defaults WHERE a = ?1', variables: [ @@ -1866,13 +1874,13 @@ class AnotherResult extends CustomResultSet { } class MultipleResult extends CustomResultSet { - final String a; - final int b; + final String? a; + final int? b; final WithConstraint? c; MultipleResult({ required QueryRow row, - required this.a, - required this.b, + this.a, + this.b, this.c, }) : super(row); @override @@ -1901,16 +1909,16 @@ typedef Multiple$predicate = Expression Function( class ReadRowIdResult extends CustomResultSet { final int rowid; final String configKey; - final String configValue; - final SyncType syncState; - final SyncType syncStateImplicit; + final String? configValue; + final SyncType? syncState; + final SyncType? syncStateImplicit; ReadRowIdResult({ required QueryRow row, required this.rowid, required this.configKey, - required this.configValue, - required this.syncState, - required this.syncStateImplicit, + this.configValue, + this.syncState, + this.syncStateImplicit, }) : super(row); @override int get hashCode => @@ -1941,15 +1949,15 @@ typedef ReadRowId$expr = Expression Function($ConfigTable config); class ReadViewResult extends CustomResultSet { final String configKey; - final String configValue; - final SyncType syncState; - final SyncType syncStateImplicit; + final String? configValue; + final SyncType? syncState; + final SyncType? syncStateImplicit; ReadViewResult({ required QueryRow row, required this.configKey, - required this.configValue, - required this.syncState, - required this.syncStateImplicit, + this.configValue, + this.syncState, + this.syncStateImplicit, }) : super(row); @override int get hashCode => diff --git a/drift/test/generated/todos.g.dart b/drift/test/generated/todos.g.dart index a9e675f6..52795a82 100644 --- a/drift/test/generated/todos.g.dart +++ b/drift/test/generated/todos.g.dart @@ -1612,10 +1612,10 @@ abstract class _$TodoDb extends GeneratedDatabase { return AllTodosWithCategoryResult( row: row, id: row.read('id'), - title: row.read('title'), + title: row.readNullable('title'), content: row.read('content'), - targetDate: row.read('target_date'), - category: row.read('category'), + targetDate: row.readNullable('target_date'), + category: row.readNullable('category'), catId: row.read('catId'), catDesc: row.read('catDesc'), ); @@ -1631,7 +1631,7 @@ abstract class _$TodoDb extends GeneratedDatabase { ); } - Selectable withIn(String var1, String var2, List var3) { + Selectable withIn(String? var1, String? var2, List var3) { var $arrayStartIndex = 3; final expandedvar3 = $expandVar($arrayStartIndex, var3.length); $arrayStartIndex += var3.length; @@ -1686,19 +1686,19 @@ abstract class _$TodoDb extends GeneratedDatabase { class AllTodosWithCategoryResult extends CustomResultSet { final int id; - final String title; + final String? title; final String content; - final DateTime targetDate; - final int category; + final DateTime? targetDate; + final int? category; final int catId; final String catDesc; AllTodosWithCategoryResult({ required QueryRow row, required this.id, - required this.title, + this.title, required this.content, - required this.targetDate, - required this.category, + this.targetDate, + this.category, required this.catId, required this.catDesc, }) : super(row); diff --git a/drift_dev/lib/src/analysis/resolver/drift/sqlparser/mapping.dart b/drift_dev/lib/src/analysis/resolver/drift/sqlparser/mapping.dart index dfe5ae07..6663e704 100644 --- a/drift_dev/lib/src/analysis/resolver/drift/sqlparser/mapping.dart +++ b/drift_dev/lib/src/analysis/resolver/drift/sqlparser/mapping.dart @@ -56,9 +56,10 @@ class TypeMapping { ResolvedType _columnType(DriftColumn column) { return _driftTypeToParser(column.sqlType, - overrideHint: column.typeConverter != null - ? TypeConverterHint(column.typeConverter!) - : null); + overrideHint: column.typeConverter != null + ? TypeConverterHint(column.typeConverter!) + : null) + .withNullable(column.nullable); } ResolvedType _driftTypeToParser(DriftSqlType type, {TypeHint? overrideHint}) {