32 lines
1.1 KiB
PHP
32 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\QueryBuilder;
|
|
|
|
use Ulmus\Common\Sql;
|
|
use Ulmus\Query;
|
|
|
|
trait JsonQueryBuilderTrait
|
|
{
|
|
public function whereJsonArray(string|\Stringable $field, mixed $value, string $operator = Query\Where::OPERATOR_EQUAL, string $condition = Query\Where::CONDITION_AND, bool $not = false) : self
|
|
{
|
|
$uid = uniqid("json_");
|
|
|
|
$extract = Sql::function("json_extract", $field, '$');
|
|
|
|
$each = Sql::function('json_each', $extract)->as($uid);
|
|
|
|
return $this->from( $each )->where(Sql::raw("{$uid}.value"), $value, $operator, $condition, $not);
|
|
}
|
|
|
|
public function whereJsonKey(string|\Stringable $field, string|null $key, mixed $value, string $operator = Query\Where::OPERATOR_EQUAL, string $condition = Query\Where::CONDITION_AND, bool $not = false) : self
|
|
{
|
|
$uid = uniqid("json_");
|
|
|
|
$extract = Sql::function("json_extract", $field, $key ? "$.$key" : "$");
|
|
|
|
$each = Sql::function('json_each', $extract)->as($uid);
|
|
|
|
return $this->from( $each )->where(Sql::raw("{$uid}.value"), $value, $operator, $condition, $not);
|
|
}
|
|
}
|