From 916143e27fd7338c945fc4e5b0cb9ec4982e807a Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 22 Jul 2012 18:32:43 +0530 Subject: [PATCH] Fix multiple LEFT JOIN generation problem --- libraries/DBQbe.class.php | 25 ++---------- libraries/relation.lib.php | 84 +++++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 56 deletions(-) diff --git a/libraries/DBQbe.class.php b/libraries/DBQbe.class.php index 7288b2b173..f936f25b60 100644 --- a/libraries/DBQbe.class.php +++ b/libraries/DBQbe.class.php @@ -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 diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index 22f8d7dfd4..84884ecb3d 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -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