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.
36 * @phan-file-suppress PhanParamSignatureMismatch resource vs mysqli_result
38 class DatabaseMysqli
extends DatabaseMysqlBase
{
41 * @return mysqli_result|bool
43 protected function doQuery( $sql ) {
44 return $this->getBindingHandle()->query( $sql );
48 * @param string $realServer
49 * @param string|null $dbName
51 * @throws DBConnectionError
53 protected function mysqlConnect( $realServer, $dbName ) {
54 if ( !function_exists( 'mysqli_init' ) ) {
55 throw $this->newExceptionAfterConnectError(
56 "MySQLi functions missing, have you compiled PHP with the --with-mysqli option?"
60 // Other than mysql_connect, mysqli_real_connect expects an explicit port number
61 // e.g. "localhost:1234" or "127.0.0.1:1234"
62 // or Unix domain socket path
63 // e.g. "localhost:/socket_path" or "localhost:/foo/bar:bar:bar"
64 // colons are known to be used by Google AppEngine,
65 // see <https://cloud.google.com/sql/docs/mysql/connect-app-engine>
67 // We need to parse the port or socket path out of $realServer
70 $hostAndPort = IP
::splitHostAndPort( $realServer );
72 $realServer = $hostAndPort[0];
73 if ( $hostAndPort[1] ) {
74 $port = $hostAndPort[1];
76 } elseif ( substr_count( $realServer, ':/' ) == 1 ) {
77 // If we have a colon slash instead of a colon and a port number
78 // after the ip or hostname, assume it's the Unix domain socket path
79 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
82 $mysqli = mysqli_init();
83 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
84 // of whether any column values changed. This is what callers want to know and is
85 // consistent with what Postgres, SQLite, and SQL Server return.
86 $connFlags = MYSQLI_CLIENT_FOUND_ROWS
;
87 if ( $this->getFlag( self
::DBO_SSL
) ) {
88 $connFlags |
= MYSQLI_CLIENT_SSL
;
97 if ( $this->getFlag( self
::DBO_COMPRESS
) ) {
98 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
100 if ( $this->getFlag( 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(
131 protected function closeConnection() {
132 $conn = $this->getBindingHandle();
134 return $conn->close();
140 function insertId() {
141 $conn = $this->getBindingHandle();
143 return (int)$conn->insert_id
;
149 function lastErrno() {
150 if ( $this->conn
instanceof mysqli
) {
151 return $this->conn
->errno
;
153 return mysqli_connect_errno();
160 protected function fetchAffectedRowCount() {
161 $conn = $this->getBindingHandle();
163 return $conn->affected_rows
;
167 * @param mysqli_result $res
170 protected function mysqlFreeResult( $res ) {
177 * @param mysqli_result $res
178 * @return stdClass|bool
180 protected function mysqlFetchObject( $res ) {
181 $object = $res->fetch_object();
182 if ( $object === null ) {
190 * @param mysqli_result $res
191 * @return array|false
193 protected function mysqlFetchArray( $res ) {
194 $array = $res->fetch_array();
195 if ( $array === null ) {
203 * @param mysqli_result $res
206 protected function mysqlNumRows( $res ) {
207 return $res->num_rows
;
211 * @param mysqli_result $res
214 protected function mysqlNumFields( $res ) {
215 return $res->field_count
;
219 * @param mysqli_result $res
223 protected function mysqlFetchField( $res, $n ) {
224 $field = $res->fetch_field_direct( $n );
226 // Add missing properties to result (using flags property)
227 // which will be part of function mysql-fetch-field for backward compatibility
228 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
229 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
230 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
231 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
232 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
233 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
234 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
235 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
236 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
242 * @param mysqli_result $res
246 protected function mysqlFieldName( $res, $n ) {
247 $field = $res->fetch_field_direct( $n );
253 * @param mysqli_result $res
257 protected function mysqlFieldType( $res, $n ) {
258 $field = $res->fetch_field_direct( $n );
264 * @param mysqli_result $res
268 protected function mysqlDataSeek( $res, $row ) {
269 return $res->data_seek( $row );
273 * @param mysqli|null $conn Optional connection object
276 protected function mysqlError( $conn = null ) {
277 if ( $conn === null ) {
278 return mysqli_connect_error();
285 * Escapes special characters in a string for use in an SQL statement
289 protected function mysqlRealEscapeString( $s ) {
290 $conn = $this->getBindingHandle();
292 return $conn->real_escape_string( (string)$s );
298 protected function getBindingHandle() {
299 return parent
::getBindingHandle();
304 * @deprecated since 1.29
306 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );