r51918 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r51917‎ | r51918 | r51919 >
Date:18:39, 15 June 2009
Author:simetrical
Status:ok (Comments)
Tags:
Comment:
Move some stuff from DatabaseBase to DatabaseMysql

I moved a number of methods and marked them abstract in DatabaseBase,
namely those that met the following criteria:

1) Used some mysql_* function, so would obviously fail on any other DB.
2) Were already implemented by all five non-MySQL subclasses.

It would be reasonable to revisit these and make the less essential ones
return a dummy value instead of being abstract. And document them. And
move more methods that are MySQL-specific. But one thing at a time.

I also split the classes Database, DatabaseMysql, and MySQLMasterPos to
DatabaseMysql.php.

This should theoretically have no practical changes, but it's a bunch of
changed code, so it will possibly break something by accident.

Followup to r51795.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMysql.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseMysql.php
@@ -0,0 +1,297 @@
 2+<?php
 3+/**
 4+ * Database abstraction object for mySQL
 5+ * Inherit all methods and properties of Database::Database()
 6+ *
 7+ * @ingroup Database
 8+ * @see Database
 9+ */
 10+class DatabaseMysql extends DatabaseBase {
 11+ /*private*/ function doQuery( $sql ) {
 12+ if( $this->bufferResults() ) {
 13+ $ret = mysql_query( $sql, $this->mConn );
 14+ } else {
 15+ $ret = mysql_unbuffered_query( $sql, $this->mConn );
 16+ }
 17+ return $ret;
 18+ }
 19+
 20+ function open( $server, $user, $password, $dbName ) {
 21+ global $wgAllDBsAreLocalhost;
 22+ wfProfileIn( __METHOD__ );
 23+
 24+ # Test for missing mysql.so
 25+ # First try to load it
 26+ if (!@extension_loaded('mysql')) {
 27+ @dl('mysql.so');
 28+ }
 29+
 30+ # Fail now
 31+ # Otherwise we get a suppressed fatal error, which is very hard to track down
 32+ if ( !function_exists( 'mysql_connect' ) ) {
 33+ throw new DBConnectionError( $this, "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
 34+ }
 35+
 36+ # Debugging hack -- fake cluster
 37+ if ( $wgAllDBsAreLocalhost ) {
 38+ $realServer = 'localhost';
 39+ } else {
 40+ $realServer = $server;
 41+ }
 42+ $this->close();
 43+ $this->mServer = $server;
 44+ $this->mUser = $user;
 45+ $this->mPassword = $password;
 46+ $this->mDBname = $dbName;
 47+
 48+ $success = false;
 49+
 50+ wfProfileIn("dbconnect-$server");
 51+
 52+ # The kernel's default SYN retransmission period is far too slow for us,
 53+ # so we use a short timeout plus a manual retry. Retrying means that a small
 54+ # but finite rate of SYN packet loss won't cause user-visible errors.
 55+ $this->mConn = false;
 56+ if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
 57+ $numAttempts = 2;
 58+ } else {
 59+ $numAttempts = 1;
 60+ }
 61+ $this->installErrorHandler();
 62+ for ( $i = 0; $i < $numAttempts && !$this->mConn; $i++ ) {
 63+ if ( $i > 1 ) {
 64+ usleep( 1000 );
 65+ }
 66+ if ( $this->mFlags & DBO_PERSISTENT ) {
 67+ $this->mConn = mysql_pconnect( $realServer, $user, $password );
 68+ } else {
 69+ # Create a new connection...
 70+ $this->mConn = mysql_connect( $realServer, $user, $password, true );
 71+ }
 72+ if ($this->mConn === false) {
 73+ #$iplus = $i + 1;
 74+ #wfLogDBError("Connect loop error $iplus of $max ($server): " . mysql_errno() . " - " . mysql_error()."\n");
 75+ }
 76+ }
 77+ $phpError = $this->restoreErrorHandler();
 78+ # Always log connection errors
 79+ if ( !$this->mConn ) {
 80+ $error = $this->lastError();
 81+ if ( !$error ) {
 82+ $error = $phpError;
 83+ }
 84+ wfLogDBError( "Error connecting to {$this->mServer}: $error\n" );
 85+ wfDebug( "DB connection error\n" );
 86+ wfDebug( "Server: $server, User: $user, Password: " .
 87+ substr( $password, 0, 3 ) . "..., error: " . mysql_error() . "\n" );
 88+ $success = false;
 89+ }
 90+
 91+ wfProfileOut("dbconnect-$server");
 92+
 93+ if ( $dbName != '' && $this->mConn !== false ) {
 94+ $success = @/**/mysql_select_db( $dbName, $this->mConn );
 95+ if ( !$success ) {
 96+ $error = "Error selecting database $dbName on server {$this->mServer} " .
 97+ "from client host " . wfHostname() . "\n";
 98+ wfLogDBError(" Error selecting database $dbName on server {$this->mServer} \n");
 99+ wfDebug( $error );
 100+ }
 101+ } else {
 102+ # Delay USE query
 103+ $success = (bool)$this->mConn;
 104+ }
 105+
 106+ if ( $success ) {
 107+ $version = $this->getServerVersion();
 108+ if ( version_compare( $version, '4.1' ) >= 0 ) {
 109+ // Tell the server we're communicating with it in UTF-8.
 110+ // This may engage various charset conversions.
 111+ global $wgDBmysql5;
 112+ if( $wgDBmysql5 ) {
 113+ $this->query( 'SET NAMES utf8', __METHOD__ );
 114+ }
 115+ // Turn off strict mode
 116+ $this->query( "SET sql_mode = ''", __METHOD__ );
 117+ }
 118+
 119+ // Turn off strict mode if it is on
 120+ } else {
 121+ $this->reportConnectionError( $phpError );
 122+ }
 123+
 124+ $this->mOpened = $success;
 125+ wfProfileOut( __METHOD__ );
 126+ return $success;
 127+ }
 128+
 129+ function close() {
 130+ $this->mOpened = false;
 131+ if ( $this->mConn ) {
 132+ if ( $this->trxLevel() ) {
 133+ $this->immediateCommit();
 134+ }
 135+ return mysql_close( $this->mConn );
 136+ } else {
 137+ return true;
 138+ }
 139+ }
 140+
 141+ function freeResult( $res ) {
 142+ if ( $res instanceof ResultWrapper ) {
 143+ $res = $res->result;
 144+ }
 145+ if ( !@/**/mysql_free_result( $res ) ) {
 146+ throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
 147+ }
 148+ }
 149+
 150+ function fetchObject( $res ) {
 151+ if ( $res instanceof ResultWrapper ) {
 152+ $res = $res->result;
 153+ }
 154+ @/**/$row = mysql_fetch_object( $res );
 155+ if( $this->lastErrno() ) {
 156+ throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
 157+ }
 158+ return $row;
 159+ }
 160+
 161+ function fetchRow( $res ) {
 162+ if ( $res instanceof ResultWrapper ) {
 163+ $res = $res->result;
 164+ }
 165+ @/**/$row = mysql_fetch_array( $res );
 166+ if ( $this->lastErrno() ) {
 167+ throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
 168+ }
 169+ return $row;
 170+ }
 171+
 172+ function numRows( $res ) {
 173+ if ( $res instanceof ResultWrapper ) {
 174+ $res = $res->result;
 175+ }
 176+ @/**/$n = mysql_num_rows( $res );
 177+ if( $this->lastErrno() ) {
 178+ throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
 179+ }
 180+ return $n;
 181+ }
 182+
 183+ function numFields( $res ) {
 184+ if ( $res instanceof ResultWrapper ) {
 185+ $res = $res->result;
 186+ }
 187+ return mysql_num_fields( $res );
 188+ }
 189+
 190+ function fieldName( $res, $n ) {
 191+ if ( $res instanceof ResultWrapper ) {
 192+ $res = $res->result;
 193+ }
 194+ return mysql_field_name( $res, $n );
 195+ }
 196+
 197+ function insertId() { return mysql_insert_id( $this->mConn ); }
 198+
 199+ function dataSeek( $res, $row ) {
 200+ if ( $res instanceof ResultWrapper ) {
 201+ $res = $res->result;
 202+ }
 203+ return mysql_data_seek( $res, $row );
 204+ }
 205+
 206+ function lastErrno() {
 207+ if ( $this->mConn ) {
 208+ return mysql_errno( $this->mConn );
 209+ } else {
 210+ return mysql_errno();
 211+ }
 212+ }
 213+
 214+ function lastError() {
 215+ if ( $this->mConn ) {
 216+ # Even if it's non-zero, it can still be invalid
 217+ wfSuppressWarnings();
 218+ $error = mysql_error( $this->mConn );
 219+ if ( !$error ) {
 220+ $error = mysql_error();
 221+ }
 222+ wfRestoreWarnings();
 223+ } else {
 224+ $error = mysql_error();
 225+ }
 226+ if( $error ) {
 227+ $error .= ' (' . $this->mServer . ')';
 228+ }
 229+ return $error;
 230+ }
 231+
 232+ function affectedRows() { return mysql_affected_rows( $this->mConn ); }
 233+
 234+ function fieldInfo( $table, $field ) {
 235+ $table = $this->tableName( $table );
 236+ $res = $this->query( "SELECT * FROM $table LIMIT 1" );
 237+ $n = mysql_num_fields( $res->result );
 238+ for( $i = 0; $i < $n; $i++ ) {
 239+ $meta = mysql_fetch_field( $res->result, $i );
 240+ if( $field == $meta->name ) {
 241+ return new MySQLField($meta);
 242+ }
 243+ }
 244+ return false;
 245+ }
 246+
 247+ function selectDB( $db ) {
 248+ $this->mDBname = $db;
 249+ return mysql_select_db( $db, $this->mConn );
 250+ }
 251+
 252+ function strencode( $s ) {
 253+ return mysql_real_escape_string( $s, $this->mConn );
 254+ }
 255+
 256+ function ping() {
 257+ if( !function_exists( 'mysql_ping' ) ) {
 258+ wfDebug( "Tried to call mysql_ping but this is ancient PHP version. Faking it!\n" );
 259+ return true;
 260+ }
 261+ $ping = mysql_ping( $this->mConn );
 262+ if ( $ping ) {
 263+ return true;
 264+ }
 265+
 266+ // Need to reconnect manually in MySQL client 5.0.13+
 267+ if ( version_compare( mysql_get_client_info(), '5.0.13', '>=' ) ) {
 268+ mysql_close( $this->mConn );
 269+ $this->mOpened = false;
 270+ $this->mConn = false;
 271+ $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
 272+ return true;
 273+ }
 274+ return false;
 275+ }
 276+
 277+ function getServerVersion() {
 278+ return mysql_get_server_info( $this->mConn );
 279+ }
 280+}
 281+
 282+/**
 283+ * Legacy support: Database == DatabaseMysql
 284+ */
 285+class Database extends DatabaseMysql {}
 286+
 287+class MySQLMasterPos {
 288+ var $file, $pos;
 289+
 290+ function __construct( $file, $pos ) {
 291+ $this->file = $file;
 292+ $this->pos = $pos;
 293+ }
 294+
 295+ function __toString() {
 296+ return "{$this->file}/{$this->pos}";
 297+ }
 298+}
