Index: trunk/phase3/includes/Database.php |
— | — | @@ -931,9 +931,11 @@ |
932 | 932 | * @param string $fname Calling function name (use __METHOD__) for logs/profiling |
933 | 933 | * @param array $options Associative array of options (e.g. array('GROUP BY' => 'page_title')), |
934 | 934 | * 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') ) |
935 | 937 | * @return mixed Database result resource (feed to Database::fetchObject or whatever), or false on failure |
936 | 938 | */ |
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() ) |
938 | 940 | { |
939 | 941 | if( is_array( $vars ) ) { |
940 | 942 | $vars = implode( ',', $vars ); |
— | — | @@ -942,8 +944,8 @@ |
943 | 945 | $options = array( $options ); |
944 | 946 | } |
945 | 947 | 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 ); |
948 | 950 | else |
949 | 951 | $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) ); |
950 | 952 | } elseif ($table!='') { |
— | — | @@ -1454,16 +1456,38 @@ |
1455 | 1457 | /** |
1456 | 1458 | * @private |
1457 | 1459 | */ |
1458 | | - function tableNamesWithUseIndex( $tables, $use_index ) { |
| 1460 | + function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) { |
1459 | 1461 | $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) ); |
1468 | 1492 | } |
1469 | 1493 | |
1470 | 1494 | /** |