DatabaseInitializer
This commit is contained in:
@@ -9,48 +9,18 @@ require_once 'Article.php';
|
|||||||
class DatabaseArticleManager implements ArticleManagerDAO {
|
class DatabaseArticleManager implements ArticleManagerDAO {
|
||||||
|
|
||||||
private static $instance = null;
|
private static $instance = null;
|
||||||
|
private $dbPath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Konstruktor
|
* Konstruktor
|
||||||
*
|
*
|
||||||
* Erstellt die Datenbankverbindung und die Tabelle articles.
|
* Erstellt die Datenbankverbindung und die Tabelle articles.
|
||||||
* @throws InternalServerErrorException
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
if (!file_exists(__DIR__ . '/../../db/eduforgeDB.db')) {
|
$this->dbPath = __DIR__ . '/../../db/eduforgeDB.db';
|
||||||
try {
|
DatabaseInitializer::initialize($this->dbPath);
|
||||||
|
|
||||||
$db = $this->getConnection();
|
|
||||||
|
|
||||||
// Tabelle für Beiträge
|
|
||||||
$db->exec("
|
|
||||||
CREATE TABLE IF NOT EXISTS articles (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
title TEXT,
|
|
||||||
content TEXT,
|
|
||||||
author TEXT,
|
|
||||||
category TEXT,
|
|
||||||
tags TEXT,
|
|
||||||
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE
|
|
||||||
);");
|
|
||||||
|
|
||||||
|
|
||||||
// Tabelle für Likes
|
|
||||||
$db->exec("
|
|
||||||
CREATE TABLE IF NOT EXISTS likes (
|
|
||||||
article_id INTEGER,
|
|
||||||
user_id TEXT,
|
|
||||||
PRIMARY KEY (article_id, user_id),
|
|
||||||
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
|
|
||||||
FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE
|
|
||||||
);");
|
|
||||||
unset($db);
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
throw new InternalServerErrorException($e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -62,12 +32,9 @@ class DatabaseArticleManager implements ArticleManagerDAO {
|
|||||||
private function getConnection()
|
private function getConnection()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db';
|
$db = new PDO('sqlite:' . $this->dbPath, null, null);
|
||||||
|
|
||||||
$db = new PDO($dsn, null, null);
|
|
||||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
$db->exec('PRAGMA foreign_keys = ON;');
|
$db->exec('PRAGMA foreign_keys = ON;');
|
||||||
|
|
||||||
return $db;
|
return $db;
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
throw new RuntimeException("internal_error");
|
throw new RuntimeException("internal_error");
|
||||||
|
|||||||
@@ -12,46 +12,15 @@ require_once "Comment.php";
|
|||||||
class DatabaseCommentManager implements CommentManagerDAO
|
class DatabaseCommentManager implements CommentManagerDAO
|
||||||
{
|
{
|
||||||
private static $instance = null;
|
private static $instance = null;
|
||||||
|
private $dbPath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erstellt die Kommentartabelle, falls diese noch nicht existiert.
|
* Erstellt die Kommentartabelle, falls diese noch nicht existiert.
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
try {
|
$this->dbPath = __DIR__ . '/../../db/eduforgeDB.db';
|
||||||
$db = $this->getConnection();
|
DatabaseInitializer::initialize($this->dbPath);
|
||||||
|
|
||||||
$db->exec("
|
|
||||||
CREATE TABLE IF NOT EXISTS comments (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
article_id INTEGER NOT NULL,
|
|
||||||
parent_comment_id INTEGER NULL,
|
|
||||||
author TEXT NOT NULL,
|
|
||||||
content TEXT NOT NULL,
|
|
||||||
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
|
|
||||||
FOREIGN KEY (author) REFERENCES users(email) ON DELETE CASCADE,
|
|
||||||
FOREIGN KEY (parent_comment_id) REFERENCES comments(id) ON DELETE CASCADE
|
|
||||||
);
|
|
||||||
");
|
|
||||||
|
|
||||||
$columns = $db->query("PRAGMA table_info(comments);")->fetchAll(PDO::FETCH_ASSOC);
|
|
||||||
$hasParentColumn = false;
|
|
||||||
|
|
||||||
foreach ($columns as $column) {
|
|
||||||
if ($column["name"] === "parent_comment_id") {
|
|
||||||
$hasParentColumn = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$hasParentColumn) {
|
|
||||||
$db->exec("ALTER TABLE comments ADD COLUMN parent_comment_id INTEGER NULL;");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
throw new RuntimeException("internal_error");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -62,14 +31,10 @@ class DatabaseCommentManager implements CommentManagerDAO
|
|||||||
private function getConnection()
|
private function getConnection()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db';
|
$db = new PDO('sqlite:' . $this->dbPath, null, null);
|
||||||
|
|
||||||
$db = new PDO($dsn, null, null);
|
|
||||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
$db->exec('PRAGMA foreign_keys = ON;');
|
$db->exec('PRAGMA foreign_keys = ON;');
|
||||||
|
|
||||||
return $db;
|
return $db;
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
throw new RuntimeException("internal_error");
|
throw new RuntimeException("internal_error");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Diese Klasse legt einmalig beim allerersten Start in der exakt korrekten Reihenfolge die Tabellen an
|
||||||
|
*
|
||||||
|
* @author Niklas Ortmann
|
||||||
|
*/
|
||||||
|
class DatabaseInitializer {
|
||||||
|
public static function initialize($dbPath) {
|
||||||
|
if (!file_exists($dbPath)) {
|
||||||
|
try {
|
||||||
|
$db = new PDO('sqlite:' . $dbPath);
|
||||||
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
$db->exec('PRAGMA foreign_keys = ON;');
|
||||||
|
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
email TEXT PRIMARY KEY,
|
||||||
|
vorname TEXT NOT NULL,
|
||||||
|
nachname TEXT NOT NULL,
|
||||||
|
password TEXT NOT NULL
|
||||||
|
);
|
||||||
|
");
|
||||||
|
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE IF NOT EXISTS articles (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT,
|
||||||
|
content TEXT,
|
||||||
|
author TEXT,
|
||||||
|
category TEXT,
|
||||||
|
tags TEXT,
|
||||||
|
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE
|
||||||
|
);
|
||||||
|
");
|
||||||
|
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE IF NOT EXISTS likes (
|
||||||
|
article_id INTEGER,
|
||||||
|
user_id TEXT,
|
||||||
|
PRIMARY KEY (article_id, user_id),
|
||||||
|
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
");
|
||||||
|
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE IF NOT EXISTS comments (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
article_id INTEGER NOT NULL,
|
||||||
|
parent_comment_id INTEGER NULL,
|
||||||
|
author TEXT NOT NULL,
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (author) REFERENCES users(email) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (parent_comment_id) REFERENCES comments(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
");
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
throw new RuntimeException("internal-error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ require_once "UserManagerDAO.php";
|
|||||||
class DatabaseUserManager implements UserManagerDAO {
|
class DatabaseUserManager implements UserManagerDAO {
|
||||||
|
|
||||||
private static $instance = null;
|
private static $instance = null;
|
||||||
|
private $dbPath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Konstruktor.
|
* Konstruktor.
|
||||||
@@ -22,23 +23,8 @@ class DatabaseUserManager implements UserManagerDAO {
|
|||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
try {
|
$this->dbPath = __DIR__ . '/../../db/eduforgeDB.db';
|
||||||
$db = $this->getConnection();
|
DatabaseInitializer::initialize($this->dbPath);
|
||||||
|
|
||||||
$db->exec("
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
email TEXT PRIMARY KEY,
|
|
||||||
vorname TEXT NOT NULL,
|
|
||||||
nachname TEXT NOT NULL,
|
|
||||||
password TEXT NOT NULL
|
|
||||||
);
|
|
||||||
");
|
|
||||||
|
|
||||||
unset($db);
|
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
throw new RuntimeException("Benutzerdatenbank konnte nicht erstellt werden.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,16 +37,12 @@ class DatabaseUserManager implements UserManagerDAO {
|
|||||||
private function getConnection()
|
private function getConnection()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db';
|
$db = new PDO('sqlite:' . $this->dbPath, null, null);
|
||||||
|
|
||||||
$db = new PDO($dsn, null, null);
|
|
||||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
$db->exec('PRAGMA foreign_keys = ON;');
|
$db->exec('PRAGMA foreign_keys = ON;');
|
||||||
|
|
||||||
return $db;
|
return $db;
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
throw new RuntimeException("Verbindung zur Benutzerdatenbank fehlgeschlagen.");
|
throw new RuntimeException("internal_error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user