Skip to content

Commit 5fbcdb2

Browse files
committed
move logic to QueryFunction class
1 parent 716b97b commit 5fbcdb2

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
* @method static QueryExpression concat(array $params, ?string $alias = null) Build a 'CONCAT' SQL function call
5656
* @method static QueryExpression floor(string|QueryExpression $expression, ?string $alias = null) Build a 'FLOOR' function call
5757
* @method static QueryExpression greatest(array $params, ?string $alias = null) Build a 'GREATEST' function call
58-
* @method static QueryExpression jsonContains(array $params, ?string $alias = null) Build a 'JSON_CONTAINS' function call
5958
* @method static QueryExpression jsonExtract(array $params, ?string $alias = null) Build a 'JSON_EXTRACT' function call
6059
* @method static QueryExpression jsonUnquote(string|QueryExpression $expression, ?string $alias = null) Build a 'JSON_UNQUOTE' function call
6160
* @method static QueryExpression jsonRemove(array $params, ?string $alias = null) Build a 'JSON_REMOVE' function call
@@ -454,4 +453,24 @@ public static function concat_ws(string|QueryExpression $separator, array $param
454453
$separator = $separator instanceof QueryExpression ? $separator : $DB::quoteName($separator);
455454
return new QueryExpression('CONCAT_WS(' . $separator . ', ' . implode(', ', $params) . ')', $alias);
456455
}
456+
457+
public static function jsonContains(string|QueryExpression $target, string|QueryExpression $candidate, string $path, ?string $alias = null): QueryExpression
458+
{
459+
global $DB;
460+
461+
if (is_string($target)) {
462+
$target = new QueryExpression($DB::quoteName($target));
463+
}
464+
465+
if (is_string($candidate)) {
466+
$candidate = preg_match('/-MariaDB/', $DB->getVersion())
467+
? new QueryExpression($DB::quoteName($candidate))
468+
: QueryFunction::cast($candidate, 'JSON')
469+
;
470+
}
471+
472+
$path = new QueryExpression($DB::quoteValue($path));
473+
474+
return self::getExpression('JSON_CONTAINS', [$target, $candidate, $path], $alias);
475+
}
457476
}

0 commit comments

Comments
 (0)