From eb07812dbb2d4e31a5a19afdf4d41414fed6b29d Mon Sep 17 00:00:00 2001 From: Sebastian Mendel Date: Thu, 7 Feb 2008 14:59:02 +0000 Subject: [PATCH] aggregate more index code into PMA_index class --- libraries/Index.class.php | 116 +++++++++++++++++++++- libraries/display_tbl.lib.php | 65 ++++--------- libraries/tbl_indexes.lib.php | 177 ---------------------------------- pmd_common.php | 68 ++++--------- sql.php | 13 +-- tbl_indexes.php | 27 +----- tbl_printview.php | 1 - 7 files changed, 153 insertions(+), 314 deletions(-) delete mode 100644 libraries/tbl_indexes.lib.php diff --git a/libraries/Index.class.php b/libraries/Index.class.php index bf97de36f..e02950b78 100644 --- a/libraries/Index.class.php +++ b/libraries/Index.class.php @@ -290,6 +290,40 @@ class PMA_Index return $this->_type; } + /** + * Return a list of all index types + * + * @return array index types + */ + static public function getTypes() + { + return array( + 'PRIMARY', + 'INDEX', + 'UNIQUE', + 'FULLTEXT', + ); + } + + public function getTypeSelector() + { + $html_options = ''; + + foreach (PMA_Index::getTypes() as $each_index_type) { + if ($each_index_type === 'PRIMARY' + && $this->_name !== 'PRIMARY' + && PMA_Index::getPrimary($this->_table, $this->_schema)) { + // skip PRIMARY if there is already one in the table + continue; + } + $html_options .= '' . "\n"; + } + + return $html_options; + } + public function getPacked() { return $this->_packed; @@ -364,9 +398,9 @@ class PMA_Index * @return array Index collection array * @author Garvin Hicking (pma@supergarv.de) */ - static public function getView($table, $db, $print_mode = false) + static public function getView($table, $schema, $print_mode = false) { - $indexes = PMA_Index::getFromTable($table, $db); + $indexes = PMA_Index::getFromTable($table, $schema); if (count($indexes) < 1) { return PMA_Message::warning('strNoIndex')->getDisplay(); @@ -459,11 +493,76 @@ class PMA_Index $r .= ''; if (! $print_mode) { - //$r .= PMA_check_indexes($ret_keys); + $r .= PMA_Index::findDuplicates($table, $schema); } return $r; } + + public function getCompareData() + { + $data = array( + // 'Non_unique' => $this->_non_unique, + 'Packed' => $this->_packed, + 'Index_type' => $this->_type, + ); + + foreach ($this->_columns as $column) { + $data['columns'][] = $column->getCompareData(); + } + + return $data; + } + + /** + * Function to check over array of indexes and look for common problems + * + * @uses $GLOBALS['strIndexesSeemEqual'] + * @uses is_string() + * @uses is_array() + * @uses count() + * @uses array_pop() + * @uses reset() + * @uses current() + * @access public + * @param string name of table + * @return string Output HTML + */ + function findDuplicates($table, $schema) + { + $indexes = PMA_Index::getFromTable($table, $schema); + + $output = ''; + + // count($indexes) < 2: + // there is no need to check if there less than two indexes + if (count($indexes) < 2) { + return $output; + } + + // remove last index from stack and ... + while ($while_index = array_pop($indexes)) { + // ... compare with every remaining index in stack + foreach ($indexes as $each_index) { + if ($each_index->getCompareData() !== $while_index->getCompareData()) { + continue; + } + + // did not find any difference + // so it makes no sense to have this two equal indexes + + $message = PMA_Message::warning('strIndexesSeemEqual'); + $message->addParam($each_index->getName()); + $message->addParam($while_index->getName()); + $output .= $message->getDisplay(); + + // there is no need to check any further indexes if we have already + // found that this one has a duplicate + continue 2; + } + } + return $output; + } } class PMA_Index_Column @@ -566,5 +665,16 @@ class PMA_Index_Column { return $this->_sub_part; } + + public function getCompareData() + { + return array( + 'Column_name' => $this->_name, + 'Seq_in_index' => $this->_seq_in_index, + 'Collation' => $this->_collation, + 'Sub_part' => $this->_sub_part, + 'Null' => $this->_null, + ); + } } ?> \ No newline at end of file diff --git a/libraries/display_tbl.lib.php b/libraries/display_tbl.lib.php index 58f6f2698..580794270 100644 --- a/libraries/display_tbl.lib.php +++ b/libraries/display_tbl.lib.php @@ -10,6 +10,7 @@ * */ require_once './libraries/Table.class.php'; +require_once './libraries/Index.class.php'; /** * Defines the display mode to use for the results of a SQL query @@ -483,40 +484,10 @@ function PMA_displayTableHeaders(&$is_display, &$fields_meta, $fields_cnt = 0, $ isset($analyzed_sql[0]['table_ref']) && count($analyzed_sql[0]['table_ref']) == 1) { // grab indexes data: - PMA_DBI_select_db($db); - if (!defined('PMA_IDX_INCLUDED')) { - $ret_keys = PMA_get_indexes($table); - } - - $prev_index = ''; - foreach ($ret_keys as $row) { - - if ($row['Key_name'] != $prev_index){ - $indexes[] = $row['Key_name']; - $prev_index = $row['Key_name']; - } - $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index']; - $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique']; - if (isset($row['Cardinality'])) { - $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality']; - } - // I don't know what does the following column mean.... - // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed']; - $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment'])) - ? $row['Comment'] - : ''; - $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type'])) - ? $row['Index_type'] - : ''; - - $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name']; - if (isset($row['Sub_part'])) { - $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part']; - } - } // end while + $indexes = PMA_Index::getFromTable($table, $db); // do we have any index? - if (isset($indexes_data)) { + if ($indexes) { if ($_SESSION['userconf']['disp_direction'] == 'horizontal' || $_SESSION['userconf']['disp_direction'] == 'horizontalflipped') { @@ -535,30 +506,28 @@ function PMA_displayTableHeaders(&$is_display, &$fields_meta, $fields_cnt = 0, $ } echo '
' . "\n"; - echo PMA_generate_common_hidden_inputs($db, $table, 5); + echo PMA_generate_common_hidden_inputs($db, $table); echo $GLOBALS['strSortByKey'] . ': ' . "\n"; echo ''; - echo "\n"; echo '
' . "\n"; } } diff --git a/libraries/tbl_indexes.lib.php b/libraries/tbl_indexes.lib.php deleted file mode 100644 index d08bb6d22..000000000 --- a/libraries/tbl_indexes.lib.php +++ /dev/null @@ -1,177 +0,0 @@ - $each_index) { - if (count($while_index) !== count($each_index)) { - // number of fields are not equal - continue; - } - - // compare some key elements of every column in this two indexes - foreach ($each_index as $col_name => $each_index_column) { - if (! isset($while_index[$col_name]) - // the position - || $while_index[$col_name]['Seq_in_index'] !== $each_index_column['Seq_in_index'] - // the order, ASC or DESC - || $while_index[$col_name]['Collation'] !== $each_index_column['Collation'] - // the length - || $while_index[$col_name]['Sub_part'] !== $each_index_column['Sub_part'] - // BTREE or HASH - || $while_index[$col_name]['Index_type'] !== $each_index_column['Index_type']) { - continue 2; - } - } - - // did not find any difference - // so it makes no sense to have this two equal indexes - - // use first column from index to fetch index name - reset($while_index); - $first_column = current($while_index); - - $message = PMA_Message::warning('strIndexesSeemEqual'); - $message->addParam($each_index_name); - $message->addParam($first_column['Key_name']); - $output .= $message->getDisplay(); - - // there is no need to check any further indexes if we have already - // found that this one has a duplicate - continue 2; - } - } - - if ($output) { - $output = '' . $output . ''; - } - - return $output; -} - -/** - * Loop array of returned index keys and extract key information to - * seperate arrays. Those arrays are passed by reference. - * - * @param array Referenced Array of indexes - * @param array Referenced return array - * @param array Referenced return array - * - * @access public - * @return boolean void - * @author Garvin Hicking (pma@supergarv.de) - */ -function PMA_extract_indexes(&$ret_keys, &$indexes_info, &$indexes_data) -{ - if (! is_array($ret_keys)) { - return false; - } - - $prev_index = ''; - foreach ($ret_keys as $row) { - if ($row['Key_name'] != $prev_index){ - $indexes[] = $row['Key_name']; - $prev_index = $row['Key_name']; - } - - $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index']; - $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique']; - - if (isset($row['Cardinality'])) { - $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality']; - } - - // I don't know what does following column mean.... - // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed']; - $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment'])) - ? $row['Comment'] - : ''; - $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type'])) - ? $row['Index_type'] - : ''; - - $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name']; - if (isset($row['Sub_part'])) { - $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part']; - } - } // end while - - return true; -} - -?> diff --git a/pmd_common.php b/pmd_common.php index cf20ab49e..5f8bbe819 100644 --- a/pmd_common.php +++ b/pmd_common.php @@ -186,72 +186,42 @@ function get_script_contr() } /** - * @uses $GLOBALS['db'] - * @uses $GLOBALS['PMD'] - * @uses PMA_DBI_select_db() - * @uses PMA_get_indexes() - * @uses PMA_extract_indexes() - * @uses count() + * @uses get_all_keys() * @return array unique or primary indizes */ function get_pk_or_unique_keys() { - require_once './libraries/tbl_indexes.lib.php'; - - PMA_DBI_select_db($GLOBALS['db']); - $tables_pk_or_unique_keys = array(); - - for ($I = 0; $I < count($GLOBALS['PMD']['TABLE_NAME_SMALL']); $I++) { - $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]); - if (! empty($ret_keys)) { - // reset those as the function uses them by reference - $indexes_info = $indexes_data = array(); - PMA_extract_indexes($ret_keys, $indexes_info, $indexes_data); - // for now, take into account only the first index segment - foreach ($indexes_data as $key_name => $one_index) { - $column_name = $one_index[1]['Column_name']; - if (isset($indexes_info[$key_name]) - && $indexes_info[$key_name]['Non_unique'] == 0) { - $tables_pk_or_unique_keys[$GLOBALS['PMD']['OWNER'][$I] . '.' .$GLOBALS['PMD']['TABLE_NAME_SMALL'][$I] . '.' . $column_name] = 1; - } - } - } - } - return $tables_pk_or_unique_keys; + return get_all_keys(true); } /** * returns all indizes * - * @uses $GLOBALS['db'] * @uses $GLOBALS['PMD'] - * @uses PMA_DBI_select_db() - * @uses PMA_get_indexes() - * @uses PMA_extract_indexes() - * @uses count() + * @uses PMA_Index::getFromTable() + * @uses PMA_Index->isUnique() + * @uses PMA_Index->getColumns() + * @param boolean whether to include ony unique ones * @return array indizes */ -function get_all_keys() +function get_all_keys($unique_only = false) { - require_once './libraries/tbl_indexes.lib.php'; + require_once './libraries/Index.class.php'; - PMA_DBI_select_db($GLOBALS['db']); - $tables_all_keys = array(); - - for ($I = 0; $I < count($GLOBALS['PMD']['TABLE_NAME_SMALL']); $I++) { - $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]); - if (! empty($ret_keys)) { - // reset those as the function uses them by reference - $indexes_info = $indexes_data = array(); - PMA_extract_indexes($ret_keys, $indexes_info, $indexes_data); - // for now, take into account only the first index segment - foreach ($indexes_data as $one_index) { - $column_name = $one_index[1]['Column_name']; - $tables_all_keys[$GLOBALS['PMD']['OWNER'][$I] . '.' .$GLOBALS['PMD']['TABLE_NAME_SMALL'][$I] . '.' . $column_name] = 1; + $keys = array(); + + foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) { + $schema = $GLOBALS['PMD']['OWNER'][$I]; + // for now, take into account only the first index segment + foreach (PMA_Index::getFromTable($table, $schema) as $index) { + if ($unique_only && $index->isUnique()) { + $column = key($index->getColumns()); + $keys[$schema . '.' .$table . '.' . $column] = 1; } } } - return $tables_all_keys; + + return $keys; } /** diff --git a/sql.php b/sql.php index 99a9b5a3f..1b8aace4f 100644 --- a/sql.php +++ b/sql.php @@ -11,7 +11,6 @@ */ require_once './libraries/common.inc.php'; require_once './libraries/Table.class.php'; -require_once './libraries/tbl_indexes.lib.php'; require_once './libraries/check_user_privileges.lib.php'; require_once './libraries/bookmark.lib.php'; @@ -625,16 +624,10 @@ else { // BEGIN INDEX CHECK See if indexes should be checked. if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) { foreach ($selected as $idx => $tbl_name) { - $check = PMA_check_indexes($tbl_name); + $check = PMA_Index::findDuplicates($tbl_name, $db); if (! empty($check)) { - ?> - - - - - -
- diff --git a/tbl_printview.php b/tbl_printview.php index be3553202..3dc914ca9 100644 --- a/tbl_printview.php +++ b/tbl_printview.php @@ -31,7 +31,6 @@ if (! isset($the_tables) || ! is_array($the_tables)) { */ require_once './libraries/relation.lib.php'; require_once './libraries/transformations.lib.php'; -require_once './libraries/tbl_indexes.lib.php'; require_once './libraries/Index.class.php'; $cfgRelation = PMA_getRelationsParam();