Skip to content

Commit

Permalink
do not add parentheses when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Oct 31, 2024
1 parent 7df25bf commit f052169
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/SqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/unit/SqlProcessorTest.where.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit f052169

Please sign in to comment.