Files
phpmyadmin/libraries/tbl_indexes.lib.php
Sebastian Mendel 4dea5b6bfd new Class PMA_Index;
show more information about indexes;
2008-02-04 15:01:43 +00:00

178 lines
5.6 KiB
PHP

<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* function library for handling table indexes
*
* @version $Id$
*/
/**
* Return a list of all index types
*
* @access public
* @return array Index types
* @author Garvin Hicking (pma@supergarv.de)
*/
function PMA_get_indextypes()
{
return array(
'PRIMARY',
'INDEX',
'UNIQUE',
'FULLTEXT',
);
}
/**
* Function to get all index information from a certain table
*
* @uses PMA_DBI_fetch_result()
* @uses PMA_backquote()
* @param string $tbl_name Table name to ftech indexes from
* @param string $err_url_0 Error URL
*
* @access public
* @return array Index keys
*/
function PMA_get_indexes($tbl_name, $err_url_0 = '')
{
return PMA_DBI_fetch_result('SHOW KEYS FROM ' . PMA_backquote($tbl_name));
}
/**
* Function to check over array of indexes and look for common problems
*
* @uses $GLOBALS['strIndexesSeemEqual']
* @uses PMA_get_indexes()
* @uses is_string()
* @uses is_array()
* @uses count()
* @uses array_pop()
* @uses reset()
* @uses current()
* @access public
* @param mixed array of indexes from PMA_get_indexes()
* or name of table
* @return string Output HTML
*/
function PMA_check_indexes($idx_collection)
{
if (is_string($idx_collection)) {
$idx_collection = PMA_get_indexes($idx_collection);
}
// count($idx_collection) < 2:
// there is no need to check if there less than two indexes
if (! is_array($idx_collection) || count($idx_collection) < 2) {
return false;
}
$indexes = array();
foreach ($idx_collection as $index_field) {
$indexes[$index_field['Key_name']][$index_field['Column_name']]
= $index_field;
}
$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_name => $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 = '<tr><td colspan=7">' . $output . '</td></tr>';
}
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;
}
?>