echo $begrüßung;
das werde ich mal umzusetzen versuchen.
Anbei mein Vorschlag.
class DBException extends Exception {}
// Tut nichts besonderes, dient nur zur Unterscheidung von anderen Exceptions
class DB {
private $host;
private $database;
private $user;
private $password;
private $connection = null;
function __construct ($host, $database, $user, $password) {
$this->host = $host;
$this->database = $database;
$this->user = $user;
$this->password = $password;
}
// __destruct() weggelassen, wegen unwichtig
private function getConnect() {
if (!$this->connection)
$this->connection = pg_connect('etc.');
if (is_resource($connection))
return $this->connection;
else
$this->connection = null;
throw new DBException('Connection failed: ' . pg_last_error());
}
public function fetchAll($sql) {
$result = $this->query($sql);
// Fehler treten beim Abarbeiten der Query auf.
// Wenn dort eine Exception ausgelöst wird, bricht auch fetchAll ab
// und reicht die Exception an den Aufrufer durch.
$rows = array();
while ($row = pg_fetch_object($result))
$rows[] = $row;
pg_free_result($result);
return $rows;
}
protected function query($sql) {
$connection = $this->getConnection();
// Connection-Exceptions werden unbehandelt dem Aufrufer durchgereicht.
$result = pg_query($connection, $sql);
if (!$result)
throw new DBException('Query failed: ' . pg_last_error($connection));
return $result;
}
public function update($table, $data, $where = array()) {
$sql = 'UPDATE ...'; // Herstellen der Query für die Struktur unwichtig, deswegen gekürzt
$result = $this->query($sql);
// gleicher Kommentar wie bei fetchAll()
return pg_affected_rows($result);
}
}
class weblogDB extends DB {
public function __construct() {
parent::__construct('host', 'database', 'user', 'password');
}
public function analyzePingbacks() {
$errors = array();
$sql = 'SELECT "id", "value" FROM "block_attributes" WHERE "name" = \'pingback\'';
try {
$rows = $this->fetchAll($sql);
} catch (DBException $ex) {
// Hier können wir abbrechen, ohne DB-Ergebnis kann analyzePingback() nichts sinnvolles machen.
// Das Reinschachteln des nachfolgenden foreach in obigen try-Block tut auch nicht Not.
// Anders sähe es aus, wenn in beiden Fällen (Fetch-Fehler und erfolgreiches foreach) noch was Auszuführendes folgte.
$errors[] = array($ex, null);
return $errors;
}
foreach ($rows as $row)
try {
$this->fixPingback($row);
} catch (DBException $ex) {
$errors[] = array($ex, $row);
} catch (Exception $ex) {
$errors[] = array($ex, $row);
} // ggf. die einzelnen Exceptionsarten unterschiedlich behandeln
return $errors;
}
public function fixPingback($row) {
// Geschäftslogik weggekürzt
// Bei Bedarf können darin auch Exceptions geworfen werden.
try {
$this->update('block_attributes', $data, $where);
} catch (DBException $ex) {
// Logfile schreiben, ignorieren oder
// try-catch ganz weglassen, wenn der Aufrufer vom Fehler erfahren soll
}
}
}
$weblogDatabase = new weblogDB();
if ($errors = $weblogDatabase->analyzePingbacks()) {
echo "<pre>Fehler: \n";
foreach ($errors as $error) {
printf("Meldung: %s\nRow-ID: %s\n\n",
$error[0]->getMessage(),
is_null($error[1]) ? '-' : $error[1]->ID);
}
}
echo "$verabschiedung $name";