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
;
29 use Wikimedia\AtEase\AtEase
;
32 * Database abstraction object for PHP extension mysqli.
37 * @phan-file-suppress PhanParamSignatureMismatch resource vs mysqli_result
39 class DatabaseMysqli
extends DatabaseMysqlBase
{
42 * @return mysqli_result|bool
44 protected function doQuery( $sql ) {
45 AtEase
::suppressWarnings();
46 $res = $this->getBindingHandle()->query( $sql );
47 AtEase
::restoreWarnings();
53 * @param string $realServer
54 * @param string|null $dbName
56 * @throws DBConnectionError
58 protected function mysqlConnect( $realServer, $dbName ) {
59 if ( !function_exists( 'mysqli_init' ) ) {
60 throw $this->newExceptionAfterConnectError(
61 "MySQLi functions missing, have you compiled PHP with the --with-mysqli option?"
65 // Other than mysql_connect, mysqli_real_connect expects an explicit port number
66 // e.g. "localhost:1234" or "127.0.0.1:1234"
67 // or Unix domain socket path
68 // e.g. "localhost:/socket_path" or "localhost:/foo/bar:bar:bar"
69 // colons are known to be used by Google AppEngine,
70 // see <https://cloud.google.com/sql/docs/mysql/connect-app-engine>
72 // We need to parse the port or socket path out of $realServer
75 $hostAndPort = IP
::splitHostAndPort( $realServer );
77 $realServer = $hostAndPort[0];
78 if ( $hostAndPort[1] ) {
79 $port = $hostAndPort[1];
81 } elseif ( substr_count( $realServer, ':/' ) == 1 ) {
82 // If we have a colon slash instead of a colon and a port number
83 // after the ip or hostname, assume it's the Unix domain socket path
84 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
87 $mysqli = mysqli_init();
88 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
89 // of whether any column values changed. This is what callers want to know and is
90 // consistent with what Postgres, SQLite, and SQL Server return.
91 $connFlags = MYSQLI_CLIENT_FOUND_ROWS
;
92 if ( $this->getFlag( self
::DBO_SSL
) ) {
93 $connFlags |
= MYSQLI_CLIENT_SSL
;
102 if ( $this->getFlag( self
::DBO_COMPRESS
) ) {
103 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
105 if ( $this->getFlag( self
::DBO_PERSISTENT
) ) {
106 $realServer = 'p:' . $realServer;
109 if ( $this->utf8Mode
) {
110 // Tell the server we're communicating with it in UTF-8.
111 // This may engage various charset conversions.
112 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
114 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
116 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
118 if ( $mysqli->real_connect(
136 protected function closeConnection() {
137 $conn = $this->getBindingHandle();
139 return $conn->close();
145 function insertId() {
146 $conn = $this->getBindingHandle();
148 return (int)$conn->insert_id
;
154 function lastErrno() {
155 if ( $this->conn
instanceof mysqli
) {
156 return $this->conn
->errno
;
158 return mysqli_connect_errno();
165 protected function fetchAffectedRowCount() {
166 $conn = $this->getBindingHandle();
168 return $conn->affected_rows
;
172 * @param mysqli_result $res
175 protected function mysqlFreeResult( $res ) {
182 * @param mysqli_result $res
183 * @return stdClass|bool
185 protected function mysqlFetchObject( $res ) {
186 $object = $res->fetch_object();
187 if ( $object === null ) {
195 * @param mysqli_result $res
196 * @return array|false
198 protected function mysqlFetchArray( $res ) {
199 $array = $res->fetch_array();
200 if ( $array === null ) {
208 * @param mysqli_result $res
211 protected function mysqlNumRows( $res ) {
212 return $res->num_rows
;
216 * @param mysqli_result $res
219 protected function mysqlNumFields( $res ) {
220 return $res->field_count
;
224 * @param mysqli_result $res
228 protected function mysqlFetchField( $res, $n ) {
229 $field = $res->fetch_field_direct( $n );
231 // Add missing properties to result (using flags property)
232 // which will be part of function mysql-fetch-field for backward compatibility
233 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
234 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
235 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
236 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
237 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
238 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
239 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
240 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
241 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
247 * @param mysqli_result $res
251 protected function mysqlFieldName( $res, $n ) {
252 $field = $res->fetch_field_direct( $n );
258 * @param mysqli_result $res
262 protected function mysqlFieldType( $res, $n ) {
263 $field = $res->fetch_field_direct( $n );
269 * @param mysqli_result $res
273 protected function mysqlDataSeek( $res, $row ) {
274 return $res->data_seek( $row );
278 * @param mysqli|null $conn Optional connection object
281 protected function mysqlError( $conn = null ) {
282 if ( $conn === null ) {
283 return mysqli_connect_error();
290 * Escapes special characters in a string for use in an SQL statement
294 protected function mysqlRealEscapeString( $s ) {
295 $conn = $this->getBindingHandle();
297 return $conn->real_escape_string( (string)$s );
303 protected function getBindingHandle() {
304 return parent
::getBindingHandle();
309 * @deprecated since 1.29
311 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );