Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for filtering and sorting #25

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/Db/ObjectEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function countAll(?array $filters = [], ?array $searchConditions = [], ?a
* @param array $searchParams The search parameters to apply to the objects
* @return array An array of ObjectEntitys
*/
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = [], array $sort = []): array
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -166,8 +166,9 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
$qb->setParameter($param, $value);
}
}
$qb = $this->databaseJsonService->filterJson($qb, $filters);

$qb = $this->databaseJsonService->filterJson(builder: $qb, filters: $filters);
$qb = $this->databaseJsonService->orderJson(builder: $qb, order: $sort);

return $this->findEntities(query: $qb);
}
Expand Down
38 changes: 36 additions & 2 deletions lib/Service/MySQLJsonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,52 @@

class MySQLJsonService implements IDatabaseJsonService
{
function orderJson(IQueryBuilder $builder, array $order = []): IQueryBuilder
{

foreach($order as $item=>$direction) {
$builder->createNamedParameter(value: "$.$item", placeHolder: ":path$item");
$builder->createNamedParameter(value: $direction, placeHolder: ":direction$item");

$builder->orderBy($builder->createFunction("json_unquote(json_extract(object, :path$item))"),$direction);
}

return $builder;
}

function filterJson(IQueryBuilder $builder, array $filters): IQueryBuilder
{
unset($filters['register'], $filters['schema'], $filters['updated'], $filters['created'], $filters['_queries']);

foreach($filters as $filter=>$value) {
$builder->createNamedParameter(value: $value, placeHolder: ":value$filter");

$builder->createNamedParameter(value: "$.$filter", placeHolder: ":path$filter");

if(is_array($value) === true) {
switch(array_keys($value)[0]) {
case 'after':
$builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter");
$builder
->andWhere("json_unquote(json_extract(object, :path$filter)) >= (:value$filter)");
break;
case 'before':
$builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter");
$builder
->andWhere("json_unquote(json_extract(object, :path$filter)) <= (:value$filter)");
break;
default:
$builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter");
$builder
->andWhere("json_unquote(json_extract(object, :path$filter)) IN (:value$filter)");
break;
}
continue;
}

$builder->createNamedParameter(value: $value, placeHolder: ":value$filter");
$builder
->andWhere("json_extract(object, :path$filter) = :value$filter");
}

return $builder;
}

Expand Down
9 changes: 5 additions & 4 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public function delete(array|\JsonSerializable $object): bool
);
}

public function findAll(?int $limit = null, ?int $offset = null, array $filters = []): array
public function findAll(?int $limit = null, ?int $offset = null, array $filters = [], array $sort = []): array
{
$objects = $this->getObjects(
register: $this->getRegister(),
schema: $this->getSchema(),
limit: $limit,
offset: $offset,
filters: $filters
filters: $filters,
sort: $sort
);
// $data = array_map([$this, 'getDataFromObject'], $objects);

Expand Down Expand Up @@ -138,7 +139,7 @@ private function getDataFromObject(mixed $object) {
* @return array The retrieved objects.
* @throws \Exception
*/
public function getObjects(?string $objectType = null, ?int $register = null, ?int $schema = null, ?int $limit = null, ?int $offset = null, array $filters = []): array
public function getObjects(?string $objectType = null, ?int $register = null, ?int $schema = null, ?int $limit = null, ?int $offset = null, array $filters = [], array $sort = []): array
{
if($objectType === null && $register !== null && $schema !== null) {
$objectType = 'objectEntity';
Expand All @@ -150,7 +151,7 @@ public function getObjects(?string $objectType = null, ?int $register = null, ?i
$mapper = $this->getMapper($objectType);

// Use the mapper to find and return all objects of the specified type
return $mapper->findAll(limit: $limit, offset: $offset, filters: $filters);
return $mapper->findAll(limit: $limit, offset: $offset, filters: $filters, sort: $sort);
}

/**
Expand Down