1 | <?php |
||
13 | class DB { |
||
14 | use |
||
15 | Singleton; |
||
16 | const CONNECTIONS_ALL = null; |
||
17 | const CONNECTIONS_FAILED = 0; |
||
18 | const CONNECTIONS_SUCCESSFUL = 1; |
||
19 | const CONNECTIONS_MIRRORS = 'mirror'; |
||
20 | const MASTER_MIRROR = -1; |
||
21 | const MIRROR_MODE_MASTER_MASTER = 0; |
||
22 | const MIRROR_MODE_MASTER_SLAVE = 1; |
||
23 | /** |
||
24 | * @var DB\_Abstract[] |
||
25 | */ |
||
26 | protected $connections = []; |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $successful_connections = []; |
||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $failed_connections = []; |
||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $mirrors = []; |
||
39 | /** |
||
40 | * Get list of connections of specified type |
||
41 | * |
||
42 | * @param bool|null|string $type One of constants `self::CONNECTIONS_*` |
||
43 | * |
||
44 | * @return array For `self::CONNECTIONS_ALL` array of successful connections with corresponding objects as values of array<br> |
||
45 | * Otherwise array where keys are database ids and values are strings with information about database |
||
46 | */ |
||
47 | function get_connections_list ($type = self::CONNECTIONS_ALL) { |
||
59 | /** |
||
60 | * Total number of executed queries |
||
61 | * |
||
62 | * @return int |
||
63 | */ |
||
64 | function queries () { |
||
71 | /** |
||
72 | * Total time spent on all queries and connections |
||
73 | * |
||
74 | * @return float |
||
75 | */ |
||
76 | function time () { |
||
83 | /** |
||
84 | * Get database instance for read queries |
||
85 | * |
||
86 | * @param int $database_id |
||
87 | * |
||
88 | * @return DB\_Abstract|False_class Returns instance of False_class on failure |
||
89 | * |
||
90 | * @throws ExitException |
||
91 | */ |
||
92 | 6 | function db ($database_id) { |
|
93 | 6 | return $this->generic_connecting($database_id, true); |
|
94 | } |
||
95 | /** |
||
96 | * Get database instance for read queries |
||
97 | * |
||
98 | * @param int $database_id |
||
99 | * |
||
100 | * @return DB\_Abstract|False_class Returns instance of False_class on failure |
||
101 | * |
||
102 | * @throws ExitException |
||
103 | */ |
||
104 | function __get ($database_id) { |
||
107 | /** |
||
108 | * Get database instance for write queries |
||
109 | * |
||
110 | * @param int $database_id |
||
111 | * |
||
112 | * @return DB\_Abstract|False_class Returns instance of False_class on failure |
||
113 | * |
||
114 | * @throws ExitException |
||
115 | */ |
||
116 | 26 | function db_prime ($database_id) { |
|
119 | /** |
||
120 | * Get database instance for read queries or process implicit calls to methods of main database instance |
||
121 | * |
||
122 | * @param int|string $method |
||
123 | * @param array $arguments |
||
124 | * |
||
125 | * @return DB\_Abstract|False_class Returns instance of False_class on failure |
||
126 | * |
||
127 | * @throws ExitException |
||
128 | */ |
||
129 | function __call ($method, $arguments) { |
||
138 | /** |
||
139 | * @param int $database_id |
||
140 | * @param bool $read_query |
||
141 | * |
||
142 | * @return DB\_Abstract|False_class |
||
143 | * |
||
144 | * @throws ExitException |
||
145 | */ |
||
146 | 30 | protected function generic_connecting ($database_id, $read_query) { |
|
147 | 30 | if (!is_int($database_id) && $database_id != '0') { |
|
148 | return False_class::instance(); |
||
149 | } |
||
150 | /** |
||
151 | * Establish wne connection to the database |
||
152 | */ |
||
153 | 30 | $connection = $this->connecting($database_id, $read_query); |
|
154 | /** |
||
155 | * If connection fails - try once more |
||
156 | */ |
||
157 | 30 | if (!$connection) { |
|
158 | $connection = $this->connecting($database_id, $read_query); |
||
159 | } |
||
160 | /** |
||
161 | * If failed twice - show error |
||
162 | */ |
||
163 | 30 | if (!$connection) { |
|
164 | throw new ExitException(500); |
||
165 | } |
||
166 | 30 | return $connection; |
|
167 | } |
||
168 | /** |
||
169 | * Processing of all DB request |
||
170 | * |
||
171 | * @param int $database_id |
||
172 | * @param bool $read_query |
||
173 | * |
||
174 | * @return DB\_Abstract|False_class |
||
175 | */ |
||
176 | 30 | protected function connecting ($database_id, $read_query = true) { |
|
177 | /** |
||
178 | * If connection found in list of failed connections - return instance of False_class |
||
179 | */ |
||
180 | 30 | if (isset($this->failed_connections[$database_id])) { |
|
181 | return False_class::instance(); |
||
182 | } |
||
183 | /** |
||
184 | * If connection to DB mirror already established |
||
185 | */ |
||
186 | if ( |
||
187 | 30 | $read_query && |
|
188 | 30 | isset($this->mirrors[$database_id]) |
|
189 | ) { |
||
190 | return $this->mirrors[$database_id]; |
||
191 | } |
||
192 | /** |
||
193 | * If connection already established |
||
194 | */ |
||
195 | 30 | if (isset($this->connections[$database_id])) { |
|
196 | 22 | return $this->connections[$database_id]; |
|
197 | } |
||
198 | 30 | $Core = Core::instance(); |
|
199 | 30 | list($database_settings, $is_mirror) = $this->get_db_connection_settings($database_id, $read_query); |
|
200 | /** |
||
201 | * Establish new connection |
||
202 | * |
||
203 | * @var DB\_Abstract $connection |
||
204 | */ |
||
205 | 30 | $engine_class = "cs\\DB\\$database_settings[type]"; |
|
206 | 30 | $connection = new $engine_class( |
|
207 | 30 | $database_settings['name'], |
|
208 | 30 | $database_settings['user'], |
|
209 | 30 | $database_settings['password'], |
|
210 | 30 | $database_settings['host'], |
|
211 | 30 | $database_settings['prefix'] |
|
212 | ); |
||
213 | 30 | $connection_name = ($database_id == 0 ? "Core DB ($Core->db_type)" : $database_id)."/$database_settings[host]/$database_settings[type]"; |
|
214 | 30 | unset($engine_class, $database_settings); |
|
215 | /** |
||
216 | * If successfully - add connection to the list of success connections and return instance of DB engine object |
||
217 | */ |
||
218 | 30 | if (is_object($connection) && $connection->connected()) { |
|
219 | 30 | $this->successful_connections[] = $connection_name; |
|
220 | 30 | $this->$database_id = $connection; |
|
221 | 30 | if ($is_mirror) { |
|
222 | 2 | $this->mirrors[$database_id] = $connection; |
|
223 | } else { |
||
224 | 28 | $this->connections[$database_id] = $connection; |
|
225 | } |
||
226 | 30 | return $connection; |
|
227 | } |
||
228 | /** |
||
229 | * If failed - add connection to the list of failed connections and log error |
||
230 | */ |
||
231 | $this->failed_connections[$database_id] = $connection_name; |
||
232 | trigger_error( |
||
233 | $database_id == 0 ? 'Error connecting to core DB of site' : "Error connecting to DB $connection_name", |
||
234 | E_USER_ERROR |
||
235 | ); |
||
236 | return False_class::instance(); |
||
237 | } |
||
238 | /** |
||
239 | * Get database connection settings, depending on query type and system configuration settings of main db or one of mirrors might be returned |
||
240 | * |
||
241 | * @param int $database_id |
||
242 | * @param bool $read_query |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | 30 | protected function get_db_connection_settings ($database_id, $read_query) { |
|
276 | /** |
||
277 | * Choose index of DB mirrors among available |
||
278 | * |
||
279 | * @param int $database_id |
||
280 | * @param bool $read_query |
||
281 | * |
||
282 | * @return int |
||
283 | */ |
||
284 | 30 | protected function choose_mirror ($database_id, $read_query = true) { |
|
309 | } |
||
310 |