MediaWiki  REL1_24
DatabaseMysqli.php
Go to the documentation of this file.
00001 <?php
00031 class DatabaseMysqli extends DatabaseMysqlBase {
00036     protected function doQuery( $sql ) {
00037         if ( $this->bufferResults() ) {
00038             $ret = $this->mConn->query( $sql );
00039         } else {
00040             $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
00041         }
00042 
00043         return $ret;
00044     }
00045 
00051     protected function mysqlConnect( $realServer ) {
00052         global $wgDBmysql5;
00053         # Fail now
00054         # Otherwise we get a suppressed fatal error, which is very hard to track down
00055         if ( !function_exists( 'mysqli_init' ) ) {
00056             throw new DBConnectionError( $this, "MySQLi functions missing,"
00057                 . " have you compiled PHP with the --with-mysqli option?\n" );
00058         }
00059 
00060         // Other than mysql_connect, mysqli_real_connect expects an explicit port
00061         // and socket parameters. So we need to parse the port and socket out of
00062         // $realServer
00063         $port = null;
00064         $socket = null;
00065         $hostAndPort = IP::splitHostAndPort( $realServer );
00066         if ( $hostAndPort ) {
00067             $realServer = $hostAndPort[0];
00068             if ( $hostAndPort[1] ) {
00069                 $port = $hostAndPort[1];
00070             }
00071         } elseif ( substr_count( $realServer, ':' ) == 1 ) {
00072             // If we have a colon and something that's not a port number
00073             // inside the hostname, assume it's the socket location
00074             $hostAndSocket = explode( ':', $realServer );
00075             $realServer = $hostAndSocket[0];
00076             $socket = $hostAndSocket[1];
00077         }
00078 
00079         $connFlags = 0;
00080         if ( $this->mFlags & DBO_SSL ) {
00081             $connFlags |= MYSQLI_CLIENT_SSL;
00082         }
00083         if ( $this->mFlags & DBO_COMPRESS ) {
00084             $connFlags |= MYSQLI_CLIENT_COMPRESS;
00085         }
00086         if ( $this->mFlags & DBO_PERSISTENT ) {
00087             $realServer = 'p:' . $realServer;
00088         }
00089 
00090         $mysqli = mysqli_init();
00091         if ( $wgDBmysql5 ) {
00092             // Tell the server we're communicating with it in UTF-8.
00093             // This may engage various charset conversions.
00094             $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
00095         } else {
00096             $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
00097         }
00098         $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
00099 
00100         if ( $mysqli->real_connect( $realServer, $this->mUser,
00101             $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
00102         ) {
00103             return $mysqli;
00104         }
00105 
00106         return false;
00107     }
00108 
00109     protected function connectInitCharset() {
00110         // already done in mysqlConnect()
00111         return true;
00112     }
00113 
00118     protected function mysqlSetCharset( $charset ) {
00119         if ( method_exists( $this->mConn, 'set_charset' ) ) {
00120             return $this->mConn->set_charset( $charset );
00121         } else {
00122             return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
00123         }
00124     }
00125 
00129     protected function closeConnection() {
00130         return $this->mConn->close();
00131     }
00132 
00136     function insertId() {
00137         return $this->mConn->insert_id;
00138     }
00139 
00143     function lastErrno() {
00144         if ( $this->mConn ) {
00145             return $this->mConn->errno;
00146         } else {
00147             return mysqli_connect_errno();
00148         }
00149     }
00150 
00154     function affectedRows() {
00155         return $this->mConn->affected_rows;
00156     }
00157 
00162     function selectDB( $db ) {
00163         $this->mDBname = $db;
00164 
00165         return $this->mConn->select_db( $db );
00166     }
00167 
00171     function getServerVersion() {
00172         return $this->mConn->server_info;
00173     }
00174 
00179     protected function mysqlFreeResult( $res ) {
00180         $res->free_result();
00181 
00182         return true;
00183     }
00184 
00189     protected function mysqlFetchObject( $res ) {
00190         $object = $res->fetch_object();
00191         if ( $object === null ) {
00192             return false;
00193         }
00194 
00195         return $object;
00196     }
00197 
00202     protected function mysqlFetchArray( $res ) {
00203         $array = $res->fetch_array();
00204         if ( $array === null ) {
00205             return false;
00206         }
00207 
00208         return $array;
00209     }
00210 
00215     protected function mysqlNumRows( $res ) {
00216         return $res->num_rows;
00217     }
00218 
00223     protected function mysqlNumFields( $res ) {
00224         return $res->field_count;
00225     }
00226 
00232     protected function mysqlFetchField( $res, $n ) {
00233         $field = $res->fetch_field_direct( $n );
00234         $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
00235         $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
00236         $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
00237         $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
00238         $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
00239 
00240         return $field;
00241     }
00242 
00248     protected function mysqlFieldName( $res, $n ) {
00249         $field = $res->fetch_field_direct( $n );
00250 
00251         return $field->name;
00252     }
00253 
00259     protected function mysqlFieldType( $res, $n ) {
00260         $field = $res->fetch_field_direct( $n );
00261 
00262         return $field->type;
00263     }
00264 
00270     protected function mysqlDataSeek( $res, $row ) {
00271         return $res->data_seek( $row );
00272     }
00273 
00278     protected function mysqlError( $conn = null ) {
00279         if ( $conn === null ) {
00280             return mysqli_connect_error();
00281         } else {
00282             return $conn->error;
00283         }
00284     }
00285 
00291     protected function mysqlRealEscapeString( $s ) {
00292         return $this->mConn->real_escape_string( $s );
00293     }
00294 
00295     protected function mysqlPing() {
00296         return $this->mConn->ping();
00297     }
00298 
00305     public function __toString() {
00306         if ( $this->mConn instanceof Mysqli ) {
00307             return (string)$this->mConn->thread_id;
00308         } else {
00309             // mConn might be false or something.
00310             return (string)$this->mConn;
00311         }
00312     }
00313 }