r71762 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71761‎ | r71762 | r71763 >
Date:23:10, 26 August 2010
Author:demon
Status:ok (Comments)
Tags:
Comment:
Move all abstract stuff that DatabaseBase children must implement to new interface DatabaseType. Puts all of the 'must implement' things in one place. Also lets me use abstract statics, as a workaround for r71441
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseType.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/db/Database.php
@@ -19,7 +19,7 @@
2020 * Database abstraction object
2121 * @ingroup Database
2222 */
23 -abstract class DatabaseBase {
 23+abstract class DatabaseBase implements DatabaseType {
2424
2525 #------------------------------------------------------------------------------
2626 # Variables
@@ -276,11 +276,6 @@
277277 }
278278 }
279279
280 - /**
281 - * Get the type of the DBMS, as it appears in $wgDBtype.
282 - */
283 - abstract function getType();
284 -
285280 #------------------------------------------------------------------------------
286281 # Other functions
287282 #------------------------------------------------------------------------------
@@ -349,16 +344,6 @@
350345 return new DatabaseMysql( $server, $user, $password, $dbName, $failFunction, $flags );
351346 }
352347
353 - /**
354 - * Usually aborts on failure
355 - * If the failFunction is set to a non-zero integer, returns success
356 - * @param $server String: database server host
357 - * @param $user String: database user name
358 - * @param $password String: database user password
359 - * @param $dbName String: database name
360 - */
361 - abstract function open( $server, $user, $password, $dbName );
362 -
363348 protected function installErrorHandler() {
364349 $this->mPHPError = false;
365350 $this->htmlErrors = ini_set( 'html_errors', '0' );
@@ -541,14 +526,6 @@
542527 }
543528
544529 /**
545 - * The DBMS-dependent part of query()
546 - * @param $sql String: SQL query.
547 - * @return Result object to feed to fetchObject, fetchRow, ...; or false on failure
548 - * @private
549 - */
550 - /*private*/ abstract function doQuery( $sql );
551 -
552 - /**
553530 * @param $error String
554531 * @param $errno Integer
555532 * @param $sql String
@@ -676,87 +653,6 @@
677654 }
678655
679656 /**
680 - * Fetch the next row from the given result object, in object form.
681 - * Fields can be retrieved with $row->fieldname, with fields acting like
682 - * member variables.
683 - *
684 - * @param $res SQL result object as returned from DatabaseBase::query(), etc.
685 - * @return Row object
686 - * @throws DBUnexpectedError Thrown if the database returns an error
687 - */
688 - abstract function fetchObject( $res );
689 -
690 - /**
691 - * Fetch the next row from the given result object, in associative array
692 - * form. Fields are retrieved with $row['fieldname'].
693 - *
694 - * @param $res SQL result object as returned from DatabaseBase::query(), etc.
695 - * @return Row object
696 - * @throws DBUnexpectedError Thrown if the database returns an error
697 - */
698 - abstract function fetchRow( $res );
699 -
700 - /**
701 - * Get the number of rows in a result object
702 - * @param $res Mixed: A SQL result
703 - */
704 - abstract function numRows( $res );
705 -
706 - /**
707 - * Get the number of fields in a result object
708 - * See documentation for mysql_num_fields()
709 - * @param $res Mixed: A SQL result
710 - */
711 - abstract function numFields( $res );
712 -
713 - /**
714 - * Get a field name in a result object
715 - * See documentation for mysql_field_name():
716 - * http://www.php.net/mysql_field_name
717 - * @param $res Mixed: A SQL result
718 - * @param $n Integer
719 - */
720 - abstract function fieldName( $res, $n );
721 -
722 - /**
723 - * Get the inserted value of an auto-increment row
724 - *
725 - * The value inserted should be fetched from nextSequenceValue()
726 - *
727 - * Example:
728 - * $id = $dbw->nextSequenceValue('page_page_id_seq');
729 - * $dbw->insert('page',array('page_id' => $id));
730 - * $id = $dbw->insertId();
731 - */
732 - abstract function insertId();
733 -
734 - /**
735 - * Change the position of the cursor in a result object
736 - * See mysql_data_seek()
737 - * @param $res Mixed: A SQL result
738 - * @param $row Mixed: Either MySQL row or ResultWrapper
739 - */
740 - abstract function dataSeek( $res, $row );
741 -
742 - /**
743 - * Get the last error number
744 - * See mysql_errno()
745 - */
746 - abstract function lastErrno();
747 -
748 - /**
749 - * Get a description of the last error
750 - * See mysql_error() for more details
751 - */
752 - abstract function lastError();
753 -
754 - /**
755 - * Get the number of rows affected by the last write query
756 - * See mysql_affected_rows() for more details
757 - */
758 - abstract function affectedRows();
759 -
760 - /**
761657 * Simple UPDATE wrapper
762658 * Usually aborts on failure
763659 * If errors are explicitly ignored, returns success
@@ -1068,15 +964,6 @@
1069965 }
1070966
1071967 /**
1072 - * mysql_fetch_field() wrapper
1073 - * Returns false if the field doesn't exist
1074 - *
1075 - * @param $table
1076 - * @param $field
1077 - */
1078 - abstract function fieldInfo( $table, $field );
1079 -
1080 - /**
1081968 * mysql_field_type() wrapper
1082969 */
1083970 function fieldType( $res, $index ) {
@@ -1555,13 +1442,6 @@
15561443 }
15571444
15581445 /**
1559 - * Wrapper for addslashes()
1560 - * @param $s String: to be slashed.
1561 - * @return String: slashed string.
1562 - */
1563 - abstract function strencode( $s );
1564 -
1565 - /**
15661446 * If it's a string, adds quotes and backslashes
15671447 * Otherwise returns as-is
15681448 */
@@ -2150,26 +2030,6 @@
21512031 }
21522032
21532033 /**
2154 - * Returns a wikitext link to the DB's website, e.g.,
2155 - * return "[http://www.mysql.com/ MySQL]";
2156 - * Should at least contain plain text, if for some reason
2157 - * your database has no website.
2158 - *
2159 - * @return String: wikitext of a link to the server software's web site
2160 - */
2161 - public static function getSoftwareLink() {
2162 - throw new MWException( "A child class of DatabaseBase didn't implement getSoftwareLink(), shame on them" );
2163 - }
2164 -
2165 - /**
2166 - * A string describing the current software version, like from
2167 - * mysql_get_server_info(). Will be listed on Special:Version, etc.
2168 - *
2169 - * @return String: Version information from the database
2170 - */
2171 - abstract function getServerVersion();
2172 -
2173 - /**
21742034 * Ping the server and try to reconnect if it there is no connection
21752035 *
21762036 * @return bool Success or failure
Index: trunk/phase3/includes/db/DatabaseType.php
@@ -0,0 +1,168 @@
 2+<?php
 3+
 4+/**
 5+ * Base interface for all DBMS-specific code. At a bare minimum, all of the
 6+ * following must be implemented to support MediaWiki
 7+ */
 8+interface DatabaseType {
 9+
 10+ /**
 11+ * Get the type of the DBMS, as it appears in $wgDBtype.
 12+ *
 13+ * @return string
 14+ */
 15+ public function getType();
 16+
 17+ /**
 18+ * Open a connection to the database. Usually aborts on failure
 19+ * If the failFunction is set to a non-zero integer, returns success
 20+ *
 21+ * @param $server String: database server host
 22+ * @param $user String: database user name
 23+ * @param $password String: database user password
 24+ * @param $dbName String: database name
 25+ * @return bool
 26+ * @throws DBConnectionError
 27+ */
 28+ public function open( $server, $user, $password, $dbName );
 29+
 30+ /**
 31+ * The DBMS-dependent part of query()
 32+ * @todo @fixme Make this private someday
 33+ *
 34+ * @param $sql String: SQL query.
 35+ * @return Result object to feed to fetchObject, fetchRow, ...; or false on failure
 36+ * @private
 37+ */
 38+ /*private*/ function doQuery( $sql );
 39+
 40+ /**
 41+ * Fetch the next row from the given result object, in object form.
 42+ * Fields can be retrieved with $row->fieldname, with fields acting like
 43+ * member variables.
 44+ *
 45+ * @param $res SQL result object as returned from DatabaseBase::query(), etc.
 46+ * @return Row object
 47+ * @throws DBUnexpectedError Thrown if the database returns an error
 48+ */
 49+ public function fetchObject( $res );
 50+
 51+ /**
 52+ * Fetch the next row from the given result object, in associative array
 53+ * form. Fields are retrieved with $row['fieldname'].
 54+ *
 55+ * @param $res SQL result object as returned from DatabaseBase::query(), etc.
 56+ * @return Row object
 57+ * @throws DBUnexpectedError Thrown if the database returns an error
 58+ */
 59+ public function fetchRow( $res );
 60+
 61+ /**
 62+ * Get the number of rows in a result object
 63+ *
 64+ * @param $res Mixed: A SQL result
 65+ * @return int
 66+ */
 67+ public function numRows( $res );
 68+
 69+ /**
 70+ * Get the number of fields in a result object
 71+ * @see http://www.php.net/mysql_num_fields
 72+ *
 73+ * @param $res Mixed: A SQL result
 74+ * @return int
 75+ */
 76+ public function numFields( $res );
 77+
 78+ /**
 79+ * Get a field name in a result object
 80+ * @see http://www.php.net/mysql_field_name
 81+ *
 82+ * @param $res Mixed: A SQL result
 83+ * @param $n Integer
 84+ * @return string
 85+ */
 86+ public function fieldName( $res, $n );
 87+
 88+ /**
 89+ * Get the inserted value of an auto-increment row
 90+ *
 91+ * The value inserted should be fetched from nextSequenceValue()
 92+ *
 93+ * Example:
 94+ * $id = $dbw->nextSequenceValue('page_page_id_seq');
 95+ * $dbw->insert('page',array('page_id' => $id));
 96+ * $id = $dbw->insertId();
 97+ *
 98+ * @return int
 99+ */
 100+ public function insertId();
 101+
 102+ /**
 103+ * Change the position of the cursor in a result object
 104+ * @see http://www.php.net/mysql_data_seek
 105+ *
 106+ * @param $res Mixed: A SQL result
 107+ * @param $row Mixed: Either MySQL row or ResultWrapper
 108+ */
 109+ public function dataSeek( $res, $row );
 110+
 111+ /**
 112+ * Get the last error number
 113+ * @see http://www.php.net/mysql_errno
 114+ *
 115+ * @return int
 116+ */
 117+ public function lastErrno();
 118+
 119+ /**
 120+ * Get a description of the last error
 121+ * @see http://www.php.net/mysql_error
 122+ *
 123+ * @return string
 124+ */
 125+ public function lastError();
 126+
 127+ /**
 128+ * mysql_fetch_field() wrapper
 129+ * Returns false if the field doesn't exist
 130+ *
 131+ * @param $table string: table name
 132+ * @param $field string: field name
 133+ */
 134+ public function fieldInfo( $table, $field );
 135+
 136+ /**
 137+ * Get the number of rows affected by the last write query
 138+ * @see http://www.php.net/mysql_affected_rows
 139+ *
 140+ * @return int
 141+ */
 142+ public function affectedRows();
 143+
 144+ /**
 145+ * Wrapper for addslashes()
 146+ *
 147+ * @param $s string: to be slashed.
 148+ * @return string: slashed string.
 149+ */
 150+ public function strencode( $s );
 151+
 152+ /**
 153+ * Returns a wikitext link to the DB's website, e.g.,
 154+ * return "[http://www.mysql.com/ MySQL]";
 155+ * Should at least contain plain text, if for some reason
 156+ * your database has no website.
 157+ *
 158+ * @return string: wikitext of a link to the server software's web site
 159+ */
 160+ public static function getSoftwareLink();
 161+
 162+ /**
 163+ * A string describing the current software version, like from
 164+ * mysql_get_server_info(). Will be listed on Special:Version, etc.
 165+ *
 166+ * @return string: Version information from the database
 167+ */
 168+ public function getServerVersion();
 169+}
Property changes on: trunk/phase3/includes/db/DatabaseType.php
___________________________________________________________________
Added: svn:eol-style
1170 + native
Index: trunk/phase3/includes/AutoLoader.php
@@ -358,6 +358,7 @@
359359 'DatabasePostgres' => 'includes/db/DatabasePostgres.php',
360360 'DatabaseSqlite' => 'includes/db/DatabaseSqlite.php',
361361 'DatabaseSqliteStandalone' => 'includes/db/DatabaseSqlite.php',
 362+ 'DatabaseType' => 'includes/db/DatabaseType.php',
362363 'DBConnectionError' => 'includes/db/Database.php',
363364 'DBError' => 'includes/db/Database.php',
364365 'DBObject' => 'includes/db/Database.php',

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r71441Make getSoftwareLink() static so I can use it without instantiating (and open...demon20:55, 22 August 2010

Comments

#Comment by Tim Starling (talk | contribs)   07:28, 20 June 2011

It doesn't entirely make sense, interfaces aren't meant for this. You would think from reading the doc comment that you could create a working Database class that just implements DatabaseType, but of course you can't, it needs to inherit DatabaseBase. Plus this change means that you can't have a protected abstract function (oops r90432).

Status & tagging log