From 53b5afcda7005565b3c46f0df65fea44730d0ec0 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 4 Dec 2024 13:21:52 +0100 Subject: [PATCH] Fix embedding Dart expressions without type --- drift_dev/CHANGELOG.md | 4 +++ drift_dev/lib/src/writer/queries/utils.dart | 12 +++++---- .../writer/queries/query_writer_test.dart | 27 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index a313cf0d6..0f1f3049f 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.22.1-dev + +- Fix generating Dart expressions without an inferred type. + ## 2.22.0 - CLI options dealing with schemas now support views defined in Dart ([#3285](https://github.com/simolus3/drift/issues/3285)). diff --git a/drift_dev/lib/src/writer/queries/utils.dart b/drift_dev/lib/src/writer/queries/utils.dart index d903bc2f6..37ba076e5 100644 --- a/drift_dev/lib/src/writer/queries/utils.dart +++ b/drift_dev/lib/src/writer/queries/utils.dart @@ -23,11 +23,13 @@ extension FoundElementType on FoundElement { break; } } else if (kind is ExpressionDartPlaceholderType) { - builder - ..addSymbol('Expression', AnnotatedDartCode.drift) - ..addText('<') - ..addCode(scope.innerColumnType(kind.columnType!)) - ..addText('>'); + builder.addSymbol('Expression', AnnotatedDartCode.drift); + if (kind.columnType case final type?) { + builder + ..addText('<') + ..addCode(scope.innerColumnType(type)) + ..addText('>'); + } } else if (kind is InsertableDartPlaceholderType) { final table = kind.table; diff --git a/drift_dev/test/writer/queries/query_writer_test.dart b/drift_dev/test/writer/queries/query_writer_test.dart index bc7d10dc2..1ef3922ec 100644 --- a/drift_dev/test/writer/queries/query_writer_test.dart +++ b/drift_dev/test/writer/queries/query_writer_test.dart @@ -547,4 +547,31 @@ query (:foo AS TEXT): SELECT :foo; ), ); }); + + test('can use placeholders without specific type', () async { + final result = await generateForQueryInDriftFile( + r''' +CREATE TABLE examples ( + description TEXT NOT NULL +); + +query: SELECT CAST ($status AS INT) AS status FROM examples; +''', + options: const DriftOptions.defaults( + dialect: DialectOptions(null, [SqlDialect.sqlite], null), + ), + ); + + expect( + result, + allOf( + contains( + r'''return customSelect('SELECT CAST(${generatedstatus.sql} AS INT) AS status''', + ), + contains( + r'typedef Query$status = Expression Function(Examples examples);', + ), + ), + ); + }); }