Completed
Pull Request — master (#3610)
by Sergei
31:59 queued 28:53
created

MysqliConnection::setDriverOptions()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 11.5343

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 13
cts 28
cp 0.4643
rs 8.7537
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 11.5343
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like \mysqli_init() of type object<mysql> is incompatible with the declared type object<mysqli> of property $conn.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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 requiresQueryForServerVersion() : bool
124
    {
125 169
        return false;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 2781
    public function prepare(string $sql) : DriverStatement
132
    {
133 2781
        return new MysqliStatement($this->conn, $sql);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1829
    public function query(string $sql) : ResultStatement
140
    {
141 1829
        $stmt = $this->prepare($sql);
142 1805
        $stmt->execute();
143
144 1805
        return $stmt;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 16
    public function quote(string $input) : string
151
    {
152 16
        return "'" . $this->conn->escape_string($input) . "'";
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 1557
    public function exec(string $statement) : int
159
    {
160 1557
        if ($this->conn->query($statement) === false) {
161 765
            throw ConnectionError::new($this->conn);
162
        }
163
164 1448
        return $this->conn->affected_rows;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 16
    public function lastInsertId(?string $name = null) : string
171
    {
172 16
        return (string) $this->conn->insert_id;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 136
    public function beginTransaction() : void
179
    {
180 136
        $this->conn->query('START TRANSACTION');
181 136
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 56
    public function commit() : void
187
    {
188 56
        if (! $this->conn->commit()) {
189
            throw ConnectionError::new($this->conn);
190
        }
191 56
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 88
    public function rollBack() : void
197
    {
198 88
        if (! $this->conn->rollback()) {
199
            throw ConnectionError::new($this->conn);
200
        }
201 88
    }
202
203
    /**
204
     * Apply the driver options to the connection.
205
     *
206
     * @param array<int, mixed> $driverOptions
207
     *
208
     * @throws MysqliException When one of of the options is not supported.
209
     * @throws MysqliException When applying doesn't work - e.g. due to incorrect value.
210
     */
211 368
    private function setDriverOptions(array $driverOptions = []) : void
212
    {
213
        $supportedDriverOptions = [
214 368
            MYSQLI_OPT_CONNECT_TIMEOUT,
215
            MYSQLI_OPT_LOCAL_INFILE,
216
            MYSQLI_INIT_COMMAND,
217
            MYSQLI_READ_DEFAULT_FILE,
218
            MYSQLI_READ_DEFAULT_GROUP,
219
        ];
220
221 368
        if (defined('MYSQLI_SERVER_PUBLIC_KEY')) {
222 368
            $supportedDriverOptions[] = MYSQLI_SERVER_PUBLIC_KEY;
223
        }
224
225 368
        $exceptionMsg = "%s option '%s' with value '%s'";
226
227 368
        foreach ($driverOptions as $option => $value) {
228 16
            if ($option === static::OPTION_FLAGS) {
229
                continue;
230
            }
231
232 16
            if (! in_array($option, $supportedDriverOptions, true)) {
233 8
                throw new MysqliException(
234 8
                    sprintf($exceptionMsg, 'Unsupported', $option, $value)
235
                );
236
            }
237
238 8
            if (@mysqli_options($this->conn, $option, $value)) {
239 8
                continue;
240
            }
241
242
            throw ConnectionError::new($this->conn);
243
        }
244 360
    }
245
246
    /**
247
     * Pings the server and re-connects when `mysqli.reconnect = 1`
248
     *
249
     * {@inheritDoc}
250
     */
251 8
    public function ping() : void
252
    {
253 8
        if (! $this->conn->ping()) {
254
            throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
255
        }
256 8
    }
257
258
    /**
259
     * Establish a secure connection
260
     *
261
     * @param array<string, mixed> $params
262
     *
263
     * @throws MysqliException
264
     */
265 368
    private function setSecureConnection(array $params) : void
266
    {
267 368
        if (! isset($params['ssl_key']) &&
268 368
            ! isset($params['ssl_cert']) &&
269 368
            ! isset($params['ssl_ca']) &&
270 368
            ! isset($params['ssl_capath']) &&
271 368
            ! isset($params['ssl_cipher'])
272
        ) {
273 368
            return;
274
        }
275
276
        $this->conn->ssl_set(
277
            $params['ssl_key']    ?? null,
278
            $params['ssl_cert']   ?? null,
279
            $params['ssl_ca']     ?? null,
280
            $params['ssl_capath'] ?? null,
281
            $params['ssl_cipher'] ?? null
282
        );
283
    }
284
}
285