r52002 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52001‎ | r52002 | r52003 >
Date:21:00, 16 June 2009
Author:simetrical
Status:ok
Tags:
Comment:
Abstract more methods in DatabaseBase

Notably, this will switch conditional() in MySQL from using IF() to
using CASE, like all other DBMSes. Documentation suggests this works
back to 4.0. If it's a problem, it's a matter of a few lines to
override it in DatabaseMysql.php.

Also, some extra explanatory comments have been added to a number of
methods in DatabaseBase.
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseIbm_db2.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMssql.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMysql.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseOracle.php (modified) (history)
  • /trunk/phase3/includes/db/DatabasePostgres.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseSqlite.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseMysql.php
@@ -276,6 +276,23 @@
277277 function getServerVersion() {
278278 return mysql_get_server_info( $this->mConn );
279279 }
 280+
 281+ function useIndexClause( $index ) {
 282+ return "FORCE INDEX (" . $this->indexName( $index ) . ")";
 283+ }
 284+
 285+ function lowPriorityOption() {
 286+ return 'LOW_PRIORITY';
 287+ }
 288+
 289+ function getSoftwareLink() {
 290+ return '[http://www.mysql.com/ MySQL]';
 291+ }
 292+
 293+ public function setTimeout( $timeout ) {
 294+ $this->query( "SET net_read_timeout=$timeout" );
 295+ $this->query( "SET net_write_timeout=$timeout" );
 296+ }
