Skip to content

Commit 28612fe

Browse files
committed
refactor(database): address whereColumn review polish
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 95d5437 commit 28612fe

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ public function orWhere($key, $value = null, ?bool $escape = null)
747747
*
748748
* @throws InvalidArgumentException
749749
*/
750-
public function whereColumn(string $first, string $second, ?bool $escape = null)
750+
public function whereColumn(string $first, string $second, ?bool $escape = null): static
751751
{
752752
return $this->whereColumnHaving('QBWhere', $first, $second, 'AND ', $escape);
753753
}
@@ -763,7 +763,7 @@ public function whereColumn(string $first, string $second, ?bool $escape = null)
763763
*
764764
* @throws InvalidArgumentException
765765
*/
766-
public function orWhereColumn(string $first, string $second, ?bool $escape = null)
766+
public function orWhereColumn(string $first, string $second, ?bool $escape = null): static
767767
{
768768
return $this->whereColumnHaving('QBWhere', $first, $second, 'OR ', $escape);
769769
}
@@ -782,7 +782,7 @@ public function orWhereColumn(string $first, string $second, ?bool $escape = nul
782782
*
783783
* @throws InvalidArgumentException
784784
*/
785-
protected function whereColumnHaving(string $qbKey, string $first, string $second, string $type = 'AND ', ?bool $escape = null)
785+
protected function whereColumnHaving(string $qbKey, string $first, string $second, string $type = 'AND ', ?bool $escape = null): static
786786
{
787787
[$first, $operator] = $this->parseWhereColumnFirst($first);
788788
$second = trim($second);
@@ -793,9 +793,7 @@ protected function whereColumnHaving(string $qbKey, string $first, string $secon
793793
throw new InvalidArgumentException(sprintf('%s() expects $first and $second to be non-empty strings', $caller));
794794
}
795795

796-
if (! is_bool($escape)) {
797-
$escape = $this->db->protectIdentifiers;
798-
}
796+
$escape ??= $this->db->protectIdentifiers;
799797

800798
$prefix = $this->{$qbKey} === [] ? $this->groupGetType('') : $this->groupGetType($type);
801799

@@ -3278,6 +3276,8 @@ protected function compileWhereHaving(string $qbKey): string
32783276
}
32793277

32803278
/**
3279+
* @used-by compileWhereHaving()
3280+
*
32813281
* @param array{columnComparison: true, condition: string, escape: bool, first: string, operator: string, second: string} $condition
32823282
*/
32833283
private function compileColumnComparison(array $condition): string

tests/system/Database/Builder/WhereTest.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,30 +352,35 @@ public function testOrWhereSameColumn(): void
352352
$this->assertSame($expectedBinds, $builder->getBinds());
353353
}
354354

355-
public function testWhereColumn(): void
355+
#[DataProvider('provideWhereColumnWithOperators')]
356+
public function testWhereColumnWithOperators(string $first, string $operator): void
356357
{
357358
$builder = $this->db->table('users');
358359

359-
$builder->whereColumn('created_at', 'updated_at');
360+
$builder->whereColumn($first, 'updated_at');
360361

361-
$expectedSQL = 'SELECT * FROM "users" WHERE "created_at" = "updated_at"';
362+
$expectedSQL = sprintf('SELECT * FROM "users" WHERE "created_at" %s "updated_at"', $operator);
362363
$expectedBinds = [];
363364

364365
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
365366
$this->assertSame($expectedBinds, $builder->getBinds());
366367
}
367368

368-
public function testWhereColumnWithOperator(): void
369+
/**
370+
* @return iterable<string, array{string, string}>
371+
*/
372+
public static function provideWhereColumnWithOperators(): iterable
369373
{
370-
$builder = $this->db->table('users');
371-
372-
$builder->whereColumn('updated_at >', 'created_at');
373-
374-
$expectedSQL = 'SELECT * FROM "users" WHERE "updated_at" > "created_at"';
375-
$expectedBinds = [];
376-
377-
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
378-
$this->assertSame($expectedBinds, $builder->getBinds());
374+
return [
375+
'default' => ['created_at', '='],
376+
'=' => ['created_at =', '='],
377+
'!=' => ['created_at !=', '!='],
378+
'<>' => ['created_at <>', '<>'],
379+
'<' => ['created_at <', '<'],
380+
'>' => ['created_at >', '>'],
381+
'<=' => ['created_at <=', '<='],
382+
'>=' => ['created_at >=', '>='],
383+
];
379384
}
380385

381386
public function testWhereColumnWithAlias(): void

0 commit comments

Comments
 (0)