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
;
31 * Database abstraction object for PHP extension mysqli.
37 class DatabaseMysqli
extends DatabaseMysqlBase
{
40 * @return mysqli_result|bool
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
56 * @param string|null $dbName
58 * @throws DBConnectionError
60 protected function mysqlConnect( $realServer, $dbName ) {
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 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
84 $mysqli = mysqli_init();
87 if ( $this->flags
& self
::DBO_SSL
) {
88 $connFlags |
= MYSQLI_CLIENT_SSL
;
97 if ( $this->flags
& self
::DBO_COMPRESS
) {
98 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
100 if ( $this->flags
& self
::DBO_PERSISTENT
) {
101 $realServer = 'p:' . $realServer;
104 if ( $this->utf8Mode
) {
105 // Tell the server we're communicating with it in UTF-8.
106 // This may engage various charset conversions.
107 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
111 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
113 if ( $mysqli->real_connect(
128 protected function connectInitCharset() {
129 // already done in mysqlConnect()
134 * @param string $charset
137 protected function mysqlSetCharset( $charset ) {
138 $conn = $this->getBindingHandle();
140 return $conn->set_charset( $charset );
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->conn
instanceof mysqli
) {
166 return $this->conn
->errno
;
168 return mysqli_connect_errno();
175 protected function fetchAffectedRowCount() {
176 $conn = $this->getBindingHandle();
178 return $conn->affected_rows
;
181 function doSelectDomain( DatabaseDomain
$domain ) {
182 if ( $domain->getSchema() !== null ) {
183 throw new DBExpectedError( $this, __CLASS__
. ": domain schemas are not supported." );
186 $database = $domain->getDatabase();
187 if ( $database !== $this->getDBname() ) {
188 $conn = $this->getBindingHandle();
189 if ( !$conn->select_db( $database ) ) {
190 throw new DBExpectedError( $this, "Could not select database '$database'." );
194 // Update that domain fields on success (no exception thrown)
195 $this->currentDomain
= $domain;
201 * @param mysqli_result $res
204 protected function mysqlFreeResult( $res ) {
211 * @param mysqli_result $res
212 * @return stdClass|bool
214 protected function mysqlFetchObject( $res ) {
215 $object = $res->fetch_object();
216 if ( $object === null ) {
224 * @param mysqli_result $res
227 protected function mysqlFetchArray( $res ) {
228 $array = $res->fetch_array();
229 if ( $array === null ) {
237 * @param mysqli_result $res
240 protected function mysqlNumRows( $res ) {
241 return $res->num_rows
;
245 * @param mysqli_result $res
248 protected function mysqlNumFields( $res ) {
249 return $res->field_count
;
253 * @param mysqli_result $res
257 protected function mysqlFetchField( $res, $n ) {
258 $field = $res->fetch_field_direct( $n );
260 // Add missing properties to result (using flags property)
261 // which will be part of function mysql-fetch-field for backward compatibility
262 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
263 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
264 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
265 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
266 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
267 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
268 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
269 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
270 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
276 * @param mysqli_result $res
280 protected function mysqlFieldName( $res, $n ) {
281 $field = $res->fetch_field_direct( $n );
287 * @param mysqli_result $res
291 protected function mysqlFieldType( $res, $n ) {
292 $field = $res->fetch_field_direct( $n );
298 * @param mysqli_result $res
302 protected function mysqlDataSeek( $res, $row ) {
303 return $res->data_seek( $row );
307 * @param mysqli|null $conn Optional connection object
310 protected function mysqlError( $conn = null ) {
311 if ( $conn === null ) {
312 return mysqli_connect_error();
319 * Escapes special characters in a string for use in an SQL statement
323 protected function mysqlRealEscapeString( $s ) {
324 $conn = $this->getBindingHandle();
326 return $conn->real_escape_string( (string)$s );
330 * Give an id for the connection
332 * mysql driver used resource id, but mysqli objects cannot be cast to string.
335 public function __toString() {
336 if ( $this->conn
instanceof mysqli
) {
337 return (string)$this->conn
->thread_id
;
339 // mConn might be false or something.
340 return (string)$this->conn
;
347 protected function getBindingHandle() {
348 return parent
::getBindingHandle();
353 * @deprecated since 1.29
355 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );