From 915a8830fd4b89e0e68d6628603a07450a186058 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 18 Oct 2024 14:40:45 +0200 Subject: [PATCH 1/3] Complex filters: in array --- lib/Service/MySQLJsonService.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/Service/MySQLJsonService.php b/lib/Service/MySQLJsonService.php index a159d4c..fddd1bd 100644 --- a/lib/Service/MySQLJsonService.php +++ b/lib/Service/MySQLJsonService.php @@ -12,13 +12,24 @@ 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"); +// var_dump($value); + $builder->createNamedParameter(value: "$.$filter", placeHolder: ":path$filter"); + if(is_array($value) === true) { + $builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter"); + $builder + ->andWhere("json_unquote(json_extract(object, :path$filter)) IN (:value$filter)"); + continue; + } + + $builder->createNamedParameter(value: $value, placeHolder: ":value$filter"); $builder ->andWhere("json_extract(object, :path$filter) = :value$filter"); } - +// +// var_dump($builder->getSQL()); +// var_Dump($builder->getParameters()); return $builder; } From ca7a881230ce8d31f41e227cac9c9d9b092b7e77 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 18 Oct 2024 15:19:35 +0200 Subject: [PATCH 2/3] Add ordering on JSON --- lib/Db/ObjectEntityMapper.php | 5 +++-- lib/Service/MySQLJsonService.php | 17 +++++++++++++---- lib/Service/ObjectService.php | 9 +++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/Db/ObjectEntityMapper.php b/lib/Db/ObjectEntityMapper.php index 88e830b..4a32555 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 fddd1bd..4096c24 100644 --- a/lib/Service/MySQLJsonService.php +++ b/lib/Service/MySQLJsonService.php @@ -7,12 +7,24 @@ 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) { -// var_dump($value); $builder->createNamedParameter(value: "$.$filter", placeHolder: ":path$filter"); @@ -27,9 +39,6 @@ function filterJson(IQueryBuilder $builder, array $filters): IQueryBuilder $builder ->andWhere("json_extract(object, :path$filter) = :value$filter"); } -// -// var_dump($builder->getSQL()); -// var_Dump($builder->getParameters()); return $builder; } diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index af6d179..6adb8a3 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); } /** From 503ae510d9beae931a416dece34fb4c09ab87c16 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 18 Oct 2024 16:55:56 +0200 Subject: [PATCH 3/3] Add after/before filters --- lib/Service/MySQLJsonService.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/Service/MySQLJsonService.php b/lib/Service/MySQLJsonService.php index 4096c24..3a8e267 100644 --- a/lib/Service/MySQLJsonService.php +++ b/lib/Service/MySQLJsonService.php @@ -29,9 +29,23 @@ function filterJson(IQueryBuilder $builder, array $filters): IQueryBuilder $builder->createNamedParameter(value: "$.$filter", placeHolder: ":path$filter"); if(is_array($value) === true) { - $builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter"); - $builder - ->andWhere("json_unquote(json_extract(object, :path$filter)) IN (:value$filter)"); + 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; }