bug #1627210, #1083301, #1482401 [data] warning on duplicate indexes

This commit is contained in:
Sebastian Mendel
2007-03-14 16:16:47 +00:00
parent b0e1be48fa
commit 71d4904b71
4 changed files with 124 additions and 98 deletions

View File

@@ -8,6 +8,7 @@ $HeadURL$
+ [import] support handling of DELIMITER to mimic mysql CLI, thanks to fb1
- bug #1674914 [structure] changing definition of a TIMESTAMP field
- bug #1615530 [upload] added more specific error message if field upload fails
- bug #1627210, #1083301, #1482401 [data] warning on duplicate indexes
- [gui] avoid displaying a wide selector in server selection
+ [core] added PMA_fatalError() and made use of it
. [i18n] use generic $strOptions

View File

@@ -1,8 +1,9 @@
<?php
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:
/* vim: expandtab sw=4 ts=4 sts=4: */
/**
* function library for handling table indexes
*
* $Id$
*/
/**
@@ -12,68 +13,114 @@
* @return array Index types
* @author Garvin Hicking (pma@supergarv.de)
*/
function PMA_get_indextypes() {
function PMA_get_indextypes()
{
return array(
'PRIMARY',
'INDEX',
'UNIQUE',
'FULLTEXT'
'FULLTEXT',
);
}
/**
* Function to get all index information from a certain table
*
* @param string Table name
* @param string Error URL
* @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 = '') {
$tbl_local_query = 'SHOW KEYS FROM ' . PMA_backquote($tbl_name);
$tbl_result = PMA_DBI_query($tbl_local_query) or PMA_mysqlDie('', $tbl_local_query, '', $err_url_0);
$tbl_ret_keys = array();
while ($tbl_row = PMA_DBI_fetch_assoc($tbl_result)) {
$tbl_ret_keys[] = $tbl_row;
}
PMA_DBI_free_result($tbl_result);
return $tbl_ret_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
*
* @param array Array of indexes
* @param boolean Whether to output HTML in table layout
*
* @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
* @author Garvin Hicking (pma@supergarv.de)
*/
function PMA_check_indexes($idx_collection, $table = true) {
$index_types = PMA_get_indextypes();
$output = '';
if ( ! is_array($idx_collection) || empty($idx_collection['ALL'])) {
return $output;
function PMA_check_indexes($idx_collection)
{
if (is_string($idx_collection)) {
$idx_collection = PMA_get_indexes($idx_collection);
}
foreach ($idx_collection['ALL'] AS $w_keyname => $w_count) {
if (isset($idx_collection['PRIMARY'][$w_keyname]) && (isset($idx_collection['INDEX'][$w_keyname]) || isset($idx_collection['UNIQUE'][$w_keyname]))) {
$output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningPrimary'], htmlspecialchars($w_keyname)), $table);
} elseif (isset($idx_collection['UNIQUE'][$w_keyname]) && isset($idx_collection['INDEX'][$w_keyname])) {
$output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningUnique'], htmlspecialchars($w_keyname)), $table);
}
// 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;
}
foreach ($index_types AS $index_type) {
if (isset($idx_collection[$index_type][$w_keyname]) && $idx_collection[$index_type][$w_keyname] > 1) {
$output .= PMA_index_warning(sprintf($GLOBALS['strIndexWarningMultiple'], $index_type, htmlspecialchars($w_keyname)), $table);
$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);
$output .= '<div class="warning">';
$output .= $GLOBALS['strIndexesSeemEqual'];
$output .= $each_index_name . ', ' . $first_column['Key_name'];
$output .= '</div>';
// 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;
}
@@ -90,8 +137,9 @@ function PMA_check_indexes($idx_collection, $table = true) {
* @return boolean void
* @author Garvin Hicking (pma@supergarv.de)
*/
function PMA_extract_indexes(&$ret_keys, &$indexes, &$indexes_info, &$indexes_data) {
if (!is_array($ret_keys)) {
function PMA_extract_indexes(&$ret_keys, &$indexes, &$indexes_info, &$indexes_data)
{
if (! is_array($ret_keys)) {
return false;
}
@@ -141,7 +189,9 @@ function PMA_extract_indexes(&$ret_keys, &$indexes, &$indexes_info, &$indexes_da
* @return array Index collection array
* @author Garvin Hicking (pma@supergarv.de)
*/
function PMA_show_indexes($table, &$indexes, &$indexes_info, &$indexes_data, $display_html = true, $print_mode = false) {
function PMA_show_indexes($table, &$indexes, &$indexes_info, &$indexes_data,
$display_html = true, $print_mode = false)
{
$idx_collection = array();
$odd_row = true;
foreach ($indexes as $index_name) {
@@ -175,7 +225,9 @@ function PMA_show_indexes($table, &$indexes, &$indexes_info, &$indexes_data, $di
if (!$print_mode) {
echo ' <td ' . $row_span . '>' . "\n"
. ' <a href="tbl_indexes.php?' . $GLOBALS['url_query'] . '&amp;index=' . urlencode($index_name) . '">' . $GLOBALS['edit_link_text'] . '</a>' . "\n"
. ' <a href="tbl_indexes.php?'
. $GLOBALS['url_query'] . '&amp;index=' . urlencode($index_name)
. '">' . $GLOBALS['edit_link_text'] . '</a>' . "\n"
. ' </td>' . "\n";
if ($index_name == 'PRIMARY') {
@@ -189,7 +241,10 @@ function PMA_show_indexes($table, &$indexes, &$indexes_info, &$indexes_data, $di
}
echo ' <td ' . $row_span . '>' . "\n"
. ' <a href="sql.php?' . $GLOBALS['url_query'] . '&amp;sql_query=' . $local_query . '&amp;zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $GLOBALS['drop_link_text'] . '</a>' . "\n"
. ' <a href="sql.php?' . $GLOBALS['url_query']
. '&amp;sql_query=' . $local_query . '&amp;zero_rows='
. $zero_rows . '" onclick="return confirmLink(this, \''
. $js_msg . '\')">' . $GLOBALS['drop_link_text'] . '</a>' . "\n"
. ' </td>' . "\n";
}
}
@@ -237,22 +292,4 @@ function PMA_show_indexes($table, &$indexes, &$indexes_info, &$indexes_data, $di
return $idx_collection;
}
/**
* Function to emit a index warning
*
* @author Garvin Hicking (pma@supergarv.de)
* @access public
* @param string $string Message string
* @param boolean $table Whether to output HTML in table layout
* @return string Output HTML
*/
function PMA_index_warning($string, $table = true) {
$output = '<div class="warning">' . $string . '</div>';
if ( $table ) {
$output = '<tr><td colspan=7">' . $output . '</td></tr>';
}
return $output . "\n";
}
?>

12
sql.php
View File

@@ -724,15 +724,9 @@ 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) {
$indexes = $indexes_info = $indexes_data = array();
$tbl_ret_keys = PMA_get_indexes(urldecode($tbl_name), $err_url_0);
PMA_extract_indexes($tbl_ret_keys, $indexes, $indexes_info, $indexes_data);
$idx_collection = PMA_show_indexes(urldecode($tbl_name), $indexes, $indexes_info, $indexes_data, false);
$check = PMA_check_indexes($idx_collection);
if (!empty($check)) {
foreach ($selected as $idx => $tbl_name) {
$check = PMA_check_indexes($tbl_name);
if (! empty($check)) {
?>
<table border="0" cellpadding="2" cellspacing="0">
<tr>

View File

@@ -1,18 +1,16 @@
<?php
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:
/* vim: expandtab sw=4 ts=4 sts=4: */
/**
* display information about indexes in a table
*
* @version $Id$
*/
/**
* Gets some core libraries
*/
require_once('./libraries/common.lib.php');
require_once('./libraries/tbl_indexes.lib.php');
/**
* Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
*/
$index_types = PMA_get_indextypes();
$index_types_cnt = count($index_types);
require_once './libraries/common.lib.php';
require_once './libraries/tbl_indexes.lib.php';
/**
* Ensures the db & table are valid, then loads headers and gets indexes
@@ -56,7 +54,7 @@ if (!defined('PMA_IDX_INCLUDED')) {
$js_to_run = isset($index) && isset($do_save_data)
? 'functions.js'
: 'indexes.js';
require_once('./libraries/header.inc.php');
require_once './libraries/header.inc.php';
} // end if
@@ -184,7 +182,7 @@ if (!defined('PMA_IDX_INCLUDED')
. $strHasBeenAltered;
$active_page = 'tbl_structure.php';
require('./tbl_structure.php');
require './tbl_structure.php';
} // end builds the new index
@@ -296,22 +294,18 @@ elseif (!defined('PMA_IDX_INCLUDED')
<label for="select_index_type"><?php echo $strIndexType; ?></label>
<select name="index_type" id="select_index_type" onchange="return checkIndexName()">
<?php
for ($i = 0; $i < $index_types_cnt; $i++) {
if ($index_types[$i] == 'PRIMARY') {
if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
echo ' '
. '<option value="PRIMARY"'
. (($index_type == 'PRIMARY') ? ' selected="selected"' : '')
. '>PRIMARY</option>' . "\n";
}
} else {
echo ' '
. '<option value="' . $index_types[$i] . '"'
. (($index_type == $index_types[$i]) ? ' selected="selected"' : '')
. '>'. $index_types[$i] . '</option>' . "\n";
} // end if... else...
} // end for
foreach (PMA_get_indextypes() as $each_index_type) {
if ($each_index_type === 'PRIMARY'
&& $index !== 'PRIMARY'
&& isset($indexes_info['PRIMARY'])) {
// skip PRIMARY if there is already one in the table
continue;
}
echo ' '
. '<option value="' . $each_index_type . '"'
. (($index_type == $each_index_type) ? ' selected="selected"' : '')
. '>'. $each_index_type . '</option>' . "\n";
}
?>
</select>
<?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
@@ -412,7 +406,7 @@ elseif (!defined('PMA_IDX_INCLUDED')
</caption>
<?php
if ( count($ret_keys) > 0) {
if (count($ret_keys) > 0) {
$edit_link_text = '';
$drop_link_text = '';
@@ -446,7 +440,7 @@ elseif (!defined('PMA_IDX_INCLUDED')
<?php
$idx_collection = PMA_show_indexes($table, $indexes, $indexes_info,
$indexes_data, true);
echo PMA_check_indexes($idx_collection);
echo PMA_check_indexes($ret_keys);
} // end display indexes
else {
// none indexes
@@ -478,6 +472,6 @@ elseif (!defined('PMA_IDX_INCLUDED')
echo "\n";
if (!defined('PMA_IDX_INCLUDED')){
require_once('./libraries/footer.inc.php');
require_once './libraries/footer.inc.php';
}
?>