Skip to content

Commit 6e34449

Browse files
committed
move logic to QueryFunction class
1 parent 716b97b commit 6e34449

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/Glpi/Asset/CustomFieldType/DropdownType.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
use Glpi\DBAL\QueryExpression;
4141
use Glpi\DBAL\QueryFunction;
4242

43-
use function Safe\preg_match;
44-
4543
class DropdownType extends AbstractType
4644
{
4745
public static function getName(): string
@@ -155,11 +153,10 @@ public function getSearchOption(): ?array
155153
];
156154
} else {
157155
$opt['joinparams']['condition'] = [
158-
QueryFunction::jsonContains([
156+
QueryFunction::jsonContains(
159157
'REFTABLE.custom_fields',
160-
preg_match('/-MariaDB/', $DB->getVersion()) ? 'NEWTABLE.id' : QueryFunction::cast('NEWTABLE.id', 'JSON'),
161-
new QueryExpression($DB::quoteValue('$."' . $this->custom_field->fields['id'] . '"')),
162-
]),
158+
'NEWTABLE.id'
159+
),
163160
];
164161
$opt['forcegroupby'] = true;
165162
$opt['usehaving'] = true;

src/Glpi/DBAL/QueryFunction.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,24 @@ public static function concat_ws(string|QueryExpression $separator, array $param
454454
$separator = $separator instanceof QueryExpression ? $separator : $DB::quoteName($separator);
455455
return new QueryExpression('CONCAT_WS(' . $separator . ', ' . implode(', ', $params) . ')', $alias);
456456
}
457+
458+
public static function jsonContains(string|QueryExpression $target, string|QueryExpression $candidate, string $path, ?string $alias = null): QueryExpression
459+
{
460+
global $DB;
461+
462+
if (is_string($target)) {
463+
$target = new QueryExpression($DB::quoteName($target));
464+
}
465+
466+
if (is_string($candidate)) {
467+
$candidate = preg_match('/-MariaDB/', $DB->getVersion())
468+
? new QueryExpression($DB::quoteName($candidate))
469+
: QueryFunction::cast($candidate, 'JSON')
470+
;
471+
}
472+
473+
$path = new QueryExpression($DB::quoteValue($path));
474+
475+
return self::getExpression('JSON_CONTAINS', [$target, $candidate, $path], $alias);
476+
}
457477
}

0 commit comments

Comments
 (0)