/
pdolb.php
248 lines (228 loc) · 5.94 KB
/
pdolb.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
*
* A Load balance class for the PHP PDO extension, can be used in the master/slave mode of MySQL.
*
* Basically, the PDOLB can dispatch the query of changing data to the master(s), and dispatch the select query to the slave(s), master(s) or both, which depends on the PDOLB_CONFIG::$queryMode variable.
*
* @author xianhua.zhou@gmail.com
* @version 0.1 beta
*
*/
require realpath(__DIR__) . '/pdolb_config.php';
/**
*
* PDOLB class
*
* Examples:
*
* $pdo = PDOLB::getInstance();
* foreach ($pdo->query("SELECT * FROM users") as $row) {
* echo $row['name'];
* }
*
* $pdo = PDOLB::getInstance();
* $stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
* $stmt->execute(array(10));
* while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
* }
*
* $pdo = PDOLB::getInstance();
* $pdo->exec("DROP TABLE users");
*
* $pdo = PDOLB::getInstance();
* $stm = $pdo->prepare("UPDATE users SET name = ?");
* $stmt->execute(array('user name'));
*
*/
class PDOLB extends PDO {
/**
* PDOLB connections
*
* @var private $connections
*/
private static $connections = array();
/**
* databases list for PDO
*
* @var private $databases
*/
private static $databases = array();
/**
* for the PDOLB_CONFIG::$queryMode variable
*
* MASTER_SLAVE: query data from master(s) and slave(s)
* MASTER: query data from master(s) only
* SLAVE: query data from slave(s) only
*
*/
const MASTER_SLAVE = 0;
const MASTER = 1;
const SLAVE = 2;
/**
*
* get PDOLB instance from master(s)
*
* @param array $databases
*
* @access public
* @return PDOLB
*
*/
public static function getInstance($databases = array()) {
self::$databases = $databases ? $databases : PDOLB_CONFIG::$databases;
return self::getMaster();
}
/**
*
* shuffle databases for the method getRandomPDOLB
*
* @param array $databases
*
* @access private
* @return null
* @see PDOLB::getRandomPDOLB
*
*/
private static function shuffleDatabases(&$databases) {
$size = count($databases);
$database = null;
for($i = 0; $i < $size; $i++) {
$database = $databases[$i];
for ($weight = 1; $weight <= $database['weight']; $weight++) {
$databases[] = $database;
}
}
shuffle($databases);
}
/**
*
* get a random PDOLB instance
*
* @param int $type
*
* @access private
* @return PDOLB
*
*/
private static function getRandomPDOLB($type = self::MASTER_SLAVE) {
switch($type) {
case self::MASTER_SLAVE:
$databases = array_merge(self::$databases['master'], self::$databases['slave']);
break;
case self::MASTER:
$databases = self::$databases['master'];
break;
case self::SLAVE:
$databases = self::$databases['slave'];
break;
}
self::shuffleDatabases($databases);
$pdolb = null;
$exceptions = array();
foreach ($databases as $database) {
if (isset(self::$connections[$database['dsn']])) {
return self::$connections[$database['dsn']];
}
try {
$pdolb = new PDOLB(
$database['dsn'],
$database['username'],
$database['password'],
$database['driver_options']
);
} catch (Exception $e) {
$exceptions[$database['dsn']] = $e->getMessage();
continue;
}
if ($pdolb && '' == $pdolb->errorCode()) {
return self::$connections[$database['dsn']] = $pdolb;
} else {
$exceptions[$database['dsn']] = $pdolb->errorInfo();
}
}
throw new PDOLBException("No available database!\n" . print_r($exceptions, true));
}
/**
*
* get a PDOLB instance from master(s) and slave(s)
*
* @access private
* @return PDOLB
*
*/
private static function getMasterSlave() {
return self::getRandomPDOLB(self::MASTER_SLAVE);
}
/**
*
* get a PDOLB instance from master(s) only
*
* @access private
* @return PDOLB
*
*/
private static function getMaster() {
return self::getRandomPDOLB(self::MASTER);
}
/**
*
* get a PDOLB instance from slave(s) only
*
* @access private
* @return PDOLB
*
*/
private static function getSlave() {
return self::getRandomPDOLB(self::SLAVE);
}
/**
*
* get a PDOLB instance depends on the PDOLB_CONFIG::$queryMode variable
*
* @access private
* @return PDOLB
*
*/
private static function getPDOLB() {
switch (PDOLB_CONFIG::$queryMode) {
case self::MASTER_SLAVE:
return self::getMasterSlave();
case self::MASTER:
return self::getMaster();
case self::SLAVE:
return self::getSlave();
}
}
/**
* overwriten PDO::query
*/
public function query($statement) {
return self::getPDOLB()->_query($statement);
}
private function _query($statement) {
return parent::query($statement);
}
/**
* overwriten PDO::prepare
*/
public function prepare($statement, $driverOptions = array()) {
return self::getPDOLB()->_prepare($statement, $driverOptions);
}
private function _prepare($statement, $driverOptions = array()) {
return parent::prepare($statement, $driverOptions);
}
/**
* overwriten PDO::exec
*/
public function exec($statement) {
return self::getMaster()->_exec($statement);
}
private function _exec($statement) {
return parent::exec($statement);
}
}
/**
* PDOLBException
*/
class PDOLBException extends Exception {}