From a65d424263adfbff9629c7d91a613e4504c84613 Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Tue, 17 Sep 2024 14:51:23 +0200 Subject: Add search --- src/application/mystic/forum/Database.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/application/mystic/forum/Database.php') diff --git a/src/application/mystic/forum/Database.php b/src/application/mystic/forum/Database.php index a1e67ea..8ebc36b 100644 --- a/src/application/mystic/forum/Database.php +++ b/src/application/mystic/forum/Database.php @@ -414,6 +414,33 @@ class Database { return $items; } + public function execCustomQuery(string $query, ?array $customQueryParams = null, ?string $entityClassName = null): array { + if ($customQueryParams === null) + $result = \pg_query($this->connection, $query); + else + $result = \pg_query_params($this->connection, $query, $customQueryParams); + if ($result === false) + throw new \RuntimeException("Query failed: " . \pg_last_error($this->connection)); + $cols = null; + if ($entityClassName !== null) { + $reflClass = new ReflectionClass($entityClassName); + $cols = self::getColumns($reflClass); + } + $rowsOrItems = []; + while (($row = \pg_fetch_assoc($result)) !== false) { + if ($entityClassName !== null) { + $entity = new $entityClassName(); + foreach ($cols as $colName => $colProps) + self::assignValue($entity, $colProps, $row[$colName]); + $rowsOrItems []= $entity; + } else { + $rowsOrItems []= $row; + } + } + \pg_free_result($result); + return $rowsOrItems; + } + public function delete(Entity &$entity): bool { $entityClassName = get_class($entity); $tableName = self::getTableName($entityClassName); -- cgit v1.2.3