|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver\Mysqli; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Driver\Connection as Connection; |
|
23
|
|
|
use Doctrine\DBAL\Driver\PingableConnection; |
|
24
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
|
25
|
|
|
use Doctrine\DBAL\ParameterType; |
|
26
|
|
|
use function defined; |
|
27
|
|
|
use function floor; |
|
28
|
|
|
use function func_get_args; |
|
29
|
|
|
use function in_array; |
|
30
|
|
|
use function ini_get; |
|
31
|
|
|
use function mysqli_errno; |
|
32
|
|
|
use function mysqli_error; |
|
33
|
|
|
use function mysqli_init; |
|
34
|
|
|
use function mysqli_options; |
|
35
|
|
|
use function restore_error_handler; |
|
36
|
|
|
use function set_error_handler; |
|
37
|
|
|
use function sprintf; |
|
38
|
|
|
use function stripos; |
|
39
|
|
|
use function var_dump; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @author Kim Hemsø Rasmussen <[email protected]> |
|
43
|
|
|
* @author Till Klampaeckel <[email protected]> |
|
44
|
|
|
*/ |
|
45
|
|
|
class MysqliConnection implements Connection, PingableConnection, ServerInfoAwareConnection |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* Name of the option to set connection flags |
|
49
|
|
|
*/ |
|
50
|
|
|
const OPTION_FLAGS = 'flags'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var \mysqli |
|
54
|
|
|
*/ |
|
55
|
|
|
private $_conn; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param array $params |
|
59
|
|
|
* @param string $username |
|
60
|
|
|
* @param string $password |
|
61
|
|
|
* @param array $driverOptions |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \Doctrine\DBAL\Driver\Mysqli\MysqliException |
|
64
|
|
|
*/ |
|
65
|
129 |
|
public function __construct(array $params, $username, $password, array $driverOptions = []) |
|
66
|
|
|
{ |
|
67
|
129 |
|
$port = $params['port'] ?? ini_get('mysqli.default_port'); |
|
68
|
|
|
|
|
69
|
|
|
// Fallback to default MySQL port if not given. |
|
70
|
129 |
|
if ( ! $port) { |
|
71
|
|
|
$port = 3306; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
129 |
|
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket'); |
|
75
|
129 |
|
$dbname = $params['dbname'] ?? null; |
|
76
|
|
|
|
|
77
|
129 |
|
$flags = $driverOptions[static::OPTION_FLAGS] ?? null; |
|
78
|
|
|
|
|
79
|
129 |
|
$this->_conn = mysqli_init(); |
|
80
|
|
|
|
|
81
|
129 |
|
$this->setSecureConnection($params); |
|
82
|
129 |
|
$this->setDriverOptions($driverOptions); |
|
83
|
|
|
|
|
84
|
|
|
set_error_handler(function (...$args) { |
|
85
|
21 |
|
var_dump($args); |
|
|
|
|
|
|
86
|
126 |
|
}); |
|
87
|
|
|
try { |
|
88
|
126 |
|
if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) { |
|
89
|
21 |
|
throw new MysqliException($this->_conn->connect_error, $this->_conn->sqlstate ?? 'HY000', $this->_conn->connect_errno); |
|
90
|
|
|
} |
|
91
|
108 |
|
} finally { |
|
92
|
126 |
|
restore_error_handler(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
108 |
|
if (isset($params['charset'])) { |
|
96
|
3 |
|
$this->_conn->set_charset($params['charset']); |
|
97
|
|
|
} |
|
98
|
108 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Retrieves mysqli native resource handle. |
|
102
|
|
|
* |
|
103
|
|
|
* Could be used if part of your application is not using DBAL. |
|
104
|
|
|
* |
|
105
|
|
|
* @return \mysqli |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getWrappedResourceHandle() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->_conn; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
* |
|
115
|
|
|
* The server version detection includes a special case for MariaDB |
|
116
|
|
|
* to support '5.5.5-' prefixed versions introduced in Maria 10+ |
|
117
|
|
|
* @link https://jira.mariadb.org/browse/MDEV-4088 |
|
118
|
|
|
*/ |
|
119
|
72 |
|
public function getServerVersion() |
|
120
|
|
|
{ |
|
121
|
72 |
|
$serverInfos = $this->_conn->get_server_info(); |
|
122
|
72 |
|
if (false !== stripos($serverInfos, 'mariadb')) { |
|
123
|
24 |
|
return $serverInfos; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
48 |
|
$majorVersion = floor($this->_conn->server_version / 10000); |
|
127
|
48 |
|
$minorVersion = floor(($this->_conn->server_version - $majorVersion * 10000) / 100); |
|
128
|
48 |
|
$patchVersion = floor($this->_conn->server_version - $majorVersion * 10000 - $minorVersion * 100); |
|
129
|
|
|
|
|
130
|
48 |
|
return $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
78 |
|
public function requiresQueryForServerVersion() |
|
137
|
|
|
{ |
|
138
|
78 |
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
765 |
|
public function prepare($prepareString) |
|
145
|
|
|
{ |
|
146
|
765 |
|
return new MysqliStatement($this->_conn, $prepareString); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
540 |
|
public function query() |
|
153
|
|
|
{ |
|
154
|
540 |
|
$args = func_get_args(); |
|
155
|
540 |
|
$sql = $args[0]; |
|
156
|
540 |
|
$stmt = $this->prepare($sql); |
|
157
|
531 |
|
$stmt->execute(); |
|
158
|
|
|
|
|
159
|
531 |
|
return $stmt; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
12 |
|
public function quote($input, $type = ParameterType::STRING) |
|
166
|
|
|
{ |
|
167
|
12 |
|
return "'". $this->_conn->escape_string($input) ."'"; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
528 |
|
public function exec($statement) |
|
174
|
|
|
{ |
|
175
|
528 |
|
if (false === $this->_conn->query($statement)) { |
|
176
|
342 |
|
throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
446 |
|
return $this->_conn->affected_rows; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* {@inheritdoc} |
|
184
|
|
|
*/ |
|
185
|
6 |
|
public function lastInsertId($name = null) |
|
186
|
|
|
{ |
|
187
|
6 |
|
return $this->_conn->insert_id; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* {@inheritdoc} |
|
192
|
|
|
*/ |
|
193
|
48 |
|
public function beginTransaction() |
|
194
|
|
|
{ |
|
195
|
48 |
|
$this->_conn->query('START TRANSACTION'); |
|
196
|
|
|
|
|
197
|
48 |
|
return true; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
21 |
|
public function commit() |
|
204
|
|
|
{ |
|
205
|
21 |
|
return $this->_conn->commit(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* {@inheritdoc}non-PHPdoc) |
|
210
|
|
|
*/ |
|
211
|
30 |
|
public function rollBack() |
|
212
|
|
|
{ |
|
213
|
30 |
|
return $this->_conn->rollback(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* {@inheritdoc} |
|
218
|
|
|
*/ |
|
219
|
|
|
public function errorCode() |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->_conn->errno; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* {@inheritdoc} |
|
226
|
|
|
*/ |
|
227
|
|
|
public function errorInfo() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->_conn->error; |
|
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Apply the driver options to the connection. |
|
234
|
|
|
* |
|
235
|
|
|
* @param array $driverOptions |
|
236
|
|
|
* |
|
237
|
|
|
* @throws MysqliException When one of of the options is not supported. |
|
238
|
|
|
* @throws MysqliException When applying doesn't work - e.g. due to incorrect value. |
|
239
|
|
|
*/ |
|
240
|
129 |
|
private function setDriverOptions(array $driverOptions = []) |
|
241
|
|
|
{ |
|
242
|
|
|
$supportedDriverOptions = [ |
|
243
|
129 |
|
\MYSQLI_OPT_CONNECT_TIMEOUT, |
|
244
|
|
|
\MYSQLI_OPT_LOCAL_INFILE, |
|
245
|
|
|
\MYSQLI_INIT_COMMAND, |
|
246
|
|
|
\MYSQLI_READ_DEFAULT_FILE, |
|
247
|
|
|
\MYSQLI_READ_DEFAULT_GROUP, |
|
248
|
|
|
]; |
|
249
|
|
|
|
|
250
|
129 |
|
if (defined('MYSQLI_SERVER_PUBLIC_KEY')) { |
|
251
|
129 |
|
$supportedDriverOptions[] = \MYSQLI_SERVER_PUBLIC_KEY; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
129 |
|
$exceptionMsg = "%s option '%s' with value '%s'"; |
|
255
|
|
|
|
|
256
|
129 |
|
foreach ($driverOptions as $option => $value) { |
|
257
|
|
|
|
|
258
|
6 |
|
if ($option === static::OPTION_FLAGS) { |
|
259
|
|
|
continue; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
6 |
|
if (!in_array($option, $supportedDriverOptions, true)) { |
|
263
|
3 |
|
throw new MysqliException( |
|
264
|
3 |
|
sprintf($exceptionMsg, 'Unsupported', $option, $value) |
|
265
|
|
|
); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
3 |
|
if (@mysqli_options($this->_conn, $option, $value)) { |
|
269
|
3 |
|
continue; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$msg = sprintf($exceptionMsg, 'Failed to set', $option, $value); |
|
273
|
|
|
$msg .= sprintf(', error: %s (%d)', mysqli_error($this->_conn), mysqli_errno($this->_conn)); |
|
274
|
|
|
|
|
275
|
|
|
throw new MysqliException( |
|
276
|
|
|
$msg, |
|
277
|
|
|
$this->_conn->sqlstate, |
|
278
|
|
|
$this->_conn->errno |
|
279
|
|
|
); |
|
280
|
|
|
} |
|
281
|
126 |
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Pings the server and re-connects when `mysqli.reconnect = 1` |
|
285
|
|
|
* |
|
286
|
|
|
* @return bool |
|
287
|
|
|
*/ |
|
288
|
6 |
|
public function ping() |
|
289
|
|
|
{ |
|
290
|
6 |
|
return $this->_conn->ping(); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Establish a secure connection |
|
295
|
|
|
* |
|
296
|
|
|
* @param array $params |
|
297
|
|
|
* @throws MysqliException |
|
298
|
|
|
*/ |
|
299
|
129 |
|
private function setSecureConnection(array $params) |
|
300
|
|
|
{ |
|
301
|
129 |
|
if (! isset($params['ssl_key']) && |
|
302
|
129 |
|
! isset($params['ssl_cert']) && |
|
303
|
129 |
|
! isset($params['ssl_ca']) && |
|
304
|
129 |
|
! isset($params['ssl_capath']) && |
|
305
|
129 |
|
! isset($params['ssl_cipher']) |
|
306
|
|
|
) { |
|
307
|
129 |
|
return; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
$this->_conn->ssl_set( |
|
311
|
|
|
$params['ssl_key'] ?? null, |
|
312
|
|
|
$params['ssl_cert'] ?? null, |
|
313
|
|
|
$params['ssl_ca'] ?? null, |
|
314
|
|
|
$params['ssl_capath'] ?? null, |
|
315
|
|
|
$params['ssl_cipher'] ?? null |
|
316
|
|
|
); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|