From d837ef4b86aac1b0b06260f3b8d88476ed7acd2c Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Wed, 25 Sep 2024 18:14:54 +0200 Subject: [PATCH 1/3] feat: do a single query to get all resource ids instead of a recursive and complex query --- .../persistence/smoothsql/class.Class.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/core/kernel/persistence/smoothsql/class.Class.php b/core/kernel/persistence/smoothsql/class.Class.php index 3be8f14f1..8d3678b27 100644 --- a/core/kernel/persistence/smoothsql/class.Class.php +++ b/core/kernel/persistence/smoothsql/class.Class.php @@ -203,6 +203,16 @@ public function getProperties(core_kernel_classes_Class $resource, $recursive = public function getInstances(core_kernel_classes_Class $resource, $recursive = false, $params = []) { $returnValue = []; + + if (empty($params)) { + $resourceIds = $this->getInstanceIds($resource->getUri(), $recursive); + + foreach ($resourceIds as $resourceId) { + $returnValue[$resourceId] = $this->getModel()->getResource($resourceId); + } + + return $returnValue; + } $params = array_merge($params, ['like' => false, 'recursive' => $recursive]); @@ -630,4 +640,47 @@ public function updateUri(core_kernel_classes_Class $resource, string $newUri): ] ); } + + public function getInstanceIds(string $classUri, bool $recursive = false): array + { + if ($recursive) { + $query = 'SELECT subject FROM statements WHERE predicate = ? AND object = ?'; + $params = [ + OntologyRdf::RDF_TYPE, + $classUri, + ]; + } else { + $query = <<<'SQL' +WITH RECURSIVE statements_tree AS ( + SELECT + r.subject, + r.predicate + FROM statements r + WHERE r.subject = ? + AND r.predicate IN (?, ?) + UNION ALL + SELECT + s.subject, + s.predicate + FROM statements s + JOIN statements_tree st + ON s.object = st.subject + WHERE s.predicate IN (?, ?) +) +SELECT subject FROM statements_tree WHERE predicate = ?; +SQL; + $params = [ + $classUri, + OntologyRdfs::RDFS_SUBCLASSOF, + OntologyRdf::RDF_TYPE, + OntologyRdfs::RDFS_SUBCLASSOF, + OntologyRdf::RDF_TYPE, + OntologyRdf::RDF_TYPE, + ]; + } + + $statement = $this->getPersistence()->query($query, $params); + + return array_column($statement->fetchAll(), 'subject'); + } } From 9984d50cc545ca6027db7e9658e9d8e9874a9773 Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Thu, 26 Sep 2024 12:05:12 +0200 Subject: [PATCH 2/3] chore: recursive condition adjusted --- core/kernel/persistence/smoothsql/class.Class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/kernel/persistence/smoothsql/class.Class.php b/core/kernel/persistence/smoothsql/class.Class.php index 8d3678b27..66a92ea82 100644 --- a/core/kernel/persistence/smoothsql/class.Class.php +++ b/core/kernel/persistence/smoothsql/class.Class.php @@ -643,7 +643,7 @@ public function updateUri(core_kernel_classes_Class $resource, string $newUri): public function getInstanceIds(string $classUri, bool $recursive = false): array { - if ($recursive) { + if (!$recursive) { $query = 'SELECT subject FROM statements WHERE predicate = ? AND object = ?'; $params = [ OntologyRdf::RDF_TYPE, From 6c2c76a214eb7e16a226755905c3ee5a8333b03c Mon Sep 17 00:00:00 2001 From: Gabriel Felipe Soares Date: Tue, 1 Oct 2024 14:42:00 +0200 Subject: [PATCH 3/3] chore: cs and method name fix --- core/kernel/persistence/smoothsql/class.Class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/kernel/persistence/smoothsql/class.Class.php b/core/kernel/persistence/smoothsql/class.Class.php index 66a92ea82..9a3b09a4b 100644 --- a/core/kernel/persistence/smoothsql/class.Class.php +++ b/core/kernel/persistence/smoothsql/class.Class.php @@ -203,9 +203,9 @@ public function getProperties(core_kernel_classes_Class $resource, $recursive = public function getInstances(core_kernel_classes_Class $resource, $recursive = false, $params = []) { $returnValue = []; - + if (empty($params)) { - $resourceIds = $this->getInstanceIds($resource->getUri(), $recursive); + $resourceIds = $this->getInstanceUris($resource->getUri(), $recursive); foreach ($resourceIds as $resourceId) { $returnValue[$resourceId] = $this->getModel()->getResource($resourceId); @@ -641,7 +641,7 @@ public function updateUri(core_kernel_classes_Class $resource, string $newUri): ); } - public function getInstanceIds(string $classUri, bool $recursive = false): array + public function getInstanceUris(string $classUri, bool $recursive = false): array { if (!$recursive) { $query = 'SELECT subject FROM statements WHERE predicate = ? AND object = ?';