MediaWiki
REL1_22
|
00001 <?php 00031 class DatabaseMysqli extends DatabaseMysqlBase { 00032 00037 protected function doQuery( $sql ) { 00038 if ( $this->bufferResults() ) { 00039 $ret = $this->mConn->query( $sql ); 00040 } else { 00041 $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT ); 00042 } 00043 return $ret; 00044 } 00045 00046 protected function mysqlConnect( $realServer ) { 00047 # Fail now 00048 # Otherwise we get a suppressed fatal error, which is very hard to track down 00049 if ( !function_exists( 'mysqli_init' ) ) { 00050 throw new DBConnectionError( $this, "MySQLi functions missing," 00051 . " have you compiled PHP with the --with-mysqli option?\n" ); 00052 } 00053 00054 // Other than mysql_connect, mysqli_real_connect expects an explicit port 00055 // parameter. So we need to parse the port out of $realServer 00056 $port = null; 00057 $hostAndPort = IP::splitHostAndPort( $realServer ); 00058 if ( $hostAndPort ) { 00059 $realServer = $hostAndPort[0]; 00060 if ( $hostAndPort[1] ) { 00061 $port = $hostAndPort[1]; 00062 } 00063 } 00064 00065 $connFlags = 0; 00066 if ( $this->mFlags & DBO_SSL ) { 00067 $connFlags |= MYSQLI_CLIENT_SSL; 00068 } 00069 if ( $this->mFlags & DBO_COMPRESS ) { 00070 $connFlags |= MYSQLI_CLIENT_COMPRESS; 00071 } 00072 if ( $this->mFlags & DBO_PERSISTENT ) { 00073 $realServer = 'p:' . $realServer; 00074 } 00075 00076 $mysqli = mysqli_init(); 00077 $numAttempts = 2; 00078 00079 for ( $i = 0; $i < $numAttempts; $i++ ) { 00080 if ( $i > 1 ) { 00081 usleep( 1000 ); 00082 } 00083 if ( $mysqli->real_connect( $realServer, $this->mUser, 00084 $this->mPassword, $this->mDBname, $port, null, $connFlags ) ) 00085 { 00086 return $mysqli; 00087 } 00088 } 00089 00090 return false; 00091 } 00092 00096 protected function closeConnection() { 00097 return $this->mConn->close(); 00098 } 00099 00103 function insertId() { 00104 return $this->mConn->insert_id; 00105 } 00106 00110 function lastErrno() { 00111 if ( $this->mConn ) { 00112 return $this->mConn->errno; 00113 } else { 00114 return mysqli_connect_errno(); 00115 } 00116 } 00117 00121 function affectedRows() { 00122 return $this->mConn->affected_rows; 00123 } 00124 00129 function selectDB( $db ) { 00130 $this->mDBname = $db; 00131 return $this->mConn->select_db( $db ); 00132 } 00133 00137 function getServerVersion() { 00138 return $this->mConn->server_info; 00139 } 00140 00141 protected function mysqlFreeResult( $res ) { 00142 $res->free_result(); 00143 return true; 00144 } 00145 00146 protected function mysqlFetchObject( $res ) { 00147 $object = $res->fetch_object(); 00148 if ( $object === null ) { 00149 return false; 00150 } 00151 return $object; 00152 } 00153 00154 protected function mysqlFetchArray( $res ) { 00155 $array = $res->fetch_array(); 00156 if ( $array === null ) { 00157 return false; 00158 } 00159 return $array; 00160 } 00161 00162 protected function mysqlNumRows( $res ) { 00163 return $res->num_rows; 00164 } 00165 00166 protected function mysqlNumFields( $res ) { 00167 return $res->field_count; 00168 } 00169 00170 protected function mysqlFetchField( $res, $n ) { 00171 $field = $res->fetch_field_direct( $n ); 00172 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG; 00173 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG; 00174 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG; 00175 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG; 00176 $field->binary = $field->flags & MYSQLI_BINARY_FLAG; 00177 return $field; 00178 } 00179 00180 protected function mysqlFieldName( $res, $n ) { 00181 $field = $res->fetch_field_direct( $n ); 00182 return $field->name; 00183 } 00184 00185 protected function mysqlDataSeek( $res, $row ) { 00186 return $res->data_seek( $row ); 00187 } 00188 00189 protected function mysqlError( $conn = null ) { 00190 if ($conn === null) { 00191 return mysqli_connect_error(); 00192 } else { 00193 return $conn->error; 00194 } 00195 } 00196 00197 protected function mysqlRealEscapeString( $s ) { 00198 return $this->mConn->real_escape_string( $s ); 00199 } 00200 00201 protected function mysqlPing() { 00202 return $this->mConn->ping(); 00203 } 00204 00205 }