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 return $this->getBindingHandle()->query( $sql );
47 * @param string $realServer
48 * @param string|null $dbName
50 * @throws DBConnectionError
52 protected function mysqlConnect( $realServer, $dbName ) {
53 if ( !function_exists( 'mysqli_init' ) ) {
54 throw $this->newExceptionAfterConnectError(
55 "MySQLi functions missing, have you compiled PHP with the --with-mysqli option?"
59 // Other than mysql_connect, mysqli_real_connect expects an explicit port number
60 // e.g. "localhost:1234" or "127.0.0.1:1234"
61 // or Unix domain socket path
62 // e.g. "localhost:/socket_path" or "localhost:/foo/bar:bar:bar"
63 // colons are known to be used by Google AppEngine,
64 // see <https://cloud.google.com/sql/docs/mysql/connect-app-engine>
66 // We need to parse the port or socket path out of $realServer
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 slash instead of a colon and a port number
77 // after the ip or hostname, assume it's the Unix domain socket path
78 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
81 $mysqli = mysqli_init();
82 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
83 // of whether any column values changed. This is what callers want to know and is
84 // consistent with what Postgres, SQLite, and SQL Server return.
85 $connFlags = MYSQLI_CLIENT_FOUND_ROWS
;
86 if ( $this->getFlag( self
::DBO_SSL
) ) {
87 $connFlags |
= MYSQLI_CLIENT_SSL
;
96 if ( $this->getFlag( self
::DBO_COMPRESS
) ) {
97 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
99 if ( $this->getFlag( 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(
130 protected function closeConnection() {
131 $conn = $this->getBindingHandle();
133 return $conn->close();
139 function insertId() {
140 $conn = $this->getBindingHandle();
142 return (int)$conn->insert_id
;
148 function lastErrno() {
149 if ( $this->conn
instanceof mysqli
) {
150 return $this->conn
->errno
;
152 return mysqli_connect_errno();
159 protected function fetchAffectedRowCount() {
160 $conn = $this->getBindingHandle();
162 return $conn->affected_rows
;
166 * @param mysqli_result $res
169 protected function mysqlFreeResult( $res ) {
176 * @param mysqli_result $res
177 * @return stdClass|bool
179 protected function mysqlFetchObject( $res ) {
180 $object = $res->fetch_object();
181 if ( $object === null ) {
189 * @param mysqli_result $res
190 * @return array|false
192 protected function mysqlFetchArray( $res ) {
193 $array = $res->fetch_array();
194 if ( $array === null ) {
202 * @param mysqli_result $res
205 protected function mysqlNumRows( $res ) {
206 return $res->num_rows
;
210 * @param mysqli_result $res
213 protected function mysqlNumFields( $res ) {
214 return $res->field_count
;
218 * @param mysqli_result $res
222 protected function mysqlFetchField( $res, $n ) {
223 $field = $res->fetch_field_direct( $n );
225 // Add missing properties to result (using flags property)
226 // which will be part of function mysql-fetch-field for backward compatibility
227 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
228 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
229 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
230 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
231 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
232 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
233 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
234 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
235 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
241 * @param mysqli_result $res
245 protected function mysqlFieldName( $res, $n ) {
246 $field = $res->fetch_field_direct( $n );
252 * @param mysqli_result $res
256 protected function mysqlFieldType( $res, $n ) {
257 $field = $res->fetch_field_direct( $n );
263 * @param mysqli_result $res
267 protected function mysqlDataSeek( $res, $row ) {
268 return $res->data_seek( $row );
272 * @param mysqli|null $conn Optional connection object
275 protected function mysqlError( $conn = null ) {
276 if ( $conn === null ) {
277 return mysqli_connect_error();
284 * Escapes special characters in a string for use in an SQL statement
288 protected function mysqlRealEscapeString( $s ) {
289 $conn = $this->getBindingHandle();
291 return $conn->real_escape_string( (string)$s );
297 protected function getBindingHandle() {
298 return parent
::getBindingHandle();
303 * @deprecated since 1.29
305 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );