Skip to content

Commit

Permalink
Fix order of arguments when reading custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Nov 9, 2023
1 parent d770c16 commit e79124e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
27 changes: 27 additions & 0 deletions drift/test/generated/converter.dart
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 4 additions & 4 deletions drift/test/generated/custom_tables.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion drift/test/generated/tables.drift
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
2 changes: 1 addition & 1 deletion drift/test/integration_tests/drift_files_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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, '
Expand Down
4 changes: 4 additions & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion drift_dev/lib/src/writer/queries/query_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)';
Expand Down

0 comments on commit e79124e

Please sign in to comment.