Skip to content

Commit f7a23f0

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

3 files changed

Lines changed: 112 additions & 47 deletions

File tree

‎system/Database/BaseBuilder.php‎

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

1127+
$keyValue = [];
11211128
if ($field instanceof RawSql) {
1122-
$k = (string) $field;
1123-
$v = $match;
1124-
$insensitiveSearch = false;
1125-
1126-
$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);
1127-
1128-
if ($side === 'none') {
1129-
$bind = $this->setBind($field->getBindingKey(), $v, $escape);
1130-
} elseif ($side === 'before') {
1131-
$bind = $this->setBind($field->getBindingKey(), "%{$v}", $escape);
1132-
} elseif ($side === 'after') {
1133-
$bind = $this->setBind($field->getBindingKey(), "{$v}%", $escape);
1134-
} else {
1135-
$bind = $this->setBind($field->getBindingKey(), "%{$v}%", $escape);
1136-
}
1137-
1138-
$likeStatement = $this->_like_statement($prefix, $k, $not, $bind, $insensitiveSearch);
1139-
1140-
// some platforms require an escape sequence definition for LIKE wildcards
1141-
if ($escape === true && $this->db->likeEscapeStr !== '') {
1142-
$likeStatement .= sprintf($this->db->likeEscapeStr, $this->db->likeEscapeChar);
1129+
$keyValue[] = ['field' => $field, 'match' => $match];
1130+
} else {
1131+
foreach (is_array($field) ? $field : [$field => $match] as $k => $v) {
1132+
$keyValue[] = ['field' => $k, 'match' => $v];
11431133
}
1134+
}
11441135

1145-
$this->{$clause}[] = [
1146-
'condition' => $field->with($likeStatement),
1147-
'escape' => $escape,
1148-
];
1149-
1136+
if ($keyValue === []) {
11501137
return $this;
11511138
}
11521139

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

1155-
foreach ($keyValue as $k => $v) {
1156-
if ($insensitiveSearch) {
1142+
foreach ($keyValue as $item) {
1143+
$k = $item['field'];
1144+
$v = $item['match'];
1145+
1146+
if ($insensitiveSearch && ! ($k instanceof RawSql)) {
11571147
$v = mb_strtolower($v, 'UTF-8');
11581148
}
11591149

1160-
$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);
1150+
$val = match ($side) {
1151+
'none' => $v,
1152+
'before' => "%{$v}",
1153+
'after' => "{$v}%",
1154+
default => "%{$v}%",
1155+
};
11611156

1162-
if ($side === 'none') {
1163-
$bind = $this->setBind($k, $v, $escape);
1164-
} elseif ($side === 'before') {
1165-
$bind = $this->setBind($k, "%{$v}", $escape);
1166-
} elseif ($side === 'after') {
1167-
$bind = $this->setBind($k, "{$v}%", $escape);
1168-
} else {
1169-
$bind = $this->setBind($k, "%{$v}%", $escape);
1170-
}
1157+
$bind = ($k instanceof RawSql)
1158+
? $this->setBind($k->getBindingKey(), $val, $escape)
1159+
: $this->setBind($k, $val, $escape);
11711160

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

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

11791167
$this->{$clause}[] = [
1180-
'condition' => $likeStatement,
1168+
'condition' => ($k instanceof RawSql) ? $k->with($likeStatement) : $likeStatement,
11811169
'escape' => $escape,
11821170
];
1171+
1172+
$prefix = $type;
11831173
}
11841174

11851175
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)