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
;
30 * Database abstraction object for PHP extension mysqli.
36 class DatabaseMysqli
extends DatabaseMysqlBase
{
37 /** @var mysqli $mConn */
43 protected function doQuery( $sql ) {
44 $conn = $this->getBindingHandle();
46 if ( $this->bufferResults() ) {
47 $ret = $conn->query( $sql );
49 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
56 * @param string $realServer
58 * @throws DBConnectionError
60 protected function mysqlConnect( $realServer ) {
61 # Avoid suppressed fatal error, which is very hard to track down
62 if ( !function_exists( 'mysqli_init' ) ) {
63 throw new DBConnectionError( $this, "MySQLi functions missing,"
64 . " have you compiled PHP with the --with-mysqli option?\n" );
67 // Other than mysql_connect, mysqli_real_connect expects an explicit port
68 // and socket parameters. So we need to parse the port and socket out of
72 $hostAndPort = IP
::splitHostAndPort( $realServer );
74 $realServer = $hostAndPort[0];
75 if ( $hostAndPort[1] ) {
76 $port = $hostAndPort[1];
78 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
79 // If we have a colon and something that's not a port number
80 // inside the hostname, assume it's the socket location
81 $hostAndSocket = explode( ':', $realServer );
82 $realServer = $hostAndSocket[0];
83 $socket = $hostAndSocket[1];
86 $mysqli = mysqli_init();
89 if ( $this->mFlags
& self
::DBO_SSL
) {
90 $connFlags |
= MYSQLI_CLIENT_SSL
;
99 if ( $this->mFlags
& self
::DBO_COMPRESS
) {
100 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
102 if ( $this->mFlags
& self
::DBO_PERSISTENT
) {
103 $realServer = 'p:' . $realServer;
106 if ( $this->utf8Mode
) {
107 // Tell the server we're communicating with it in UTF-8.
108 // This may engage various charset conversions.
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
111 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
113 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
115 if ( $mysqli->real_connect( $realServer, $this->mUser
,
116 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
124 protected function connectInitCharset() {
125 // already done in mysqlConnect()
130 * @param string $charset
133 protected function mysqlSetCharset( $charset ) {
134 $conn = $this->getBindingHandle();
136 if ( method_exists( $conn, 'set_charset' ) ) {
137 return $conn->set_charset( $charset );
139 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
146 protected function closeConnection() {
147 $conn = $this->getBindingHandle();
149 return $conn->close();
155 function insertId() {
156 $conn = $this->getBindingHandle();
158 return (int)$conn->insert_id
;
164 function lastErrno() {
165 if ( $this->mConn
) {
166 return $this->mConn
->errno
;
168 return mysqli_connect_errno();
175 protected function fetchAffectedRowCount() {
176 $conn = $this->getBindingHandle();
178 return $conn->affected_rows
;
185 function selectDB( $db ) {
186 $conn = $this->getBindingHandle();
188 $this->mDBname
= $db;
190 return $conn->select_db( $db );
194 * @param mysqli_result $res
197 protected function mysqlFreeResult( $res ) {
204 * @param mysqli_result $res
207 protected function mysqlFetchObject( $res ) {
208 $object = $res->fetch_object();
209 if ( $object === null ) {
217 * @param mysqli_result $res
220 protected function mysqlFetchArray( $res ) {
221 $array = $res->fetch_array();
222 if ( $array === null ) {
230 * @param mysqli_result $res
233 protected function mysqlNumRows( $res ) {
234 return $res->num_rows
;
241 protected function mysqlNumFields( $res ) {
242 return $res->field_count
;
250 protected function mysqlFetchField( $res, $n ) {
251 $field = $res->fetch_field_direct( $n );
253 // Add missing properties to result (using flags property)
254 // which will be part of function mysql-fetch-field for backward compatibility
255 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
256 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
257 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
258 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
259 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
260 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
261 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
262 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
263 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
273 protected function mysqlFieldName( $res, $n ) {
274 $field = $res->fetch_field_direct( $n );
284 protected function mysqlFieldType( $res, $n ) {
285 $field = $res->fetch_field_direct( $n );
291 * @param mysqli_result $res
295 protected function mysqlDataSeek( $res, $row ) {
296 return $res->data_seek( $row );
300 * @param mysqli $conn Optional connection object
303 protected function mysqlError( $conn = null ) {
304 if ( $conn === null ) {
305 return mysqli_connect_error();
312 * Escapes special characters in a string for use in an SQL statement
316 protected function mysqlRealEscapeString( $s ) {
317 $conn = $this->getBindingHandle();
319 return $conn->real_escape_string( (string)$s );
323 * Give an id for the connection
325 * mysql driver used resource id, but mysqli objects cannot be cast to string.
328 public function __toString() {
329 if ( $this->mConn
instanceof mysqli
) {
330 return (string)$this->mConn
->thread_id
;
332 // mConn might be false or something.
333 return (string)$this->mConn
;
338 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );