Fix multiple LEFT JOIN generation problem

This commit is contained in:
Atul Pratap Singh 2012-07-22 18:32:43 +05:30
parent 9ac38c49de
commit 916143e27f
2 changed files with 53 additions and 56 deletions

View File

@ -1100,7 +1100,7 @@ class PMA_DbQbe
$checked_tables = $candidate_columns;
foreach ($candidate_columns as $table) {
if ($checked_tables[$table] != 1) {
$tsize[$table] = PMA_Table::countRecords($db, $table, false);
$tsize[$table] = PMA_Table::countRecords($this->_db, $table, false);
$checked_tables[$table] = 1;
}
$csize[$table] = $tsize[$table];
@ -1186,28 +1186,9 @@ class PMA_DbQbe
$master = $this->_getMasterTable(
$all_tables, $all_columns, $where_clause_columns, $where_clause_tables
);
$remaining_tables = $all_tables;
unset($remaining_tables[$master]);
$known_tables[$master] = $master;
$run = 0;
$emerg = '';
while (count($remaining_tables) > 0) {
if ($run % 2 == 0) {
$left_join .= PMA_getRelatives('master', $remaining_tables, $known_tables);
} else {
$left_join .= PMA_getRelatives('foreign', $remaining_tables, $known_tables);
}
$run++;
if ($run > 5) {
foreach ($remaining_tables as $table) {
$emerg .= ', ' . $this->getCommonFunctions()->backquote($table);
unset($remaining_tables[$table]);
}
}
} // end while
$from_clause = $this->getCommonFunctions()->backquote($master)
. $emerg . $left_join;
. PMA_getRelatives($all_tables, $master);
} // end if ($cfgRelation['relwork'] && count($all_tables) > 0)
} // end count($_POST['criteriaColumn']) > 0

View File

@ -1161,49 +1161,65 @@ function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filte
/**
* Finds all related tables
*
* @param string $from Whether to go from master to foreign or vice versa
* @param array $remaining_tables The list of tables that we still couldn't connect
* @param array $known_tables The list of allready connected tables
* @param array $all_tables All the involved tables
* @param string $master The master table to form the LEFT JOIN clause
*
* @return string LEFT JOIN
* @access private
*/
function PMA_getRelatives($from, $remaining_tables, $known_tables)
function PMA_getRelatives($all_tables, $master)
{
$fromclause = '';
$emerg = '';
$common_functions = PMA_CommonFunctions::getInstance();
if ($from == 'master') {
$to = 'foreign';
} else {
$to = 'master';
}
$in_know = '(\'' . implode('\', \'', $known_tables) . '\')';
$in_left = '(\'' . implode('\', \'', $remaining_tables) . '\')';
$rel_query = 'SELECT *'
. ' FROM ' . $common_functions->backquote($GLOBALS['cfgRelation']['db'])
. '.' . $common_functions->backquote($GLOBALS['cfgRelation']['relation'])
. ' WHERE ' . $from . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\''
. ' AND ' . $to . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\''
. ' AND ' . $from . '_table IN ' . $in_know
. ' AND ' . $to . '_table IN ' . $in_left;
$relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
while ($row = PMA_DBI_fetch_assoc($relations)) {
$found_table = $row[$to . '_table'];
if (isset($remaining_tables[$found_table])) {
$fromclause
.= "\n" . ' LEFT JOIN '
. $common_functions->backquote($GLOBALS['db']) . '.' . $common_functions->backquote($row[$to . '_table']) . ' ON '
. $common_functions->backquote($row[$from . '_table']) . '.'
. $common_functions->backquote($row[$from . '_field']) . ' = '
. $common_functions->backquote($row[$to . '_table']) . '.'
. $common_functions->backquote($row[$to . '_field']) . ' ';
$known_tables[$found_table] = $found_table;
unset($remaining_tables[$found_table]);
// The list of tables that we still couldn't connect
$remaining_tables = $all_tables;
unset($remaining_tables[$master]);
// The list of allready connected tables
$known_tables[$master] = $master;
$run = 0;
while (count($remaining_tables) > 0) {
// Whether to go from master to foreign or vice versa
if ($run % 2 == 0) {
$from = 'master';
$to = 'foreign';
} else {
$from = 'foreign';
$to = 'master';
}
$in_know = '(\'' . implode('\', \'', $known_tables) . '\')';
$in_left = '(\'' . implode('\', \'', $remaining_tables) . '\')';
$rel_query = 'SELECT *'
. ' FROM ' . $common_functions->backquote($GLOBALS['cfgRelation']['db'])
. '.' . $common_functions->backquote($GLOBALS['cfgRelation']['relation'])
. ' WHERE ' . $from . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\''
. ' AND ' . $to . '_db = \'' . $common_functions->sqlAddSlashes($GLOBALS['db']) . '\''
. ' AND ' . $from . '_table IN ' . $in_know
. ' AND ' . $to . '_table IN ' . $in_left;
$relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
while ($row = PMA_DBI_fetch_assoc($relations)) {
$found_table = $row[$to . '_table'];
if (isset($remaining_tables[$found_table])) {
$fromclause
.= "\n" . ' LEFT JOIN '
. $common_functions->backquote($GLOBALS['db']) . '.' . $common_functions->backquote($row[$to . '_table']) . ' ON '
. $common_functions->backquote($row[$from . '_table']) . '.'
. $common_functions->backquote($row[$from . '_field']) . ' = '
. $common_functions->backquote($row[$to . '_table']) . '.'
. $common_functions->backquote($row[$to . '_field']) . ' ';
$known_tables[$found_table] = $found_table;
unset($remaining_tables[$found_table]);
}
} // end while
$run++;
if ($run > 5) {
foreach ($remaining_tables as $table) {
$emerg .= ', ' . $this->getCommonFunctions()->backquote($table);
unset($remaining_tables[$table]);
}
}
} // end while
$fromclause = $emerg . $fromclause;
return $fromclause;
} // end of the "PMA_getRelatives()" function