This commit is contained in:
2026-03-02 22:58:41 +01:00
parent 610c153e25
commit ffb52c6789

View File

@@ -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();
}
}
}