1
0
This commit is contained in:
Mike Schwörer 2020-01-15 00:01:20 +01:00
parent ea1aae60c3
commit 020515c4af
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 39 additions and 54 deletions

View File

@ -1,46 +1,35 @@
<?php <?php
require_once 'website.php';
class Database class Database
{ {
/* @var PDO $PDO */ /* @var PDO $pdo */
public static $PDO = NULL; private $pdo = NULL;
public static function connect() public function __construct(Website $site)
{ {
global $CONFIG; $dsn = "mysql:host=" . $site->config['host'] . ";dbname=" . $site->config['database'] . ";charset=utf8";
$opt =
if (self::$PDO !== NULL) return; [
$dsn = "mysql:host=" . $CONFIG['host'] . ";dbname=" . $CONFIG['database'] . ";charset=utf8";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false, 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 { $r = $this->pdo->query($query)->fetch(PDO::FETCH_NUM)[0];
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];
return $r; 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) foreach ($params as $p)
{ {
@ -53,16 +42,16 @@ class Database
return $r; 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; 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) foreach ($params as $p)
{ {
@ -70,21 +59,17 @@ class Database
} }
$stmt->execute(); $stmt->execute();
$r = $stmt->fetchAll(PDO::FETCH_ASSOC); return $stmt->fetchAll(PDO::FETCH_ASSOC);
return $r;
} }
public static function sql_query_single($query) public function sql_query_single($query)
{ {
$r = self::$PDO->query($query)->fetch(PDO::FETCH_ASSOC); return $this->pdo->query($query)->fetch(PDO::FETCH_ASSOC);
return $r;
} }
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) foreach ($params as $p)
{ {
@ -92,14 +77,12 @@ class Database
} }
$stmt->execute(); $stmt->execute();
$r = $stmt->fetch(PDO::FETCH_ASSOC); return $stmt->fetch(PDO::FETCH_ASSOC);
return $r;
} }
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) foreach ($params as $p)
{ {

View File

@ -6,17 +6,6 @@ require_once 'pageframeoptions.php';
require_once 'utils.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 'mikeschergitgraph.php';
require_once 'parsedowncustom.php'; require_once 'parsedowncustom.php';
@ -26,7 +15,10 @@ class Website
private static $instance; private static $instance;
/** @var array */ /** @var array */
private $config; public $config;
/** @var Database|null */
private $database = null;
public function init() public function init()
{ {
@ -168,6 +160,16 @@ class Website
require __DIR__ . '/../pages/frame/' . $FRAME_OPTIONS->frame; 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 * @return bool
*/ */