From 52b1b6e9c332faf9dfd1aac60bdede2c00e0836d Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 9 Sep 2004 00:02:38 +0000 Subject: [PATCH] Add Database::conditional() function to build IF() or CASE statements depending on the RDBMS used. Allowed NULL in some RC fields on PostgreSQL. Fix for bug 407: rebuildrecentchanges.php fails on PostgreSQL http://bugzilla.wikipedia.org/show_bug.cgi?id=407 --- includes/Database.php | 13 +++++++++++++ includes/DatabasePostgreSQL.php | 13 +++++++++++++ maintenance/postgresql/pg_tables.sql | 4 ++-- maintenance/rebuildrecentchanges.inc | 20 +++++++++++--------- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/includes/Database.php b/includes/Database.php index c2d87301cc..d9ab045719 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -1041,6 +1041,19 @@ class Database { return ' LIMIT '.(is_numeric($offset)?"{$offset},":"")."{$limit} "; } + /** + * Returns an SQL expression for a simple conditional. + * Uses IF on MySQL. + * + * @param string $cond SQL expression which will result in a boolean value + * @param string $trueVal SQL expression to return if true + * @param string $falseVal SQL expression to return if false + * @return string SQL fragment + */ + function conditional( $cond, $trueVal, $falseVal ) { + return " IF($cond, $trueVal, $falseVal) "; + } + /** * @todo document */ diff --git a/includes/DatabasePostgreSQL.php b/includes/DatabasePostgreSQL.php index db4c7d8454..75c7fe4808 100644 --- a/includes/DatabasePostgreSQL.php +++ b/includes/DatabasePostgreSQL.php @@ -351,6 +351,19 @@ class DatabasePgsql extends Database { function limitResult($limit,$offset) { return " LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":""); } + + /** + * Returns an SQL expression for a simple conditional. + * Uses CASE on PostgreSQL. + * + * @param string $cond SQL expression which will result in a boolean value + * @param string $trueVal SQL expression to return if true + * @param string $falseVal SQL expression to return if false + * @return string SQL fragment + */ + function conditional( $cond, $trueVal, $falseVal ) { + return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) "; + } # FIXME: actually detecting deadlocks might be nice function wasDeadlock() { diff --git a/maintenance/postgresql/pg_tables.sql b/maintenance/postgresql/pg_tables.sql index 41b979ace2..5e0324e21f 100644 --- a/maintenance/postgresql/pg_tables.sql +++ b/maintenance/postgresql/pg_tables.sql @@ -182,8 +182,8 @@ CREATE TABLE recentchanges ( rc_this_oldid integer NOT NULL, rc_last_oldid integer NOT NULL, rc_type smallint NOT NULL, - rc_moved_to_ns smallint NOT NULL, - rc_moved_to_title varchar NOT NULL, + rc_moved_to_ns smallint, + rc_moved_to_title varchar, rc_ip inet, rc_patrolled smallint ); diff --git a/maintenance/rebuildrecentchanges.inc b/maintenance/rebuildrecentchanges.inc index 2d9fc2f072..13a8da0db4 100644 --- a/maintenance/rebuildrecentchanges.inc +++ b/maintenance/rebuildrecentchanges.inc @@ -17,7 +17,7 @@ function rebuildRecentChangesTablePass1() $dbw->delete( 'recentchanges', '*' ); print( "Loading from CUR table...\n" ); - + $dbw->insertSelect( 'recentchanges', 'cur', array( 'rc_timestamp' => 'cur_timestamp', @@ -33,7 +33,7 @@ function rebuildRecentChangesTablePass1() 'rc_cur_id' => 'cur_id', 'rc_this_oldid' => 0, 'rc_last_oldid' => 0, - 'rc_type' => 'IF(cur_is_new,' . RC_NEW . ',' . RC_EDIT . ')' + 'rc_type' => $dbw->conditional( 'cur_is_new != 0', RC_NEW, RC_EDIT ), ), '*', $fname, array( 'ORDER BY' => 'inverse_timestamp', 'LIMIT' => 5000 ) ); @@ -41,21 +41,23 @@ function rebuildRecentChangesTablePass1() $sql = "INSERT INTO $recentchanges (rc_timestamp,rc_cur_time,rc_user," . "rc_user_text,rc_namespace,rc_title,rc_comment,rc_minor,rc_bot,rc_new," . - "rc_cur_id,rc_this_oldid,rc_last_oldid) SELECT old_timestamp,cur_timestamp," . + "rc_cur_id,rc_this_oldid,rc_last_oldid,rc_type) SELECT old_timestamp,cur_timestamp," . "old_user,old_user_text,old_namespace,old_title,old_comment," . - "old_minor_edit,0,0,cur_id,old_id,0 FROM $old,$cur " . + "old_minor_edit,0,0,cur_id,old_id,0,0 FROM $old,$cur " . "WHERE old_namespace=cur_namespace AND old_title=cur_title ORDER BY $old.inverse_timestamp " . "LIMIT 5000"; $dbw->query( $sql ); $sql = "SELECT rc_timestamp FROM $recentchanges " . - "ORDER BY rc_timestamp DESC LIMIT 5000,1"; + "ORDER BY rc_timestamp DESC" . $dbw->limitResult( 1, 5000 ); $res = $dbw->query( $sql ); $obj = $dbw->fetchObject( $res ); - $ts = $obj->rc_timestamp; - - $sql = "DELETE FROM $recentchanges WHERE rc_timestamp < '{$ts}'"; - $dbw->query( $sql ); + if( $obj ) { + $ts = $obj->rc_timestamp; + + $sql = "DELETE FROM $recentchanges WHERE rc_timestamp < '{$ts}'"; + $dbw->query( $sql ); + } } function rebuildRecentChangesTablePass2() -- 2.20.1