From fd260edaa3459a3e01188501786e1aebd442fcd1 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 11 Nov 2023 21:23:53 +0100 Subject: [PATCH] Fix order of arguments when reading custom types --- drift/test/generated/converter.dart | 27 +++++++++++++++++++ drift/test/generated/custom_tables.g.dart | 8 +++--- drift/test/generated/tables.drift | 2 +- .../integration_tests/drift_files_test.dart | 2 +- drift_dev/CHANGELOG.md | 4 +++ .../lib/src/writer/queries/query_writer.dart | 2 +- drift_dev/pubspec.yaml | 2 +- 7 files changed, 39 insertions(+), 8 deletions(-) diff --git a/drift/test/generated/converter.dart b/drift/test/generated/converter.dart index a864e9ec..4ab9c425 100644 --- a/drift/test/generated/converter.dart +++ b/drift/test/generated/converter.dart @@ -1,5 +1,32 @@ import 'package:drift/drift.dart'; +class CustomTextType implements CustomSqlType { + const CustomTextType(); + + @override + String mapToSqlLiteral(String dartValue) { + final escapedChars = dartValue.replaceAll('\'', '\'\''); + return "'$escapedChars'"; + } + + @override + Object mapToSqlParameter(String dartValue) { + return dartValue; + } + + @override + String read(Object fromSql) { + return fromSql.toString(); + } + + @override + String sqlTypeName(GenerationContext context) { + // Still has text column affinity, but can be used to verify that the type + // really is used. + return 'MY_TEXT'; + } +} + enum SyncType { locallyCreated, locallyUpdated, diff --git a/drift/test/generated/custom_tables.g.dart b/drift/test/generated/custom_tables.g.dart index a6018c24..281de529 100644 --- a/drift/test/generated/custom_tables.g.dart +++ b/drift/test/generated/custom_tables.g.dart @@ -106,7 +106,7 @@ class WithDefaults extends Table with TableInfo { static const VerificationMeta _aMeta = const VerificationMeta('a'); late final GeneratedColumn a = GeneratedColumn( 'a', aliasedName, true, - type: DriftSqlType.string, + type: const CustomTextType(), requiredDuringInsert: false, $customConstraints: 'DEFAULT \'something\'', defaultValue: const CustomExpression('\'something\'')); @@ -144,7 +144,7 @@ class WithDefaults extends Table with TableInfo { final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; return WithDefault( a: attachedDatabase.typeMapping - .read(DriftSqlType.string, data['${effectivePrefix}a']), + .read(const CustomTextType(), data['${effectivePrefix}a']), b: attachedDatabase.typeMapping .read(DriftSqlType.int, data['${effectivePrefix}b']), ); @@ -267,7 +267,7 @@ class WithDefaultsCompanion extends UpdateCompanion { Map toColumns(bool nullToAbsent) { final map = {}; if (a.present) { - map['a'] = Variable(a.value); + map['a'] = Variable(a.value, const CustomTextType()); } if (b.present) { map['b'] = Variable(b.value); @@ -1801,7 +1801,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { ...generatedpredicate.watchedTables, }).asyncMap((QueryRow row) async => MultipleResult( row: row, - a: row.readNullable('a'), + a: row.readNullableWithType(const CustomTextType(), 'a'), b: row.readNullable('b'), c: await withConstraints.mapFromRowOrNull(row, tablePrefix: 'nested_0'), diff --git a/drift/test/generated/tables.drift b/drift/test/generated/tables.drift index 36e7dc1b..f5258057 100644 --- a/drift/test/generated/tables.drift +++ b/drift/test/generated/tables.drift @@ -6,7 +6,7 @@ CREATE TABLE no_ids ( ) WITHOUT ROWID WITH NoIdRow; CREATE TABLE with_defaults ( - a TEXT JSON KEY customJsonName DEFAULT 'something', + a `const CustomTextType()` JSON KEY customJsonName DEFAULT 'something', b INT UNIQUE ); diff --git a/drift/test/integration_tests/drift_files_test.dart b/drift/test/integration_tests/drift_files_test.dart index fe841632..0b0f2baa 100644 --- a/drift/test/integration_tests/drift_files_test.dart +++ b/drift/test/integration_tests/drift_files_test.dart @@ -11,7 +11,7 @@ const _createNoIds = 'WITHOUT ROWID;'; const _createWithDefaults = 'CREATE TABLE IF NOT EXISTS "with_defaults" (' - "\"a\" TEXT DEFAULT 'something', \"b\" INTEGER UNIQUE);"; + "\"a\" MY_TEXT DEFAULT 'something', \"b\" INTEGER UNIQUE);"; const _createWithConstraints = 'CREATE TABLE IF NOT EXISTS "with_constraints" (' '"a" TEXT, "b" INTEGER NOT NULL, "c" REAL, ' diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index 85403030..cf0cd861 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.13.2 + +- Fix generated queries relying on custom types. + ## 2.13.1 - Add `has_separate_analyzer` option to optimize builds using the `not_shared` builder. diff --git a/drift_dev/lib/src/writer/queries/query_writer.dart b/drift_dev/lib/src/writer/queries/query_writer.dart index b150576a..e370c41d 100644 --- a/drift_dev/lib/src/writer/queries/query_writer.dart +++ b/drift_dev/lib/src/writer/queries/query_writer.dart @@ -210,7 +210,7 @@ class QueryWriter { if (column.sqlType.isCustom) { final method = isNullable ? 'readNullableWithType' : 'readWithType'; final typeImpl = _emitter.dartCode(column.sqlType.custom!.expression); - code = 'row.$method<$rawDartType>($dartLiteral, $typeImpl)'; + code = 'row.$method<$rawDartType>($typeImpl, $dartLiteral)'; } else { final method = isNullable ? 'readNullable' : 'read'; code = 'row.$method<$rawDartType>($dartLiteral)'; diff --git a/drift_dev/pubspec.yaml b/drift_dev/pubspec.yaml index bc9f9d98..698fb83b 100644 --- a/drift_dev/pubspec.yaml +++ b/drift_dev/pubspec.yaml @@ -1,6 +1,6 @@ name: drift_dev description: Dev-dependency for users of drift. Contains the generator and development tools. -version: 2.13.1 +version: 2.13.2 repository: https://github.com/simolus3/drift homepage: https://drift.simonbinder.eu/ issue_tracker: https://github.com/simolus3/drift/issues