2014-08-30 12:07:34 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Config;
|
2014-08-30 12:07:34 +02:00
|
|
|
|
2014-09-14 18:41:14 +02:00
|
|
|
class DatabaseConnection
|
2014-08-30 12:07:34 +02:00
|
|
|
{
|
2014-09-14 16:16:15 +02:00
|
|
|
private $pdo;
|
|
|
|
private $config;
|
2014-08-30 12:07:34 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function __construct(Config $config)
|
2014-08-30 12:07:34 +02:00
|
|
|
{
|
2014-09-14 16:16:15 +02:00
|
|
|
$this->config = $config;
|
2014-08-30 12:07:34 +02:00
|
|
|
}
|
|
|
|
|
2014-09-14 16:16:15 +02:00
|
|
|
public function getPDO()
|
2014-08-30 12:07:34 +02:00
|
|
|
{
|
2014-09-14 16:16:15 +02:00
|
|
|
if (!$this->pdo)
|
|
|
|
{
|
|
|
|
$this->createPDO();
|
|
|
|
}
|
|
|
|
return $this->pdo;
|
2014-08-30 12:07:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-03 20:17:00 +02:00
|
|
|
public function getDriver()
|
|
|
|
{
|
|
|
|
return $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
|
|
|
}
|
|
|
|
|
2014-09-14 16:16:15 +02:00
|
|
|
public function close()
|
2014-08-30 12:07:34 +02:00
|
|
|
{
|
2014-09-14 16:16:15 +02:00
|
|
|
$this->pdo = null;
|
2014-08-30 12:07:34 +02:00
|
|
|
}
|
|
|
|
|
2014-09-14 16:16:15 +02:00
|
|
|
private function createPDO()
|
2014-08-30 12:07:34 +02:00
|
|
|
{
|
2014-09-14 16:16:15 +02:00
|
|
|
$cwd = getcwd();
|
|
|
|
if ($this->config->getDataDirectory())
|
|
|
|
chdir($this->config->getDataDirectory());
|
2014-10-04 13:28:04 +02:00
|
|
|
$this->pdo = new PDOEx($this->config->database->dsn, $this->config->database->user,
|
2014-09-28 16:26:44 +02:00
|
|
|
$this->config->database->password);
|
2014-09-14 16:16:15 +02:00
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
|
|
chdir($cwd);
|
2014-08-30 12:07:34 +02:00
|
|
|
}
|
|
|
|
}
|