Merge "Add .pipeline/ with dev image variant"
[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 use Wikimedia\AtEase\AtEase;
30
31 /**
32 * Database abstraction object for PHP extension mysqli.
33 *
34 * @ingroup Database
35 * @since 1.22
36 * @see Database
37 * @phan-file-suppress PhanParamSignatureMismatch resource vs mysqli_result
38 */
39 class DatabaseMysqli extends DatabaseMysqlBase {
40 /**
41 * @param string $sql
42 * @return mysqli_result|bool
43 */
44 protected function doQuery( $sql ) {
45 AtEase::suppressWarnings();
46 $res = $this->getBindingHandle()->query( $sql );
47 AtEase::restoreWarnings();
48
49 return $res;
50 }
51
52 /**
53 * @param string $realServer
54 * @param string|null $dbName
55 * @return mysqli|null
56 * @throws DBConnectionError
57 */
58 protected function mysqlConnect( $realServer, $dbName ) {
59 if ( !function_exists( 'mysqli_init' ) ) {
60 throw $this->newExceptionAfterConnectError(
61 "MySQLi functions missing, have you compiled PHP with the --with-mysqli option?"
62 );
63 }
64
65 // Other than mysql_connect, mysqli_real_connect expects an explicit port number
66 // e.g. "localhost:1234" or "127.0.0.1:1234"
67 // or Unix domain socket path
68 // e.g. "localhost:/socket_path" or "localhost:/foo/bar:bar:bar"
69 // colons are known to be used by Google AppEngine,
70 // see <https://cloud.google.com/sql/docs/mysql/connect-app-engine>
71 //
72 // We need to parse the port or socket path out of $realServer
73 $port = null;
74 $socket = null;
75 $hostAndPort = IP::splitHostAndPort( $realServer );
76 if ( $hostAndPort ) {
77 $realServer = $hostAndPort[0];
78 if ( $hostAndPort[1] ) {
79 $port = $hostAndPort[1];
80 }
81 } elseif ( substr_count( $realServer, ':/' ) == 1 ) {
82 // If we have a colon slash instead of a colon and a port number
83 // after the ip or hostname, assume it's the Unix domain socket path
84 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
85 }
86
87 $mysqli = mysqli_init();
88 // Make affectedRows() for UPDATE reflect the number of matching rows, regardless
89 // of whether any column values changed. This is what callers want to know and is
90 // consistent with what Postgres, SQLite, and SQL Server return.
91 $connFlags = MYSQLI_CLIENT_FOUND_ROWS;
92 if ( $this->getFlag( self::DBO_SSL ) ) {
93 $connFlags |= MYSQLI_CLIENT_SSL;
94 $mysqli->ssl_set(
95 $this->sslKeyPath,
96 $this->sslCertPath,
97 $this->sslCAFile,
98 $this->sslCAPath,
99 $this->sslCiphers
100 );
101 }
102 if ( $this->getFlag( self::DBO_COMPRESS ) ) {
103 $connFlags |= MYSQLI_CLIENT_COMPRESS;
104 }
105 if ( $this->getFlag( self::DBO_PERSISTENT ) ) {
106 $realServer = 'p:' . $realServer;
107 }
108
109 if ( $this->utf8Mode ) {
110 // Tell the server we're communicating with it in UTF-8.
111 // This may engage various charset conversions.
112 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
113 } else {
114 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
115 }
116 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
117
118 if ( $mysqli->real_connect(
119 $realServer,
120 $this->user,
121 $this->password,
122 $dbName,
123 $port,
124 $socket,
125 $connFlags
126 ) ) {
127 return $mysqli;
128 }
129
130 return null;
131 }
132
133 /**
134 * @return bool
135 */
136 protected function closeConnection() {
137 $conn = $this->getBindingHandle();
138
139 return $conn->close();
140 }
141
142 /**
143 * @return int
144 */
145 function insertId() {
146 $conn = $this->getBindingHandle();
147
148 return (int)$conn->insert_id;
149 }
150
151 /**
152 * @return int
153 */
154 function lastErrno() {
155 if ( $this->conn instanceof mysqli ) {
156 return $this->conn->errno;
157 } else {
158 return mysqli_connect_errno();
159 }
160 }
161
162 /**
163 * @return int
164 */
165 protected function fetchAffectedRowCount() {
166 $conn = $this->getBindingHandle();
167
168 return $conn->affected_rows;
169 }
170
171 /**
172 * @param mysqli_result $res
173 * @return bool
174 */
175 protected function mysqlFreeResult( $res ) {
176 $res->free_result();
177
178 return true;
179 }
180
181 /**
182 * @param mysqli_result $res
183 * @return stdClass|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_result $res
196 * @return array|false
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_result $res
209 * @return mixed
210 */
211 protected function mysqlNumRows( $res ) {
212 return $res->num_rows;
213 }
214
215 /**
216 * @param mysqli_result $res
217 * @return mixed
218 */
219 protected function mysqlNumFields( $res ) {
220 return $res->field_count;
221 }
222
223 /**
224 * @param mysqli_result $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 mysqli_result $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 mysqli_result $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 mysqli_result $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|null $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 $conn = $this->getBindingHandle();
296
297 return $conn->real_escape_string( (string)$s );
298 }
299
300 /**
301 * @return mysqli
302 */
303 protected function getBindingHandle() {
304 return parent::getBindingHandle();
305 }
306 }
307
308 /**
309 * @deprecated since 1.29
310 */
311 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );