From 020515c4af19eafe2d2ccd7ce8f1172eb45ae6c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 15 Jan 2020 00:01:20 +0100 Subject: [PATCH] database --- www/internals/database.php | 67 ++++++++++++++------------------------ www/internals/website.php | 26 ++++++++------- 2 files changed, 39 insertions(+), 54 deletions(-) diff --git a/www/internals/database.php b/www/internals/database.php index 159d05d..dc607c7 100644 --- a/www/internals/database.php +++ b/www/internals/database.php @@ -1,46 +1,35 @@ config['host'] . ";dbname=" . $site->config['database'] . ";charset=utf8"; + $opt = + [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; - self::$PDO = new PDO($dsn, $CONFIG['user'], $CONFIG['password'], $opt); + $this->pdo = new PDO($dsn, $site->config['user'], $site->config['password'], $opt); } - public static function tryconnect() + public function sql_query_num($query) { - try { - self::connect(); - return true; - } catch (exception $e) { - return false; - } - } - - public static function sql_query_num($query) - { - $r = self::$PDO->query($query)->fetch(PDO::FETCH_NUM)[0]; + $r = $this->pdo->query($query)->fetch(PDO::FETCH_NUM)[0]; return $r; } - public static function sql_query_num_prep($query, $params) + public function sql_query_num_prep($query, $params) { - $stmt = self::$PDO->prepare($query); + $stmt = $this->pdo->prepare($query); foreach ($params as $p) { @@ -53,16 +42,16 @@ class Database return $r; } - public static function sql_query_assoc($query) + public function sql_query_assoc($query) { - $r = self::$PDO->query($query)->fetchAll(PDO::FETCH_ASSOC); + $r = $this->pdo->query($query)->fetchAll(PDO::FETCH_ASSOC); return $r; } - public static function sql_query_assoc_prep($query, $params) + public function sql_query_assoc_prep($query, $params) { - $stmt = self::$PDO->prepare($query); + $stmt = $this->pdo->prepare($query); foreach ($params as $p) { @@ -70,21 +59,17 @@ class Database } $stmt->execute(); - $r = $stmt->fetchAll(PDO::FETCH_ASSOC); - - return $r; + return $stmt->fetchAll(PDO::FETCH_ASSOC); } - public static function sql_query_single($query) + public function sql_query_single($query) { - $r = self::$PDO->query($query)->fetch(PDO::FETCH_ASSOC); - - return $r; + return $this->pdo->query($query)->fetch(PDO::FETCH_ASSOC); } - public static function sql_query_single_prep($query, $params) + public function sql_query_single_prep($query, $params) { - $stmt = self::$PDO->prepare($query); + $stmt = $this->pdo->prepare($query); foreach ($params as $p) { @@ -92,14 +77,12 @@ class Database } $stmt->execute(); - $r = $stmt->fetch(PDO::FETCH_ASSOC); - - return $r; + return $stmt->fetch(PDO::FETCH_ASSOC); } - public static function sql_exec_prep($query, $params) + public function sql_exec_prep($query, $params) { - $stmt = self::$PDO->prepare($query); + $stmt = $this->pdo->prepare($query); foreach ($params as $p) { diff --git a/www/internals/website.php b/www/internals/website.php index 972bf29..125821b 100644 --- a/www/internals/website.php +++ b/www/internals/website.php @@ -6,17 +6,6 @@ require_once 'pageframeoptions.php'; require_once 'utils.php'; -require_once 'database.php'; -require_once 'adventofcode.php'; -require_once 'alephnoteStatistics.php'; -require_once 'blog.php'; -require_once 'books.php'; -require_once 'euler.php'; -require_once 'highscores.php'; -require_once 'programs.php'; -require_once 'updateslog.php'; -require_once 'webapp.php'; - require_once 'mikeschergitgraph.php'; require_once 'parsedowncustom.php'; @@ -26,7 +15,10 @@ class Website private static $instance; /** @var array */ - private $config; + public $config; + + /** @var Database|null */ + private $database = null; public function init() { @@ -168,6 +160,16 @@ class Website require __DIR__ . '/../pages/frame/' . $FRAME_OPTIONS->frame; } + public function database() + { + if ($this->database === null) + { + require_once 'database.php'; + $this->database = new Database($this); + } + return $this->database; + } + /** * @return bool */