diff --git a/php/model/DatabaseArticleManager.php b/php/model/DatabaseArticleManager.php index 7745919..3d282a4 100644 --- a/php/model/DatabaseArticleManager.php +++ b/php/model/DatabaseArticleManager.php @@ -25,23 +25,26 @@ class DatabaseArticleManager implements ArticleManagerDAO { // Tabelle für Beiträge $db->exec(" - CREATE TABLE articles ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - title TEXT, - content TEXT, - author TEXT, - category TEXT, - tags TEXT, - created TIMESTAMP DEFAULT CURRENT_TIMESTAMP - );"); + 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 likes ( + 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 (article_id) REFERENCES articles(id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE );"); unset($db); } catch (PDOException $e) { @@ -52,17 +55,22 @@ class DatabaseArticleManager implements ArticleManagerDAO { /** * Baut die Verbindung zur Datenbank auf. - * @throws InternalServerErrorException + * + * @return PDO Datenbankverbindung + * @throws RuntimeException */ private function getConnection() { try { - $user = 'root'; - $pw = null; $dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db'; - return new PDO($dsn, $user, $pw); + + $db = new PDO($dsn, null, null); + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $db->exec('PRAGMA foreign_keys = ON;'); + + return $db; } catch (PDOException $e) { - throw new InternalServerErrorException($e->getMessage()); + throw new RuntimeException("internal_error"); } } diff --git a/php/model/DatabaseCommentManager.php b/php/model/DatabaseCommentManager.php index 8228975..1ad537a 100644 --- a/php/model/DatabaseCommentManager.php +++ b/php/model/DatabaseCommentManager.php @@ -23,13 +23,16 @@ class DatabaseCommentManager implements CommentManagerDAO $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 - ); + 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); @@ -63,6 +66,7 @@ class DatabaseCommentManager implements CommentManagerDAO $db = new PDO($dsn, null, null); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $db->exec('PRAGMA foreign_keys = ON;'); return $db; diff --git a/php/model/DatabaseUserManager.php b/php/model/DatabaseUserManager.php index 7ecf119..311366d 100644 --- a/php/model/DatabaseUserManager.php +++ b/php/model/DatabaseUserManager.php @@ -55,6 +55,7 @@ class DatabaseUserManager implements UserManagerDAO { $db = new PDO($dsn, null, null); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $db->exec('PRAGMA foreign_keys = ON;'); return $db;