1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\Mysqli; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\Connection; |
8
|
|
|
use Doctrine\DBAL\Driver\PingableConnection; |
9
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
10
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
11
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
12
|
|
|
use mysqli; |
13
|
|
|
use const MYSQLI_INIT_COMMAND; |
14
|
|
|
use const MYSQLI_OPT_CONNECT_TIMEOUT; |
15
|
|
|
use const MYSQLI_OPT_LOCAL_INFILE; |
16
|
|
|
use const MYSQLI_READ_DEFAULT_FILE; |
17
|
|
|
use const MYSQLI_READ_DEFAULT_GROUP; |
18
|
|
|
use const MYSQLI_SERVER_PUBLIC_KEY; |
19
|
|
|
use function defined; |
20
|
|
|
use function floor; |
21
|
|
|
use function in_array; |
22
|
|
|
use function ini_get; |
23
|
|
|
use function mysqli_init; |
24
|
|
|
use function mysqli_options; |
25
|
|
|
use function restore_error_handler; |
26
|
|
|
use function set_error_handler; |
27
|
|
|
use function sprintf; |
28
|
|
|
use function stripos; |
29
|
|
|
|
30
|
|
|
class MysqliConnection implements Connection, PingableConnection, ServerInfoAwareConnection |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Name of the option to set connection flags |
34
|
|
|
*/ |
35
|
|
|
public const OPTION_FLAGS = 'flags'; |
36
|
|
|
|
37
|
|
|
/** @var mysqli */ |
38
|
|
|
private $conn; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed[] $params |
42
|
|
|
* @param string $username |
43
|
|
|
* @param string $password |
44
|
|
|
* @param mixed[] $driverOptions |
45
|
|
|
* |
46
|
|
|
* @throws MysqliException |
47
|
|
|
*/ |
48
|
1977 |
|
public function __construct(array $params, $username, $password, array $driverOptions = []) |
49
|
|
|
{ |
50
|
1977 |
|
$port = $params['port'] ?? (int) ini_get('mysqli.default_port'); |
51
|
|
|
|
52
|
|
|
// Fallback to default MySQL port if not given. |
53
|
1977 |
|
if (! $port) { |
54
|
|
|
$port = 3306; |
55
|
|
|
} |
56
|
|
|
|
57
|
1977 |
|
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket'); |
58
|
1977 |
|
$dbname = $params['dbname'] ?? ''; |
59
|
|
|
|
60
|
1977 |
|
$flags = $driverOptions[static::OPTION_FLAGS] ?? 0; |
61
|
|
|
|
62
|
1977 |
|
$this->conn = mysqli_init(); |
63
|
|
|
|
64
|
1977 |
|
$this->setSecureConnection($params); |
65
|
1977 |
|
$this->setDriverOptions($driverOptions); |
66
|
|
|
|
67
|
|
|
set_error_handler(static function () { |
68
|
1977 |
|
}); |
69
|
|
|
try { |
70
|
1977 |
|
if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) { |
71
|
1970 |
|
throw MysqliException::fromConnectionError($this->conn); |
72
|
|
|
} |
73
|
1970 |
|
} finally { |
74
|
1977 |
|
restore_error_handler(); |
75
|
|
|
} |
76
|
|
|
|
77
|
1970 |
|
if (! isset($params['charset'])) { |
78
|
1970 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
1221 |
|
$this->conn->set_charset($params['charset']); |
82
|
1221 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrieves mysqli native resource handle. |
86
|
|
|
* |
87
|
|
|
* Could be used if part of your application is not using DBAL. |
88
|
|
|
* |
89
|
|
|
* @return mysqli |
90
|
|
|
*/ |
91
|
|
|
public function getWrappedResourceHandle() |
92
|
|
|
{ |
93
|
|
|
return $this->conn; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
* |
99
|
|
|
* The server version detection includes a special case for MariaDB |
100
|
|
|
* to support '5.5.5-' prefixed versions introduced in Maria 10+ |
101
|
|
|
* |
102
|
|
|
* @link https://jira.mariadb.org/browse/MDEV-4088 |
103
|
|
|
*/ |
104
|
1970 |
|
public function getServerVersion() |
105
|
|
|
{ |
106
|
1970 |
|
$serverInfos = $this->conn->get_server_info(); |
107
|
1970 |
|
if (stripos($serverInfos, 'mariadb') !== false) { |
108
|
1120 |
|
return $serverInfos; |
109
|
|
|
} |
110
|
|
|
|
111
|
850 |
|
$majorVersion = floor($this->conn->server_version / 10000); |
112
|
850 |
|
$minorVersion = floor(($this->conn->server_version - $majorVersion * 10000) / 100); |
113
|
850 |
|
$patchVersion = floor($this->conn->server_version - $majorVersion * 10000 - $minorVersion * 100); |
114
|
|
|
|
115
|
850 |
|
return $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1984 |
|
public function requiresQueryForServerVersion() |
122
|
|
|
{ |
123
|
1984 |
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
1956 |
|
public function prepare(string $sql) : DriverStatement |
130
|
|
|
{ |
131
|
1956 |
|
return new MysqliStatement($this->conn, $sql); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
1949 |
|
public function query(string $sql) : ResultStatement |
138
|
|
|
{ |
139
|
1949 |
|
$stmt = $this->prepare($sql); |
140
|
1949 |
|
$stmt->execute(); |
141
|
|
|
|
142
|
1949 |
|
return $stmt; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
1844 |
|
public function quote(string $input) : string |
149
|
|
|
{ |
150
|
1844 |
|
return "'" . $this->conn->escape_string($input) . "'"; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
1970 |
|
public function exec(string $statement) : int |
157
|
|
|
{ |
158
|
1970 |
|
if ($this->conn->query($statement) === false) { |
159
|
1956 |
|
throw MysqliException::fromConnectionError($this->conn); |
160
|
|
|
} |
161
|
|
|
|
162
|
1970 |
|
return $this->conn->affected_rows; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
49 |
|
public function lastInsertId($name = null) |
169
|
|
|
{ |
170
|
49 |
|
return $this->conn->insert_id; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
1907 |
|
public function beginTransaction() : void |
177
|
|
|
{ |
178
|
1907 |
|
$this->conn->query('START TRANSACTION'); |
179
|
1907 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
1893 |
|
public function commit() : void |
185
|
|
|
{ |
186
|
1893 |
|
if (! $this->conn->commit()) { |
187
|
|
|
throw MysqliException::fromConnectionError($this->conn); |
188
|
|
|
} |
189
|
1893 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
1907 |
|
public function rollBack() : void |
195
|
|
|
{ |
196
|
1907 |
|
if (! $this->conn->rollback()) { |
197
|
|
|
throw MysqliException::fromConnectionError($this->conn); |
198
|
|
|
} |
199
|
1907 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function errorCode() |
205
|
|
|
{ |
206
|
|
|
return $this->conn->errno; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
|
|
public function errorInfo() |
213
|
|
|
{ |
214
|
|
|
return $this->conn->error; |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Apply the driver options to the connection. |
219
|
|
|
* |
220
|
|
|
* @param mixed[] $driverOptions |
221
|
|
|
* |
222
|
|
|
* @throws MysqliException When one of of the options is not supported. |
223
|
|
|
* @throws MysqliException When applying doesn't work - e.g. due to incorrect value. |
224
|
|
|
*/ |
225
|
1977 |
|
private function setDriverOptions(array $driverOptions = []) |
226
|
|
|
{ |
227
|
|
|
$supportedDriverOptions = [ |
228
|
1977 |
|
MYSQLI_OPT_CONNECT_TIMEOUT, |
229
|
|
|
MYSQLI_OPT_LOCAL_INFILE, |
230
|
|
|
MYSQLI_INIT_COMMAND, |
231
|
|
|
MYSQLI_READ_DEFAULT_FILE, |
232
|
|
|
MYSQLI_READ_DEFAULT_GROUP, |
233
|
|
|
]; |
234
|
|
|
|
235
|
1977 |
|
if (defined('MYSQLI_SERVER_PUBLIC_KEY')) { |
236
|
1977 |
|
$supportedDriverOptions[] = MYSQLI_SERVER_PUBLIC_KEY; |
237
|
|
|
} |
238
|
|
|
|
239
|
1977 |
|
$exceptionMsg = "%s option '%s' with value '%s'"; |
240
|
|
|
|
241
|
1977 |
|
foreach ($driverOptions as $option => $value) { |
242
|
1382 |
|
if ($option === static::OPTION_FLAGS) { |
243
|
|
|
continue; |
244
|
|
|
} |
245
|
|
|
|
246
|
1382 |
|
if (! in_array($option, $supportedDriverOptions, true)) { |
247
|
1375 |
|
throw new MysqliException( |
248
|
1375 |
|
sprintf($exceptionMsg, 'Unsupported', $option, $value) |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
1382 |
|
if (@mysqli_options($this->conn, $option, $value)) { |
253
|
1382 |
|
continue; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
throw MysqliException::fromConnectionError($this->conn); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Pings the server and re-connects when `mysqli.reconnect = 1` |
262
|
|
|
* |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
1977 |
|
public function ping() |
266
|
|
|
{ |
267
|
|
|
return $this->conn->ping(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Establish a secure connection |
272
|
1837 |
|
* |
273
|
|
|
* @param mixed[] $params |
274
|
1837 |
|
* |
275
|
|
|
* @throws MysqliException |
276
|
|
|
*/ |
277
|
|
|
private function setSecureConnection(array $params) |
278
|
|
|
{ |
279
|
|
|
if (! isset($params['ssl_key']) && |
280
|
|
|
! isset($params['ssl_cert']) && |
281
|
|
|
! isset($params['ssl_ca']) && |
282
|
|
|
! isset($params['ssl_capath']) && |
283
|
|
|
! isset($params['ssl_cipher']) |
284
|
1977 |
|
) { |
285
|
|
|
return; |
286
|
1977 |
|
} |
287
|
1977 |
|
|
288
|
1977 |
|
$this->conn->ssl_set( |
289
|
1977 |
|
$params['ssl_key'] ?? null, |
290
|
1977 |
|
$params['ssl_cert'] ?? null, |
291
|
|
|
$params['ssl_ca'] ?? null, |
292
|
1977 |
|
$params['ssl_capath'] ?? null, |
293
|
|
|
$params['ssl_cipher'] ?? null |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: