r77597 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77596‎ | r77597 | r77598 >
Date:19:39, 2 December 2010
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
Per Roans request, Merge in r65475 (Database.php only) from querypage-work2 branch
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/Database.php
@@ -1012,7 +1012,7 @@
10131013 /**
10141014 * SELECT wrapper
10151015 *
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)
10171017 * @param $vars Mixed: Array or string, field name(s) to be retrieved
10181018 * @param $conds Mixed: Array or string, condition(s) for WHERE
10191019 * @param $fname String: Calling function name (use __METHOD__) for logs/profiling
@@ -1035,7 +1035,7 @@
10361036 if ( !empty( $join_conds ) || ( isset( $options['USE INDEX'] ) && is_array( @$options['USE INDEX'] ) ) ) {
10371037 $from = ' FROM ' . $this->tableNamesWithUseIndexOrJOIN( $table, @$options['USE INDEX'], $join_conds );
10381038 } else {
1039 - $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) );
 1039+ $from = ' FROM ' . implode( ',', $this->tableNamesWithAlias( $table ) );
10401040 }
10411041 } elseif ( $table != '' ) {
10421042 if ( $table { 0 } == ' ' ) {
@@ -1576,6 +1576,35 @@
15771577 }
15781578
15791579 /**
 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+ /**
15801609 * @private
15811610 */
15821611 function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) {
@@ -1584,35 +1613,37 @@
15851614 $use_index_safe = is_array( $use_index ) ? $use_index : array();
15861615 $join_conds_safe = is_array( $join_conds ) ? $join_conds : array();
15871616
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+ }
15891622 // 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 );
15951627 if ( $on != '' ) {
15961628 $tableClause .= ' ON (' . $on . ')';
15971629 }
15981630
15991631 $retJOIN[] = $tableClause;
16001632 // 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] ) );
16041636 $ret[] = $tableClause;
16051637 // 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 );
16101641 if ( $on != '' ) {
16111642 $tableClause .= ' ON (' . $on . ')';
16121643 }
16131644
16141645 $retJOIN[] = $tableClause;
16151646 } else {
1616 - $tableClause = $this->tableName( $table );
 1647+ $tableClause = $this->tableNameWithAlias( $table, $alias );
16171648 $ret[] = $tableClause;
16181649 }
16191650 }
Property changes on: trunk/phase3/includes/db/Database.php
___________________________________________________________________
Added: svn:mergeinfo
16201651 Merged /branches/new-installer/phase3/includes/db/Database.php:r43664-66004
16211652 Merged /branches/querypage-work2/phase3/includes/db/Database.php:r65475
16221653 Merged /branches/wmf-deployment/includes/db/Database.php:r53381
16231654 Merged /branches/REL1_15/phase3/includes/db/Database.php:r51646
16241655 Merged /branches/sqlite/includes/db/Database.php:r58211-58321

Follow-up revisions

RevisionCommit summaryAuthorDate
r77636Per MaxSem CR on r77597, make $alias paramater optional, as per the documenta...reedy09:40, 3 December 2010
r77637Documentation improvement to r77597reedy09:57, 3 December 2010
r81657Per fixme on r77597, change to $this->addQuotes() instead of hardcoded quote ...reedy23:21, 7 February 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r65475* querypage-work2: Tweak DB backend so stuff like 'tables' => array( 'alias' ...catrope19:06, 23 April 2010

Comments

#Comment by MaxSem (talk | contribs)   06:17, 3 December 2010

+ * @param $alias string Alias (optional) If declared as function tableNameWithAlias( $name, $alias ), it's not optional. Should be explicitly declared as $alias = false.

#Comment by Nikerabbit (talk | contribs)   09:41, 3 December 2010

Took me a moment to figure what is an aliased table name. Would have liked an example sometable AS alias somewhere :)

#Comment by OverlordQ (talk | contribs)   23:19, 7 February 2011

Not all databases use backticks for identifiers.

#Comment by MaxSem (talk | contribs)   02:40, 8 February 2011

Sam, if a revision does not break Wikimedia, it's not an automatic ok.

Status & tagging log