mirror of https://github.com/AMT-Cheif/drift.git
Fix order of arguments when reading custom types
This commit is contained in:
parent
d770c16d8d
commit
e79124e5af
|
@ -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,
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
||||
|
|
|
@ -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, '
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
## 2.14.0-dev
|
||||
|
||||
- Fix generated queries relying on custom types.
|
||||
|
||||
## 2.13.1
|
||||
|
||||
- Add `has_separate_analyzer` option to optimize builds using the `not_shared` builder.
|
||||
|
|
|
@ -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)';
|
||||
|
|
Loading…
Reference in New Issue