Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,14 +764,18 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
$keyValue = $key;
}

if ($keyValue === []) {
return $this;
}

// If the escape value was not set will base it on the global setting
if (! is_bool($escape)) {
$escape = $this->db->protectIdentifiers;
}

foreach ($keyValue as $k => $v) {
$prefix = empty($this->{$qbKey}) ? $this->groupGetType('') : $this->groupGetType($type);
$prefix = empty($this->{$qbKey}) ? $this->groupGetType('') : $this->groupGetType($type);

foreach ($keyValue as $k => $v) {
if ($rawSqlOnly) {
$k = '';
$op = '';
Expand Down Expand Up @@ -830,6 +834,8 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
'escape' => $escape,
];
}

$prefix = $type;
}

return $this;
Expand Down Expand Up @@ -1152,13 +1158,17 @@ protected function _like($field, string $match = '', string $type = 'AND ', stri

$keyValue = is_array($field) ? $field : [$field => $match];

if ($keyValue === []) {
return $this;
}

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

Comment on lines +1165 to +1166

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs the same empty-array guard as whereHaving(). groupGetType() mutates the group state, so calling it before an empty loop can break grouped queries like groupStart()->like([])->where(...), producing invalid SQL with AND immediately after (. Please add a regression test for that case.

foreach ($keyValue as $k => $v) {
if ($insensitiveSearch) {
$v = mb_strtolower($v, 'UTF-8');
}

$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);

if ($side === 'none') {
$bind = $this->setBind($k, $v, $escape);
} elseif ($side === 'before') {
Expand All @@ -1180,6 +1190,8 @@ protected function _like($field, string $match = '', string $type = 'AND ', stri
'condition' => $likeStatement,
'escape' => $escape,
];

$prefix = $type;
}

return $this;
Expand Down
111 changes: 111 additions & 0 deletions tests/system/Database/Builder/LikeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Database\RawSql;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down Expand Up @@ -231,4 +232,114 @@ public function testDBPrefixAndCoulmnWithTablename(): void
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
$this->assertSame($expectedBinds, $builder->getBinds());
}

public function testLikeMultipleFields(): void
{
$builder = new BaseBuilder('job', $this->db);

$builder->like([
'name' => 'veloper',
'title' => 'dev',
]);

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' AND \"title\" LIKE '%dev%' ESCAPE '!'";
$expectedBinds = [
'name' => [
'%veloper%',
true,
],
'title' => [
'%dev%',
true,
],
];

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

public function testLikeMultipleCallsWithRawSqlAndString(): void
{
$builder = new BaseBuilder('users', $this->db);

$sql = "concat(users.name, ' ', users.surname)";
$rawSql = new RawSql($sql);

$builder->like($rawSql, 'value')->like('name', 'veloper');

$expectedSQL = "SELECT * FROM \"users\" WHERE {$sql} LIKE '%value%' ESCAPE '!' AND \"name\" LIKE '%veloper%' ESCAPE '!'";
$expectedBinds = [
$rawSql->getBindingKey() => [
'%value%',
true,
],
'name' => [
'%veloper%',
true,
],
];

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

public function testOrLikeMultipleFields(): void
{
$builder = new BaseBuilder('job', $this->db);

$builder->orLike([
'name' => 'veloper',
'title' => 'dev',
]);

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' OR \"title\" LIKE '%dev%' ESCAPE '!'";
$expectedBinds = [
'name' => [
'%veloper%',
true,
],
'title' => [
'%dev%',
true,
],
];

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

#[DataProvider('provideLikeMethodsWithEmptyArray')]
public function testLikeMethodsWithEmptyArray(string $method): void
{
$builder = new BaseBuilder('job', $this->db);

$builder->groupStart()
->{$method}([])
->where('id', 1)
->groupEnd();

$expectedSQL = 'SELECT * FROM "job" WHERE ( "id" = 1 )';
$expectedBinds = [
'id' => [
1,
true,
],
];

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

/**
* @return array<string, array{0: string}>
*/
public static function provideLikeMethodsWithEmptyArray(): iterable
{
return [
'like' => ['like'],
'orLike' => ['orLike'],
'notLike' => ['notLike'],
'orNotLike' => ['orNotLike'],
];
}
}
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/empty.notAllowed.neon
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ parameters:

-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
count: 28
count: 27
path: ../../system/Database/BaseBuilder.php

-
Expand Down
Loading