diff --git a/lib/Db/ObjectEntityMapper.php b/lib/Db/ObjectEntityMapper.php index 88e830bb46..4a32555eea 100644 --- a/lib/Db/ObjectEntityMapper.php +++ b/lib/Db/ObjectEntityMapper.php @@ -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(); @@ -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); } diff --git a/lib/Service/MySQLJsonService.php b/lib/Service/MySQLJsonService.php index a159d4cb61..3a8e2675f3 100644 --- a/lib/Service/MySQLJsonService.php +++ b/lib/Service/MySQLJsonService.php @@ -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; } diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index af6d179cd9..6adb8a3bda 100755 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -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); @@ -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'; @@ -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); } /**