database: Small DB class cleanups
[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 /** @var mysqli */
33 protected $mConn;
34
35 /**
36 * @param string $sql
37 * @return resource
38 */
39 protected function doQuery( $sql ) {
40 if ( $this->bufferResults() ) {
41 $ret = $this->mConn->query( $sql );
42 } else {
43 $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
44 }
45
46 return $ret;
47 }
48
49 /**
50 * @param string $realServer
51 * @return bool|mysqli
52 * @throws DBConnectionError
53 */
54 protected function mysqlConnect( $realServer ) {
55 global $wgDBmysql5;
56 # Fail now
57 # Otherwise we get a suppressed fatal error, which is very hard to track down
58 if ( !function_exists( 'mysqli_init' ) ) {
59 throw new DBConnectionError( $this, "MySQLi functions missing,"
60 . " have you compiled PHP with the --with-mysqli option?\n" );
61 }
62
63 // Other than mysql_connect, mysqli_real_connect expects an explicit port
64 // and socket parameters. So we need to parse the port and socket out of
65 // $realServer
66 $port = null;
67 $socket = null;
68 $hostAndPort = IP::splitHostAndPort( $realServer );
69 if ( $hostAndPort ) {
70 $realServer = $hostAndPort[0];
71 if ( $hostAndPort[1] ) {
72 $port = $hostAndPort[1];
73 }
74 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
75 // If we have a colon and something that's not a port number
76 // inside the hostname, assume it's the socket location
77 $hostAndSocket = explode( ':', $realServer );
78 $realServer = $hostAndSocket[0];
79 $socket = $hostAndSocket[1];
80 }
81
82 $connFlags = 0;
83 if ( $this->mFlags & DBO_SSL ) {
84 $connFlags |= MYSQLI_CLIENT_SSL;
85 }
86 if ( $this->mFlags & DBO_COMPRESS ) {
87 $connFlags |= MYSQLI_CLIENT_COMPRESS;
88 }
89 if ( $this->mFlags & DBO_PERSISTENT ) {
90 $realServer = 'p:' . $realServer;
91 }
92
93 $mysqli = mysqli_init();
94 if ( $wgDBmysql5 ) {
95 // Tell the server we're communicating with it in UTF-8.
96 // This may engage various charset conversions.
97 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
98 } else {
99 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
100 }
101 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
102
103 if ( $mysqli->real_connect( $realServer, $this->mUser,
104 $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
105 ) {
106 return $mysqli;
107 }
108
109 return false;
110 }
111
112 protected function connectInitCharset() {
113 // already done in mysqlConnect()
114 return true;
115 }
116
117 /**
118 * @param string $charset
119 * @return bool
120 */
121 protected function mysqlSetCharset( $charset ) {
122 if ( method_exists( $this->mConn, 'set_charset' ) ) {
123 return $this->mConn->set_charset( $charset );
124 } else {
125 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
126 }
127 }
128
129 /**
130 * @return bool
131 */
132 protected function closeConnection() {
133 return $this->mConn->close();
134 }
135
136 /**
137 * @return int
138 */
139 function insertId() {
140 return (int)$this->mConn->insert_id;
141 }
142
143 /**
144 * @return int
145 */
146 function lastErrno() {
147 if ( $this->mConn ) {
148 return $this->mConn->errno;
149 } else {
150 return mysqli_connect_errno();
151 }
152 }
153
154 /**
155 * @return int
156 */
157 function affectedRows() {
158 return $this->mConn->affected_rows;
159 }
160
161 /**
162 * @param string $db
163 * @return bool
164 */
165 function selectDB( $db ) {
166 $this->mDBname = $db;
167
168 return $this->mConn->select_db( $db );
169 }
170
171 /**
172 * @param mysqli $res
173 * @return bool
174 */
175 protected function mysqlFreeResult( $res ) {
176 $res->free_result();
177
178 return true;
179 }
180
181 /**
182 * @param mysqli $res
183 * @return bool
184 */
185 protected function mysqlFetchObject( $res ) {
186 $object = $res->fetch_object();
187 if ( $object === null ) {
188 return false;
189 }
190
191 return $object;
192 }
193
194 /**
195 * @param mysqli $res
196 * @return bool
197 */
198 protected function mysqlFetchArray( $res ) {
199 $array = $res->fetch_array();
200 if ( $array === null ) {
201 return false;
202 }
203
204 return $array;
205 }
206
207 /**
208 * @param mysqli $res
209 * @return mixed
210 */
211 protected function mysqlNumRows( $res ) {
212 return $res->num_rows;
213 }
214
215 /**
216 * @param mysqli $res
217 * @return mixed
218 */
219 protected function mysqlNumFields( $res ) {
220 return $res->field_count;
221 }
222
223 /**
224 * @param mysqli $res
225 * @param int $n
226 * @return mixed
227 */
228 protected function mysqlFetchField( $res, $n ) {
229 $field = $res->fetch_field_direct( $n );
230
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;
242
243 return $field;
244 }
245
246 /**
247 * @param resource|ResultWrapper $res
248 * @param int $n
249 * @return mixed
250 */
251 protected function mysqlFieldName( $res, $n ) {
252 $field = $res->fetch_field_direct( $n );
253
254 return $field->name;
255 }
256
257 /**
258 * @param resource|ResultWrapper $res
259 * @param int $n
260 * @return mixed
261 */
262 protected function mysqlFieldType( $res, $n ) {
263 $field = $res->fetch_field_direct( $n );
264
265 return $field->type;
266 }
267
268 /**
269 * @param resource|ResultWrapper $res
270 * @param int $row
271 * @return mixed
272 */
273 protected function mysqlDataSeek( $res, $row ) {
274 return $res->data_seek( $row );
275 }
276
277 /**
278 * @param mysqli $conn Optional connection object
279 * @return string
280 */
281 protected function mysqlError( $conn = null ) {
282 if ( $conn === null ) {
283 return mysqli_connect_error();
284 } else {
285 return $conn->error;
286 }
287 }
288
289 /**
290 * Escapes special characters in a string for use in an SQL statement
291 * @param string $s
292 * @return string
293 */
294 protected function mysqlRealEscapeString( $s ) {
295 return $this->mConn->real_escape_string( $s );
296 }
297
298 protected function mysqlPing() {
299 return $this->mConn->ping();
300 }
301
302 /**
303 * Give an id for the connection
304 *
305 * mysql driver used resource id, but mysqli objects cannot be cast to string.
306 * @return string
307 */
308 public function __toString() {
309 if ( $this->mConn instanceof Mysqli ) {
310 return (string)$this->mConn->thread_id;
311 } else {
312 // mConn might be false or something.
313 return (string)$this->mConn;
314 }
315 }
316 }