Skip to content

Commit

Permalink
Add test to ensure that generated expressions support dialect-aware c…
Browse files Browse the repository at this point in the history
…ustom types
  • Loading branch information
davidhole authored and simolus3 committed Mar 5, 2024
1 parent e423157 commit ca0c70e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions drift/lib/src/runtime/query_builder/expressions/custom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class CustomExpression<D extends Object> extends Expression<D> {
@override
final Precedence precedence;

final CustomSqlType<D>? _customSqlType;
final UserDefinedSqlType<D>? _customSqlType;

/// Constructs a custom expression by providing the raw sql [content].
const CustomExpression(
this.content, {
this.watchedTables = const [],
this.precedence = Precedence.unknown,
CustomSqlType<D>? customType,
UserDefinedSqlType<D>? customType,
}) : _dialectSpecificContent = null,
_customSqlType = customType;

Expand All @@ -41,7 +41,7 @@ class CustomExpression<D extends Object> extends Expression<D> {
Map<SqlDialect, String> content, {
this.watchedTables = const [],
this.precedence = Precedence.unknown,
CustomSqlType<D>? customType,
UserDefinedSqlType<D>? customType,
}) : _dialectSpecificContent = content,
content = '',
_customSqlType = customType;
Expand Down
17 changes: 17 additions & 0 deletions drift/test/database/expressions/custom_types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ void main() {
expect(exp, generates('?', [10]));
expect(exp.driftSqlType, isA<_NegatedIntType>());
});

test('also supports dialect-aware types', () {
const b = CustomExpression(
'b',
customType: DialectAwareSqlType<int>.via(
fallback: _NegatedIntType(),
overrides: {SqlDialect.postgres: DriftSqlType.int},
),
precedence: Precedence.primary,
);

expect(b.equals(3), generates('b = ?', [-3]));
expect(
b.equals(3),
generatesWithOptions('b = \$1',
variables: [3], dialect: SqlDialect.postgres));
});
}

class _NegatedIntType implements CustomSqlType<int> {
Expand Down

0 comments on commit ca0c70e

Please sign in to comment.