funktion zum erstellen der tabelle requests wieder eingebunden
This commit is contained in:
parent
3a77fc2d01
commit
f1dafb3f4d
43
lib/db.php
43
lib/db.php
|
@ -100,33 +100,64 @@ class Database {
|
|||
|
||||
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->log = $log;
|
||||
$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
|
||||
table_type = 'BASE TABLE' and
|
||||
table_name = 'requests'";
|
||||
$response = $this->pdo->query($stmt);
|
||||
$count = $response->rowCount();
|
||||
return $count;
|
||||
if ($response->rowCount() === 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createTable()
|
||||
public function createTable(): bool
|
||||
{
|
||||
/**
|
||||
* Erstellt die Tabelle Requests.
|
||||
*/
|
||||
|
||||
$stmt = "CREATE TABLE IF NOT EXISTS requests (
|
||||
id serial PRIMARY KEY,
|
||||
nick varchar(80) NOT NULL UNIQUE,
|
||||
email varchar(80) NOT NULL,
|
||||
token char(32) NOT NULL UNIQUE,
|
||||
time integer NOT NULL);";
|
||||
try {
|
||||
$this->pdo->exec($stmt);
|
||||
return $this;
|
||||
} catch (PDOException $e) {
|
||||
$this->log-e("Failed to create table requests");
|
||||
return false;
|
||||
}
|
||||
$this->log-n("Table requests successfull created");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue