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) { |
||
48 | if ($type == self::CONNECTIONS_FAILED) { |
||
49 | return $this->failed_connections; |
||
50 | } |
||
51 | if ($type == self::CONNECTIONS_SUCCESSFUL) { |
||
52 | return $this->successful_connections; |
||
53 | } |
||
54 | if ($type == self::CONNECTIONS_MIRRORS) { |
||
55 | return $this->mirrors; |
||
56 | } |
||
57 | return $this->connections; |
||
58 | } |
||
59 | /** |
||
60 | * Total number of executed queries |
||
61 | * |
||
62 | * @return int |
||
63 | */ |
||
64 | 2 | function queries () { |
|
65 | 2 | $queries = 0; |
|
66 | 2 | foreach ($this->connections as $c) { |
|
67 | 2 | $queries += $c->queries()['num']; |
|
68 | } |
||
69 | 2 | return $queries; |
|
70 | } |
||
71 | /** |
||
72 | * Total time spent on all queries and connections |
||
73 | * |
||
74 | * @return float |
||
75 | */ |
||
76 | 2 | function time () { |
|
77 | 2 | $time = 0; |
|
78 | 2 | foreach ($this->connections as $c) { |
|
79 | 2 | $time += $c->connecting_time() + $c->time(); |
|
80 | } |
||
81 | 2 | return $time; |
|
82 | } |
||
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 | 38 | function db ($database_id) { |
|
95 | /** |
||
96 | * Get database instance for write 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 | 40 | function db_prime ($database_id) { |
|
107 | /** |
||
108 | * @param int $database_id |
||
109 | * @param bool $read_query |
||
110 | * |
||
111 | * @return DB\_Abstract|False_class |
||
112 | * |
||
113 | * @throws ExitException |
||
114 | */ |
||
115 | 48 | protected function generic_connecting ($database_id, $read_query) { |
|
137 | /** |
||
138 | * Processing of all DB request |
||
139 | * |
||
140 | * @param int $database_id |
||
141 | * @param bool $read_query |
||
142 | * |
||
143 | * @return DB\_Abstract|False_class |
||
144 | */ |
||
145 | 48 | protected function connecting ($database_id, $read_query = true) { |
|
207 | /** |
||
208 | * Get database connection settings, depending on query type and system configuration settings of main db or one of mirrors might be returned |
||
209 | * |
||
210 | * @param int $database_id |
||
211 | * @param bool $read_query |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | 48 | protected function get_db_connection_settings ($database_id, $read_query) { |
|
245 | /** |
||
246 | * Choose index of DB mirrors among available |
||
247 | * |
||
248 | * @param int $database_id |
||
249 | * @param bool $read_query |
||
250 | * |
||
251 | * @return int |
||
252 | */ |
||
253 | 48 | protected function choose_mirror ($database_id, $read_query = true) { |
|
278 | } |
||
279 |