Skip to content

Commit f052169

Browse files
committed
do not add parentheses when not needed
1 parent 7df25bf commit f052169

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/SqlProcessor.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ private function processValues(array $value): string
560560
*/
561561
private function processWhere(string $type, array $value): string
562562
{
563-
if (count($value) === 0) {
563+
$totalCount = \count($value);
564+
if ($totalCount === 0) {
564565
return '1=1';
565566
}
566567

@@ -572,7 +573,7 @@ private function processWhere(string $type, array $value): string
572573
throw new InvalidArgumentException("Modifier %$type requires items with numeric index to be array, $subValueType given.");
573574
}
574575

575-
if (count($subValue) > 0 && $subValue[0] instanceof Fqn) {
576+
if (count($subValue) > 0 && ($subValue[0] ?? null) instanceof Fqn) {
576577
$column = $this->processModifier('column', $subValue[0]);
577578
$subType = substr($subValue[2] ?? '%any', 1);
578579
if ($subValue[1] === null) {
@@ -584,7 +585,11 @@ private function processWhere(string $type, array $value): string
584585
}
585586
$operand = $column . $op . $this->processModifier($subType, $subValue[1]);
586587
} else {
587-
$operand = '(' . $this->process($subValue) . ')';
588+
if ($totalCount === 1) {
589+
$operand = $this->process($subValue);
590+
} else {
591+
$operand = '(' . $this->process($subValue) . ')';
592+
}
588593
}
589594

590595
} else {

tests/cases/unit/SqlProcessorTest.where.phpt

+9
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ class SqlProcessorWhereTest extends TestCase
189189
}
190190

191191

192+
public function testSingleCondsParetheses()
193+
{
194+
Assert::same(
195+
'test = 1',
196+
$this->parser->processModifier('and', [['test = 1']]),
197+
);
198+
}
199+
200+
192201
public function testMultiColumnOr()
193202
{
194203
$this->platform->shouldReceive('formatIdentifier')->once()->with('a')->andReturn('a');

0 commit comments

Comments
 (0)