- 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:
Dave M. 2024-11-16 16:05:26 +00:00
parent 4ad44f73d7
commit e3314f40ac
2 changed files with 50 additions and 0 deletions

@ -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);
}
}

@ -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;
}
}