MysqliConnection.php 8.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Driver\Mysqli;

22
use Doctrine\DBAL\Driver\Connection as Connection;
Steve Müller's avatar
Steve Müller committed
23
use Doctrine\DBAL\Driver\PingableConnection;
24
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
25
use Doctrine\DBAL\ParameterType;
26 27 28 29 30 31 32 33 34 35 36 37 38
use function defined;
use function floor;
use function func_get_args;
use function in_array;
use function ini_get;
use function mysqli_errno;
use function mysqli_error;
use function mysqli_init;
use function mysqli_options;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use function stripos;
39 40

/**
41
 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
till's avatar
till committed
42
 * @author Till Klampaeckel <till@php.net>
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 53 54 55

    /**
     * @var \mysqli
     */
    private $_conn;

Benjamin Morel's avatar
Benjamin Morel committed
56 57 58 59 60 61 62 63
    /**
     * @param array  $params
     * @param string $username
     * @param string $password
     * @param array  $driverOptions
     *
     * @throws \Doctrine\DBAL\Driver\Mysqli\MysqliException
     */
64
    public function __construct(array $params, $username, $password, array $driverOptions = [])
65
    {
66
        $port = $params['port'] ?? ini_get('mysqli.default_port');
67 68 69 70 71 72

        // Fallback to default MySQL port if not given.
        if ( ! $port) {
            $port = 3306;
        }

73 74
        $socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
        $dbname = $params['dbname'] ?? null;
75

76
        $flags = $driverOptions[static::OPTION_FLAGS] ?? null;
77

78
        $this->_conn = mysqli_init();
79

80
        $this->setSecureConnection($params);
81 82
        $this->setDriverOptions($driverOptions);

83
        set_error_handler(function () {});
84 85 86 87 88
        try {
            if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
                throw new MysqliException($this->_conn->connect_error, $this->_conn->sqlstate ?? 'HY000', $this->_conn->connect_errno);
            }
        } finally {
89
            restore_error_handler();
90
        }
91

92
        if (isset($params['charset'])) {
93 94 95 96
            $this->_conn->set_charset($params['charset']);
        }
    }

97
    /**
Benjamin Morel's avatar
Benjamin Morel committed
98
     * Retrieves mysqli native resource handle.
99
     *
Benjamin Morel's avatar
Benjamin Morel committed
100
     * Could be used if part of your application is not using DBAL.
101
     *
102
     * @return \mysqli
103 104 105 106 107 108
     */
    public function getWrappedResourceHandle()
    {
        return $this->_conn;
    }

109 110
    /**
     * {@inheritdoc}
111 112 113 114
     *
     * The server version detection includes a special case for MariaDB
     * to support '5.5.5-' prefixed versions introduced in Maria 10+
     * @link https://jira.mariadb.org/browse/MDEV-4088
115 116 117
     */
    public function getServerVersion()
    {
118 119 120 121 122
        $serverInfos = $this->_conn->get_server_info();
        if (false !== stripos($serverInfos, 'mariadb')) {
            return $serverInfos;
        }

123 124
        $majorVersion = floor($this->_conn->server_version / 10000);
        $minorVersion = floor(($this->_conn->server_version - $majorVersion * 10000) / 100);
125
        $patchVersion = floor($this->_conn->server_version - $majorVersion * 10000 - $minorVersion * 100);
126 127 128 129 130 131 132 133 134 135 136 137

        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
    }

    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return false;
    }

138 139 140
    /**
     * {@inheritdoc}
     */
141 142 143 144 145
    public function prepare($prepareString)
    {
        return new MysqliStatement($this->_conn, $prepareString);
    }

146 147 148
    /**
     * {@inheritdoc}
     */
149 150 151 152 153 154
    public function query()
    {
        $args = func_get_args();
        $sql = $args[0];
        $stmt = $this->prepare($sql);
        $stmt->execute();
155

156 157 158
        return $stmt;
    }

159 160 161
    /**
     * {@inheritdoc}
     */
162
    public function quote($input, $type = ParameterType::STRING)
163 164 165 166
    {
        return "'". $this->_conn->escape_string($input) ."'";
    }

167 168 169
    /**
     * {@inheritdoc}
     */
170 171
    public function exec($statement)
    {
172 173 174 175
        if (false === $this->_conn->query($statement)) {
            throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno);
        }

176 177 178
        return $this->_conn->affected_rows;
    }

179 180 181
    /**
     * {@inheritdoc}
     */
182 183 184 185 186
    public function lastInsertId($name = null)
    {
        return $this->_conn->insert_id;
    }

187 188 189
    /**
     * {@inheritdoc}
     */
190 191 192
    public function beginTransaction()
    {
        $this->_conn->query('START TRANSACTION');
193

194 195 196
        return true;
    }

197 198 199
    /**
     * {@inheritdoc}
     */
200 201 202 203 204
    public function commit()
    {
        return $this->_conn->commit();
    }

205 206 207
    /**
     * {@inheritdoc}non-PHPdoc)
     */
208 209 210 211 212
    public function rollBack()
    {
        return $this->_conn->rollback();
    }

213 214 215
    /**
     * {@inheritdoc}
     */
216 217 218 219 220
    public function errorCode()
    {
        return $this->_conn->errno;
    }

221 222 223
    /**
     * {@inheritdoc}
     */
224 225 226 227
    public function errorInfo()
    {
        return $this->_conn->error;
    }
228 229 230 231 232 233 234 235 236

    /**
     * Apply the driver options to the connection.
     *
     * @param array $driverOptions
     *
     * @throws MysqliException When one of of the options is not supported.
     * @throws MysqliException When applying doesn't work - e.g. due to incorrect value.
     */
237
    private function setDriverOptions(array $driverOptions = [])
238
    {
239
        $supportedDriverOptions = [
240 241 242 243 244
            \MYSQLI_OPT_CONNECT_TIMEOUT,
            \MYSQLI_OPT_LOCAL_INFILE,
            \MYSQLI_INIT_COMMAND,
            \MYSQLI_READ_DEFAULT_FILE,
            \MYSQLI_READ_DEFAULT_GROUP,
245
        ];
246

247
        if (defined('MYSQLI_SERVER_PUBLIC_KEY')) {
248 249
            $supportedDriverOptions[] = \MYSQLI_SERVER_PUBLIC_KEY;
        }
250

251
        $exceptionMsg = "%s option '%s' with value '%s'";
252 253 254

        foreach ($driverOptions as $option => $value) {

255
            if ($option === static::OPTION_FLAGS) {
256 257 258
                continue;
            }

259
            if (!in_array($option, $supportedDriverOptions, true)) {
260 261 262 263 264
                throw new MysqliException(
                    sprintf($exceptionMsg, 'Unsupported', $option, $value)
                );
            }

265 266
            if (@mysqli_options($this->_conn, $option, $value)) {
                continue;
267
            }
268 269 270 271 272 273

            $msg  = sprintf($exceptionMsg, 'Failed to set', $option, $value);
            $msg .= sprintf(', error: %s (%d)', mysqli_error($this->_conn), mysqli_errno($this->_conn));

            throw new MysqliException(
                $msg,
274 275
                $this->_conn->sqlstate,
                $this->_conn->errno
276
            );
277 278
        }
    }
279 280 281 282 283 284 285 286 287 288

    /**
     * Pings the server and re-connects when `mysqli.reconnect = 1`
     *
     * @return bool
     */
    public function ping()
    {
        return $this->_conn->ping();
    }
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

    /**
     * Establish a secure connection
     *
     * @param array $params
     * @throws MysqliException
     */
    private function setSecureConnection(array $params)
    {
        if (! isset($params['ssl_key']) &&
            ! isset($params['ssl_cert']) &&
            ! isset($params['ssl_ca']) &&
            ! isset($params['ssl_capath']) &&
            ! isset($params['ssl_cipher'])
        ) {
            return;
        }

        $this->_conn->ssl_set(
308 309
            $params['ssl_key']    ?? null,
            $params['ssl_cert']   ?? null,
310 311 312 313 314
            $params['ssl_ca']     ?? null,
            $params['ssl_capath'] ?? null,
            $params['ssl_cipher'] ?? null
        );
    }
315
}