Skip to content

Commit

Permalink
Add utility for reading columns with converter
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Sep 19, 2023
1 parent 2f73220 commit 0ed7358
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drift/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- Add the `@TableIndex` annotation for table classes to add an index to the
table.
- Support `json_each` and `json_tree`.
- Add `TypedResult.readWithConverter` to read a column with a type converter
from a join result row.

## 2.11.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,13 @@ class TypedResult {
'Invalid call to read(): $expr. This result set does not have a column '
'for that expression.');
}

/// Reads a column that has a type converter applied to it from the row.
///
/// This calls [read] internally, which reads the column but without applying
/// a type converter.
D? readWithConverter<D, S extends Object>(
GeneratedColumnWithTypeConverter<D, S> column) {
return column.converter.fromSql(read<S>(column));
}
}
5 changes: 5 additions & 0 deletions drift/test/database/statements/join_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void main() {
't.content': 'content',
't.target_date': date.millisecondsSinceEpoch ~/ 1000,
't.category': 3,
't.status': 'workInProgress',
'c.id': 3,
'c.desc': 'description',
'c.description_in_upper_case': 'DESCRIPTION',
Expand All @@ -85,6 +86,7 @@ void main() {
content: 'content',
targetDate: date,
category: 3,
status: TodoStatus.workInProgress,
));

expect(
Expand All @@ -101,6 +103,9 @@ void main() {
expect(row.read(todos.id), 5);
expect(row.read(categories.description), 'description');

expect(row.read(todos.status), 'workInProgress');
expect(row.readWithConverter(todos.status), TodoStatus.workInProgress);

verify(executor.runSelect(argThat(contains('DISTINCT')), any));
});

Expand Down

0 comments on commit 0ed7358

Please sign in to comment.