From ffb52c67897cd6de06e66937864ede8b2b6d1c66 Mon Sep 17 00:00:00 2001 From: Lars Gebhardt-Kusche Date: Mon, 2 Mar 2026 22:58:41 +0100 Subject: [PATCH] asdad --- src/Repository/KeaHostRepository.php | 67 +++++++++++++++++++--------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/Repository/KeaHostRepository.php b/src/Repository/KeaHostRepository.php index 2260729..eab654a 100644 --- a/src/Repository/KeaHostRepository.php +++ b/src/Repository/KeaHostRepository.php @@ -18,17 +18,28 @@ final class KeaHostRepository */ public function findAll(int $limit = 50): array { - // 'dhcp_identifier' ist in KEA i.d.R. die MAC-Adresse (bei type=1) - $stmt = $this->pdo->prepare( - 'SELECT host_id, dhcp_identifier, ipv4_address, hostname, user_context - FROM hosts - ORDER BY host_id DESC - LIMIT :limit' - ); - $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); - $stmt->execute(); + try { + // 'dhcp_identifier' ist in KEA i.d.R. die MAC-Adresse (bei type=1) + $stmt = $this->pdo->prepare( + 'SELECT host_id, dhcp_identifier, ipv4_address, hostname, user_context + FROM hosts + ORDER BY host_id DESC + LIMIT :limit' + ); + $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); + $stmt->execute(); - return $stmt->fetchAll(PDO::FETCH_ASSOC); + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } catch (\PDOException $e) { + if ($this->isMissingTable($e)) { + throw new \RuntimeException( + 'KEA schema not initialized. Enable APP_DB_AUTO_INIT or run kea-admin db-init pgsql.', + 0, + $e + ); + } + throw $e; + } } /** @@ -39,16 +50,32 @@ final class KeaHostRepository // Hinweis: KEA speichert MACs in PostgreSQL oft als BYTEA. // Je nach Treiber-Konfiguration muss $mac hier ggf. als Hex-String (z.B. '\x...') // formatiert übergeben werden. - $stmt = $this->pdo->prepare( - 'SELECT host_id, dhcp_identifier, ipv4_address, hostname, user_context - FROM hosts - WHERE dhcp_identifier = :mac - LIMIT 1' - ); - $stmt->execute(['mac' => $mac]); + try { + $stmt = $this->pdo->prepare( + 'SELECT host_id, dhcp_identifier, ipv4_address, hostname, user_context + FROM hosts + WHERE dhcp_identifier = :mac + LIMIT 1' + ); + $stmt->execute(['mac' => $mac]); - $row = $stmt->fetch(PDO::FETCH_ASSOC); - return $row ?: null; + $row = $stmt->fetch(PDO::FETCH_ASSOC); + return $row ?: null; + } catch (\PDOException $e) { + if ($this->isMissingTable($e)) { + throw new \RuntimeException( + 'KEA schema not initialized. Enable APP_DB_AUTO_INIT or run kea-admin db-init pgsql.', + 0, + $e + ); + } + throw $e; + } + } + + private function isMissingTable(\PDOException $e): bool + { + return $e->getCode() === '42P01'; } /** @@ -101,4 +128,4 @@ final class KeaHostRepository return (int)$this->pdo->lastInsertId(); } -} \ No newline at end of file +}