Passed
Pull Request — master (#3025)
by Michael
19:27 queued 26s
created

MysqliConnection::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 3
cp 0.6667
cc 1
nc 1
nop 1
crap 1.037
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
40
/**
41
 * @author Kim Hemsø Rasmussen <[email protected]>
42
 * @author Till Klampaeckel <[email protected]>
43
 */
44
class MysqliConnection implements Connection, PingableConnection, ServerInfoAwareConnection
45
{
46
    /**
47
     * Name of the option to set connection flags
48
     */
49
    const OPTION_FLAGS = 'flags';
50
51
    /**
52
     * @var \mysqli
53
     */
54
    private $_conn;
55
56
    /**
57
     * @param array  $params
58
     * @param string $username
59
     * @param string $password
60
     * @param array  $driverOptions
61
     *
62
     * @throws \Doctrine\DBAL\Driver\Mysqli\MysqliException
63
     */
64 127
    public function __construct(array $params, $username, $password, array $driverOptions = [])
65
    {
66 127
        $port = $params['port'] ?? ini_get('mysqli.default_port');
67
68
        // Fallback to default MySQL port if not given.
69 127
        if ( ! $port) {
70
            $port = 3306;
71
        }
72
73 127
        $socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
74 127
        $dbname = $params['dbname'] ?? null;
75
76 127
        $flags = $driverOptions[static::OPTION_FLAGS] ?? null;
77
78 127
        $this->_conn = mysqli_init();
79
80 127
        $this->setSecureConnection($params);
81 127
        $this->setDriverOptions($driverOptions);
82
83
        set_error_handler(function () {});
84
        try {
85 124
            if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
86 19
                throw new MysqliException($this->_conn->connect_error, $this->_conn->sqlstate ?? 'HY000', $this->_conn->connect_errno);
87
            }
88 108
        } finally {
89 124
            restore_error_handler();
90
        }
91
92 108
        if (isset($params['charset'])) {
93 3
            $this->_conn->set_charset($params['charset']);
94
        }
95 108
    }
96
97
    /**
98
     * Retrieves mysqli native resource handle.
99
     *
100
     * Could be used if part of your application is not using DBAL.
101
     *
102
     * @return \mysqli
103
     */
104
    public function getWrappedResourceHandle()
105
    {
106
        return $this->_conn;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     *
112
     * The server version detection includes a special case for MariaDB
113
     * to support '5.5.5-' prefixed versions introduced in Maria 10+
114
     * @link https://jira.mariadb.org/browse/MDEV-4088
115
     */
116 72
    public function getServerVersion()
117
    {
118 72
        $serverInfos = $this->_conn->get_server_info();
119 72
        if (false !== stripos($serverInfos, 'mariadb')) {
120
            return $serverInfos;
121
        }
122
123 72
        $majorVersion = floor($this->_conn->server_version / 10000);
124 72
        $minorVersion = floor(($this->_conn->server_version - $majorVersion * 10000) / 100);
125 72
        $patchVersion = floor($this->_conn->server_version - $majorVersion * 10000 - $minorVersion * 100);
126
127 72
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 76
    public function requiresQueryForServerVersion()
134
    {
135 76
        return false;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 765
    public function prepare($prepareString)
142
    {
143 765
        return new MysqliStatement($this->_conn, $prepareString);
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 540
    public function query()
150
    {
151 540
        $args = func_get_args();
152 540
        $sql = $args[0];
153 540
        $stmt = $this->prepare($sql);
154 531
        $stmt->execute();
155
156 531
        return $stmt;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 12
    public function quote($input, $type = ParameterType::STRING)
163
    {
164 12
        return "'". $this->_conn->escape_string($input) ."'";
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 528
    public function exec($statement)
171
    {
172 528
        if (false === $this->_conn->query($statement)) {
173 342
            throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno);
174
        }
175
176 446
        return $this->_conn->affected_rows;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 6
    public function lastInsertId($name = null)
183
    {
184 6
        return $this->_conn->insert_id;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190 48
    public function beginTransaction()
191
    {
192 48
        $this->_conn->query('START TRANSACTION');
193
194 48
        return true;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 21
    public function commit()
201
    {
202 21
        return $this->_conn->commit();
203
    }
204
205
    /**
206
     * {@inheritdoc}non-PHPdoc)
207
     */
208 30
    public function rollBack()
209
    {
210 30
        return $this->_conn->rollback();
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function errorCode()
217
    {
218
        return $this->_conn->errno;
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function errorInfo()
225
    {
226
        return $this->_conn->error;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_conn->error returns the type string which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::errorInfo() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
227
    }
228
229
    /**
230
     * Apply the driver options to the connection.
231
     *
232
     * @param array $driverOptions
233
     *
234
     * @throws MysqliException When one of of the options is not supported.
235
     * @throws MysqliException When applying doesn't work - e.g. due to incorrect value.
236
     */
237 127
    private function setDriverOptions(array $driverOptions = [])
238
    {
239
        $supportedDriverOptions = [
240 127
            \MYSQLI_OPT_CONNECT_TIMEOUT,
241
            \MYSQLI_OPT_LOCAL_INFILE,
242
            \MYSQLI_INIT_COMMAND,
243
            \MYSQLI_READ_DEFAULT_FILE,
244
            \MYSQLI_READ_DEFAULT_GROUP,
245
        ];
246
247 127
        if (defined('MYSQLI_SERVER_PUBLIC_KEY')) {
248 127
            $supportedDriverOptions[] = \MYSQLI_SERVER_PUBLIC_KEY;
249
        }
250
251 127
        $exceptionMsg = "%s option '%s' with value '%s'";
252
253 127
        foreach ($driverOptions as $option => $value) {
254
255 6
            if ($option === static::OPTION_FLAGS) {
256
                continue;
257
            }
258
259 6
            if (!in_array($option, $supportedDriverOptions, true)) {
260 3
                throw new MysqliException(
261 3
                    sprintf($exceptionMsg, 'Unsupported', $option, $value)
262
                );
263
            }
264
265 3
            if (@mysqli_options($this->_conn, $option, $value)) {
266 3
                continue;
267
            }
268
269
            $msg  = sprintf($exceptionMsg, 'Failed to set', $option, $value);
270
            $msg .= sprintf(', error: %s (%d)', mysqli_error($this->_conn), mysqli_errno($this->_conn));
271
272
            throw new MysqliException(
273
                $msg,
274
                $this->_conn->sqlstate,
275
                $this->_conn->errno
276
            );
277
        }
278 124
    }
279
280
    /**
281
     * Pings the server and re-connects when `mysqli.reconnect = 1`
282
     *
283
     * @return bool
284
     */
285 6
    public function ping()
286
    {
287 6
        return $this->_conn->ping();
288
    }
289
290
    /**
291
     * Establish a secure connection
292
     *
293
     * @param array $params
294
     * @throws MysqliException
295
     */
296 127
    private function setSecureConnection(array $params)
297
    {
298 127
        if (! isset($params['ssl_key']) &&
299 127
            ! isset($params['ssl_cert']) &&
300 127
            ! isset($params['ssl_ca']) &&
301 127
            ! isset($params['ssl_capath']) &&
302 127
            ! isset($params['ssl_cipher'])
303
        ) {
304 127
            return;
305
        }
306
307
        $this->_conn->ssl_set(
308
            $params['ssl_key']    ?? null,
309
            $params['ssl_cert']   ?? null,
310
            $params['ssl_ca']     ?? null,
311
            $params['ssl_capath'] ?? null,
312
            $params['ssl_cipher'] ?? null
313
        );
314
    }
315
}
316