Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mysqliconnection.php
1<?php
2
3namespace Bitrix\Main\DB;
4
6
13{
14 public function __construct(array $configuration)
15 {
16 parent::__construct($configuration);
17
18 $this->configureReportLevel();
19 }
20
21 /**********************************************************
22 * SqlHelper
23 **********************************************************/
24
25 protected function createSqlHelper()
26 {
27 return new MysqliSqlHelper($this);
28 }
29
30 /***********************************************************
31 * Connection and disconnection
32 ***********************************************************/
33
42 protected function connectInternal()
43 {
44 if ($this->isConnected)
45 {
46 return;
47 }
48
49 $host = $this->host;
50 $port = 0;
51 if (($pos = strpos($host, ":")) !== false)
52 {
53 $port = intval(substr($host, $pos + 1));
54 $host = substr($host, 0, $pos);
55 }
56 if (($this->options & self::PERSISTENT) != 0)
57 {
58 $host = "p:".$host;
59 }
60
61 $connection = \mysqli_init();
62 if (!$connection)
63 {
64 throw new ConnectionException('Mysql init failed');
65 }
66
67 if (!empty($this->initCommand))
68 {
69 if (!$connection->options(MYSQLI_INIT_COMMAND, $this->initCommand))
70 {
71 throw new ConnectionException('Setting mysql init command failed');
72 }
73 }
74
75 if ($port > 0)
76 {
77 $success = $connection->real_connect($host, $this->login, $this->password, $this->database, $port);
78 }
79 else
80 {
81 $success = $connection->real_connect($host, $this->login, $this->password, $this->database);
82 }
83
84 if (!$success)
85 {
86 throw new ConnectionException(
87 'Mysql connect error ['.$this->host.']',
88 sprintf('(%s) %s', $connection->connect_errno, $connection->connect_error)
89 );
90 }
91
92 $this->resource = $connection;
93 $this->isConnected = true;
94
95 if (isset($this->configuration['charset']))
96 {
97 $connection->set_charset($this->configuration['charset']);
98 }
99
100 // nosql memcached driver
101 if (isset($this->configuration['memcache']))
102 {
103 if (function_exists('mysqlnd_memcache_set'))
104 {
105 $memcached = \Bitrix\Main\Application::getInstance()->getConnectionPool()->getConnection($this->configuration['memcache']);
106 mysqlnd_memcache_set($this->resource, $memcached->getResource());
107 }
108 }
109
110 $this->afterConnected();
111 }
112
119 protected function disconnectInternal()
120 {
121 if ($this->isConnected)
122 {
123 $this->isConnected = false;
124 $this->resource->close();
125 }
126 }
127
128 /*********************************************************
129 * Query
130 *********************************************************/
131
135 protected function queryInternal($sql, array $binds = null, Diag\SqlTrackerQuery $trackerQuery = null)
136 {
137 $this->connectInternal();
138
139 $trackerQuery?->startQuery($sql, $binds);
140
141 $result = $this->resource->query($sql);
142
143 $trackerQuery?->finishQuery();
144
145 $this->lastQueryResult = $result;
146
147 if (!$result)
148 {
149 throw new SqlQueryException('Mysql query error', $this->getErrorMessage(), $sql);
150 }
151
152 return $result;
153 }
154
158 protected function createResult($result, Diag\SqlTrackerQuery $trackerQuery = null)
159 {
160 return new MysqliResult($result, $this, $trackerQuery);
161 }
162
166 public function getInsertedId()
167 {
168 return $this->getResource()->insert_id;
169 }
170
174 public function getAffectedRowsCount()
175 {
176 return $this->getResource()->affected_rows;
177 }
178
179 /*********************************************************
180 * Type, version, cache, etc.
181 *********************************************************/
182
186 public function getVersion()
187 {
188 if ($this->version == null)
189 {
190 $version = trim($this->getResource()->server_info);
191
192 preg_match("#[0-9]+\\.[0-9]+\\.[0-9]+#", $version, $ar);
193 $this->version = $ar[0];
194 }
195
196 return array($this->version, null);
197 }
198
202 public function getErrorMessage()
203 {
204 return sprintf("(%s) %s", $this->resource->errno, $this->resource->error);
205 }
206
213 public function selectDatabase($database)
214 {
215 return $this->resource->select_db($database);
216 }
217
218 protected function configureReportLevel(): void
219 {
220 // back to default before PHP 8.1
221 mysqli_report(MYSQLI_REPORT_OFF);
222 }
223}
createResult($result, Diag\SqlTrackerQuery $trackerQuery=null)
queryInternal($sql, array $binds=null, Diag\SqlTrackerQuery $trackerQuery=null)
__construct(array $configuration)