Skip to content

Commit

Permalink
Fix embedding Dart expressions without type
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Dec 4, 2024
1 parent 6a7f6df commit 53b5afc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
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.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)).
Expand Down
12 changes: 7 additions & 5 deletions drift_dev/lib/src/writer/queries/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
27 changes: 27 additions & 0 deletions drift_dev/test/writer/queries/query_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);',
),
),
);
});
}

0 comments on commit 53b5afc

Please sign in to comment.