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