r34543 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34542‎ | r34543 | r34544 >
Date:00:48, 10 May 2008
Author:aaron
Status:old
Tags:
Comment:
Add a way to do different JOINs with $tables
Modified paths:
  • /trunk/phase3/includes/Database.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Database.php
@@ -931,9 +931,11 @@
932932 * @param string $fname Calling function name (use __METHOD__) for logs/profiling
933933 * @param array $options Associative array of options (e.g. array('GROUP BY' => 'page_title')),
934934 * see Database::makeSelectOptions code for list of supported stuff
 935+ * @param array $join_conds Associative array of table join conditions (optional)
 936+ * (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
935937 * @return mixed Database result resource (feed to Database::fetchObject or whatever), or false on failure
936938 */
937 - function select( $table, $vars, $conds='', $fname = 'Database::select', $options = array() )
 939+ function select( $table, $vars, $conds='', $fname = 'Database::select', $options = array(), $join_conds = array() )
938940 {
939941 if( is_array( $vars ) ) {
940942 $vars = implode( ',', $vars );
@@ -942,8 +944,8 @@
943945 $options = array( $options );
944946 }
945947 if( is_array( $table ) ) {
946 - if ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
947 - $from = ' FROM ' . $this->tableNamesWithUseIndex( $table, $options['USE INDEX'] );
 948+ if ( !empty($join_conds) || (isset($options['USE INDEX']) && is_array($options['USE INDEX'])) )
 949+ $from = ' FROM ' . $this->tableNamesWithUseIndexOrJOIN( $table, $options['USE INDEX'], $join_conds );
948950 else
949951 $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) );
950952 } elseif ($table!='') {
@@ -1454,16 +1456,38 @@
14551457 /**
14561458 * @private
14571459 */
1458 - function tableNamesWithUseIndex( $tables, $use_index ) {
 1460+ function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) {
14591461 $ret = array();
1460 -
1461 - foreach ( $tables as $table )
1462 - if ( @$use_index[$table] !== null )
1463 - $ret[] = $this->tableName( $table ) . ' ' . $this->useIndexClause( implode( ',', (array)$use_index[$table] ) );
1464 - else
1465 - $ret[] = $this->tableName( $table );
1466 -
1467 - return implode( ',', $ret );
 1462+ $retJOIN = array();
 1463+ $use_index_safe = is_array($use_index) ? $use_index : array();
 1464+ $join_conds_safe = is_array($join_conds) ? $join_conds : array();
 1465+ foreach ( $tables as $table ) {
 1466+ // Is there a JOIN and INDEX clause for this table?
 1467+ if ( isset($join_conds_safe[$table]) && isset($use_index_safe[$table]) ) {
 1468+ $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
 1469+ $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
 1470+ $tableClause .= ' ON (' . $join_conds_safe[$table][1] . ')';
 1471+ $retJOIN[] = $tableClause;
 1472+ // Is there an INDEX clause?
 1473+ } else if ( isset($use_index_safe[$table]) ) {
 1474+ $tableClause = $this->tableName( $table );
 1475+ $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
 1476+ $ret[] = $tableClause;
 1477+ // Is there a JOIN clause?
 1478+ } else if ( isset($join_conds_safe[$table]) ) {
 1479+ $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
 1480+ $tableClause .= ' ON (' . $join_conds_safe[$table][1] . ')';
 1481+ $retJOIN[] = $tableClause;
 1482+ } else {
 1483+ $tableClause = $this->tableName( $table );
 1484+ $ret[] = $tableClause;
 1485+ }
 1486+ }
 1487+ // We can't separate explicit JOIN clauses with ',', use ' ' for those
 1488+ $straightJoins = !empty($ret) ? implode( ',', $ret ) : "";
 1489+ $otherJoins = !empty($retJOIN) ? implode( ' ', $retJOIN ) : "";
 1490+ // Compile our final table clause
 1491+ return implode(' ',array($straightJoins,$otherJoins) );
14681492 }
14691493
14701494 /**

Status & tagging log