Property changes on: trunk/phase3/includes/db/DatabaseMysql.php
___________________________________________________________________
Name: svn:eol-style
1299 + native
Index: trunk/phase3/includes/db/Database.php
@@ -328,115 +328,8 @@
329329 * @param $password String: database user password
330330 * @param $dbName String: database name
331331 */
332 - function open( $server, $user, $password, $dbName ) {
333 - global $wgAllDBsAreLocalhost;
334 - wfProfileIn( __METHOD__ );
 332+ abstract function open( $server, $user, $password, $dbName );
335333
336 - # Test for missing mysql.so
337 - # First try to load it
338 - if (!@extension_loaded('mysql')) {
339 - @dl('mysql.so');
340 - }
341 -
342 - # Fail now
343 - # Otherwise we get a suppressed fatal error, which is very hard to track down
344 - if ( !function_exists( 'mysql_connect' ) ) {
345 - throw new DBConnectionError( $this, "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
346 - }
347 -
348 - # Debugging hack -- fake cluster
349 - if ( $wgAllDBsAreLocalhost ) {
350 - $realServer = 'localhost';
351 - } else {
352 - $realServer = $server;
353 - }
354 - $this->close();
355 - $this->mServer = $server;
356 - $this->mUser = $user;
357 - $this->mPassword = $password;
358 - $this->mDBname = $dbName;
359 -
360 - $success = false;
361 -
362 - wfProfileIn("dbconnect-$server");
363 -
364 - # The kernel's default SYN retransmission period is far too slow for us,
365 - # so we use a short timeout plus a manual retry. Retrying means that a small
366 - # but finite rate of SYN packet loss won't cause user-visible errors.
367 - $this->mConn = false;
368 - if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
369 - $numAttempts = 2;
370 - } else {
371 - $numAttempts = 1;
372 - }
373 - $this->installErrorHandler();
374 - for ( $i = 0; $i < $numAttempts && !$this->mConn; $i++ ) {
375 - if ( $i > 1 ) {
376 - usleep( 1000 );
377 - }
378 - if ( $this->mFlags & DBO_PERSISTENT ) {
379 - $this->mConn = mysql_pconnect( $realServer, $user, $password );
380 - } else {
381 - # Create a new connection...
382 - $this->mConn = mysql_connect( $realServer, $user, $password, true );
383 - }
384 - if ($this->mConn === false) {
385 - #$iplus = $i + 1;
386 - #wfLogDBError("Connect loop error $iplus of $max ($server): " . mysql_errno() . " - " . mysql_error()."\n");
387 - }
388 - }
389 - $phpError = $this->restoreErrorHandler();
390 - # Always log connection errors
391 - if ( !$this->mConn ) {
392 - $error = $this->lastError();
393 - if ( !$error ) {
394 - $error = $phpError;
395 - }
396 - wfLogDBError( "Error connecting to {$this->mServer}: $error\n" );
397 - wfDebug( "DB connection error\n" );
398 - wfDebug( "Server: $server, User: $user, Password: " .
399 - substr( $password, 0, 3 ) . "..., error: " . mysql_error() . "\n" );
400 - $success = false;
401 - }
402 -
403 - wfProfileOut("dbconnect-$server");
404 -
405 - if ( $dbName != '' && $this->mConn !== false ) {
406 - $success = @/**/mysql_select_db( $dbName, $this->mConn );
407 - if ( !$success ) {
408 - $error = "Error selecting database $dbName on server {$this->mServer} " .
409 - "from client host " . wfHostname() . "\n";
410 - wfLogDBError(" Error selecting database $dbName on server {$this->mServer} \n");
411 - wfDebug( $error );
412 - }
413 - } else {
414 - # Delay USE query
415 - $success = (bool)$this->mConn;
416 - }
417 -
418 - if ( $success ) {
419 - $version = $this->getServerVersion();
420 - if ( version_compare( $version, '4.1' ) >= 0 ) {
421 - // Tell the server we're communicating with it in UTF-8.
422 - // This may engage various charset conversions.
423 - global $wgDBmysql5;
424 - if( $wgDBmysql5 ) {
425 - $this->query( 'SET NAMES utf8', __METHOD__ );
426 - }
427 - // Turn off strict mode
428 - $this->query( "SET sql_mode = ''", __METHOD__ );
429 - }
430 -
431 - // Turn off strict mode if it is on
432 - } else {
433 - $this->reportConnectionError( $phpError );
434 - }
435 -
436 - $this->mOpened = $success;
437 - wfProfileOut( __METHOD__ );
438 - return $success;
439 - }
440 -
441334 protected function installErrorHandler() {
442335 $this->mPHPError = false;
443336 $this->htmlErrors = ini_set( 'html_errors', '0' );
@@ -467,18 +360,7 @@
468361 *
469362 * @return Bool operation success. true if already closed.
470363 */
471 - function close()
472 - {
473 - $this->mOpened = false;
474 - if ( $this->mConn ) {
475 - if ( $this->trxLevel() ) {
476 - $this->immediateCommit();
477 - }
478 - return mysql_close( $this->mConn );
479 - } else {
480 - return true;
481 - }
482 - }
 364+ abstract function close();
483365
484366 /**
485367 * @param $error String: fallback error message, used if none is given by MySQL
@@ -630,14 +512,7 @@
631513 * @return Result object to feed to fetchObject, fetchRow, ...; or false on failure
632514 * @private
633515 */
634 - /*private*/ function doQuery( $sql ) {
635 - if( $this->bufferResults() ) {
636 - $ret = mysql_query( $sql, $this->mConn );
637 - } else {
638 - $ret = mysql_unbuffered_query( $sql, $this->mConn );
639 - }
640 - return $ret;
641 - }
 516+ /*private*/ abstract function doQuery( $sql );
642517
643518 /**
644519 * @param $error String
@@ -762,14 +637,7 @@
763638 * Free a result object
764639 * @param $res Mixed: A SQL result
765640 */
766 - function freeResult( $res ) {
767 - if ( $res instanceof ResultWrapper ) {
768 - $res = $res->result;
769 - }
770 - if ( !@/**/mysql_free_result( $res ) ) {
771 - throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
772 - }
773 - }
 641+ abstract function freeResult( $res );
774642
775643 /**
776644 * Fetch the next row from the given result object, in object form.
@@ -780,16 +648,7 @@
781649 * @return MySQL row object
782650 * @throws DBUnexpectedError Thrown if the database returns an error
783651 */
784 - function fetchObject( $res ) {
785 - if ( $res instanceof ResultWrapper ) {
786 - $res = $res->result;
787 - }
788 - @/**/$row = mysql_fetch_object( $res );
789 - if( $this->lastErrno() ) {
790 - throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
791 - }
792 - return $row;
793 - }
 652+ abstract function fetchObject( $res );
794653
795654 /**
796655 * Fetch the next row from the given result object, in associative array
@@ -799,43 +658,20 @@
800659 * @return MySQL row object
801660 * @throws DBUnexpectedError Thrown if the database returns an error
802661 */
803 - function fetchRow( $res ) {
804 - if ( $res instanceof ResultWrapper ) {
805 - $res = $res->result;
806 - }
807 - @/**/$row = mysql_fetch_array( $res );
808 - if ( $this->lastErrno() ) {
809 - throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
810 - }
811 - return $row;
812 - }
 662+ abstract function fetchRow( $res );
813663
814664 /**
815665 * Get the number of rows in a result object
816666 * @param $res Mixed: A SQL result
817667 */
818 - function numRows( $res ) {
819 - if ( $res instanceof ResultWrapper ) {
820 - $res = $res->result;
821 - }
822 - @/**/$n = mysql_num_rows( $res );
823 - if( $this->lastErrno() ) {
824 - throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
825 - }
826 - return $n;
827 - }
 668+ abstract function numRows( $res );
828669
829670 /**
830671 * Get the number of fields in a result object
831672 * See documentation for mysql_num_fields()
832673 * @param $res Mixed: A SQL result
833674 */
834 - function numFields( $res ) {
835 - if ( $res instanceof ResultWrapper ) {
836 - $res = $res->result;
837 - }
838 - return mysql_num_fields( $res );
839 - }
 675+ abstract function numFields( $res );
840676
841677 /**
842678 * Get a field name in a result object
@@ -844,12 +680,7 @@
845681 * @param $res Mixed: A SQL result
846682 * @param $n Integer
847683 */
848 - function fieldName( $res, $n ) {
849 - if ( $res instanceof ResultWrapper ) {
850 - $res = $res->result;
851 - }
852 - return mysql_field_name( $res, $n );
853 - }
 684+ abstract function fieldName( $res, $n );
854685
855686 /**
856687 * Get the inserted value of an auto-increment row
@@ -861,7 +692,7 @@
862693 * $dbw->insert('page',array('page_id' => $id));
863694 * $id = $dbw->insertId();
864695 */
865 - function insertId() { return mysql_insert_id( $this->mConn ); }
 696+ abstract function insertId();
866697
867698 /**
868699 * Change the position of the cursor in a result object
@@ -869,51 +700,25 @@
870701 * @param $res Mixed: A SQL result
871702 * @param $row Mixed: Either MySQL row or ResultWrapper
872703 */
873 - function dataSeek( $res, $row ) {
874 - if ( $res instanceof ResultWrapper ) {
875 - $res = $res->result;
876 - }
877 - return mysql_data_seek( $res, $row );
878 - }
 704+ abstract function dataSeek( $res, $row );
879705
880706 /**
881707 * Get the last error number
882708 * See mysql_errno()
883709 */
884 - function lastErrno() {
885 - if ( $this->mConn ) {
886 - return mysql_errno( $this->mConn );
887 - } else {
888 - return mysql_errno();
889 - }
890 - }
 710+ abstract function lastErrno();
891711
892712 /**
893713 * Get a description of the last error
894714 * See mysql_error() for more details
895715 */
896 - function lastError() {
897 - if ( $this->mConn ) {
898 - # Even if it's non-zero, it can still be invalid
899 - wfSuppressWarnings();
900 - $error = mysql_error( $this->mConn );
901 - if ( !$error ) {
902 - $error = mysql_error();
903 - }
904 - wfRestoreWarnings();
905 - } else {
906 - $error = mysql_error();
907 - }
908 - if( $error ) {
909 - $error .= ' (' . $this->mServer . ')';
910 - }
911 - return $error;
912 - }
 716+ abstract function lastError();
 717+
913718 /**
914719 * Get the number of rows affected by the last write query
915720 * See mysql_affected_rows() for more details
916721 */
917 - function affectedRows() { return mysql_affected_rows( $this->mConn ); }
 722+ abstract function affectedRows();
918723
919724 /**
920725 * Simple UPDATE wrapper
@@ -1258,18 +1063,7 @@
12591064 * @param $table
12601065 * @param $field
12611066 */
1262 - function fieldInfo( $table, $field ) {
1263 - $table = $this->tableName( $table );
1264 - $res = $this->query( "SELECT * FROM $table LIMIT 1" );
1265 - $n = mysql_num_fields( $res->result );
1266 - for( $i = 0; $i < $n; $i++ ) {
1267 - $meta = mysql_fetch_field( $res->result, $i );
1268 - if( $field == $meta->name ) {
1269 - return new MySQLField($meta);
1270 - }
1271 - }
1272 - return false;
1273 - }
 1067+ abstract function fieldInfo( $table, $field );
12741068
12751069 /**
12761070 * mysql_field_type() wrapper
@@ -1459,10 +1253,7 @@
14601254 /**
14611255 * Change the current database
14621256 */
1463 - function selectDB( $db ) {
1464 - $this->mDBname = $db;
1465 - return mysql_select_db( $db, $this->mConn );
1466 - }
 1257+ abstract function selectDB( $db );
14671258
14681259 /**
14691260 * Get the current DB name
@@ -1638,9 +1429,7 @@
16391430 * @param $s String: to be slashed.
16401431 * @return String: slashed string.
16411432 */
1642 - function strencode( $s ) {
1643 - return mysql_real_escape_string( $s, $this->mConn );
1644 - }
 1433+ abstract function strencode( $s );
16451434
16461435 /**
16471436 * If it's a string, adds quotes and backslashes
@@ -2128,34 +1917,13 @@
21291918 /**
21301919 * @return String: Version information from the database
21311920 */
2132 - function getServerVersion() {
2133 - return mysql_get_server_info( $this->mConn );
2134 - }
 1921+ abstract function getServerVersion();
21351922
21361923 /**
21371924 * Ping the server and try to reconnect if it there is no connection
21381925 */
2139 - function ping() {
2140 - if( !function_exists( 'mysql_ping' ) ) {
2141 - wfDebug( "Tried to call mysql_ping but this is ancient PHP version. Faking it!\n" );
2142 - return true;
2143 - }
2144 - $ping = mysql_ping( $this->mConn );
2145 - if ( $ping ) {
2146 - return true;
2147 - }
 1926+ abstract function ping();
21481927
2149 - // Need to reconnect manually in MySQL client 5.0.13+
2150 - if ( version_compare( mysql_get_client_info(), '5.0.13', '>=' ) ) {
2151 - mysql_close( $this->mConn );
2152 - $this->mOpened = false;
2153 - $this->mConn = false;
2154 - $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
2155 - return true;
2156 - }
2157 - return false;
2158 - }
2159 -
21601928 /**
21611929 * Get slave lag.
21621930 * At the moment, this will only work if the DB user has the PROCESS privilege
@@ -2435,22 +2203,7 @@
24362204 }
24372205 }
24382206
2439 -/**
2440 - * Database abstraction object for mySQL
2441 - * Inherit all methods and properties of Database::Database()
2442 - *
2443 - * @ingroup Database
2444 - * @see Database
2445 - */
2446 -class DatabaseMysql extends DatabaseBase {
2447 - # Inherit all
2448 -}
24492207
2450 -/**
2451 - * Legacy support: Database == DatabaseMysql
2452 - */
2453 -class Database extends DatabaseMysql {}
2454 -
24552208 /******************************************************************************
24562209 * Utility classes
24572210 *****************************************************************************/
@@ -2902,16 +2655,3 @@
29032656 return $this->current() !== false;
29042657 }
29052658 }
2906 -
2907 -class MySQLMasterPos {
2908 - var $file, $pos;
2909 -
2910 - function __construct( $file, $pos ) {
2911 - $this->file = $file;
2912 - $this->pos = $pos;
2913 - }
2914 -
2915 - function __toString() {
2916 - return "{$this->file}/{$this->pos}";
2917 - }
2918 -}
Index: trunk/phase3/includes/AutoLoader.php
@@ -302,10 +302,10 @@
303303 # includes/db
304304 'Blob' => 'includes/db/Database.php',
305305 'ChronologyProtector' => 'includes/db/LBFactory.php',
306 - 'Database' => 'includes/db/Database.php',
 306+ 'Database' => 'includes/db/DatabaseMysql.php',
