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 | 18 | 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 | 22 | protected function generic_connecting ($database_id, $read_query) { |
|
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 | 22 | protected function connecting ($database_id, $read_query = true) { |
|
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 | 22 | 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 | 22 | protected function choose_mirror ($database_id, $read_query = true) { |
|
309 | } |
||
310 |