Fix order of arguments when reading custom types

This commit is contained in:
Simon Binder 2023-11-09 19:03:32 +01:00
parent d770c16d8d
commit e79124e5af
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 38 additions and 7 deletions

View File

@ -1,5 +1,32 @@
import 'package:drift/drift.dart'; 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 { enum SyncType {
locallyCreated, locallyCreated,
locallyUpdated, locallyUpdated,

View File

@ -106,7 +106,7 @@ class WithDefaults extends Table with TableInfo<WithDefaults, WithDefault> {
static const VerificationMeta _aMeta = const VerificationMeta('a'); static const VerificationMeta _aMeta = const VerificationMeta('a');
late final GeneratedColumn<String> a = GeneratedColumn<String>( late final GeneratedColumn<String> a = GeneratedColumn<String>(
'a', aliasedName, true, 'a', aliasedName, true,
type: DriftSqlType.string, type: const CustomTextType(),
requiredDuringInsert: false, requiredDuringInsert: false,
$customConstraints: 'DEFAULT \'something\'', $customConstraints: 'DEFAULT \'something\'',
defaultValue: const CustomExpression('\'something\'')); defaultValue: const CustomExpression('\'something\''));
@ -144,7 +144,7 @@ class WithDefaults extends Table with TableInfo<WithDefaults, WithDefault> {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
return WithDefault( return WithDefault(
a: attachedDatabase.typeMapping a: attachedDatabase.typeMapping
.read(DriftSqlType.string, data['${effectivePrefix}a']), .read(const CustomTextType(), data['${effectivePrefix}a']),
b: attachedDatabase.typeMapping b: attachedDatabase.typeMapping
.read(DriftSqlType.int, data['${effectivePrefix}b']), .read(DriftSqlType.int, data['${effectivePrefix}b']),
); );
@ -267,7 +267,7 @@ class WithDefaultsCompanion extends UpdateCompanion<WithDefault> {
Map<String, Expression> toColumns(bool nullToAbsent) { Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{}; final map = <String, Expression>{};
if (a.present) { if (a.present) {
map['a'] = Variable<String>(a.value); map['a'] = Variable<String>(a.value, const CustomTextType());
} }
if (b.present) { if (b.present) {
map['b'] = Variable<int>(b.value); map['b'] = Variable<int>(b.value);
@ -1801,7 +1801,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
...generatedpredicate.watchedTables, ...generatedpredicate.watchedTables,
}).asyncMap((QueryRow row) async => MultipleResult( }).asyncMap((QueryRow row) async => MultipleResult(
row: row, row: row,
a: row.readNullable<String>('a'), a: row.readNullableWithType<String>(const CustomTextType(), 'a'),
b: row.readNullable<int>('b'), b: row.readNullable<int>('b'),
c: await withConstraints.mapFromRowOrNull(row, c: await withConstraints.mapFromRowOrNull(row,
tablePrefix: 'nested_0'), tablePrefix: 'nested_0'),

View File

@ -6,7 +6,7 @@ CREATE TABLE no_ids (
) WITHOUT ROWID WITH NoIdRow; ) WITHOUT ROWID WITH NoIdRow;
CREATE TABLE with_defaults ( CREATE TABLE with_defaults (
a TEXT JSON KEY customJsonName DEFAULT 'something', a `const CustomTextType()` JSON KEY customJsonName DEFAULT 'something',
b INT UNIQUE b INT UNIQUE
); );

View File

@ -11,7 +11,7 @@ const _createNoIds =
'WITHOUT ROWID;'; 'WITHOUT ROWID;';
const _createWithDefaults = 'CREATE TABLE IF NOT EXISTS "with_defaults" (' 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" (' const _createWithConstraints = 'CREATE TABLE IF NOT EXISTS "with_constraints" ('
'"a" TEXT, "b" INTEGER NOT NULL, "c" REAL, ' '"a" TEXT, "b" INTEGER NOT NULL, "c" REAL, '

View File

@ -1,3 +1,7 @@
## 2.14.0-dev
- Fix generated queries relying on custom types.
## 2.13.1 ## 2.13.1
- Add `has_separate_analyzer` option to optimize builds using the `not_shared` builder. - 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) { if (column.sqlType.isCustom) {
final method = isNullable ? 'readNullableWithType' : 'readWithType'; final method = isNullable ? 'readNullableWithType' : 'readWithType';
final typeImpl = _emitter.dartCode(column.sqlType.custom!.expression); final typeImpl = _emitter.dartCode(column.sqlType.custom!.expression);
code = 'row.$method<$rawDartType>($dartLiteral, $typeImpl)'; code = 'row.$method<$rawDartType>($typeImpl, $dartLiteral)';
} else { } else {
final method = isNullable ? 'readNullable' : 'read'; final method = isNullable ? 'readNullable' : 'read';
code = 'row.$method<$rawDartType>($dartLiteral)'; code = 'row.$method<$rawDartType>($dartLiteral)';