307307 'DatabaseBase' => 'includes/db/Database.php',
308308 'DatabaseMssql' => 'includes/db/DatabaseMssql.php',
309 - 'DatabaseMysql' => 'includes/db/Database.php',
 309+ 'DatabaseMysql' => 'includes/db/DatabaseMysql.php',
310310 'DatabaseOracle' => 'includes/db/DatabaseOracle.php',
311311 'DatabasePostgres' => 'includes/db/DatabasePostgres.php',
312312 'DatabaseSqlite' => 'includes/db/DatabaseSqlite.php',
@@ -322,7 +322,7 @@
323323 'LoadMonitor_MySQL' => 'includes/db/LoadMonitor.php',
324324 'MSSQLField' => 'includes/db/DatabaseMssql.php',
325325 'MySQLField' => 'includes/db/Database.php',
326 - 'MySQLMasterPos' => 'includes/db/Database.php',
 326+ 'MySQLMasterPos' => 'includes/db/DatabaseMysql.php',
327327 'ORABlob' => 'includes/db/DatabaseOracle.php',
328328 'ORAResult' => 'includes/db/DatabaseOracle.php',
329329 'PostgresField' => 'includes/db/DatabasePostgres.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r51999Make some Database methods optional to override...simetrical20:22, 16 June 2009

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r51795Make Database into abstract class DatabaseBase...simetrical17:59, 12 June 2009

Comments

#Comment by Tim Starling (talk | contribs)   00:12, 16 June 2009

Nice work. There's still lots of stuff to move though, including blatantly MySQL-specific functions like wasDeadlock() and getSoftwareLink().

You should use stubs for close(), freeResult(), selectDB() and ping() instead of making them abstract. There's some reasonable, non-harmful default behaviour that you can use for each of them, so there's no need to force the subclass to override them.

Status & tagging log