Update OOjs UI to v0.1.0-pre (0fbf6bd14e)
[lhc/web/wiklou.git] / includes / db / DatabaseMysqli.php
1 <?php
2 /**
3 * This is the MySQLi database abstraction layer.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database abstraction object for PHP extension mysqli.
26 *
27 * @ingroup Database
28 * @since 1.22
29 * @see Database
30 */
31 class DatabaseMysqli extends DatabaseMysqlBase {
32 /**
33 * @param string $sql
34 * @return resource
35 */
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 );
41 }
42
43 return $ret;
44 }
45
46 /**
47 * @param string $realServer
48 * @return bool|mysqli
49 * @throws DBConnectionError
50 */
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" );
58 }
59
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];
68 }
69 }
70
71 $connFlags = 0;
72 if ( $this->mFlags & DBO_SSL ) {
73 $connFlags |= MYSQLI_CLIENT_SSL;
74 }
75 if ( $this->mFlags & DBO_COMPRESS ) {
76 $connFlags |= MYSQLI_CLIENT_COMPRESS;
77 }
78 if ( $this->mFlags & DBO_PERSISTENT ) {
79 $realServer = 'p:' . $realServer;
80 }
81
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' );
89 }
90
91 $numAttempts = 2;
92 for ( $i = 0; $i < $numAttempts; $i++ ) {
93 if ( $i > 1 ) {
94 usleep( 1000 );
95 }
96 if ( $mysqli->real_connect( $realServer, $this->mUser,
97 $this->mPassword, $this->mDBname, $port, null, $connFlags )
98 ) {
99 return $mysqli;
100 }
101 }
102
103 return false;
104 }
105
106 protected function connectInitCharset() {
107 // already done in mysqlConnect()
108 return true;
109 }
110
111 /**
112 * @param string $charset
113 * @return bool
114 */
115 protected function mysqlSetCharset( $charset ) {
116 if ( method_exists( $this->mConn, 'set_charset' ) ) {
117 return $this->mConn->set_charset( $charset );
118 } else {
119 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
120 }
121 }
122
123 /**
124 * @return bool
125 */
126 protected function closeConnection() {
127 return $this->mConn->close();
128 }
129
130 /**
131 * @return int
132 */
133 function insertId() {
134 return $this->mConn->insert_id;
135 }
136
137 /**
138 * @return int
139 */
140 function lastErrno() {
141 if ( $this->mConn ) {
142 return $this->mConn->errno;
143 } else {
144 return mysqli_connect_errno();
145 }
146 }
147
148 /**
149 * @return int
150 */
151 function affectedRows() {
152 return $this->mConn->affected_rows;
153 }
154
155 /**
156 * @param string $db
157 * @return bool
158 */
159 function selectDB( $db ) {
160 $this->mDBname = $db;
161
162 return $this->mConn->select_db( $db );
163 }
164
165 /**
166 * @return string
167 */
168 function getServerVersion() {
169 return $this->mConn->server_info;
170 }
171
172 /**
173 * @param mysqli $res
174 * @return bool
175 */
176 protected function mysqlFreeResult( $res ) {
177 $res->free_result();
178
179 return true;
180 }
181
182 /**
183 * @param mysqli $res
184 * @return bool
185 */
186 protected function mysqlFetchObject( $res ) {
187 $object = $res->fetch_object();
188 if ( $object === null ) {
189 return false;
190 }
191
192 return $object;
193 }
194
195 /**
196 * @param mysqli $res
197 * @return bool
198 */
199 protected function mysqlFetchArray( $res ) {
200 $array = $res->fetch_array();
201 if ( $array === null ) {
202 return false;
203 }
204
205 return $array;
206 }
207
208 /**
209 * @param mysqli $res
210 * @return mixed
211 */
212 protected function mysqlNumRows( $res ) {
213 return $res->num_rows;
214 }
215
216 /**
217 * @param mysqli $res
218 * @return mixed
219 */
220 protected function mysqlNumFields( $res ) {
221 return $res->field_count;
222 }
223
224 /**
225 * @param mysqli $res
226 * @param int $n
227 * @return mixed
228 */
229 protected function mysqlFetchField( $res, $n ) {
230 $field = $res->fetch_field_direct( $n );
231 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
232 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
233 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
234 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
235 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
236
237 return $field;
238 }
239
240 /**
241 * @param resource|ResultWrapper $res
242 * @param int $n
243 * @return mixed
244 */
245 protected function mysqlFieldName( $res, $n ) {
246 $field = $res->fetch_field_direct( $n );
247
248 return $field->name;
249 }
250
251 /**
252 * @param resource|ResultWrapper $res
253 * @param int $n
254 * @return mixed
255 */
256 protected function mysqlFieldType( $res, $n ) {
257 $field = $res->fetch_field_direct( $n );
258
259 return $field->type;
260 }
261
262 /**
263 * @param resource|ResultWrapper $res
264 * @param int $row
265 * @return mixed
266 */
267 protected function mysqlDataSeek( $res, $row ) {
268 return $res->data_seek( $row );
269 }
270
271 /**
272 * @param mysqli $conn Optional connection object
273 * @return string
274 */
275 protected function mysqlError( $conn = null ) {
276 if ( $conn === null ) {
277 return mysqli_connect_error();
278 } else {
279 return $conn->error;
280 }
281 }
282
283 /**
284 * Escapes special characters in a string for use in an SQL statement
285 * @param string $s
286 * @return string
287 */
288 protected function mysqlRealEscapeString( $s ) {
289 return $this->mConn->real_escape_string( $s );
290 }
291
292 protected function mysqlPing() {
293 return $this->mConn->ping();
294 }
295
296 /**
297 * Give an id for the connection
298 *
299 * mysql driver used resource id, but mysqli objects cannot be cast to string.
300 */
301 public function __toString() {
302 if ( $this->mConn instanceof Mysqli ) {
303 return (string)$this->mConn->thread_id;
304 } else {
305 // mConn might be false or something.
306 return (string)$this->mConn;
307 }
308 }
309 }