Remove "related" searches
[mediawiki.git] / includes / db / DatabaseMysqli.php
blob2ce6307918ee233106395c2bb0a06f4c7db8236b
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 /**
33 * @param string $sql
34 * @return resource
36 protected function doQuery( $sql ) {
37 if ( $this->bufferResults() ) {
38 $ret = $this->mConn->query( $sql );
39 } else {
40 $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
43 return $ret;
46 /**
47 * @param string $realServer
48 * @return bool|mysqli
49 * @throws DBConnectionError
51 protected function mysqlConnect( $realServer ) {
52 global $wgDBmysql5;
53 # Fail now
54 # Otherwise we get a suppressed fatal error, which is very hard to track down
55 if ( !function_exists( 'mysqli_init' ) ) {
56 throw new DBConnectionError( $this, "MySQLi functions missing,"
57 . " have you compiled PHP with the --with-mysqli option?\n" );
60 // Other than mysql_connect, mysqli_real_connect expects an explicit port
61 // parameter. So we need to parse the port out of $realServer
62 $port = null;
63 $hostAndPort = IP::splitHostAndPort( $realServer );
64 if ( $hostAndPort ) {
65 $realServer = $hostAndPort[0];
66 if ( $hostAndPort[1] ) {
67 $port = $hostAndPort[1];
71 $connFlags = 0;
72 if ( $this->mFlags & DBO_SSL ) {
73 $connFlags |= MYSQLI_CLIENT_SSL;
75 if ( $this->mFlags & DBO_COMPRESS ) {
76 $connFlags |= MYSQLI_CLIENT_COMPRESS;
78 if ( $this->mFlags & DBO_PERSISTENT ) {
79 $realServer = 'p:' . $realServer;
82 $mysqli = mysqli_init();
83 if ( $wgDBmysql5 ) {
84 // Tell the server we're communicating with it in UTF-8.
85 // This may engage various charset conversions.
86 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
87 } else {
88 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
90 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
92 if ( $mysqli->real_connect( $realServer, $this->mUser,
93 $this->mPassword, $this->mDBname, $port, null, $connFlags )
94 ) {
95 return $mysqli;
98 return false;
101 protected function connectInitCharset() {
102 // already done in mysqlConnect()
103 return true;
107 * @param string $charset
108 * @return bool
110 protected function mysqlSetCharset( $charset ) {
111 if ( method_exists( $this->mConn, 'set_charset' ) ) {
112 return $this->mConn->set_charset( $charset );
113 } else {
114 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
119 * @return bool
121 protected function closeConnection() {
122 return $this->mConn->close();
126 * @return int
128 function insertId() {
129 return $this->mConn->insert_id;
133 * @return int
135 function lastErrno() {
136 if ( $this->mConn ) {
137 return $this->mConn->errno;
138 } else {
139 return mysqli_connect_errno();
144 * @return int
146 function affectedRows() {
147 return $this->mConn->affected_rows;
151 * @param string $db
152 * @return bool
154 function selectDB( $db ) {
155 $this->mDBname = $db;
157 return $this->mConn->select_db( $db );
161 * @return string
163 function getServerVersion() {
164 return $this->mConn->server_info;
168 * @param mysqli $res
169 * @return bool
171 protected function mysqlFreeResult( $res ) {
172 $res->free_result();
174 return true;
178 * @param mysqli $res
179 * @return bool
181 protected function mysqlFetchObject( $res ) {
182 $object = $res->fetch_object();
183 if ( $object === null ) {
184 return false;
187 return $object;
191 * @param mysqli $res
192 * @return bool
194 protected function mysqlFetchArray( $res ) {
195 $array = $res->fetch_array();
196 if ( $array === null ) {
197 return false;
200 return $array;
204 * @param mysqli $res
205 * @return mixed
207 protected function mysqlNumRows( $res ) {
208 return $res->num_rows;
212 * @param mysqli $res
213 * @return mixed
215 protected function mysqlNumFields( $res ) {
216 return $res->field_count;
220 * @param mysqli $res
221 * @param int $n
222 * @return mixed
224 protected function mysqlFetchField( $res, $n ) {
225 $field = $res->fetch_field_direct( $n );
226 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
227 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
228 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
229 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
230 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
232 return $field;
236 * @param resource|ResultWrapper $res
237 * @param int $n
238 * @return mixed
240 protected function mysqlFieldName( $res, $n ) {
241 $field = $res->fetch_field_direct( $n );
243 return $field->name;
247 * @param resource|ResultWrapper $res
248 * @param int $n
249 * @return mixed
251 protected function mysqlFieldType( $res, $n ) {
252 $field = $res->fetch_field_direct( $n );
254 return $field->type;
258 * @param resource|ResultWrapper $res
259 * @param int $row
260 * @return mixed
262 protected function mysqlDataSeek( $res, $row ) {
263 return $res->data_seek( $row );
267 * @param mysqli $conn Optional connection object
268 * @return string
270 protected function mysqlError( $conn = null ) {
271 if ( $conn === null ) {
272 return mysqli_connect_error();
273 } else {
274 return $conn->error;
279 * Escapes special characters in a string for use in an SQL statement
280 * @param string $s
281 * @return string
283 protected function mysqlRealEscapeString( $s ) {
284 return $this->mConn->real_escape_string( $s );
287 protected function mysqlPing() {
288 return $this->mConn->ping();
292 * Give an id for the connection
294 * mysql driver used resource id, but mysqli objects cannot be cast to string.
295 * @return string
297 public function __toString() {
298 if ( $this->mConn instanceof Mysqli ) {
299 return (string)$this->mConn->thread_id;
300 } else {
301 // mConn might be false or something.
302 return (string)$this->mConn;