Index: trunk/phase3/includes/db/Database.php |
— | — | @@ -1012,7 +1012,7 @@ |
1013 | 1013 | /** |
1014 | 1014 | * SELECT wrapper |
1015 | 1015 | * |
1016 | | - * @param $table Mixed: Array or string, table name(s) (prefix auto-added) |
| 1016 | + * @param $table Mixed: Array or string, table name(s) (prefix auto-added). Array keys are table aliases (optional) |
1017 | 1017 | * @param $vars Mixed: Array or string, field name(s) to be retrieved |
1018 | 1018 | * @param $conds Mixed: Array or string, condition(s) for WHERE |
1019 | 1019 | * @param $fname String: Calling function name (use __METHOD__) for logs/profiling |
— | — | @@ -1035,7 +1035,7 @@ |
1036 | 1036 | if ( !empty( $join_conds ) || ( isset( $options['USE INDEX'] ) && is_array( @$options['USE INDEX'] ) ) ) { |
1037 | 1037 | $from = ' FROM ' . $this->tableNamesWithUseIndexOrJOIN( $table, @$options['USE INDEX'], $join_conds ); |
1038 | 1038 | } else { |
1039 | | - $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) ); |
| 1039 | + $from = ' FROM ' . implode( ',', $this->tableNamesWithAlias( $table ) ); |
1040 | 1040 | } |
1041 | 1041 | } elseif ( $table != '' ) { |
1042 | 1042 | if ( $table { 0 } == ' ' ) { |
— | — | @@ -1576,6 +1576,35 @@ |
1577 | 1577 | } |
1578 | 1578 | |
1579 | 1579 | /** |
| 1580 | + * Get an aliased table name. |
| 1581 | + * @param $name string Table name, see tableName() |
| 1582 | + * @param $alias string Alias (optional) |
| 1583 | + * @return string SQL name for aliased table. Will not alias a table to its own name |
| 1584 | + */ |
| 1585 | + public function tableNameWithAlias( $name, $alias ) { |
| 1586 | + if ( !$alias || $alias == $name ) { |
| 1587 | + return $this->tableName( $name ); |
| 1588 | + } else { |
| 1589 | + return $this->tableName( $name ) . ' `' . $alias . '`'; |
| 1590 | + } |
| 1591 | + } |
| 1592 | + |
| 1593 | + /** |
| 1594 | + * @param $tables array( [alias] => table ) |
| 1595 | + * @return array of strings, see tableNameWithAlias() |
| 1596 | + */ |
| 1597 | + public function tableNamesWithAlias( $tables ) { |
| 1598 | + $retval = array(); |
| 1599 | + foreach ( $tables as $alias => $table ) { |
| 1600 | + if ( is_numeric( $alias ) ) { |
| 1601 | + $alias = $table; |
| 1602 | + } |
| 1603 | + $retval[] = $this->tableNameWithAlias( $table, $alias ); |
| 1604 | + } |
| 1605 | + return $retval; |
| 1606 | + } |
| 1607 | + |
| 1608 | + /** |
1580 | 1609 | * @private |
1581 | 1610 | */ |
1582 | 1611 | function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) { |
— | — | @@ -1584,35 +1613,37 @@ |
1585 | 1614 | $use_index_safe = is_array( $use_index ) ? $use_index : array(); |
1586 | 1615 | $join_conds_safe = is_array( $join_conds ) ? $join_conds : array(); |
1587 | 1616 | |
1588 | | - foreach ( $tables as $table ) { |
| 1617 | + foreach ( $tables as $alias => $table ) { |
| 1618 | + if ( !is_string( $alias ) ) { |
| 1619 | + // No alias? Set it equal to the table name |
| 1620 | + $alias = $table; |
| 1621 | + } |
1589 | 1622 | // Is there a JOIN and INDEX clause for this table? |
1590 | | - if ( isset( $join_conds_safe[$table] ) && isset( $use_index_safe[$table] ) ) { |
1591 | | - $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table ); |
1592 | | - $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) ); |
1593 | | - $on = $this->makeList( (array)$join_conds_safe[$table][1], LIST_AND ); |
1594 | | - |
| 1623 | + if ( isset( $join_conds_safe[$alias] ) && isset( $use_index_safe[$alias] ) ) { |
| 1624 | + $tableClause = $join_conds_safe[$alias][0] . ' ' . $this->tableNameWithAlias( $table, $alias ); |
| 1625 | + $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$alias] ) ); |
| 1626 | + $on = $this->makeList( (array)$join_conds_safe[$alias][1], LIST_AND ); |
1595 | 1627 | if ( $on != '' ) { |
1596 | 1628 | $tableClause .= ' ON (' . $on . ')'; |
1597 | 1629 | } |
1598 | 1630 | |
1599 | 1631 | $retJOIN[] = $tableClause; |
1600 | 1632 | // Is there an INDEX clause? |
1601 | | - } else if ( isset( $use_index_safe[$table] ) ) { |
1602 | | - $tableClause = $this->tableName( $table ); |
1603 | | - $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) ); |
| 1633 | + } else if ( isset( $use_index_safe[$alias] ) ) { |
| 1634 | + $tableClause = $this->tableNameWithAlias( $table, $alias ); |
| 1635 | + $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$alias] ) ); |
1604 | 1636 | $ret[] = $tableClause; |
1605 | 1637 | // Is there a JOIN clause? |
1606 | | - } else if ( isset( $join_conds_safe[$table] ) ) { |
1607 | | - $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table ); |
1608 | | - $on = $this->makeList( (array)$join_conds_safe[$table][1], LIST_AND ); |
1609 | | - |
| 1638 | + } else if ( isset( $join_conds_safe[$alias] ) ) { |
| 1639 | + $tableClause = $join_conds_safe[$alias][0] . ' ' . $this->tableNameWithAlias( $table, $alias ); |
| 1640 | + $on = $this->makeList( (array)$join_conds_safe[$alias][1], LIST_AND ); |
1610 | 1641 | if ( $on != '' ) { |
1611 | 1642 | $tableClause .= ' ON (' . $on . ')'; |
1612 | 1643 | } |
1613 | 1644 | |
1614 | 1645 | $retJOIN[] = $tableClause; |
1615 | 1646 | } else { |
1616 | | - $tableClause = $this->tableName( $table ); |
| 1647 | + $tableClause = $this->tableNameWithAlias( $table, $alias ); |
1617 | 1648 | $ret[] = $tableClause; |
1618 | 1649 | } |
1619 | 1650 | } |
Property changes on: trunk/phase3/includes/db/Database.php |
___________________________________________________________________ |
Added: svn:mergeinfo |
1620 | 1651 | Merged /branches/new-installer/phase3/includes/db/Database.php:r43664-66004 |
1621 | 1652 | Merged /branches/querypage-work2/phase3/includes/db/Database.php:r65475 |
1622 | 1653 | Merged /branches/wmf-deployment/includes/db/Database.php:r53381 |
1623 | 1654 | Merged /branches/REL1_15/phase3/includes/db/Database.php:r51646 |
1624 | 1655 | Merged /branches/sqlite/includes/db/Database.php:r58211-58321 |