Skip to content

Commit 53b5afc

Browse files
committed
Fix embedding Dart expressions without type
1 parent 6a7f6df commit 53b5afc

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

drift_dev/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.22.1-dev
2+
3+
- Fix generating Dart expressions without an inferred type.
4+
15
## 2.22.0
26

37
- CLI options dealing with schemas now support views defined in Dart ([#3285](https://github.com/simolus3/drift/issues/3285)).

drift_dev/lib/src/writer/queries/utils.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ extension FoundElementType on FoundElement {
2323
break;
2424
}
2525
} else if (kind is ExpressionDartPlaceholderType) {
26-
builder
27-
..addSymbol('Expression', AnnotatedDartCode.drift)
28-
..addText('<')
29-
..addCode(scope.innerColumnType(kind.columnType!))
30-
..addText('>');
26+
builder.addSymbol('Expression', AnnotatedDartCode.drift);
27+
if (kind.columnType case final type?) {
28+
builder
29+
..addText('<')
30+
..addCode(scope.innerColumnType(type))
31+
..addText('>');
32+
}
3133
} else if (kind is InsertableDartPlaceholderType) {
3234
final table = kind.table;
3335

drift_dev/test/writer/queries/query_writer_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,31 @@ query (:foo AS TEXT): SELECT :foo;
547547
),
548548
);
549549
});
550+
551+
test('can use placeholders without specific type', () async {
552+
final result = await generateForQueryInDriftFile(
553+
r'''
554+
CREATE TABLE examples (
555+
description TEXT NOT NULL
556+
);
557+
558+
query: SELECT CAST ($status AS INT) AS status FROM examples;
559+
''',
560+
options: const DriftOptions.defaults(
561+
dialect: DialectOptions(null, [SqlDialect.sqlite], null),
562+
),
563+
);
564+
565+
expect(
566+
result,
567+
allOf(
568+
contains(
569+
r'''return customSelect('SELECT CAST(${generatedstatus.sql} AS INT) AS status''',
570+
),
571+
contains(
572+
r'typedef Query$status = Expression Function(Examples examples);',
573+
),
574+
),
575+
);
576+
});
550577
}

0 commit comments

Comments
 (0)