diff --git a/drift/CHANGELOG.md b/drift/CHANGELOG.md index 1325755e3..7e3d5fc67 100644 --- a/drift/CHANGELOG.md +++ b/drift/CHANGELOG.md @@ -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 diff --git a/drift/lib/src/runtime/query_builder/expressions/expression.dart b/drift/lib/src/runtime/query_builder/expressions/expression.dart index 9c5a7be31..a7ab1268e 100644 --- a/drift/lib/src/runtime/query_builder/expressions/expression.dart +++ b/drift/lib/src/runtime/query_builder/expressions/expression.dart @@ -255,7 +255,7 @@ abstract class Expression 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(')'); diff --git a/drift/test/database/expressions/algebra_test.dart b/drift/test/database/expressions/algebra_test.dart index db484ec4d..d66612310 100644 --- a/drift/test/database/expressions/algebra_test.dart +++ b/drift/test/database/expressions/algebra_test.dart @@ -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])); + }); } diff --git a/drift/test/database/expressions/datetime_expression_test.dart b/drift/test/database/expressions/datetime_expression_test.dart index 68cc01e0d..ab7155b90 100644 --- a/drift/test/database/expressions/datetime_expression_test.dart +++ b/drift/test/database/expressions/datetime_expression_test.dart @@ -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]), ); } diff --git a/drift/test/database/expressions/expression_test.dart b/drift/test/database/expressions/expression_test.dart index 3dec0e786..746ba96d4 100644 --- a/drift/test/database/expressions/expression_test.dart +++ b/drift/test/database/expressions/expression_test.dart @@ -138,7 +138,7 @@ void main() { for (var i = 0; i < 5; i++) CustomExpression('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')); @@ -152,7 +152,7 @@ void main() { for (var i = 0; i < 5; i++) CustomExpression('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')); @@ -172,7 +172,7 @@ void main() { const CustomExpression('d', precedence: Precedence.primary), ]), ]), - generates('(a OR b) AND c AND d'), + generates('(a OR b) AND (c AND d)'), ); });