Fix order of arguments when reading custom types

This commit is contained in:
Simon Binder 2023-11-11 21:23:53 +01:00
parent ddc864a7c2
commit fd260edaa3
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
7 changed files with 39 additions and 8 deletions

View File

@ -1,5 +1,32 @@
import 'package:drift/drift.dart';
class CustomTextType implements CustomSqlType<String> {
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,

View File

@ -106,7 +106,7 @@ class WithDefaults extends Table with TableInfo<WithDefaults, WithDefault> {
static const VerificationMeta _aMeta = const VerificationMeta('a');
late final GeneratedColumn<String> a = GeneratedColumn<String>(
'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<WithDefaults, WithDefault> {
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<WithDefault> {
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
if (a.present) {
map['a'] = Variable<String>(a.value);
map['a'] = Variable<String>(a.value, const CustomTextType());
}
if (b.present) {
map['b'] = Variable<int>(b.value);
@ -1801,7 +1801,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
...generatedpredicate.watchedTables,
}).asyncMap((QueryRow row) async => MultipleResult(
row: row,
a: row.readNullable<String>('a'),
a: row.readNullableWithType<String>(const CustomTextType(), 'a'),
b: row.readNullable<int>('b'),
c: await withConstraints.mapFromRowOrNull(row,
tablePrefix: 'nested_0'),

View File

@ -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
);

View File

@ -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, '

View File

@ -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.

View File

@ -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)';

View File

@ -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