1: <?php
2:
3:
4: if (!class_exists('QMySqli5Database')) {
5: require(__QCUBED_CORE__ . '/database/QMySqli5Database.class.php');
6: }
7:
8: class QMySqli5ClusterDatabase extends QMySqli5Database {
9:
10: const Adapter = 'MySqli cluster database';
11:
12: 13: 14: 15: 16:
17: private function ConnectToRandomServer($servers) {
18: if (count($servers)) {
19: $randomkey = array_rand($servers);
20: $randomserver = $servers[$randomkey];
21: try {
22: mysqli_report(MYSQLI_REPORT_STRICT);
23: return new MySqli($randomserver, $this->Username, $this->Password, $this->Database, $this->Port);
24: } catch (Exception $ex) {
25:
26: unset($servers[$randomkey]);
27:
28: return $this->ConnectToRandomServer($servers);
29: }
30: } else {
31: return false;
32: }
33: }
34:
35: public function Connect() {
36:
37: $this->objMySqli = $this->ConnectToRandomServer($this->Server);
38:
39: if (!$this->objMySqli) {
40: throw new QMySqliDatabaseException("Unable to connect to Database", -1, null);
41: }
42:
43: if ($this->objMySqli->error) {
44: throw new QMySqliDatabaseException($this->objMySqli->error, $this->objMySqli->errno, null);
45: }
46:
47:
48: $this->blnConnectedFlag = true;
49:
50:
51: $this->NonQuery('SET AUTOCOMMIT=1;');
52:
53:
54: if (array_key_exists('encoding', $this->objConfigArray)) {
55: $this->NonQuery('SET NAMES ' . $this->objConfigArray['encoding'] . ';');
56: }
57: }
58:
59: }
60: