Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
connectionpool.php
1<?php
9namespace Bitrix\Main\Data;
10
11use Bitrix\Main;
13
18{
22 protected $connections = [];
23
24 protected $connectionParameters = [];
25
26 protected $slavePossible = true;
27 protected $ignoreDml = 0;
28 protected $masterOnly = 0;
29 protected $slaveConnection = null;
30
31 const DEFAULT_CONNECTION_NAME = "default";
32
36 public function __construct()
37 {
38 }
39
46 protected function createConnection($name, $parameters)
47 {
48 $className = $parameters['className'];
49
50 if (!class_exists($className))
51 {
52 throw new Config\ConfigurationException(sprintf(
53 "Class '%s' for '%s' connection was not found", $className, $name
54 ));
55 }
56
57 $connection = new $className($parameters);
58
59 $this->connections[$name] = $connection;
60
61 return $connection;
62 }
63
70 public function getConnection($name = "")
71 {
72 if ($name === "")
73 {
75 }
76
77 if (!isset($this->connections[$name]))
78 {
79 $connParameters = $this->getConnectionParameters($name);
80 if (!empty($connParameters) && is_array($connParameters))
81 {
82 $this->createConnection($name, $connParameters);
83 }
84 }
85
86 if (isset($this->connections[$name]))
87 {
88 return $this->connections[$name];
89 }
90
91 return null;
92 }
93
102 protected function getConnectionParameters($name)
103 {
104 if (!is_string($name))
105 {
106 throw new Main\ArgumentTypeException("name", "string");
107 }
108
109 if ($name === "")
110 {
111 throw new Main\ArgumentNullException("name");
112 }
113
114 $params = null;
115 if (isset($this->connectionParameters[$name]) && !empty($this->connectionParameters[$name]))
116 {
117 $params = $this->connectionParameters[$name];
118 }
119 else
120 {
121 $configParams = Config\Configuration::getValue('connections');
122 if (isset($configParams[$name]) && !empty($configParams[$name]))
123 {
124 $params = $configParams[$name];
125 }
126 }
127
128 if ($params !== null && $name === static::DEFAULT_CONNECTION_NAME && !isset($params["include_after_connected"]))
129 {
130 $params["include_after_connected"] = \Bitrix\Main\Loader::getPersonal("php_interface/after_connect_d7.php");
131 }
132
133 return $params;
134 }
135
143 public function setConnectionParameters($name, $parameters)
144 {
145 $this->connectionParameters[$name] = $parameters;
146
147 if(isset($this->connections[$name]))
148 {
149 unset($this->connections[$name]);
150 }
151 }
152
159 public function getSlaveConnection($sql)
160 {
161 if($this->masterOnly > 0)
162 {
163 //We requested to process all queries
164 //by master connection
165 }
166 elseif($this->slavePossible)
167 {
168 $isSelect = preg_match('/^\s*(select|show)/i', $sql) && !preg_match('/get_lock/i', $sql);
169 if(!$isSelect && $this->ignoreDml <= 0)
170 {
171 $this->slavePossible = false;
172 }
173
174 if($isSelect)
175 {
176 if($this->slaveConnection === null)
177 {
178 $this->useMasterOnly(true);
179
180 $this->slaveConnection = $this->createSlaveConnection();
181
182 $this->useMasterOnly(false);
183 }
184
185 if(is_object($this->slaveConnection))
186 {
188 }
189 }
190 }
191 return null;
192 }
193
200 public function useMasterOnly($mode)
201 {
202 if($mode)
203 {
204 $this->masterOnly++;
205 }
206 else
207 {
208 $this->masterOnly--;
209 }
210 }
211
218 public function ignoreDml($mode)
219 {
220 if($mode)
221 {
222 $this->ignoreDml++;
223 }
224 else
225 {
226 $this->ignoreDml--;
227 }
228 }
229
235 protected function createSlaveConnection()
236 {
237 if(!Main\Loader::includeModule('cluster'))
238 {
239 return false;
240 }
241
242 $found = \CClusterSlave::GetRandomNode();
243
244 if($found !== false)
245 {
246 $node = \CClusterDBNode::GetByID($found["ID"]);
247
248 if(is_array($node) && $node["ACTIVE"] == "Y" && ($node["STATUS"] == "ONLINE" || $node["STATUS"] == "READY"))
249 {
250 $parameters = [
251 'host' => $node["DB_HOST"],
252 'database' => $node["DB_NAME"],
253 'login' => $node["DB_LOGIN"],
254 'password' => $node["DB_PASSWORD"],
255 ];
256
257 $connection = $this->cloneConnection(self::DEFAULT_CONNECTION_NAME, "node".$node["ID"], $parameters);
258
259 if($connection instanceof Main\DB\Connection)
260 {
261 $connection->setNodeId($node["ID"]);
262 }
263
264 return $connection;
265 }
266 }
267 return false;
268 }
269
279 public function cloneConnection($name, $newName, array $parameters=array())
280 {
281 $defParameters = $this->getConnectionParameters($name);
282 if (empty($defParameters) || !is_array($defParameters))
283 {
284 throw new Config\ConfigurationException(sprintf("Database connection '%s' is not found", $name));
285 }
286 $parameters = array_merge($defParameters, $parameters);
287
288 $connection = $this->createConnection($newName, $parameters);
289
290 return $connection;
291 }
292
298 public function isSlavePossible()
299 {
301 }
302
308 public function isMasterOnly()
309 {
310 return ($this->masterOnly > 0);
311 }
312
316 public function disconnect()
317 {
318 foreach ($this->connections as $connection)
319 {
320 if ($connection instanceof Main\DB\Connection)
321 {
322 $connection->disconnect();
323 }
324 }
325 }
326}
setConnectionParameters($name, $parameters)
cloneConnection($name, $newName, array $parameters=array())
createConnection($name, $parameters)
static includeModule($moduleName)
Definition loader.php:69