1 | <?php |
||
13 | class DB { |
||
14 | use |
||
15 | Singleton; |
||
16 | const CONNECTIONS_MASTER = 0; |
||
17 | const CONNECTIONS_MIRROR = 1; |
||
18 | const CONNECTIONS_SUCCESSFUL = 2; |
||
19 | const CONNECTIONS_FAILED = 3; |
||
20 | const MASTER_MIRROR = -1; |
||
21 | const MIRROR_MODE_MASTER_MASTER = 0; |
||
22 | const MIRROR_MODE_MASTER_SLAVE = 1; |
||
23 | /** |
||
24 | * @var array[] |
||
25 | */ |
||
26 | protected $connections = [ |
||
27 | self::CONNECTIONS_MASTER => [], |
||
28 | self::CONNECTIONS_MIRROR => [], |
||
29 | self::CONNECTIONS_SUCCESSFUL => [], |
||
30 | self::CONNECTIONS_FAILED => [], |
||
31 | ]; |
||
32 | /** |
||
33 | * Get list of connections of specified type |
||
34 | * |
||
35 | * @param int $type One of constants `self::CONNECTIONS_*` |
||
36 | * |
||
37 | * @return array For `self::CONNECTIONS_MASTER` and `self::CONNECTIONS_MIRRORS` array of successful connections with corresponding objects as values of |
||
38 | * array, otherwise array where keys are database ids and values are strings with information about database |
||
39 | */ |
||
40 | 2 | public function get_connections_list ($type) { |
|
43 | /** |
||
44 | * Total number of executed queries |
||
45 | * |
||
46 | * @return int |
||
47 | */ |
||
48 | 4 | public function queries () { |
|
49 | 4 | $queries = 0; |
|
50 | 4 | foreach ($this->connections[self::CONNECTIONS_MASTER] as $c) { |
|
51 | 4 | $queries += $c->queries()['num']; |
|
52 | } |
||
53 | 4 | foreach ($this->connections[self::CONNECTIONS_MIRROR] as $c) { |
|
54 | $queries += $c->queries()['num']; |
||
55 | } |
||
56 | 4 | return $queries; |
|
57 | } |
||
58 | /** |
||
59 | * Total time spent on all queries and connections |
||
60 | * |
||
61 | * @return float |
||
62 | */ |
||
63 | 4 | public function time () { |
|
64 | 4 | $time = 0; |
|
65 | /** |
||
66 | * @var DB\_Abstract $c |
||
67 | */ |
||
68 | 4 | foreach ($this->connections[self::CONNECTIONS_MASTER] as $c) { |
|
69 | 4 | $time += $c->connecting_time() + $c->time(); |
|
70 | } |
||
71 | 4 | foreach ($this->connections[self::CONNECTIONS_MIRROR] as $c) { |
|
72 | $time += $c->connecting_time() + $c->time(); |
||
73 | } |
||
74 | 4 | return $time; |
|
75 | } |
||
76 | /** |
||
77 | * Get database instance for read queries |
||
78 | * |
||
79 | * @param int $database_id |
||
80 | * |
||
81 | * @return DB\_Abstract|False_class Returns instance of False_class on failure |
||
82 | * |
||
83 | * @throws ExitException |
||
84 | */ |
||
85 | 38 | public function db ($database_id) { |
|
88 | /** |
||
89 | * Get database instance for write queries |
||
90 | * |
||
91 | * @param int $database_id |
||
92 | * |
||
93 | * @return DB\_Abstract|False_class Returns instance of False_class on failure |
||
94 | * |
||
95 | * @throws ExitException |
||
96 | */ |
||
97 | 44 | public function db_prime ($database_id) { |
|
100 | /** |
||
101 | * @param int $database_id |
||
102 | * @param bool $read_query |
||
103 | * |
||
104 | * @return DB\_Abstract|False_class |
||
105 | * |
||
106 | * @throws ExitException |
||
107 | */ |
||
108 | 48 | protected function connect ($database_id, $read_query) { |
|
179 | /** |
||
180 | * Get database connection settings, depending on query type and system configuration settings of main db or one of mirrors might be returned |
||
181 | * |
||
182 | * @param int $database_id |
||
183 | * @param bool $read_query |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | 48 | protected function get_db_connection_settings ($database_id, $read_query) { |
|
217 | /** |
||
218 | * Choose index of DB mirrors among available |
||
219 | * |
||
220 | * @param int $database_id |
||
221 | * @param bool $read_query |
||
222 | * |
||
223 | * @return int |
||
224 | */ |
||
225 | 48 | protected function choose_mirror ($database_id, $read_query = true) { |
|
250 | } |
||
251 |