Skip to content

Commit

Permalink
Fix missing parentheses around same precedence
Browse files Browse the repository at this point in the history
Closes #3309
  • Loading branch information
simolus3 committed Oct 26, 2024
1 parent 84ce98a commit 6d31b37
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions drift/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.22.0

- Add `sqliteAny()` method to tables to declare `ANY` columns.
- Add missing parentheses around adjacent expressions of the same precedence.

## 2.21.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ abstract class Expression<D extends Object> implements FunctionParameter {
/// - [Component.writeInto], which doesn't take any precedence relation into
/// account.
void writeAroundPrecedence(GenerationContext context, Precedence precedence) {
if (this.precedence < precedence) {
if (this.precedence <= precedence) {
context.buffer.write('(');
writeInto(context);
context.buffer.write(')');
Expand Down
7 changes: 7 additions & 0 deletions drift/test/database/expressions/algebra_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ void main() {
test('absolute values', () {
expect(i2.abs(), generates('abs(i2)'));
});

test('with columns', () {
expect(const Variable(0) - (i1 - Variable(10)),
generates('? - (i1 - ?)', [0, 10]));
expect((const Variable(0) - i1) - Variable(10),
generates('(? - i1) - ?', [0, 10]));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void main() {
expect(
expr,
generates(
'CAST(strftime(\'%s\', CURRENT_TIMESTAMP) AS INTEGER) + ? - ?',
'(CAST(strftime(\'%s\', CURRENT_TIMESTAMP) AS INTEGER) + ?) - ?',
[259200, 5]),
);
}
Expand Down
6 changes: 3 additions & 3 deletions drift/test/database/expressions/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void main() {
for (var i = 0; i < 5; i++)
CustomExpression<bool>('e$i', precedence: Precedence.primary)
]),
generates('e0 AND e1 AND e2 AND e3 AND e4'),
generates('(((e0 AND e1) AND e2) AND e3) AND e4'),
);

expect(Expression.and(const []), generates('1'));
Expand All @@ -152,7 +152,7 @@ void main() {
for (var i = 0; i < 5; i++)
CustomExpression<bool>('e$i', precedence: Precedence.primary)
]),
generates('e0 OR e1 OR e2 OR e3 OR e4'),
generates('(((e0 OR e1) OR e2) OR e3) OR e4'),
);

expect(Expression.or(const []), generates('0'));
Expand All @@ -172,7 +172,7 @@ void main() {
const CustomExpression<bool>('d', precedence: Precedence.primary),
]),
]),
generates('(a OR b) AND c AND d'),
generates('(a OR b) AND (c AND d)'),
);
});

Expand Down

0 comments on commit 6d31b37

Please sign in to comment.