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
24 use Wikimedia\Rdbms\ResultWrapper
;
27 * Database abstraction object for PHP extension mysqli.
33 class DatabaseMysqli
extends DatabaseMysqlBase
{
34 /** @var $mConn mysqli */
40 protected function doQuery( $sql ) {
41 $conn = $this->getBindingHandle();
43 if ( $this->bufferResults() ) {
44 $ret = $conn->query( $sql );
46 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
53 * @param string $realServer
55 * @throws DBConnectionError
57 protected function mysqlConnect( $realServer ) {
58 # Avoid suppressed fatal error, which is very hard to track down
59 if ( !function_exists( 'mysqli_init' ) ) {
60 throw new DBConnectionError( $this, "MySQLi functions missing,"
61 . " have you compiled PHP with the --with-mysqli option?\n" );
64 // Other than mysql_connect, mysqli_real_connect expects an explicit port
65 // and socket parameters. So we need to parse the port and socket out of
69 $hostAndPort = IP
::splitHostAndPort( $realServer );
71 $realServer = $hostAndPort[0];
72 if ( $hostAndPort[1] ) {
73 $port = $hostAndPort[1];
75 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
76 // If we have a colon and something that's not a port number
77 // inside the hostname, assume it's the socket location
78 $hostAndSocket = explode( ':', $realServer );
79 $realServer = $hostAndSocket[0];
80 $socket = $hostAndSocket[1];
83 $mysqli = mysqli_init();
86 if ( $this->mFlags
& self
::DBO_SSL
) {
87 $connFlags |
= MYSQLI_CLIENT_SSL
;
96 if ( $this->mFlags
& self
::DBO_COMPRESS
) {
97 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
99 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
100 $realServer = 'p:' . $realServer;
103 if ( $this->utf8Mode
) {
104 // Tell the server we're communicating with it in UTF-8.
105 // This may engage various charset conversions.
106 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
108 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
110 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
112 if ( $mysqli->real_connect( $realServer, $this->mUser
,
113 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
121 protected function connectInitCharset() {
122 // already done in mysqlConnect()
127 * @param string $charset
130 protected function mysqlSetCharset( $charset ) {
131 $conn = $this->getBindingHandle();
133 if ( method_exists( $conn, 'set_charset' ) ) {
134 return $conn->set_charset( $charset );
136 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
143 protected function closeConnection() {
144 $conn = $this->getBindingHandle();
146 return $conn->close();
152 function insertId() {
153 $conn = $this->getBindingHandle();
155 return (int)$conn->insert_id
;
161 function lastErrno() {
162 if ( $this->mConn
) {
163 return $this->mConn
->errno
;
165 return mysqli_connect_errno();
172 function affectedRows() {
173 $conn = $this->getBindingHandle();
175 return $conn->affected_rows
;
182 function selectDB( $db ) {
183 $conn = $this->getBindingHandle();
185 $this->mDBname
= $db;
187 return $conn->select_db( $db );
194 protected function mysqlFreeResult( $res ) {
204 protected function mysqlFetchObject( $res ) {
205 $object = $res->fetch_object();
206 if ( $object === null ) {
217 protected function mysqlFetchArray( $res ) {
218 $array = $res->fetch_array();
219 if ( $array === null ) {
230 protected function mysqlNumRows( $res ) {
231 return $res->num_rows
;
238 protected function mysqlNumFields( $res ) {
239 return $res->field_count
;
247 protected function mysqlFetchField( $res, $n ) {
248 $field = $res->fetch_field_direct( $n );
250 // Add missing properties to result (using flags property)
251 // which will be part of function mysql-fetch-field for backward compatibility
252 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
253 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
254 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
255 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
256 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
257 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
258 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
259 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
260 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
266 * @param resource|ResultWrapper $res
270 protected function mysqlFieldName( $res, $n ) {
271 $field = $res->fetch_field_direct( $n );
277 * @param resource|ResultWrapper $res
281 protected function mysqlFieldType( $res, $n ) {
282 $field = $res->fetch_field_direct( $n );
288 * @param resource|ResultWrapper $res
292 protected function mysqlDataSeek( $res, $row ) {
293 return $res->data_seek( $row );
297 * @param mysqli $conn Optional connection object
300 protected function mysqlError( $conn = null ) {
301 if ( $conn === null ) {
302 return mysqli_connect_error();
309 * Escapes special characters in a string for use in an SQL statement
313 protected function mysqlRealEscapeString( $s ) {
314 $conn = $this->getBindingHandle();
316 return $conn->real_escape_string( $s );
320 * Give an id for the connection
322 * mysql driver used resource id, but mysqli objects cannot be cast to string.
325 public function __toString() {
326 if ( $this->mConn
instanceof mysqli
) {
327 return (string)$this->mConn
->thread_id
;
329 // mConn might be false or something.
330 return (string)$this->mConn
;