From 6d31b37ae52ffa23f21e3e075b25ae7210790dc9 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 26 Oct 2024 20:53:53 +0200 Subject: [PATCH] Fix missing parentheses around same precedence Closes https://github.com/simolus3/drift/issues/3309 --- drift/CHANGELOG.md | 1 + .../src/runtime/query_builder/expressions/expression.dart | 2 +- drift/test/database/expressions/algebra_test.dart | 7 +++++++ .../database/expressions/datetime_expression_test.dart | 2 +- drift/test/database/expressions/expression_test.dart | 6 +++--- 5 files changed, 13 insertions(+), 5 deletions(-) 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)'), ); });