d32a12eb44006e6dee4379b77ea52d14a79e77a5
[lhc/web/wiklou.git] / includes / libs / rdbms / database / 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 namespace Wikimedia\Rdbms;
24
25 use mysqli;
26 use mysqli_result;
27 use IP;
28 use stdClass;
29
30 /**
31 * Database abstraction object for PHP extension mysqli.
32 *
33 * @ingroup Database
34 * @since 1.22
35 * @see Database
36 */
37 class DatabaseMysqli extends DatabaseMysqlBase {
38 /**
39 * @param string $sql
40 * @return mysqli_result|bool
41 */
42 protected function doQuery( $sql ) {
43 $conn = $this->getBindingHandle();
44
45 if ( $this->getFlag( self::DBO_NOBUFFER ) ) {
46 $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
47 } else {
48 $ret = $conn->query( $sql );
49 }
50
51 return $ret;
52 }
53
54 /**
55 * @param string $realServer
56 * @param string|null $dbName
57 * @return mysqli|null
58 * @throws DBConnectionError
59 */
60 protected function mysqlConnect( $realServer, $dbName ) {
61 if ( !function_exists( 'mysqli_init' ) ) {
62 throw $this->newExceptionAfterConnectError(
63 "MySQLi functions missing, have you compiled PHP with the --with-mysqli option?"
64 );
65 }
66
67 // Other than mysql_connect, mysqli_real_connect expects an explicit port
68 // and socket parameters. So we need to parse the port and socket out of
69 // $realServer
70 $port = null;
71 $socket = null;
72 $hostAndPort = IP::splitHostAndPort( $realServer );
73 if ( $hostAndPort ) {
74 $realServer = $hostAndPort[0];
75 if ( $hostAndPort[1] ) {
76 $port = $hostAndPort[1];
77 }
78 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
79 // If we have a colon and something that's not a port number
80 // inside the hostname, assume it's the socket location
81 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
82 }
83
84 $mysqli = mysqli_init();
85 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
86 // of whether any column values changed. This is what callers want to know and is
87 // consistent with what Postgres, SQLite, and SQL Server return.
88 $connFlags = MYSQLI_CLIENT_FOUND_ROWS;
89 if ( $this->getFlag( self::DBO_SSL ) ) {
90 $connFlags |= MYSQLI_CLIENT_SSL;
91 $mysqli->ssl_set(
92 $this->sslKeyPath,
93 $this->sslCertPath,
94 $this->sslCAFile,
95 $this->sslCAPath,
96 $this->sslCiphers
97 );
98 }
99 if ( $this->getFlag( self::DBO_COMPRESS ) ) {
100 $connFlags |= MYSQLI_CLIENT_COMPRESS;
101 }
102 if ( $this->getFlag( self::DBO_PERSISTENT ) ) {
103 $realServer = 'p:' . $realServer;
104 }
105
106 if ( $this->utf8Mode ) {
107 // Tell the server we're communicating with it in UTF-8.
108 // This may engage various charset conversions.
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
110 } else {
111 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
112 }
113 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
114
115 if ( $mysqli->real_connect(
116 $realServer,
117 $this->user,
118 $this->password,
119 $dbName,
120 $port,
121 $socket,
122 $connFlags
123 ) ) {
124 return $mysqli;
125 }
126
127 return null;
128 }
129
130 /**
131 * @return bool
132 */
133 protected function closeConnection() {
134 $conn = $this->getBindingHandle();
135
136 return $conn->close();
137 }
138
139 /**
140 * @return int
141 */
142 function insertId() {
143 $conn = $this->getBindingHandle();
144
145 return (int)$conn->insert_id;
146 }
147
148 /**
149 * @return int
150 */
151 function lastErrno() {
152 if ( $this->conn instanceof mysqli ) {
153 return $this->conn->errno;
154 } else {
155 return mysqli_connect_errno();
156 }
157 }
158
159 /**
160 * @return int
161 */
162 protected function fetchAffectedRowCount() {
163 $conn = $this->getBindingHandle();
164
165 return $conn->affected_rows;
166 }
167
168 /**
169 * @param mysqli_result $res
170 * @return bool
171 */
172 protected function mysqlFreeResult( $res ) {
173 $res->free_result();
174
175 return true;
176 }
177
178 /**
179 * @param mysqli_result $res
180 * @return stdClass|bool
181 */
182 protected function mysqlFetchObject( $res ) {
183 $object = $res->fetch_object();
184 if ( $object === null ) {
185 return false;
186 }
187
188 return $object;
189 }
190
191 /**
192 * @param mysqli_result $res
193 * @return array|false
194 */
195 protected function mysqlFetchArray( $res ) {
196 $array = $res->fetch_array();
197 if ( $array === null ) {
198 return false;
199 }
200
201 return $array;
202 }
203
204 /**
205 * @param mysqli_result $res
206 * @return mixed
207 */
208 protected function mysqlNumRows( $res ) {
209 return $res->num_rows;
210 }
211
212 /**
213 * @param mysqli_result $res
214 * @return mixed
215 */
216 protected function mysqlNumFields( $res ) {
217 return $res->field_count;
218 }
219
220 /**
221 * @param mysqli_result $res
222 * @param int $n
223 * @return mixed
224 */
225 protected function mysqlFetchField( $res, $n ) {
226 $field = $res->fetch_field_direct( $n );
227
228 // Add missing properties to result (using flags property)
229 // which will be part of function mysql-fetch-field for backward compatibility
230 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
231 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
232 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
233 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
234 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
235 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
236 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
237 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
238 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
239
240 return $field;
241 }
242
243 /**
244 * @param mysqli_result $res
245 * @param int $n
246 * @return mixed
247 */
248 protected function mysqlFieldName( $res, $n ) {
249 $field = $res->fetch_field_direct( $n );
250
251 return $field->name;
252 }
253
254 /**
255 * @param mysqli_result $res
256 * @param int $n
257 * @return mixed
258 */
259 protected function mysqlFieldType( $res, $n ) {
260 $field = $res->fetch_field_direct( $n );
261
262 return $field->type;
263 }
264
265 /**
266 * @param mysqli_result $res
267 * @param int $row
268 * @return mixed
269 */
270 protected function mysqlDataSeek( $res, $row ) {
271 return $res->data_seek( $row );
272 }
273
274 /**
275 * @param mysqli|null $conn Optional connection object
276 * @return string
277 */
278 protected function mysqlError( $conn = null ) {
279 if ( $conn === null ) {
280 return mysqli_connect_error();
281 } else {
282 return $conn->error;
283 }
284 }
285
286 /**
287 * Escapes special characters in a string for use in an SQL statement
288 * @param string $s
289 * @return string
290 */
291 protected function mysqlRealEscapeString( $s ) {
292 $conn = $this->getBindingHandle();
293
294 return $conn->real_escape_string( (string)$s );
295 }
296
297 /**
298 * @return mysqli
299 */
300 protected function getBindingHandle() {
301 return parent::getBindingHandle();
302 }
303 }
304
305 /**
306 * @deprecated since 1.29
307 */
308 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );