Postgres installation fixes
[mediawiki.git] / includes / libs / rdbms / database / DatabaseMysqli.php
blob2f27ff9736d28e8fd854bcb450c0e80220e88c17
1 <?php
2 /**
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
20 * @file
21 * @ingroup Database
24 /**
25 * Database abstraction object for PHP extension mysqli.
27 * @ingroup Database
28 * @since 1.22
29 * @see Database
31 class DatabaseMysqli extends DatabaseMysqlBase {
32 /** @var $mConn mysqli */
34 /**
35 * @param string $sql
36 * @return resource
38 protected function doQuery( $sql ) {
39 $conn = $this->getBindingHandle();
41 if ( $this->bufferResults() ) {
42 $ret = $conn->query( $sql );
43 } else {
44 $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
47 return $ret;
50 /**
51 * @param string $realServer
52 * @return bool|mysqli
53 * @throws DBConnectionError
55 protected function mysqlConnect( $realServer ) {
56 # Avoid suppressed fatal error, which is very hard to track down
57 if ( !function_exists( 'mysqli_init' ) ) {
58 throw new DBConnectionError( $this, "MySQLi functions missing,"
59 . " have you compiled PHP with the --with-mysqli option?\n" );
62 // Other than mysql_connect, mysqli_real_connect expects an explicit port
63 // and socket parameters. So we need to parse the port and socket out of
64 // $realServer
65 $port = null;
66 $socket = null;
67 $hostAndPort = IP::splitHostAndPort( $realServer );
68 if ( $hostAndPort ) {
69 $realServer = $hostAndPort[0];
70 if ( $hostAndPort[1] ) {
71 $port = $hostAndPort[1];
73 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
74 // If we have a colon and something that's not a port number
75 // inside the hostname, assume it's the socket location
76 $hostAndSocket = explode( ':', $realServer );
77 $realServer = $hostAndSocket[0];
78 $socket = $hostAndSocket[1];
81 $mysqli = mysqli_init();
83 $connFlags = 0;
84 if ( $this->mFlags & self::DBO_SSL ) {
85 $connFlags |= MYSQLI_CLIENT_SSL;
86 $mysqli->ssl_set(
87 $this->sslKeyPath,
88 $this->sslCertPath,
89 null,
90 $this->sslCAPath,
91 $this->sslCiphers
94 if ( $this->mFlags & self::DBO_COMPRESS ) {
95 $connFlags |= MYSQLI_CLIENT_COMPRESS;
97 if ( $this->mFlags & self::DBO_PERSISTENT ) {
98 $realServer = 'p:' . $realServer;
101 if ( $this->utf8Mode ) {
102 // Tell the server we're communicating with it in UTF-8.
103 // This may engage various charset conversions.
104 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
105 } else {
106 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
108 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
110 if ( $mysqli->real_connect( $realServer, $this->mUser,
111 $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
113 return $mysqli;
116 return false;
119 protected function connectInitCharset() {
120 // already done in mysqlConnect()
121 return true;
125 * @param string $charset
126 * @return bool
128 protected function mysqlSetCharset( $charset ) {
129 $conn = $this->getBindingHandle();
131 if ( method_exists( $conn, 'set_charset' ) ) {
132 return $conn->set_charset( $charset );
133 } else {
134 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
139 * @return bool
141 protected function closeConnection() {
142 $conn = $this->getBindingHandle();
144 return $conn->close();
148 * @return int
150 function insertId() {
151 $conn = $this->getBindingHandle();
153 return (int)$conn->insert_id;
157 * @return int
159 function lastErrno() {
160 if ( $this->mConn ) {
161 return $this->mConn->errno;
162 } else {
163 return mysqli_connect_errno();
168 * @return int
170 function affectedRows() {
171 $conn = $this->getBindingHandle();
173 return $conn->affected_rows;
177 * @param string $db
178 * @return bool
180 function selectDB( $db ) {
181 $conn = $this->getBindingHandle();
183 $this->mDBname = $db;
185 return $conn->select_db( $db );
189 * @param mysqli $res
190 * @return bool
192 protected function mysqlFreeResult( $res ) {
193 $res->free_result();
195 return true;
199 * @param mysqli $res
200 * @return bool
202 protected function mysqlFetchObject( $res ) {
203 $object = $res->fetch_object();
204 if ( $object === null ) {
205 return false;
208 return $object;
212 * @param mysqli $res
213 * @return bool
215 protected function mysqlFetchArray( $res ) {
216 $array = $res->fetch_array();
217 if ( $array === null ) {
218 return false;
221 return $array;
225 * @param mysqli $res
226 * @return mixed
228 protected function mysqlNumRows( $res ) {
229 return $res->num_rows;
233 * @param mysqli $res
234 * @return mixed
236 protected function mysqlNumFields( $res ) {
237 return $res->field_count;
241 * @param mysqli $res
242 * @param int $n
243 * @return mixed
245 protected function mysqlFetchField( $res, $n ) {
246 $field = $res->fetch_field_direct( $n );
248 // Add missing properties to result (using flags property)
249 // which will be part of function mysql-fetch-field for backward compatibility
250 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
251 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
252 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
253 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
254 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
255 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
256 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
257 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
258 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
260 return $field;
264 * @param resource|ResultWrapper $res
265 * @param int $n
266 * @return mixed
268 protected function mysqlFieldName( $res, $n ) {
269 $field = $res->fetch_field_direct( $n );
271 return $field->name;
275 * @param resource|ResultWrapper $res
276 * @param int $n
277 * @return mixed
279 protected function mysqlFieldType( $res, $n ) {
280 $field = $res->fetch_field_direct( $n );
282 return $field->type;
286 * @param resource|ResultWrapper $res
287 * @param int $row
288 * @return mixed
290 protected function mysqlDataSeek( $res, $row ) {
291 return $res->data_seek( $row );
295 * @param mysqli $conn Optional connection object
296 * @return string
298 protected function mysqlError( $conn = null ) {
299 if ( $conn === null ) {
300 return mysqli_connect_error();
301 } else {
302 return $conn->error;
307 * Escapes special characters in a string for use in an SQL statement
308 * @param string $s
309 * @return string
311 protected function mysqlRealEscapeString( $s ) {
312 $conn = $this->getBindingHandle();
314 return $conn->real_escape_string( $s );
318 * Give an id for the connection
320 * mysql driver used resource id, but mysqli objects cannot be cast to string.
321 * @return string
323 public function __toString() {
324 if ( $this->mConn instanceof mysqli ) {
325 return (string)$this->mConn->thread_id;
326 } else {
327 // mConn might be false or something.
328 return (string)$this->mConn;