- WIP on JsonConditionTrait for SQL flavors that supports it; fixed a bug within Entity initialization from a new method to instanciate it which was implemented
This commit is contained in:
parent
4ad44f73d7
commit
e3314f40ac
src
31
src/QueryBuilder/JsonQueryBuilderTrait.php
Normal file
31
src/QueryBuilder/JsonQueryBuilderTrait.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
19
src/Repository/JsonConditionTrait.php
Normal file
19
src/Repository/JsonConditionTrait.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Repository;
|
||||
|
||||
use Ulmus\Query;
|
||||
|
||||
trait JsonConditionTrait
|
||||
{
|
||||
public function whereJsonArray(string|\Stringable $field, mixed $value, string $operator = Query\Where::OPERATOR_EQUAL, $condition = Query\Where::CONDITION_AND) : self
|
||||
{
|
||||
if ( null === $this->queryBuilder->getFragment(Query\From::class) ) {
|
||||
$this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
||||
}
|
||||
|
||||
$this->queryBuilder->whereJsonArray($field, $value, $operator, $condition);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user