3 * Database abstraction object for mySQL
4 * Inherit all methods and properties of Database::Database()
9 class DatabaseMysql
extends DatabaseBase
{
10 /*private*/ function doQuery( $sql ) {
11 if( $this->bufferResults() ) {
12 $ret = mysql_query( $sql, $this->mConn
);
14 $ret = mysql_unbuffered_query( $sql, $this->mConn
);
19 function open( $server, $user, $password, $dbName ) {
20 global $wgAllDBsAreLocalhost;
21 wfProfileIn( __METHOD__
);
23 # Test for missing mysql.so
24 # First try to load it
25 if (!@extension_loaded
('mysql')) {
30 # Otherwise we get a suppressed fatal error, which is very hard to track down
31 if ( !function_exists( 'mysql_connect' ) ) {
32 throw new DBConnectionError( $this, "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
35 # Debugging hack -- fake cluster
36 if ( $wgAllDBsAreLocalhost ) {
37 $realServer = 'localhost';
39 $realServer = $server;
42 $this->mServer
= $server;
44 $this->mPassword
= $password;
45 $this->mDBname
= $dbName;
49 wfProfileIn("dbconnect-$server");
51 # The kernel's default SYN retransmission period is far too slow for us,
52 # so we use a short timeout plus a manual retry. Retrying means that a small
53 # but finite rate of SYN packet loss won't cause user-visible errors.
55 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
60 $this->installErrorHandler();
61 for ( $i = 0; $i < $numAttempts && !$this->mConn
; $i++
) {
65 if ( $this->mFlags
& DBO_PERSISTENT
) {
66 $this->mConn
= mysql_pconnect( $realServer, $user, $password );
68 # Create a new connection...
69 $this->mConn
= mysql_connect( $realServer, $user, $password, true );
71 if ($this->mConn
=== false) {
73 #wfLogDBError("Connect loop error $iplus of $max ($server): " . mysql_errno() . " - " . mysql_error()."\n");
76 $phpError = $this->restoreErrorHandler();
77 # Always log connection errors
78 if ( !$this->mConn
) {
79 $error = $this->lastError();
83 wfLogDBError( "Error connecting to {$this->mServer}: $error\n" );
84 wfDebug( "DB connection error\n" );
85 wfDebug( "Server: $server, User: $user, Password: " .
86 substr( $password, 0, 3 ) . "..., error: " . mysql_error() . "\n" );
90 wfProfileOut("dbconnect-$server");
92 if ( $dbName != '' && $this->mConn
!== false ) {
93 $success = @/**/mysql_select_db( $dbName, $this->mConn
);
95 $error = "Error selecting database $dbName on server {$this->mServer} " .
96 "from client host " . wfHostname() . "\n";
97 wfLogDBError(" Error selecting database $dbName on server {$this->mServer} \n");
102 $success = (bool)$this->mConn
;
106 $version = $this->getServerVersion();
107 if ( version_compare( $version, '4.1' ) >= 0 ) {
108 // Tell the server we're communicating with it in UTF-8.
109 // This may engage various charset conversions.
112 $this->query( 'SET NAMES utf8', __METHOD__
);
114 // Turn off strict mode
115 $this->query( "SET sql_mode = ''", __METHOD__
);
118 // Turn off strict mode if it is on
120 $this->reportConnectionError( $phpError );
123 $this->mOpened
= $success;
124 wfProfileOut( __METHOD__
);
129 $this->mOpened
= false;
130 if ( $this->mConn
) {
131 if ( $this->trxLevel() ) {
132 $this->immediateCommit();
134 return mysql_close( $this->mConn
);
140 function freeResult( $res ) {
141 if ( $res instanceof ResultWrapper
) {
144 if ( !@/**/mysql_free_result( $res ) ) {
145 throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
149 function fetchObject( $res ) {
150 if ( $res instanceof ResultWrapper
) {
153 @/**/$row = mysql_fetch_object( $res );
154 if( $this->lastErrno() ) {
155 throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
160 function fetchRow( $res ) {
161 if ( $res instanceof ResultWrapper
) {
164 @/**/$row = mysql_fetch_array( $res );
165 if ( $this->lastErrno() ) {
166 throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
171 function numRows( $res ) {
172 if ( $res instanceof ResultWrapper
) {
175 @/**/$n = mysql_num_rows( $res );
176 if( $this->lastErrno() ) {
177 throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
182 function numFields( $res ) {
183 if ( $res instanceof ResultWrapper
) {
186 return mysql_num_fields( $res );
189 function fieldName( $res, $n ) {
190 if ( $res instanceof ResultWrapper
) {
193 return mysql_field_name( $res, $n );
196 function insertId() { return mysql_insert_id( $this->mConn
); }
198 function dataSeek( $res, $row ) {
199 if ( $res instanceof ResultWrapper
) {
202 return mysql_data_seek( $res, $row );
205 function lastErrno() {
206 if ( $this->mConn
) {
207 return mysql_errno( $this->mConn
);
209 return mysql_errno();
213 function lastError() {
214 if ( $this->mConn
) {
215 # Even if it's non-zero, it can still be invalid
216 wfSuppressWarnings();
217 $error = mysql_error( $this->mConn
);
219 $error = mysql_error();
223 $error = mysql_error();
226 $error .= ' (' . $this->mServer
. ')';
231 function affectedRows() { return mysql_affected_rows( $this->mConn
); }
233 function fieldInfo( $table, $field ) {
234 $table = $this->tableName( $table );
235 $res = $this->query( "SELECT * FROM $table LIMIT 1" );
236 $n = mysql_num_fields( $res->result
);
237 for( $i = 0; $i < $n; $i++
) {
238 $meta = mysql_fetch_field( $res->result
, $i );
239 if( $field == $meta->name
) {
240 return new MySQLField($meta);
246 function selectDB( $db ) {
247 $this->mDBname
= $db;
248 return mysql_select_db( $db, $this->mConn
);
251 function strencode( $s ) {
252 return mysql_real_escape_string( $s, $this->mConn
);
256 if( !function_exists( 'mysql_ping' ) ) {
257 wfDebug( "Tried to call mysql_ping but this is ancient PHP version. Faking it!\n" );
260 $ping = mysql_ping( $this->mConn
);
265 // Need to reconnect manually in MySQL client 5.0.13+
266 if ( version_compare( mysql_get_client_info(), '5.0.13', '>=' ) ) {
267 mysql_close( $this->mConn
);
268 $this->mOpened
= false;
269 $this->mConn
= false;
270 $this->open( $this->mServer
, $this->mUser
, $this->mPassword
, $this->mDBname
);
276 function getServerVersion() {
277 return mysql_get_server_info( $this->mConn
);
280 function useIndexClause( $index ) {
281 return "FORCE INDEX (" . $this->indexName( $index ) . ")";
284 function lowPriorityOption() {
285 return 'LOW_PRIORITY';
288 function getSoftwareLink() {
289 return '[http://www.mysql.com/ MySQL]';
292 public function setTimeout( $timeout ) {
293 $this->query( "SET net_read_timeout=$timeout" );
294 $this->query( "SET net_write_timeout=$timeout" );
297 public function lock( $lockName, $method, $timeout = 5 ) {
298 $lockName = $this->addQuotes( $lockName );
299 $result = $this->query( "SELECT GET_LOCK($lockName, $timeout) AS lockstatus", $method );
300 $row = $this->fetchObject( $result );
301 $this->freeResult( $result );
303 if( $row->lockstatus
== 1 ) {
306 wfDebug( __METHOD__
." failed to acquire lock\n" );
311 public function unlock( $lockName, $method ) {
312 $lockName = $this->addQuotes( $lockName );
313 $result = $this->query( "SELECT RELEASE_LOCK($lockName) as lockstatus", $method );
314 $row = $this->fetchObject( $result );
315 return $row->lockstatus
;
318 public function lockTables( $read, $write, $method ) {
321 foreach( $write as $table ) {
322 $items[] = $this->tableName( $table ) . ' LOW_PRIORITY WRITE';
324 foreach( $read as $table ) {
325 $items[] = $this->tableName( $table ) . ' READ';
327 $sql = "LOCK TABLES " . implode( ',', $items );
328 $db->query( $sql, $method );
331 public function unlockTables( $method ) {
332 $this->query( "UNLOCK TABLES", $method );
337 * Legacy support: Database == DatabaseMysql
339 class Database
extends DatabaseMysql
{}
341 class MySQLMasterPos
{
344 function __construct( $file, $pos ) {
349 function __toString() {
350 return "{$this->file}/{$this->pos}";