diff --git a/drift_dev/build.yaml b/drift_dev/build.yaml index 5cc565f2..cdf3d2ec 100644 --- a/drift_dev/build.yaml +++ b/drift_dev/build.yaml @@ -61,6 +61,7 @@ builders: build_to: cache auto_apply: none required_inputs: [".drift_prep.json"] + applies_builders: [":preparing_builder"] post_process_builders: cleanup: diff --git a/drift_dev/lib/src/writer/tables/table_writer.dart b/drift_dev/lib/src/writer/tables/table_writer.dart index de08f25a..b825547e 100644 --- a/drift_dev/lib/src/writer/tables/table_writer.dart +++ b/drift_dev/lib/src/writer/tables/table_writer.dart @@ -348,7 +348,7 @@ class TableWriter extends TableOrViewWriter { for (final converter in table.appliedConverters) { final typeName = emitter.dartCode(emitter.writer.converterType(converter)); - final code = converter.expression; + final code = emitter.dartCode(converter.expression); buffer.write('static $typeName ${converter.fieldName} = $code;'); @@ -360,8 +360,8 @@ class TableWriter extends TableOrViewWriter { emitter.writer.converterType(converter, makeNullable: true)); final wrap = converter.alsoAppliesToJsonConversion - ? 'JsonTypeConverter2.asNullable' - : 'NullAwareTypeConverter.wrap'; + ? emitter.drift('JsonTypeConverter2.asNullable') + : emitter.drift('NullAwareTypeConverter.wrap'); final code = '$wrap(${converter.fieldName})'; diff --git a/drift_dev/lib/src/writer/writer.dart b/drift_dev/lib/src/writer/writer.dart index 8f07d7a0..06e15656 100644 --- a/drift_dev/lib/src/writer/writer.dart +++ b/drift_dev/lib/src/writer/writer.dart @@ -110,10 +110,9 @@ abstract class _NodeOrWriter { final fieldName = forNullable && converter.canBeSkippedForNulls ? converter.nullableFieldName : converter.fieldName; - final table = converter.owningColumn.owner; return AnnotatedDartCode([ - DartTopLevelSymbol(table.entityInfoName, table.id.libraryUri), + entityInfoType(converter.owningColumn.owner), '.$fieldName', ]); } diff --git a/examples/modular/lib/src/preferences.dart b/examples/modular/lib/src/preferences.dart new file mode 100644 index 00000000..7b99ec7b --- /dev/null +++ b/examples/modular/lib/src/preferences.dart @@ -0,0 +1,36 @@ +import 'dart:convert'; + +import 'package:drift/drift.dart'; + +class Preferences { + final bool notifyForNewPosts; + + Preferences(this.notifyForNewPosts); +} + +class PreferencesConverter extends TypeConverter + with JsonTypeConverter2> { + const PreferencesConverter(); + + @override + Preferences fromSql(String fromDb) { + return fromJson(json.decode(fromDb)); + } + + @override + String toSql(Preferences value) { + return json.encode(toJson(value)); + } + + @override + Preferences fromJson(Map json) { + return Preferences(json['notify_for_new_posts'] as bool); + } + + @override + Map toJson(Preferences value) { + return { + 'notify_for_new_posts': value.notifyForNewPosts, + }; + } +} diff --git a/examples/modular/lib/src/users.drift b/examples/modular/lib/src/users.drift index 4b061bd4..d60ae5bb 100644 --- a/examples/modular/lib/src/users.drift +++ b/examples/modular/lib/src/users.drift @@ -1,7 +1,10 @@ +import 'preferences.dart'; + CREATE TABLE users ( id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL, - biography TEXT + biography TEXT, + preferences TEXT MAPPED BY `const PreferencesConverter()` ); CREATE INDEX users_name ON users (name); diff --git a/examples/modular/lib/src/users.drift.dart b/examples/modular/lib/src/users.drift.dart index df2b0b7f..f3fce9e1 100644 --- a/examples/modular/lib/src/users.drift.dart +++ b/examples/modular/lib/src/users.drift.dart @@ -1,13 +1,16 @@ // ignore_for_file: type=lint import 'package:drift/drift.dart' as i0; import 'package:modular/src/users.drift.dart' as i1; -import 'package:drift/internal/modular.dart' as i2; +import 'package:modular/src/preferences.dart' as i2; +import 'package:drift/internal/modular.dart' as i3; class User extends i0.DataClass implements i0.Insertable { final int id; final String name; final String? biography; - const User({required this.id, required this.name, this.biography}); + final i2.Preferences? preferences; + const User( + {required this.id, required this.name, this.biography, this.preferences}); @override Map toColumns(bool nullToAbsent) { final map = {}; @@ -16,6 +19,10 @@ class User extends i0.DataClass implements i0.Insertable { if (!nullToAbsent || biography != null) { map['biography'] = i0.Variable(biography); } + if (!nullToAbsent || preferences != null) { + final converter = Users.$converterpreferencesn; + map['preferences'] = i0.Variable(converter.toSql(preferences)); + } return map; } @@ -26,6 +33,9 @@ class User extends i0.DataClass implements i0.Insertable { biography: biography == null && nullToAbsent ? const i0.Value.absent() : i0.Value(biography), + preferences: preferences == null && nullToAbsent + ? const i0.Value.absent() + : i0.Value(preferences), ); } @@ -36,6 +46,8 @@ class User extends i0.DataClass implements i0.Insertable { id: serializer.fromJson(json['id']), name: serializer.fromJson(json['name']), biography: serializer.fromJson(json['biography']), + preferences: Users.$converterpreferencesn.fromJson( + serializer.fromJson?>(json['preferences'])), ); } @override @@ -45,73 +57,86 @@ class User extends i0.DataClass implements i0.Insertable { 'id': serializer.toJson(id), 'name': serializer.toJson(name), 'biography': serializer.toJson(biography), + 'preferences': serializer.toJson?>( + Users.$converterpreferencesn.toJson(preferences)), }; } i1.User copyWith( {int? id, String? name, - i0.Value biography = const i0.Value.absent()}) => + i0.Value biography = const i0.Value.absent(), + i0.Value preferences = const i0.Value.absent()}) => i1.User( id: id ?? this.id, name: name ?? this.name, biography: biography.present ? biography.value : this.biography, + preferences: preferences.present ? preferences.value : this.preferences, ); @override String toString() { return (StringBuffer('User(') ..write('id: $id, ') ..write('name: $name, ') - ..write('biography: $biography') + ..write('biography: $biography, ') + ..write('preferences: $preferences') ..write(')')) .toString(); } @override - int get hashCode => Object.hash(id, name, biography); + int get hashCode => Object.hash(id, name, biography, preferences); @override bool operator ==(Object other) => identical(this, other) || (other is i1.User && other.id == this.id && other.name == this.name && - other.biography == this.biography); + other.biography == this.biography && + other.preferences == this.preferences); } class UsersCompanion extends i0.UpdateCompanion { final i0.Value id; final i0.Value name; final i0.Value biography; + final i0.Value preferences; const UsersCompanion({ this.id = const i0.Value.absent(), this.name = const i0.Value.absent(), this.biography = const i0.Value.absent(), + this.preferences = const i0.Value.absent(), }); UsersCompanion.insert({ this.id = const i0.Value.absent(), required String name, this.biography = const i0.Value.absent(), + this.preferences = const i0.Value.absent(), }) : name = i0.Value(name); static i0.Insertable custom({ i0.Expression? id, i0.Expression? name, i0.Expression? biography, + i0.Expression? preferences, }) { return i0.RawValuesInsertable({ if (id != null) 'id': id, if (name != null) 'name': name, if (biography != null) 'biography': biography, + if (preferences != null) 'preferences': preferences, }); } i1.UsersCompanion copyWith( {i0.Value? id, i0.Value? name, - i0.Value? biography}) { + i0.Value? biography, + i0.Value? preferences}) { return i1.UsersCompanion( id: id ?? this.id, name: name ?? this.name, biography: biography ?? this.biography, + preferences: preferences ?? this.preferences, ); } @@ -127,6 +152,11 @@ class UsersCompanion extends i0.UpdateCompanion { if (biography.present) { map['biography'] = i0.Variable(biography.value); } + if (preferences.present) { + final converter = Users.$converterpreferencesn; + map['preferences'] = + i0.Variable(converter.toSql(preferences.value)); + } return map; } @@ -135,7 +165,8 @@ class UsersCompanion extends i0.UpdateCompanion { return (StringBuffer('i1.UsersCompanion(') ..write('id: $id, ') ..write('name: $name, ') - ..write('biography: $biography') + ..write('biography: $biography, ') + ..write('preferences: $preferences') ..write(')')) .toString(); } @@ -166,8 +197,16 @@ class Users extends i0.Table with i0.TableInfo { type: i0.DriftSqlType.string, requiredDuringInsert: false, $customConstraints: ''); + static const i0.VerificationMeta _preferencesMeta = + const i0.VerificationMeta('preferences'); + late final i0.GeneratedColumnWithTypeConverter + preferences = i0.GeneratedColumn('preferences', aliasedName, true, + type: i0.DriftSqlType.string, + requiredDuringInsert: false, + $customConstraints: '') + .withConverter(Users.$converterpreferencesn); @override - List get $columns => [id, name, biography]; + List get $columns => [id, name, biography, preferences]; @override String get aliasedName => _alias ?? 'users'; @override @@ -190,6 +229,7 @@ class Users extends i0.Table with i0.TableInfo { context.handle(_biographyMeta, biography.isAcceptableOrUnknown(data['biography']!, _biographyMeta)); } + context.handle(_preferencesMeta, const i0.VerificationResult.success()); return context; } @@ -205,6 +245,9 @@ class Users extends i0.Table with i0.TableInfo { .read(i0.DriftSqlType.string, data['${effectivePrefix}name'])!, biography: attachedDatabase.typeMapping .read(i0.DriftSqlType.string, data['${effectivePrefix}biography']), + preferences: Users.$converterpreferencesn.fromSql(attachedDatabase + .typeMapping + .read(i0.DriftSqlType.string, data['${effectivePrefix}preferences'])), ); } @@ -213,6 +256,11 @@ class Users extends i0.Table with i0.TableInfo { return Users(attachedDatabase, alias); } + static i0.JsonTypeConverter2> + $converterpreferences = const i2.PreferencesConverter(); + static i0.JsonTypeConverter2?> + $converterpreferencesn = + i0.JsonTypeConverter2.asNullable($converterpreferences); @override List get customConstraints => const []; @override @@ -403,7 +451,7 @@ class Follows extends i0.Table with i0.TableInfo { bool get dontWriteConstraints => true; } -class UsersDrift extends i2.ModularAccessor { +class UsersDrift extends i3.ModularAccessor { UsersDrift(i0.GeneratedDatabase db) : super(db); i0.Selectable findUsers({FindUsers$predicate? predicate}) { var $arrayStartIndex = 1;