-
Notifications
You must be signed in to change notification settings - Fork 13
/
Database.php
executable file
·131 lines (112 loc) · 3.96 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace b8;
class Database extends \PDO
{
protected static $initialised = false;
protected static $servers = array('read' => array(), 'write' => array());
protected static $connections = array('read' => null, 'write' => null);
protected static $details = array();
/**
* @deprecated
*/
public static function setReadServers($read)
{
$config = Config::getInstance();
$settings = $config->get('b8.database', array());
$settings['servers']['read'] = $read;
$config->set('b8.database', $settings);
}
/**
* @deprecated
*/
public static function setWriteServers($write)
{
$config = Config::getInstance();
$settings = $config->get('b8.database', array());
$settings['servers']['write'] = $write;
$config->set('b8.database', $settings);
}
/**
* @deprecated
*/
public static function setDetails($database, $username, $password)
{
$config = Config::getInstance();
$settings = $config->get('b8.database', array());
$settings['name'] = $database;
$settings['username'] = $username;
$settings['password'] = $password;
$config->set('b8.database', $settings);
}
protected static function init()
{
$config = Config::getInstance();
$settings = $config->get('b8.database', array());
self::$servers['read'] = $settings['servers']['read'];
self::$servers['write'] = $settings['servers']['write'];
self::$details['db'] = $settings['name'];
self::$details['user'] = $settings['username'];
self::$details['pass'] = $settings['password'];
self::$initialised = true;
}
/**
* @param string $type
*
* @return \b8\Database
* @throws \Exception
*/
public static function getConnection($type = 'read')
{
if (!self::$initialised) {
self::init();
}
// Clear the connection and re-connect if we have a non-zero error code:
if (!is_null(self::$connections[$type]) && self::$connections[$type]->errorCode() !== '00000') {
self::$connections[$type] = null;
}
if (is_null(self::$connections[$type])) {
if (is_array(self::$servers[$type])) {
// Shuffle, so we pick a random server:
$servers = self::$servers[$type];
shuffle($servers);
} else {
// Only one server was specified
$servers = array(self::$servers[$type]);
}
$connection = null;
// Loop until we get a working connection:
while (count($servers)) {
// Pull the next server:
$server = array_shift($servers);
// Try to connect:
try {
$connection = new self('mysql:host=' . $server . ';dbname=' . self::$details['db'],
self::$details['user'],
self::$details['pass'],
array(
\PDO::ATTR_PERSISTENT => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_TIMEOUT => 2,
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
));
} 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;
}
return self::$connections[$type];
}
public function getDetails()
{
return self::$details;
}
}