funktion zum erstellen der tabelle requests wieder eingebunden

This commit is contained in:
bernd 2021-03-03 17:26:38 +01:00
parent 3a77fc2d01
commit f1dafb3f4d

View file

@ -100,33 +100,64 @@ class Database {
public function __construct($pdo, $log) public function __construct($pdo, $log)
{ {
/**
* Übernimmt beim Erstellen der Klasse die Connection zur Datenbank
* und die Instanz des Loggers. Läßt kontrollieren ob die Tabelle zum
* Eintragen der Requests vorhanden ist und stößt die Erstellung der
* Tabelle an, wenn sie fehlt.
* TODO: Geht das eleganter? So wird das bei jeder Anfrage
* ausgeführt.
*/
$this->pdo = $pdo; $this->pdo = $pdo;
$this->log = $log; $this->log = $log;
$this->log->d("Databaseobject successfull created"); $this->log->d("Databaseobject successfull created");
// hier vielleicht TableCreate hin if ($this->TableExists() === false) {
$this->log->n("No table requests found. Try to create table");
$this->createTable();
}
} }
public function TableExists(): int private function TableExists(): bool
{ {
/**
* Kontrolliert, ob eine Tabelle zum Eintragen der Requests
* vorhanden ist.
* TODO: Die Query ist Postgres spezifisch. Treiber durchreichen und
* eine Weiche anlegen?
*/
$stmt = "SELECT * FROM information_schema.tables WHERE $stmt = "SELECT * FROM information_schema.tables WHERE
table_type = 'BASE TABLE' and table_type = 'BASE TABLE' and
table_name = 'requests'"; table_name = 'requests'";
$response = $this->pdo->query($stmt); $response = $this->pdo->query($stmt);
$count = $response->rowCount(); if ($response->rowCount() === 0) {
return $count; return false;
}
return true;
} }
public function createTable() public function createTable(): bool
{ {
/**
* Erstellt die Tabelle Requests.
*/
$stmt = "CREATE TABLE IF NOT EXISTS requests ( $stmt = "CREATE TABLE IF NOT EXISTS requests (
id serial PRIMARY KEY, id serial PRIMARY KEY,
nick varchar(80) NOT NULL UNIQUE, nick varchar(80) NOT NULL UNIQUE,
email varchar(80) NOT NULL, email varchar(80) NOT NULL,
token char(32) NOT NULL UNIQUE, token char(32) NOT NULL UNIQUE,
time integer NOT NULL);"; time integer NOT NULL);";
$this->pdo->exec($stmt); try {
return $this; $this->pdo->exec($stmt);
} catch (PDOException $e) {
$this->log-e("Failed to create table requests");
return false;
}
$this->log-n("Table requests successfull created");
return true;
} }