3 * This is the MySQLi database abstraction layer.
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.
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.
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
25 * Database abstraction object for PHP extension mysqli.
31 class DatabaseMysqli
extends DatabaseMysqlBase
{
39 protected function doQuery( $sql ) {
40 $conn = $this->getBindingHandle();
42 if ( $this->bufferResults() ) {
43 $ret = $conn->query( $sql );
45 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
52 * @param string $realServer
54 * @throws DBConnectionError
56 protected function mysqlConnect( $realServer ) {
57 # Avoid 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" );
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
68 $hostAndPort = IP
::splitHostAndPort( $realServer );
70 $realServer = $hostAndPort[0];
71 if ( $hostAndPort[1] ) {
72 $port = $hostAndPort[1];
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];
82 $mysqli = mysqli_init();
85 if ( $this->mFlags
& DBO_SSL
) {
86 $connFlags |
= MYSQLI_CLIENT_SSL
;
95 if ( $this->mFlags
& DBO_COMPRESS
) {
96 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
98 if ( $this->mFlags
& DBO_PERSISTENT
) {
99 $realServer = 'p:' . $realServer;
102 if ( $this->utf8Mode
) {
103 // Tell the server we're communicating with it in UTF-8.
104 // This may engage various charset conversions.
105 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
107 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
109 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
111 if ( $mysqli->real_connect( $realServer, $this->mUser
,
112 $this->mPassword
, $this->mDBname
, $port, $socket, $connFlags )
120 protected function connectInitCharset() {
121 // already done in mysqlConnect()
126 * @param string $charset
129 protected function mysqlSetCharset( $charset ) {
130 $conn = $this->getBindingHandle();
132 if ( method_exists( $conn, 'set_charset' ) ) {
133 return $conn->set_charset( $charset );
135 return $this->query( 'SET NAMES ' . $charset, __METHOD__
);
142 protected function closeConnection() {
143 $conn = $this->getBindingHandle();
145 return $conn->close();
151 function insertId() {
152 $conn = $this->getBindingHandle();
154 return (int)$conn->insert_id
;
160 function lastErrno() {
161 if ( $this->mConn
) {
162 return $this->mConn
->errno
;
164 return mysqli_connect_errno();
171 function affectedRows() {
172 $conn = $this->getBindingHandle();
174 return $conn->affected_rows
;
181 function selectDB( $db ) {
182 $conn = $this->getBindingHandle();
184 $this->mDBname
= $db;
186 return $conn->select_db( $db );
193 protected function mysqlFreeResult( $res ) {
203 protected function mysqlFetchObject( $res ) {
204 $object = $res->fetch_object();
205 if ( $object === null ) {
216 protected function mysqlFetchArray( $res ) {
217 $array = $res->fetch_array();
218 if ( $array === null ) {
229 protected function mysqlNumRows( $res ) {
230 return $res->num_rows
;
237 protected function mysqlNumFields( $res ) {
238 return $res->field_count
;
246 protected function mysqlFetchField( $res, $n ) {
247 $field = $res->fetch_field_direct( $n );
249 // Add missing properties to result (using flags property)
250 // which will be part of function mysql-fetch-field for backward compatibility
251 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
252 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
253 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
254 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
255 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
256 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
257 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
258 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
259 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
265 * @param resource|ResultWrapper $res
269 protected function mysqlFieldName( $res, $n ) {
270 $field = $res->fetch_field_direct( $n );
276 * @param resource|ResultWrapper $res
280 protected function mysqlFieldType( $res, $n ) {
281 $field = $res->fetch_field_direct( $n );
287 * @param resource|ResultWrapper $res
291 protected function mysqlDataSeek( $res, $row ) {
292 return $res->data_seek( $row );
296 * @param mysqli $conn Optional connection object
299 protected function mysqlError( $conn = null ) {
300 if ( $conn === null ) {
301 return mysqli_connect_error();
308 * Escapes special characters in a string for use in an SQL statement
312 protected function mysqlRealEscapeString( $s ) {
313 $conn = $this->getBindingHandle();
315 return $conn->real_escape_string( $s );
319 * Give an id for the connection
321 * mysql driver used resource id, but mysqli objects cannot be cast to string.
324 public function __toString() {
325 if ( $this->mConn
instanceof mysqli
) {
326 return (string)$this->mConn
->thread_id
;
328 // mConn might be false or something.
329 return (string)$this->mConn
;