3 * This is the MySQLi database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace Wikimedia\Rdbms
;
29 * Database abstraction object for PHP extension mysqli.
35 class DatabaseMysqli
extends DatabaseMysqlBase
{
36 /** @var $mConn mysqli */
42 protected function doQuery( $sql ) {
43 $conn = $this->getBindingHandle();
45 if ( $this->bufferResults() ) {
46 $ret = $conn->query( $sql );
48 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
55 * @param string $realServer
57 * @throws DBConnectionError
59 protected function mysqlConnect( $realServer ) {
60 # Avoid suppressed fatal error, which is very hard to track down
61 if ( !function_exists( 'mysqli_init' ) ) {
62 throw new DBConnectionError( $this, "MySQLi functions missing,"
63 . " have you compiled PHP with the --with-mysqli option?\n" );
66 // Other than mysql_connect, mysqli_real_connect expects an explicit port
67 // and socket parameters. So we need to parse the port and socket out of
71 $hostAndPort = IP
::splitHostAndPort( $realServer );
73 $realServer = $hostAndPort[0];
74 if ( $hostAndPort[1] ) {
75 $port = $hostAndPort[1];
77 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
78 // If we have a colon and something that's not a port number
79 // inside the hostname, assume it's the socket location
80 $hostAndSocket = explode( ':', $realServer );
81 $realServer = $hostAndSocket[0];
82 $socket = $hostAndSocket[1];
85 $mysqli = mysqli_init();
88 if ( $this->mFlags
& self
::DBO_SSL
) {
89 $connFlags |
= MYSQLI_CLIENT_SSL
;
98 if ( $this->mFlags
& self
::DBO_COMPRESS
) {
99 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
101 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
102 $realServer = 'p:' . $realServer;
105 if ( $this->utf8Mode
) {
106 // Tell the server we're communicating with it in UTF-8.
107 // This may engage various charset conversions.
108 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
110 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
112 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
114 if ( $mysqli->real_connect( $realServer, $this->mUser
,
115 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
123 protected function connectInitCharset() {
124 // already done in mysqlConnect()
129 * @param string $charset
132 protected function mysqlSetCharset( $charset ) {
133 $conn = $this->getBindingHandle();
135 if ( method_exists( $conn, 'set_charset' ) ) {
136 return $conn->set_charset( $charset );
138 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
145 protected function closeConnection() {
146 $conn = $this->getBindingHandle();
148 return $conn->close();
154 function insertId() {
155 $conn = $this->getBindingHandle();
157 return (int)$conn->insert_id
;
163 function lastErrno() {
164 if ( $this->mConn
) {
165 return $this->mConn
->errno
;
167 return mysqli_connect_errno();
174 function affectedRows() {
175 $conn = $this->getBindingHandle();
177 return $conn->affected_rows
;
184 function selectDB( $db ) {
185 $conn = $this->getBindingHandle();
187 $this->mDBname
= $db;
189 return $conn->select_db( $db );
196 protected function mysqlFreeResult( $res ) {
206 protected function mysqlFetchObject( $res ) {
207 $object = $res->fetch_object();
208 if ( $object === null ) {
219 protected function mysqlFetchArray( $res ) {
220 $array = $res->fetch_array();
221 if ( $array === null ) {
232 protected function mysqlNumRows( $res ) {
233 return $res->num_rows
;
240 protected function mysqlNumFields( $res ) {
241 return $res->field_count
;
249 protected function mysqlFetchField( $res, $n ) {
250 $field = $res->fetch_field_direct( $n );
252 // Add missing properties to result (using flags property)
253 // which will be part of function mysql-fetch-field for backward compatibility
254 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
255 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
256 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
257 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
258 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
259 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
260 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
261 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
262 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
272 protected function mysqlFieldName( $res, $n ) {
273 $field = $res->fetch_field_direct( $n );
283 protected function mysqlFieldType( $res, $n ) {
284 $field = $res->fetch_field_direct( $n );
294 protected function mysqlDataSeek( $res, $row ) {
295 return $res->data_seek( $row );
299 * @param mysqli $conn Optional connection object
302 protected function mysqlError( $conn = null ) {
303 if ( $conn === null ) {
304 return mysqli_connect_error();
311 * Escapes special characters in a string for use in an SQL statement
315 protected function mysqlRealEscapeString( $s ) {
316 $conn = $this->getBindingHandle();
318 return $conn->real_escape_string( $s );
322 * Give an id for the connection
324 * mysql driver used resource id, but mysqli objects cannot be cast to string.
327 public function __toString() {
328 if ( $this->mConn
instanceof mysqli
) {
329 return (string)$this->mConn
->thread_id
;
331 // mConn might be false or something.
332 return (string)$this->mConn
;
337 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );