1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This class will run queries on master/slave servers depending on the query itself. |
4
|
|
|
*/ |
5
|
|
|
class mysqlims extends mysqli |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
private $mysqliW; |
8
|
|
|
private $mysqliR = null; |
9
|
|
|
private $slave = false; |
10
|
|
|
public $lastused = null; |
11
|
|
|
|
12
|
|
|
/* |
13
|
|
|
* Pass main and slave connection arrays to the constructor, and strict as true/false |
14
|
|
|
* |
15
|
|
|
* @param array $main |
16
|
|
|
* @param array $slave |
17
|
|
|
* @param boolean $strict |
18
|
|
|
* |
19
|
|
|
* @return void |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function __construct($main, $slave = false, $strict = false) |
22
|
|
|
{ |
23
|
|
|
if ($strict) { |
24
|
|
|
$this->mysqliW = new mysqli_strict($main['host'], |
|
|
|
|
25
|
|
|
$main['user'], $main['pass'], |
26
|
|
|
$main['name'], $main['port']); |
27
|
|
View Code Duplication |
if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled'] |
|
|
|
|
28
|
|
|
=== true) { |
29
|
|
|
$this->mysqliR = new mysqli_strict($slave['host'], |
|
|
|
|
30
|
|
|
$slave['user'], $slave['pass'], |
31
|
|
|
$slave['name'], $slave['port']); |
32
|
|
|
$this->slave = true; |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
$this->mysqliW = new mysqli($main['host'], |
36
|
|
|
$main['user'], $main['pass'], |
37
|
|
|
$main['name'], $main['port']); |
38
|
|
View Code Duplication |
if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled'] |
|
|
|
|
39
|
|
|
=== true) { |
40
|
|
|
$this->mysqliR = new mysqli($slave['host'], |
41
|
|
|
$slave['user'], $slave['pass'], |
42
|
|
|
$slave['name'], $slave['port']); |
43
|
|
|
$this->slave = true; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($this->mysqliW->connect_errno) { |
48
|
|
|
throw new Exception("Failed to connect to MySQL: (".$this->mysqliW->connect_errno.") ".$this->mysqliW->connect_error); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->slave === true && $this->mysqliR->connect_errno) { |
52
|
|
|
throw new Exception("Failed to connect to MySQL: (".$this->mysqliR->connect_errno.") ".$this->mysqliR->connect_error); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* Override standard mysqli_prepare to select master/slave server |
58
|
|
|
* @param $string query |
59
|
|
|
* |
60
|
|
|
* @return mysqli_stmt |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function prepare($query) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && stripos($query, "INSERT") === false && $this->slave !== false) { |
65
|
|
|
$this->lastused = $this->mysqliR; |
66
|
|
|
return $this->mysqliR->prepare($query); |
67
|
|
|
} else { |
68
|
|
|
$this->lastused = $this->mysqliW; |
69
|
|
|
return $this->mysqliW->prepare($query); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
* Override standard mysqli_query to select master/slave server |
75
|
|
|
* @param string $query |
76
|
|
|
* @param int $resultmode |
77
|
|
|
* |
78
|
|
|
* @return boolean |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function query($query, $resultmode = MYSQLI_STORE_RESULT) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && stripos($query, "INSERT") === false && $this->slave !== false) {/* Use readonly server */ |
84
|
|
|
$this->lastused = $this->mysqliR; |
85
|
|
|
return $this->mysqliR->query($query, $resultmode); |
86
|
|
|
} else { |
87
|
|
|
$this->lastused = $this->mysqliW; |
88
|
|
|
return $this->mysqliW->query($query, $resultmode); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.