280297 }
281298
282299 /**
Index: trunk/phase3/includes/db/DatabaseOracle.php
@@ -589,13 +589,6 @@
590590 return $this->mInsertId;
591591 }
592592
593 - /**
594 - * Oracle does not have a "USE INDEX" clause, so return an empty string
595 - */
596 - function useIndexClause($index) {
597 - return '';
598 - }
599 -
600593 # REPLACE query wrapper
601594 # Oracle simulates this with a DELETE followed by INSERT
602595 # $row is the row to insert, an associative array
@@ -687,10 +680,6 @@
688681 return $size;
689682 }
690683
691 - function lowPriorityOption() {
692 - return '';
693 - }
694 -
695684 function limitResult($sql, $limit, $offset) {
696685 if ($offset === false)
697686 $offset = 0;
@@ -703,19 +692,6 @@
704693 return 'SELECT * '.($all?'':'/* UNION_UNIQUE */ ').'FROM ('.implode( $glue, $sqls ).')' ;
705694 }
706695
707 - /**
708 - * Returns an SQL expression for a simple conditional.
709 - * Uses CASE on Oracle
710 - *
711 - * @param $cond String: SQL expression which will result in a boolean value
712 - * @param $trueVal String: SQL expression to return if true
713 - * @param $falseVal String: SQL expression to return if false
714 - * @return String: SQL fragment
715 - */
716 - function conditional( $cond, $trueVal, $falseVal ) {
717 - return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
718 - }
719 -
720696 function wasDeadlock() {
721697 return $this->lastErrno() == 'OCI-00060';
722698 }
@@ -1043,10 +1019,6 @@
10441020 return 'BITOR('.$fieldLeft.', '.$fieldRight.')';
10451021 }
10461022
1047 - public function setTimeout( $timeout ) {
1048 - // @todo fixme no-op
1049 - }
1050 -
10511023 /**
10521024 * How lagged is this slave?
10531025 *
Index: trunk/phase3/includes/db/DatabasePostgres.php
@@ -909,13 +909,6 @@
910910 return $currval;
911911 }
912912
913 - /**
914 - * Postgres does not have a "USE INDEX" clause, so return an empty string
915 - */
916 - function useIndexClause( $index ) {
917 - return '';
918 - }
919 -
920913 # REPLACE query wrapper
921914 # Postgres simulates this with a DELETE followed by INSERT
922915 # $row is the row to insert, an associative array
@@ -1009,27 +1002,10 @@
10101003 return $size;
10111004 }
10121005
1013 - function lowPriorityOption() {
1014 - return '';
1015 - }
1016 -
10171006 function limitResult($sql, $limit, $offset=false) {
10181007 return "$sql LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
10191008 }
10201009
1021 - /**
1022 - * Returns an SQL expression for a simple conditional.
1023 - * Uses CASE on Postgres
1024 - *
1025 - * @param $cond String: SQL expression which will result in a boolean value
1026 - * @param $trueVal String: SQL expression to return if true
1027 - * @param $falseVal String: SQL expression to return if false
1028 - * @return String: SQL fragment
1029 - */
1030 - function conditional( $cond, $trueVal, $falseVal ) {
1031 - return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
1032 - }
1033 -
10341010 function wasDeadlock() {
10351011 return $this->lastErrno() == '40P01';
10361012 }
@@ -1387,10 +1363,6 @@
13881364 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
13891365 }
13901366
1391 - public function setTimeout( $timeout ) {
1392 - // @todo fixme no-op
1393 - }
1394 -
13951367 /**
13961368 * How lagged is this slave?
13971369 *
Index: trunk/phase3/includes/db/DatabaseIbm_db2.php
@@ -1238,15 +1238,6 @@
12391239 }
12401240
12411241 /**
1242 - * USE INDEX clause
1243 - * DB2 doesn't have them and returns ""
1244 - * @param sting $index
1245 - */
1246 - public function useIndexClause( $index ) {
1247 - return "";
1248 - }
1249 -
1250 - /**
12511242 * Simulates REPLACE with a DELETE followed by INSERT
12521243 * @param $table Object
12531244 * @param array $uniqueIndexes array consisting of indexes and arrays of indexes
@@ -1462,19 +1453,6 @@
14631454 return "[http://www.ibm.com/software/data/db2/express/?s_cmp=ECDDWW01&s_tact=MediaWiki IBM DB2]";
14641455 }
14651456
1466 - /**
1467 - * Returns an SQL expression for a simple conditional.
1468 - * Uses CASE on DB2
1469 - *
1470 - * @param string $cond SQL expression which will result in a boolean value
1471 - * @param string $trueVal SQL expression to return if true
1472 - * @param string $falseVal SQL expression to return if false
1473 - * @return string SQL fragment
1474 - */
1475 - public function conditional( $cond, $trueVal, $falseVal ) {
1476 - return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
1477 - }
1478 -
14791457 ###
14801458 # Fix search crash
14811459 ###
@@ -1539,11 +1517,6 @@
15401518 public function getStatus( $which ) { wfDebug('Not implemented for DB2: getStatus()'); return ''; }
15411519 /**
15421520 * Not implemented
1543 - * @deprecated
1544 - */
1545 - public function setTimeout( $timeout ) { wfDebug('Not implemented for DB2: setTimeout()'); }
1546 - /**
1547 - * Not implemented
15481521 * TODO
15491522 * @return bool true
15501523 */
@@ -1570,12 +1543,6 @@
15711544 * @deprecated
15721545 */
15731546 public function limitResultForUpdate($sql, $num) { return $sql; }
1574 - /**
1575 - * No such option
1576 - * @return string ''
1577 - * @deprecated
1578 - */
1579 - public function lowPriorityOption() { return ''; }
15801547
15811548 ######################################
15821549 # Reflection
Index: trunk/phase3/includes/db/Database.php
@@ -1481,11 +1481,15 @@
14821482 }
14831483
14841484 /**
1485 - * USE INDEX clause
1486 - * PostgreSQL doesn't have them and returns ""
 1485+ * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
 1486+ * is only needed because a) MySQL must be as efficient as possible due to
 1487+ * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
 1488+ * which index to pick. Anyway, other databases might have different
 1489+ * indexes on a given table. So don't bother overriding this unless you're
 1490+ * MySQL.
14871491 */
14881492 function useIndexClause( $index ) {
1489 - return "FORCE INDEX (" . $this->indexName( $index ) . ")";
 1493+ return '';
14901494 }
14911495
14921496 /**
@@ -1573,10 +1577,14 @@
15741578 }
15751579
15761580 /**
 1581+ * A string to insert into queries to show that they're low-priority, like
 1582+ * MySQL's LOW_PRIORITY. If no such feature exists, return an empty
 1583+ * string and nothing bad should happen.
 1584+ *
15771585 * @return string Returns the text of the low priority option if it is supported, or a blank string otherwise
15781586 */
15791587 function lowPriorityOption() {
1580 - return 'LOW_PRIORITY';
 1588+ return '';
15811589 }
15821590
15831591 /**
@@ -1630,22 +1638,33 @@
16311639 }
16321640
16331641 /**
1634 - * Construct a LIMIT query with optional offset
1635 - * This is used for query pages
 1642+ * Construct a LIMIT query with optional offset. This is used for query
 1643+ * pages. The SQL should be adjusted so that only the first $limit rows
 1644+ * are returned. If $offset is provided as well, then the first $offset
 1645+ * rows should be discarded, and the next $limit rows should be returned.
 1646+ * If the result of the query is not ordered, then the rows to be returned
 1647+ * are theoretically arbitrary.
 1648+ *
 1649+ * $sql is expected to be a SELECT, if that makes a difference. For
 1650+ * UPDATE, limitResultForUpdate should be used.
 1651+ *
 1652+ * The version provided by default works in MySQL and SQLite. It will very
 1653+ * likely need to be overridden for most other DBMSes.
 1654+ *
16361655 * @param $sql String: SQL query we will append the limit too
16371656 * @param $limit Integer: the SQL limit
16381657 * @param $offset Integer the SQL offset (default false)
16391658 */
1640 - function limitResult($sql, $limit, $offset=false) {
1641 - if( !is_numeric($limit) ) {
 1659+ function limitResult( $sql, $limit, $offset=false ) {
 1660+ if( !is_numeric( $limit ) ) {
16421661 throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
16431662 }
16441663 return "$sql LIMIT "
16451664 . ( (is_numeric($offset) && $offset != 0) ? "{$offset}," : "" )
16461665 . "{$limit} ";
16471666 }
1648 - function limitResultForUpdate($sql, $num) {
1649 - return $this->limitResult($sql, $num, 0);
 1667+ function limitResultForUpdate( $sql, $num ) {
 1668+ return $this->limitResult( $sql, $num, 0 );
16501669 }
16511670
16521671 /**
@@ -1662,8 +1681,8 @@
16631682 }
16641683
16651684 /**
1666 - * Returns an SQL expression for a simple conditional.
1667 - * Uses IF on MySQL.
 1685+ * Returns an SQL expression for a simple conditional. This doesn't need
 1686+ * to be overridden unless CASE isn't supported in your DBMS.
16681687 *
16691688 * @param $cond String: SQL expression which will result in a boolean value
16701689 * @param $trueVal String: SQL expression to return if true
@@ -1671,7 +1690,7 @@
16721691 * @return String: SQL fragment
16731692 */
16741693 function conditional( $cond, $trueVal, $falseVal ) {
1675 - return " IF($cond, $trueVal, $falseVal) ";
 1694+ return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
16761695 }
16771696
16781697 /**
@@ -1922,13 +1941,21 @@
19231942 }
19241943
19251944 /**
 1945+ * Returns a wikitext link to the DB's website, e.g.,
 1946+ * return "[http://www.mysql.com/ MySQL]";
 1947+ * Should probably be overridden to at least contain plain text, if for
 1948+ * some reason your database has no website.
 1949+ *
19261950 * @return String: wikitext of a link to the server software's web site
19271951 */
19281952 function getSoftwareLink() {
1929 - return "[http://www.mysql.com/ MySQL]";
 1953+ return '(no software link given)';
19301954 }
19311955
19321956 /**
 1957+ * A string describing the current software version, like from
 1958+ * mysql_get_server_info(). Will be listed on Special:Version, etc.
 1959+ *
19331960 * @return String: Version information from the database
19341961 */
19351962 abstract function getServerVersion();
@@ -2007,16 +2034,14 @@
20082035 }
20092036
20102037 /**
2011 - * Override database's default connection timeout.
2012 - * May be useful for very long batch queries such as
2013 - * full-wiki dumps, where a single query reads out
2014 - * over hours or days.
 2038+ * Override database's default connection timeout. May be useful for very
 2039+ * long batch queries such as full-wiki dumps, where a single query reads
 2040+ * out over hours or days. May or may not be necessary for non-MySQL
 2041+ * databases. For most purposes, leaving it as a no-op should be fine.
 2042+ *
20152043 * @param $timeout Integer in seconds
20162044 */
2017 - public function setTimeout( $timeout ) {
2018 - $this->query( "SET net_read_timeout=$timeout" );
2019 - $this->query( "SET net_write_timeout=$timeout" );
2020 - }
 2045+ public function setTimeout( $timeout ) {}
20212046
20222047 /**
20232048 * Read and execute SQL commands from a file.
Index: trunk/phase3/includes/db/DatabaseMssql.php
@@ -708,13 +708,6 @@
709709 }
710710
711711 /**
712 - * USE INDEX clause
713 - */
714 - function useIndexClause( $index ) {
715 - return "";
716 - }
717 -
718 - /**
719712 * REPLACE query wrapper
720713 * PostgreSQL simulates this with a DELETE followed by INSERT
721714 * $row is the row to insert, an associative array
@@ -858,18 +851,6 @@
859852 }
860853
861854 /**
862 - * Returns an SQL expression for a simple conditional.
863 - *
864 - * @param $cond String: SQL expression which will result in a boolean value
865 - * @param $trueVal String: SQL expression to return if true
866 - * @param $falseVal String: SQL expression to return if false
867 - * @return string SQL fragment
868 - */
869 - function conditional( $cond, $trueVal, $falseVal ) {
870 - return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
871 - }
872 -
873 - /**
874855 * Should determine if the last failure was due to a deadlock
875856 * @return bool
876857 */
@@ -931,11 +912,6 @@
932913 }
933914
934915 /**
935 - * not done
936 - */
937 - public function setTimeout($timeout) { return; }
938 -
939 - /**
940916 * How lagged is this slave?
941917 */
942918 public function getLag() {
Index: trunk/phase3/includes/db/DatabaseSqlite.php
@@ -265,13 +265,6 @@
266266 }
267267
268268 /**
269 - * SQLite does not have a "USE INDEX" clause, so return an empty string
270 - */
271 - function useIndexClause($index) {
272 - return '';
273 - }
274 -
275 - /**
276269 * Returns the size of a text field, or -1 for "unlimited"
277270 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
278271 */
@@ -279,21 +272,6 @@
280273 return -1;
281274 }
282275
283 - /**
284 - * No low priority option in SQLite
285 - */
286 - function lowPriorityOption() {
287 - return '';
288 - }
289 -
290 - /**
291 - * Returns an SQL expression for a simple conditional.
292 - * - uses CASE on SQLite
293 - */
294 - function conditional($cond, $trueVal, $falseVal) {
295 - return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
296 - }
297 -
298276 function wasDeadlock() {
299277 return $this->lastErrno() == SQLITE_BUSY;
300278 }
@@ -390,11 +368,6 @@
391369 function quote_ident($s) { return $s; }
392370
393371 /**
394 - * not done
395 - */
396 - public function setTimeout($timeout) { return; }
397 -
398 - /**
399372 * How lagged is this slave?
400373 */
401374 public function getLag() {

Status & tagging log