From f052169e3e90b8b011589fb185d8d3886479f3ad Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Thu, 31 Oct 2024 10:38:07 +0100 Subject: [PATCH] do not add parentheses when not needed --- src/SqlProcessor.php | 11 ++++++++--- tests/cases/unit/SqlProcessorTest.where.phpt | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/SqlProcessor.php b/src/SqlProcessor.php index a915014..2fbea33 100644 --- a/src/SqlProcessor.php +++ b/src/SqlProcessor.php @@ -560,7 +560,8 @@ private function processValues(array $value): string */ private function processWhere(string $type, array $value): string { - if (count($value) === 0) { + $totalCount = \count($value); + if ($totalCount === 0) { return '1=1'; } @@ -572,7 +573,7 @@ private function processWhere(string $type, array $value): string throw new InvalidArgumentException("Modifier %$type requires items with numeric index to be array, $subValueType given."); } - if (count($subValue) > 0 && $subValue[0] instanceof Fqn) { + if (count($subValue) > 0 && ($subValue[0] ?? null) instanceof Fqn) { $column = $this->processModifier('column', $subValue[0]); $subType = substr($subValue[2] ?? '%any', 1); if ($subValue[1] === null) { @@ -584,7 +585,11 @@ private function processWhere(string $type, array $value): string } $operand = $column . $op . $this->processModifier($subType, $subValue[1]); } else { - $operand = '(' . $this->process($subValue) . ')'; + if ($totalCount === 1) { + $operand = $this->process($subValue); + } else { + $operand = '(' . $this->process($subValue) . ')'; + } } } else { diff --git a/tests/cases/unit/SqlProcessorTest.where.phpt b/tests/cases/unit/SqlProcessorTest.where.phpt index 6581778..d1cc8e5 100644 --- a/tests/cases/unit/SqlProcessorTest.where.phpt +++ b/tests/cases/unit/SqlProcessorTest.where.phpt @@ -189,6 +189,15 @@ class SqlProcessorWhereTest extends TestCase } + public function testSingleCondsParetheses() + { + Assert::same( + 'test = 1', + $this->parser->processModifier('and', [['test = 1']]), + ); + } + + public function testMultiColumnOr() { $this->platform->shouldReceive('formatIdentifier')->once()->with('a')->andReturn('a');