Skip to content

Commit 521a3e1

Browse files
committed
refactor(database): optimize groupGetType inside loop in whereHaving and _like
1 parent 19cae6e commit 521a3e1

3 files changed

Lines changed: 88 additions & 5 deletions

File tree

‎system/Database/BaseBuilder.php‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,18 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
764764
$keyValue = $key;
765765
}
766766

767+
if ($keyValue === []) {
768+
return $this;
769+
}
770+
767771
// If the escape value was not set will base it on the global setting
768772
if (! is_bool($escape)) {
769773
$escape = $this->db->protectIdentifiers;
770774
}
771775

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

778+
foreach ($keyValue as $k => $v) {
775779
if ($rawSqlOnly) {
776780
$k = '';
777781
$op = '';
@@ -830,6 +834,8 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
830834
'escape' => $escape,
831835
];
832836
}
837+
838+
$prefix = $type;
833839
}
834840

835841
return $this;
@@ -1152,13 +1158,13 @@ protected function _like($field, string $match = '', string $type = 'AND ', stri
11521158

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

1161+
$prefix = $this->{$clause} === [] ? $this->groupGetType('') : $this->groupGetType($type);
1162+
11551163
foreach ($keyValue as $k => $v) {
11561164
if ($insensitiveSearch) {
11571165
$v = mb_strtolower($v, 'UTF-8');
11581166
}
11591167

1160-
$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);
1161-
11621168
if ($side === 'none') {
11631169
$bind = $this->setBind($k, $v, $escape);
11641170
} elseif ($side === 'before') {
@@ -1180,6 +1186,8 @@ protected function _like($field, string $match = '', string $type = 'AND ', stri
11801186
'condition' => $likeStatement,
11811187
'escape' => $escape,
11821188
];
1189+
1190+
$prefix = $type;
11831191
}
11841192

11851193
return $this;

‎tests/system/Database/Builder/LikeTest.php‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,79 @@ public function testDBPrefixAndCoulmnWithTablename(): void
231231
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
232232
$this->assertSame($expectedBinds, $builder->getBinds());
233233
}
234+
235+
public function testLikeMultipleFields(): void
236+
{
237+
$builder = new BaseBuilder('job', $this->db);
238+
239+
$builder->like([
240+
'name' => 'veloper',
241+
'title' => 'dev',
242+
]);
243+
244+
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' AND \"title\" LIKE '%dev%' ESCAPE '!'";
245+
$expectedBinds = [
246+
'name' => [
247+
'%veloper%',
248+
true,
249+
],
250+
'title' => [
251+
'%dev%',
252+
true,
253+
],
254+
];
255+
256+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
257+
$this->assertSame($expectedBinds, $builder->getBinds());
258+
}
259+
260+
public function testLikeMultipleCallsWithRawSqlAndString(): void
261+
{
262+
$builder = new BaseBuilder('users', $this->db);
263+
264+
$sql = "concat(users.name, ' ', users.surname)";
265+
$rawSql = new RawSql($sql);
266+
267+
$builder->like($rawSql, 'value')->like('name', 'veloper');
268+
269+
$expectedSQL = "SELECT * FROM \"users\" WHERE {$sql} LIKE '%value%' ESCAPE '!' AND \"name\" LIKE '%veloper%' ESCAPE '!'";
270+
$expectedBinds = [
271+
$rawSql->getBindingKey() => [
272+
'%value%',
273+
true,
274+
],
275+
'name' => [
276+
'%veloper%',
277+
true,
278+
],
279+
];
280+
281+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
282+
$this->assertSame($expectedBinds, $builder->getBinds());
283+
}
284+
285+
public function testOrLikeMultipleFields(): void
286+
{
287+
$builder = new BaseBuilder('job', $this->db);
288+
289+
$builder->orLike([
290+
'name' => 'veloper',
291+
'title' => 'dev',
292+
]);
293+
294+
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' OR \"title\" LIKE '%dev%' ESCAPE '!'";
295+
$expectedBinds = [
296+
'name' => [
297+
'%veloper%',
298+
true,
299+
],
300+
'title' => [
301+
'%dev%',
302+
true,
303+
],
304+
];
305+
306+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
307+
$this->assertSame($expectedBinds, $builder->getBinds());
308+
}
234309
}

‎utils/phpstan-baseline/empty.notAllowed.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ parameters:
3434

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

4040
-

0 commit comments

Comments
 (0)