a11e936cccb0c75a1c49df420f2b06c76b103e11
[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 number
68 // e.g. "localhost:1234" or "127.0.0.1:1234"
69 // or Unix domain socket path
70 // e.g. "localhost:/socket_path" or "localhost:/foo/bar:bar:bar"
71 // colons are known to be used by Google AppEngine,
72 // see <https://cloud.google.com/sql/docs/mysql/connect-app-engine>
73 //
74 // We need to parse the port or socket path out of $realServer
75 $port = null;
76 $socket = null;
77 $hostAndPort = IP::splitHostAndPort( $realServer );
78 if ( $hostAndPort ) {
79 $realServer = $hostAndPort[0];
80 if ( $hostAndPort[1] ) {
81 $port = $hostAndPort[1];
82 }
83 } elseif ( substr_count( $realServer, ':/' ) == 1 ) {
84 // If we have a colon slash instead of a colon and a port number
85 // after the ip or hostname, assume it's the Unix domain socket path
86 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
87 }
88
89 $mysqli = mysqli_init();
90 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
91 // of whether any column values changed. This is what callers want to know and is
92 // consistent with what Postgres, SQLite, and SQL Server return.
93 $connFlags = MYSQLI_CLIENT_FOUND_ROWS;
94 if ( $this->getFlag( self::DBO_SSL ) ) {
95 $connFlags |= MYSQLI_CLIENT_SSL;
96 $mysqli->ssl_set(
97 $this->sslKeyPath,
98 $this->sslCertPath,
99 $this->sslCAFile,
100 $this->sslCAPath,
101 $this->sslCiphers
102 );
103 }
104 if ( $this->getFlag( self::DBO_COMPRESS ) ) {
105 $connFlags |= MYSQLI_CLIENT_COMPRESS;
106 }
107 if ( $this->getFlag( self::DBO_PERSISTENT ) ) {
108 $realServer = 'p:' . $realServer;
109 }
110
111 if ( $this->utf8Mode ) {
112 // Tell the server we're communicating with it in UTF-8.
113 // This may engage various charset conversions.
114 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
115 } else {
116 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
117 }
118 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
119
120 if ( $mysqli->real_connect(
121 $realServer,
122 $this->user,
123 $this->password,
124 $dbName,
125 $port,
126 $socket,
127 $connFlags
128 ) ) {
129 return $mysqli;
130 }
131
132 return null;
133 }
134
135 /**
136 * @return bool
137 */
138 protected function closeConnection() {
139 $conn = $this->getBindingHandle();
140
141 return $conn->close();
142 }
143
144 /**
145 * @return int
146 */
147 function insertId() {
148 $conn = $this->getBindingHandle();
149
150 return (int)$conn->insert_id;
151 }
152
153 /**
154 * @return int
155 */
156 function lastErrno() {
157 if ( $this->conn instanceof mysqli ) {
158 return $this->conn->errno;
159 } else {
160 return mysqli_connect_errno();
161 }
162 }
163
164 /**
165 * @return int
166 */
167 protected function fetchAffectedRowCount() {
168 $conn = $this->getBindingHandle();
169
170 return $conn->affected_rows;
171 }
172
173 /**
174 * @param mysqli_result $res
175 * @return bool
176 */
177 protected function mysqlFreeResult( $res ) {
178 $res->free_result();
179
180 return true;
181 }
182
183 /**
184 * @param mysqli_result $res
185 * @return stdClass|bool
186 */
187 protected function mysqlFetchObject( $res ) {
188 $object = $res->fetch_object();
189 if ( $object === null ) {
190 return false;
191 }
192
193 return $object;
194 }
195
196 /**
197 * @param mysqli_result $res
198 * @return array|false
199 */
200 protected function mysqlFetchArray( $res ) {
201 $array = $res->fetch_array();
202 if ( $array === null ) {
203 return false;
204 }
205
206 return $array;
207 }
208
209 /**
210 * @param mysqli_result $res
211 * @return mixed
212 */
213 protected function mysqlNumRows( $res ) {
214 return $res->num_rows;
215 }
216
217 /**
218 * @param mysqli_result $res
219 * @return mixed
220 */
221 protected function mysqlNumFields( $res ) {
222 return $res->field_count;
223 }
224
225 /**
226 * @param mysqli_result $res
227 * @param int $n
228 * @return mixed
229 */
230 protected function mysqlFetchField( $res, $n ) {
231 $field = $res->fetch_field_direct( $n );
232
233 // Add missing properties to result (using flags property)
234 // which will be part of function mysql-fetch-field for backward compatibility
235 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
236 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
237 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
238 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
239 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
240 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
241 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
242 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
243 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
244
245 return $field;
246 }
247
248 /**
249 * @param mysqli_result $res
250 * @param int $n
251 * @return mixed
252 */
253 protected function mysqlFieldName( $res, $n ) {
254 $field = $res->fetch_field_direct( $n );
255
256 return $field->name;
257 }
258
259 /**
260 * @param mysqli_result $res
261 * @param int $n
262 * @return mixed
263 */
264 protected function mysqlFieldType( $res, $n ) {
265 $field = $res->fetch_field_direct( $n );
266
267 return $field->type;
268 }
269
270 /**
271 * @param mysqli_result $res
272 * @param int $row
273 * @return mixed
274 */
275 protected function mysqlDataSeek( $res, $row ) {
276 return $res->data_seek( $row );
277 }
278
279 /**
280 * @param mysqli|null $conn Optional connection object
281 * @return string
282 */
283 protected function mysqlError( $conn = null ) {
284 if ( $conn === null ) {
285 return mysqli_connect_error();
286 } else {
287 return $conn->error;
288 }
289 }
290
291 /**
292 * Escapes special characters in a string for use in an SQL statement
293 * @param string $s
294 * @return string
295 */
296 protected function mysqlRealEscapeString( $s ) {
297 $conn = $this->getBindingHandle();
298
299 return $conn->real_escape_string( (string)$s );
300 }
301
302 /**
303 * @return mysqli
304 */
305 protected function getBindingHandle() {
306 return parent::getBindingHandle();
307 }
308 }
309
310 /**
311 * @deprecated since 1.29
312 */
313 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );