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
82 changes: 36 additions & 46 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 @@ -1115,71 +1121,55 @@ public function orNotHavingLike($field, string $match = '', string $side = 'both
*/
protected function _like($field, string $match = '', string $type = 'AND ', string $side = 'both', string $not = '', ?bool $escape = null, bool $insensitiveSearch = false, string $clause = 'QBWhere')
{
$escape = is_bool($escape) ? $escape : $this->db->protectIdentifiers;
$side = strtolower($side);
$escape ??= $this->db->protectIdentifiers;
$side = strtolower($side);

$keyValue = [];
if ($field instanceof RawSql) {
$k = (string) $field;
$v = $match;
$insensitiveSearch = false;

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

if ($side === 'none') {
$bind = $this->setBind($field->getBindingKey(), $v, $escape);
} elseif ($side === 'before') {
$bind = $this->setBind($field->getBindingKey(), "%{$v}", $escape);
} elseif ($side === 'after') {
$bind = $this->setBind($field->getBindingKey(), "{$v}%", $escape);
} else {
$bind = $this->setBind($field->getBindingKey(), "%{$v}%", $escape);
}

$likeStatement = $this->_like_statement($prefix, $k, $not, $bind, $insensitiveSearch);

// some platforms require an escape sequence definition for LIKE wildcards
if ($escape === true && $this->db->likeEscapeStr !== '') {
$likeStatement .= sprintf($this->db->likeEscapeStr, $this->db->likeEscapeChar);
$keyValue[] = ['field' => $field, 'match' => $match];
} else {
foreach (is_array($field) ? $field : [$field => $match] as $k => $v) {
$keyValue[] = ['field' => $k, 'match' => $v];
}
}

$this->{$clause}[] = [
'condition' => $field->with($likeStatement),
'escape' => $escape,
];

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

$keyValue = is_array($field) ? $field : [$field => $match];
$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);

foreach ($keyValue as $k => $v) {
if ($insensitiveSearch) {
foreach ($keyValue as $item) {
$k = $item['field'];
$v = $item['match'];

if ($insensitiveSearch && ! ($k instanceof RawSql)) {
$v = mb_strtolower($v, 'UTF-8');
}

$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);
$val = match ($side) {
'none' => $v,
'before' => "%{$v}",
'after' => "{$v}%",
default => "%{$v}%",
};

if ($side === 'none') {
$bind = $this->setBind($k, $v, $escape);
} elseif ($side === 'before') {
$bind = $this->setBind($k, "%{$v}", $escape);
} elseif ($side === 'after') {
$bind = $this->setBind($k, "{$v}%", $escape);
} else {
$bind = $this->setBind($k, "%{$v}%", $escape);
}
$bind = ($k instanceof RawSql)
? $this->setBind($k->getBindingKey(), $val, $escape)
: $this->setBind($k, $val, $escape);

$likeStatement = $this->_like_statement($prefix, $k, $not, $bind, $insensitiveSearch);
$likeStatement = $this->_like_statement($prefix, (string) $k, $not, $bind, $insensitiveSearch);

// some platforms require an escape sequence definition for LIKE wildcards
if ($escape === true && $this->db->likeEscapeStr !== '') {
$likeStatement .= sprintf($this->db->likeEscapeStr, $this->db->likeEscapeChar);
}

$this->{$clause}[] = [
'condition' => $likeStatement,
'condition' => ($k instanceof RawSql) ? $k->with($likeStatement) : $likeStatement,
'escape' => $escape,
];

$prefix = $type;
}

return $this;
Expand Down
75 changes: 75 additions & 0 deletions tests/system/Database/Builder/LikeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,79 @@ 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());
}
}
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