2016-04-12 19:31:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace b8;
|
|
|
|
|
|
|
|
class Database extends \PDO
|
|
|
|
{
|
2016-04-20 12:30:26 +02:00
|
|
|
protected static $initialised = false;
|
|
|
|
protected static $servers = ['read' => [], 'write' => []];
|
|
|
|
protected static $connections = ['read' => null, 'write' => null];
|
|
|
|
protected static $details = [];
|
|
|
|
protected static $lastUsed = ['read' => null, 'write' => null];
|
|
|
|
|
|
|
|
protected static function init()
|
|
|
|
{
|
2016-08-11 16:32:42 +02:00
|
|
|
$config = Config::getInstance();
|
2016-04-20 12:30:26 +02:00
|
|
|
$settings = $config->get('b8.database', []);
|
2016-08-11 16:32:42 +02:00
|
|
|
|
|
|
|
self::$servers['read'] = $settings['servers']['read'];
|
2016-04-20 12:30:26 +02:00
|
|
|
self::$servers['write'] = $settings['servers']['write'];
|
2017-01-29 03:49:43 +01:00
|
|
|
self::$details['type'] = $settings['type'];
|
2016-08-11 16:32:42 +02:00
|
|
|
self::$details['db'] = $settings['name'];
|
|
|
|
self::$details['user'] = $settings['username'];
|
|
|
|
self::$details['pass'] = $settings['password'];
|
2017-01-29 03:49:43 +01:00
|
|
|
|
2016-04-20 12:30:26 +02:00
|
|
|
self::$initialised = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return \b8\Database
|
2016-08-11 16:32:42 +02:00
|
|
|
*
|
2016-04-20 12:30:26 +02:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function getConnection($type = 'read')
|
|
|
|
{
|
|
|
|
if (!self::$initialised) {
|
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the connection hasn't been used for 5 minutes, force a reconnection:
|
|
|
|
if (!is_null(self::$lastUsed[$type]) && (time() - self::$lastUsed[$type]) > 300) {
|
|
|
|
self::$connections[$type] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null(self::$connections[$type])) {
|
2017-01-29 03:49:43 +01:00
|
|
|
// Shuffle, so we pick a random server:
|
|
|
|
$servers = self::$servers[$type];
|
|
|
|
shuffle($servers);
|
2016-04-20 12:30:26 +02:00
|
|
|
|
|
|
|
$connection = null;
|
|
|
|
|
|
|
|
// Loop until we get a working connection:
|
|
|
|
while (count($servers)) {
|
|
|
|
// Pull the next server:
|
|
|
|
$server = array_shift($servers);
|
|
|
|
|
2017-01-29 03:49:43 +01:00
|
|
|
$dns = self::$details['type'] . ':host=' . $server['host'];
|
|
|
|
if (isset($server['port'])) {
|
2017-02-04 11:34:19 +01:00
|
|
|
$dns .= ';port=' . (integer)$server['port'];
|
2016-04-20 12:30:26 +02:00
|
|
|
}
|
2017-01-29 03:49:43 +01:00
|
|
|
$dns .= ';dbname=' . self::$details['db'];
|
2016-04-20 12:30:26 +02:00
|
|
|
|
|
|
|
// Try to connect:
|
|
|
|
try {
|
2017-01-29 03:49:43 +01:00
|
|
|
$connection = new self(
|
|
|
|
$dns,
|
2016-04-20 12:30:26 +02:00
|
|
|
self::$details['user'],
|
|
|
|
self::$details['pass'],
|
|
|
|
[
|
|
|
|
\PDO::ATTR_PERSISTENT => false,
|
|
|
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
|
|
|
\PDO::ATTR_TIMEOUT => 2,
|
|
|
|
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
|
2017-01-29 03:49:43 +01:00
|
|
|
]
|
|
|
|
);
|
2016-04-20 12:30:26 +02:00
|
|
|
} catch (\PDOException $ex) {
|
|
|
|
$connection = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opened a connection? Break the loop:
|
|
|
|
if ($connection) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No connection? Oh dear.
|
|
|
|
if (!$connection && $type == 'read') {
|
|
|
|
throw new \Exception('Could not connect to any ' . $type . ' servers.');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$connections[$type] = $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$lastUsed[$type] = time();
|
|
|
|
return self::$connections[$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDetails()
|
|
|
|
{
|
|
|
|
return self::$details;
|
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
|
|
|
|
public static function reset()
|
|
|
|
{
|
2016-04-20 12:30:26 +02:00
|
|
|
self::$connections = ['read' => null, 'write' => null];
|
|
|
|
self::$lastUsed = ['read' => null, 'write' => null];
|
2016-04-12 19:31:39 +02:00
|
|
|
self::$initialised = false;
|
|
|
|
}
|
2017-01-29 03:49:43 +01:00
|
|
|
|
|
|
|
public function prepareCommon($statement, array $driver_options = [])
|
|
|
|
{
|
|
|
|
$quote = '';
|
|
|
|
if ('mysql' === self::$details['type']) {
|
|
|
|
$quote = '`';
|
|
|
|
} elseif ('pgsql' === self::$details['type']) {
|
|
|
|
$quote = '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$statement = preg_replace('/{{(.*?)}}/', ($quote . '\1' . $quote), $statement);
|
|
|
|
|
|
|
|
return parent::prepare($statement, $driver_options);
|
|
|
|
}
|
2016-04-12 19:31:39 +02:00
|
|
|
}
|