From de318fce3b0578d58399097db8c3e596b641db7b Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sat, 14 Jul 2012 19:56:29 +0530 Subject: [PATCH] Form functions to get LEFT JOIN column candidates --- db_qbe.php | 63 ++--------------------- libraries/db_qbe.lib.php | 107 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 60 deletions(-) diff --git a/db_qbe.php b/db_qbe.php index 07082b62a2..fed91dd01b 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -310,66 +310,9 @@ if (isset($criteriaColumn) && count($criteriaColumn) > 0) { // (When the control user is the same as the normal user // because he is using one of his databases as pmadb, // the last db selected is not always the one where we need to work) - PMA_DBI_select_db($db); - - foreach ($tab_all as $tab) { - $indexes = PMA_DBI_get_table_indexes($db, $tab); - foreach ($indexes as $ind) { - $col1 = $tab . '.' . $ind['Column_name']; - if (isset($col_all[$col1])) { - if ($ind['Non_unique'] == 0) { - if (isset($col_where[$col1])) { - $col_unique[$col1] = 'Y'; - } else { - $col_unique[$col1] = 'N'; - } - } else { - if (isset($col_where[$col1])) { - $col_index[$col1] = 'Y'; - } else { - $col_index[$col1] = 'N'; - } - } - } - } // end while (each col of tab) - } // end while (each tab) - // now we want to find the best. - if (isset($col_unique) && count($col_unique) > 0) { - $col_cand = $col_unique; - $needsort = 1; - } elseif (isset($col_index) && count($col_index) > 0) { - $col_cand = $col_index; - $needsort = 1; - } elseif (isset($col_where) && count($col_where) > 0) { - $col_cand = $tab_wher; - $needsort = 0; - } else { - $col_cand = $tab_all; - $needsort = 0; - } - - // If we came up with $col_unique (very good) or $col_index (still - // good) as $col_cand we want to check if we have any 'Y' there - // (that would mean that they were also found in the whereclauses - // which would be great). if yes, we take only those - if ($needsort == 1) { - foreach ($col_cand as $col1 => $is_where) { - $tab = explode('.', $col1); - $tab = $tab[0]; - if ($is_where == 'Y') { - $vg[$col1] = $tab; - } else { - $sg[$col1] = $tab; - } - } - if (isset($vg)) { - $col_cand = $vg; - // Candidates restricted in index+where - } else { - $col_cand = $sg; - // None of the candidates where in a where-clause - } - } + $col_cand = PMA_dbQbeGetLeftJoinColumnCandidates( + $db, $tab_all, $col_all, $col_where + ); // If our array of candidates has more than one member we'll just // find the smallest table. diff --git a/libraries/db_qbe.lib.php b/libraries/db_qbe.lib.php index 1e842b8139..90813ccf8a 100644 --- a/libraries/db_qbe.lib.php +++ b/libraries/db_qbe.lib.php @@ -690,4 +690,111 @@ function PMA_dbQbeGetOrderByClause($criteria_column_count) } return $orderby_clause; } + +/** + * Provides UNIQUE columns and INDEX columns present in criteria tables + * + * @param array $db Selected database + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * + * @return array having UNIQUE and INDEX columns + */ +function PMA_dbQbeGetIndexes($db, $all_tables, $all_columns, $where_clause_columns +) { + $unique_columns = array(); + $index_columns = array(); + + foreach ($all_tables as $table) { + $indexes = PMA_DBI_get_table_indexes($db, $table); + foreach ($indexes as $index) { + $column = $table . '.' . $index['Column_name']; + if (isset($all_columns[$column])) { + if ($index['Non_unique'] == 0) { + if (isset($where_clause_columns[$column])) { + $unique_columns[$column] = 'Y'; + } else { + $unique_columns[$column] = 'N'; + } + } else { + if (isset($where_clause_columns[$column])) { + $index_columns[$column] = 'Y'; + } else { + $index_columns[$column] = 'N'; + } + } + } + } // end while (each index of a table) + } // end while (each table) + + return array( + 'unique' => $unique_columns, + 'index' => $index_columns + ); +} + +/** + * Provides UNIQUE columns and INDEX columns present in criteria tables + * + * @param array $db Selected database + * @param array $all_tables Tables involved in the search + * @param array $all_columns Columns involved in the search + * @param array $where_clause_columns Columns having criteria where clause + * + * @return array having UNIQUE and INDEX columns + */ +function PMA_dbQbeGetLeftJoinColumnCandidates($db, $all_tables, $all_columns, + $where_clause_columns +) { + PMA_DBI_select_db($db); + $candidate_columns = array(); + + // Get unique columns and index columns + $indexes = PMA_dbQbeGetIndexes( + $db, $all_tables, $all_columns, $where_clause_columns + ); + $unique_columns = $indexes['unique']; + $index_columns = $indexes['index']; + + // now we want to find the best. + if (isset($unique_columns) && count($unique_columns) > 0) { + $candidate_columns = $unique_columns; + $needsort = 1; + } elseif (isset($index_columns) && count($index_columns) > 0) { + $candidate_columns = $index_columns; + $needsort = 1; + } elseif (isset($where_clause_columns) && count($where_clause_columns) > 0) { + $candidate_columns = $tab_wher; + $needsort = 0; + } else { + $candidate_columns = $all_tables; + $needsort = 0; + } + + // If we came up with $unique_columns (very good) or $index_columns (still + // good) as $candidate_columns we want to check if we have any 'Y' there + // (that would mean that they were also found in the whereclauses + // which would be great). if yes, we take only those + if ($needsort == 1) { + foreach ($candidate_columns as $column => $is_where) { + $table = explode('.', $column); + $table = $table[0]; + if ($is_where == 'Y') { + $vg[$column] = $table; + } else { + $sg[$column] = $table; + } + } + if (isset($vg)) { + $candidate_columns = $vg; + // Candidates restricted in index+where + } else { + $candidate_columns = $sg; + // None of the candidates where in a where-clause + } + } + + return $candidate_columns; +} ?>