new Class PMA_Index;

show more information about indexes;
This commit is contained in:
Sebastian Mendel
2008-02-04 15:01:43 +00:00
parent 71763eb71b
commit 4dea5b6bfd
6 changed files with 616 additions and 329 deletions

517
libraries/Index.class.php Normal file
View File

@@ -0,0 +1,517 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* holds the datasbe index class
*
* @version $Id: List.class.php 10786 2007-10-14 12:23:22Z lem9 $
*/
/**
* @since phpMyAdmin 3.0.0
*
*/
class PMA_Index
{
/**
* Class-wide storage container for indexes (caching, singleton)
*
* @var array
*/
protected static $_registry = array();
/**
* @var string The name of the database
*/
protected $_database = '';
/**
* @var string The name of the table
*/
protected $_table = '';
/**
* @var string The name of the index
*/
protected $_name = '';
/**
* Columns in index
*
* @var array
*/
protected $_columns = array();
/**
* The index method used (BTREE, FULLTEXT, HASH, RTREE).
*
* @var string
*/
protected $_type = '';
/**
* Various remarks.
*
* @var string
*/
protected $_remarks = '';
/**
* Any comment provided for the index with a COMMENT attribute when the
* index was created.
*
* @var string
*/
protected $_comment = '';
/**
* @var integer 0 if the index cannot contain duplicates, 1 if it can.
*/
protected $_non_unique = 0;
/**
* Indicates how the key is packed. NULL if it is not.
*
* @var string
*/
protected $_packed = null;
/**
* Constructor
*
* @uses $this->set()
* @param array $params
*/
public function __construct($params = array())
{
$this->set($params);
}
/**
* returns an array with all indexes from the given table
*
* @uses PMA_Index::_loadIndexes()
* @uses PMA_Index::$_registry
* @param string $table
* @param string $database
* @return array
*/
static public function getFromTable($table, $database)
{
PMA_Index::_loadIndexes($table, $database);
if (isset(PMA_Index::$_registry[$database][$table])) {
return PMA_Index::$_registry[$database][$table];
} else {
return array();
}
}
/**
* return primary if set, false otherwise
*
* @uses PMA_Index::_loadIndexes()
* @uses PMA_Index::$_registry
* @param string $table
* @param string $database
* @return mixed primary index or false if no one exists
*/
static public function getPrimary($table, $database)
{
PMA_Index::_loadIndexes($table, $database);
if (isset(PMA_Index::$_registry[$database][$table]['PRIMARY'])) {
return PMA_Index::$_registry[$database][$table]['PRIMARY'];
} else {
return false;
}
}
/**
* Load index data for table
*
* @uses PMA_Index::$_registry
* @uses PMA_DBI_fetch_result()
* @uses PMA_backquote()
* @uses PMA_Index
* @uses PMA_Index->addColumn()
* @param string $table
* @param string $database
* @return boolean
*/
static protected function _loadIndexes($table, $database)
{
if (isset(PMA_Index::$_registry[$database][$table])) {
return true;
}
$_raw_indexes = PMA_DBI_fetch_result('SHOW INDEX FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table));
foreach ($_raw_indexes as $_each_index) {
if (! isset(PMA_Index::$_registry[$database][$table][$_each_index['Key_name']])) {
$key = new PMA_Index($_each_index);
PMA_Index::$_registry[$database][$table][$_each_index['Key_name']] = $key;
} else {
$key = PMA_Index::$_registry[$database][$table][$_each_index['Key_name']];
}
$key->addColumn($_each_index);
}
return true;
}
/**
* Add column to index
*
* @uses $this->_columns
* @uses PMA_Index_Column
* @param array $params column params
*/
public function addColumn($params)
{
$this->_columns[$params['Column_name']] = new PMA_Index_Column($params);
}
/**
* Returns true if $column indexed in this index
*
* @uses $this->_columns
* @param string $column
* @return boolean
*/
public function hasColumn($column)
{
return isset($this->_columns[$column]);
}
public function set($params)
{
if (isset($params['Column_name'])) {
$this->_database = $params['Column_name'];
}
if (isset($params['Table'])) {
$this->_table = $params['Table'];
}
if (isset($params['Key_name'])) {
$this->_name = $params['Key_name'];
}
if (isset($params['Index_type'])) {
$this->_type = $params['Index_type'];
}
if (isset($params['Comment'])) {
$this->_remarks = $params['Comment'];
}
if (isset($params['Index_comment'])) {
$this->_comment = $params['Index_comment'];
}
if (isset($params['Non_unique'])) {
$this->_non_unique = $params['Non_unique'];
}
if (isset($params['Packed'])) {
$this->_packed = $params['Packed'];
}
}
public function getColumnCount()
{
return count($this->_columns);
}
public function getComment()
{
return $this->_comment;
}
public function getRemarks()
{
return $this->_remarks;
}
public function getComments()
{
$comments = $this->getRemarks();
if (strlen($comments)) {
$comments .= "\n";
}
$comments .= $this->getComment();
return $comments;
}
public function getType()
{
return $this->_type;
}
public function getPacked()
{
return $this->_packed;
}
public function isPacked($as_text = false)
{
if ($as_text) {
$r = array(
'0' => $GLOBALS['strNo'],
'1' => $GLOBALS['strYes'],
);
} else {
$r = array(
'0' => false,
'1' => true,
);
}
if (null === $this->_packed) {
return $r[0];
}
return $this->_packed;
}
public function getNonUnique()
{
return $this->_non_unique;
}
public function isUnique($as_text = false)
{
if ($as_text) {
$r = array(
'0' => $GLOBALS['strYes'],
'1' => $GLOBALS['strNo'],
);
} else {
$r = array(
'0' => true,
'1' => false,
);
}
return $r[$this->_non_unique];
}
public function getName()
{
return $this->_name;
}
public function getColumns()
{
return $this->_columns;
}
/**
* Show index data
*
* @param string $table The tablename
* @param array $indexes_info Referenced info array
* @param array $indexes_data Referenced data array
* @param boolean $print_mode
* @access public
* @return array Index collection array
* @author Garvin Hicking (pma@supergarv.de)
*/
static public function getView($table, $db, $print_mode = false)
{
$indexes = PMA_Index::getFromTable($table, $db);
if (count($indexes) < 1) {
return PMA_Message::warning('strNoIndex')->getDisplay();
}
$r = '';
$r .= '<h2>' . $GLOBALS['strIndexes'] . ': ';
$r .= PMA_showMySQLDocu('optimization', 'optimizing-database-structure');
$r .= '</h2>';
$r .= '<table>';
$r .= '<thead>';
$r .= '<tr>';
if (! $print_mode) {
$r .= '<th colspan="2">' . $GLOBALS['strAction'] . '</th>';
}
$r .= '<th>' . $GLOBALS['strKeyname'] . '</th>';
$r .= '<th>' . $GLOBALS['strType'] . '</th>';
$r .= '<th>' . $GLOBALS['strUnique'] . '</th>';
$r .= '<th>' . $GLOBALS['strPacked'] . '</th>';
$r .= '<th>' . $GLOBALS['strField'] . '</th>';
$r .= '<th>' . $GLOBALS['strCardinality'] . '</th>';
$r .= '<th>' . $GLOBALS['strCollation'] . '</th>';
$r .= '<th>' . $GLOBALS['strNull'] . '</th>';
$r .= '<th>' . $GLOBALS['strComment'] . '</th>';
$r .= '</tr>';
$r .= '</thead>';
$r .= '<tbody>';
$odd_row = true;
foreach ($indexes as $index) {
$row_span = ' rowspan="' . $index->getColumnCount() . '" ';
$r .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">';
if (! $print_mode) {
$this_params = $GLOBALS['url_params'];
$this_params['index'] = $index->getName();
$r .= '<td ' . $row_span . '>'
. ' <a href="tbl_indexes.php' . PMA_generate_common_url($this_params)
. '">' . PMA_getIcon('b_edit.png', $GLOBALS['strEdit']) . '</a>'
. '</td>' . "\n";
$this_params = $GLOBALS['url_params'];
if ($index->getName() == 'PRIMARY') {
$this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY';
$this_params['zero_rows'] = $GLOBALS['strPrimaryKeyHasBeenDropped'];
$js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP PRIMARY KEY');
} else {
$this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index->getName());
$this_params['zero_rows'] = sprintf($GLOBALS['strIndexHasBeenDropped'], $index->getName());
$js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP INDEX ' . $index->getName());
}
$r .= '<td ' . $row_span . '>'
. ' <a href="sql.php' . PMA_generate_common_url($this_params)
. '" onclick="return confirmLink(this, \'' . $js_msg . '\')">'
. PMA_getIcon('b_drop.png', $GLOBALS['strDrop']) . '</a>'
. '</td>' . "\n";
}
$r .= '<th ' . $row_span . '>' . htmlspecialchars($index->getName()) . '</th>';
$r .= '<td ' . $row_span . '>' . htmlspecialchars($index->getType()) . '</td>';
$r .= '<td ' . $row_span . '>' . $index->isUnique(true) . '</td>';
$r .= '<td ' . $row_span . '>' . $index->isPacked(true) . '</td>';
foreach ($index->getColumns() as $column) {
if ($column->getSeqInIndex() > 1) {
$r .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">';
}
$r .= '<td>' . htmlspecialchars($column->getName());
if ($column->getSubPart()) {
$r .= ' (' . $column->getSubPart() . ')';
}
$r .= '</td>';
$r .= '<td>' . htmlspecialchars($column->getCardinality()) . '</td>';
$r .= '<td>' . htmlspecialchars($column->getCollation()) . '</td>';
$r .= '<td>' . htmlspecialchars($column->getNull()) . '</td>';
if ($column->getSeqInIndex() == 1) {
$r .= '<td ' . $row_span . '>'
. htmlspecialchars($index->getComments()) . '</td>';
}
$r .= '</tr>';
} // end foreach $index['Sequences']
$odd_row = ! $odd_row;
} // end while
$r .= '</tbody>';
$r .= '</table>';
if (! $print_mode) {
//$r .= PMA_check_indexes($ret_keys);
}
return $r;
}
}
class PMA_Index_Column
{
/**
* @var string The column name
*/
protected $_name = '';
/**
* @var integer The column sequence number in the index, starting with 1.
*/
protected $_seq_in_index = 1;
/**
* @var string How the column is sorted in the index. “A” (Ascending) or NULL (Not sorted)
*/
protected $_collation = null;
/**
* The number of indexed characters if the column is only partly indexed,
* NULL if the entire column is indexed.
*
* @var integer
*/
protected $_sub_part = null;
/**
* Contains YES if the column may contain NULL.
* If not, the column contains NO.
*
* @var string
*/
protected $_null = '';
/**
* An estimate of the number of unique values in the index. This is updated
* by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on
* statistics stored as integers, so the value is not necessarily exact even
* for small tables. The higher the cardinality, the greater the chance that
* MySQL uses the index when doing joins.
*
* @var integer
*/
protected $_cardinality = 0;
public function __construct($params = array())
{
$this->set($params);
}
public function set($params)
{
if (isset($params['Column_name'])) {
$this->_name = $params['Column_name'];
}
if (isset($params['Seq_in_index'])) {
$this->_seq_in_index = $params['Seq_in_index'];
}
if (isset($params['Collation'])) {
$this->_collation = $params['Collation'];
}
if (isset($params['Cardinality'])) {
$this->_cardinality = $params['Cardinality'];
}
if (isset($params['Sub_part'])) {
$this->_sub_part = $params['Sub_part'];
}
if (isset($params['Null'])) {
$this->_null = $params['Null'];
}
}
public function getName()
{
return $this->_name;
}
public function getCollation()
{
return $this->_collation;
}
public function getCardinality()
{
return $this->_cardinality;
}
public function getNull()
{
return $this->_null;
}
public function getSeqInIndex()
{
return $this->_seq_in_index;
}
public function getSubPart()
{
return $this->_sub_part;
}
}
?>

View File

@@ -131,13 +131,12 @@ function PMA_check_indexes($idx_collection)
* @param array Referenced Array of indexes * @param array Referenced Array of indexes
* @param array Referenced return array * @param array Referenced return array
* @param array Referenced return array * @param array Referenced return array
* @param array Referenced return array
* *
* @access public * @access public
* @return boolean void * @return boolean void
* @author Garvin Hicking (pma@supergarv.de) * @author Garvin Hicking (pma@supergarv.de)
*/ */
function PMA_extract_indexes(&$ret_keys, &$indexes, &$indexes_info, &$indexes_data) function PMA_extract_indexes(&$ret_keys, &$indexes_info, &$indexes_data)
{ {
if (! is_array($ret_keys)) { if (! is_array($ret_keys)) {
return false; return false;
@@ -175,120 +174,4 @@ function PMA_extract_indexes(&$ret_keys, &$indexes, &$indexes_info, &$indexes_da
return true; return true;
} }
/**
* Show index data and prepare returned collection array for index
* key checks.
*
* @param string $table The tablename
* @param array $indexes Referenced Array of indexes
* @param array $indexes_info Referenced info array
* @param array $indexes_data Referenced data array
* @param boolean $display_html Output HTML code, or just return collection array?
* @param boolean $print_mode
* @access public
* @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)
{
$idx_collection = array();
$odd_row = true;
foreach ($indexes as $index_name) {
if ($display_html) {
$row_span = ' rowspan="' . count($indexes_info[$index_name]['Sequences']) . '" ';
echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n";
echo ' <th ' . $row_span . '>' . "\n"
. ' ' . htmlspecialchars($index_name) . "\n"
. ' </th>' . "\n";
}
if ($indexes_info[$index_name]['Index_type'] == 'FULLTEXT') {
$index_type = 'FULLTEXT';
} elseif ($index_name == 'PRIMARY') {
$index_type = 'PRIMARY';
} elseif ($indexes_info[$index_name]['Non_unique'] == '0') {
$index_type = 'UNIQUE';
} else {
$index_type = 'INDEX';
}
if ($display_html) {
echo ' <td ' . $row_span . '>' . "\n"
. ' ' . $index_type . '</td>' . "\n";
echo ' <td ' . $row_span . ' align="right">' . "\n"
. ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $GLOBALS['strNone']) . '&nbsp;' . "\n"
. ' </td>' . "\n";
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"
. ' </td>' . "\n";
if ($index_name == 'PRIMARY') {
$local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
$js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
$zero_rows = urlencode($GLOBALS['strPrimaryKeyHasBeenDropped']);
} else {
$local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
$js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
$zero_rows = urlencode(sprintf($GLOBALS['strIndexHasBeenDropped'], htmlspecialchars($index_name)));
}
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"
. ' </td>' . "\n";
}
}
foreach ($indexes_info[$index_name]['Sequences'] AS $row_no => $seq_index) {
$col_name = $indexes_data[$index_name][$seq_index]['Column_name'];
if ($row_no == 0) {
if (isset($idx_collection[$index_type][$col_name])) {
$idx_collection[$index_type][$col_name]++;
} else {
$idx_collection[$index_type][$col_name] = 1;
}
if (isset($idx_collection['ALL'][$col_name])) {
$idx_collection['ALL'][$col_name]++;
} else {
$idx_collection['ALL'][$col_name] = 1;
}
}
if ($display_html) {
if ($row_no > 0) {
echo ' <tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n";
}
if (isset($indexes_data[$index_name][$seq_index]['Sub_part'])
&& strlen($indexes_data[$index_name][$seq_index]['Sub_part'])) {
echo ' <td>' . $col_name . '</td>' . "\n";
echo ' <td align="right">' . "\n"
. ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
. ' </td>' . "\n";
echo ' </tr>' . "\n";
} else {
echo ' <td colspan="2">' . "\n"
. ' ' . htmlspecialchars($col_name) . "\n"
. ' </td>' . "\n";
echo ' </tr>' . "\n";
}
}
} // end foreach $indexes_info[$index_name]['Sequences']
$odd_row = ! $odd_row;
} // end while
return $idx_collection;
}
?> ?>

View File

@@ -205,8 +205,8 @@ function get_pk_or_unique_keys()
$ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]); $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]);
if (! empty($ret_keys)) { if (! empty($ret_keys)) {
// reset those as the function uses them by reference // reset those as the function uses them by reference
$indexes = $indexes_info = $indexes_data = array(); $indexes_info = $indexes_data = array();
PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data); PMA_extract_indexes($ret_keys, $indexes_info, $indexes_data);
// for now, take into account only the first index segment // for now, take into account only the first index segment
foreach ($indexes_data as $key_name => $one_index) { foreach ($indexes_data as $key_name => $one_index) {
$column_name = $one_index[1]['Column_name']; $column_name = $one_index[1]['Column_name'];
@@ -242,8 +242,8 @@ function get_all_keys()
$ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]); $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]);
if (! empty($ret_keys)) { if (! empty($ret_keys)) {
// reset those as the function uses them by reference // reset those as the function uses them by reference
$indexes = $indexes_info = $indexes_data = array(); $indexes_info = $indexes_data = array();
PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data); PMA_extract_indexes($ret_keys, $indexes_info, $indexes_data);
// for now, take into account only the first index segment // for now, take into account only the first index segment
foreach ($indexes_data as $one_index) { foreach ($indexes_data as $one_index) {
$column_name = $one_index[1]['Column_name']; $column_name = $one_index[1]['Column_name'];

View File

@@ -1,7 +1,7 @@
<?php <?php
/* vim: set expandtab sw=4 ts=4 sts=4: */ /* vim: set expandtab sw=4 ts=4 sts=4: */
/** /**
* display information about indexes in a table * Displays index edit/creation form and handles it
* *
* @version $Id$ * @version $Id$
*/ */
@@ -11,79 +11,72 @@
*/ */
require_once './libraries/common.inc.php'; require_once './libraries/common.inc.php';
require_once './libraries/tbl_indexes.lib.php'; require_once './libraries/tbl_indexes.lib.php';
require_once './libraries/Index.class.php';
/** /**
* Ensures the db & table are valid, then loads headers and gets indexes * Ensures the db & table are valid, then loads headers and gets indexes
* informations. * informations.
* Skipped if this script is called by "tbl_sql.php" * Skipped if this script is called by "tbl_sql.php"
*/ */
if (!defined('PMA_IDX_INCLUDED')) { // Not a valid db name -> back to the welcome page
// Not a valid db name -> back to the welcome page if (strlen($db)) {
if (strlen($db)) { $is_db = PMA_DBI_select_db($db);
$is_db = PMA_DBI_select_db($db); }
if (!strlen($db) || !$is_db) {
$uri_params = array('reload' => '1');
if (isset($message)) {
$uri_params['message'] = $message;
} }
if (!strlen($db) || !$is_db) { PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php'
$uri_params = array('reload' => '1'); . PMA_generate_common_url($uri_params, '&'));
if (isset($message)) { exit;
$uri_params['message'] = $message; }
} // Not a valid table name -> back to the default db sub-page
PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php' if (strlen($table)) {
. PMA_generate_common_url($uri_params, '&')); $is_table = PMA_DBI_query('SHOW TABLES LIKE \''
exit; . PMA_sqlAddslashes($table, TRUE) . '\'', null, PMA_DBI_QUERY_STORE);
} }
// Not a valid table name -> back to the default db sub-page if (! strlen($table)
if (strlen($table)) { || !($is_table && PMA_DBI_num_rows($is_table))) {
$is_table = PMA_DBI_query('SHOW TABLES LIKE \'' $uri_params = array('reload' => '1', 'db' => $db);
. PMA_sqlAddslashes($table, TRUE) . '\'', null, PMA_DBI_QUERY_STORE); if (isset($message)) {
} $uri_params['message'] = $message;
if (! strlen($table)
|| !($is_table && PMA_DBI_num_rows($is_table))) {
$uri_params = array('reload' => '1', 'db' => $db);
if (isset($message)) {
$uri_params['message'] = $message;
}
PMA_sendHeaderLocation($cfg['PmaAbsoluteUri']
. $cfg['DefaultTabDatabase']
. PMA_generate_common_url($uri_params, '&'));
exit;
} elseif (isset($is_table)) {
PMA_DBI_free_result($is_table);
} }
PMA_sendHeaderLocation($cfg['PmaAbsoluteUri']
. $cfg['DefaultTabDatabase']
. PMA_generate_common_url($uri_params, '&'));
exit;
} elseif (isset($is_table)) {
PMA_DBI_free_result($is_table);
}
// Displays headers (if needed) // Displays headers (if needed)
$GLOBALS['js_include'][] = 'functions.js'; $GLOBALS['js_include'][] = 'functions.js';
$GLOBALS['js_include'][] = 'indexes.js'; $GLOBALS['js_include'][] = 'indexes.js';
require_once './libraries/header.inc.php'; require_once './libraries/header.inc.php';
} // end if
/** /**
* Gets fields and indexes informations * Gets fields and indexes informations
*/ */
if (!defined('PMA_IDX_INCLUDED')) { $err_url_0 = 'db_sql.php?' . PMA_generate_common_url($db);
$err_url_0 = 'db_sql.php?' . PMA_generate_common_url($db);
}
// Gets table keys and store them in arrays // Gets table keys and store them in arrays
$indexes = array();
$indexes_info = array(); $indexes_info = array();
$indexes_data = array(); $indexes_data = array();
// keys had already been grabbed in "tbl_sql.php" $ret_keys = PMA_get_indexes($table, $err_url_0);
if (!defined('PMA_IDX_INCLUDED')) {
$ret_keys = PMA_get_indexes($table, $err_url_0);
}
PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data); PMA_extract_indexes($ret_keys, $indexes_info, $indexes_data);
$indexes = PMA_Index::getFromTable($table, $db);
// Get fields and stores their name/type // Get fields and stores their name/type
// fields had already been grabbed in "tbl_sql.php" // fields had already been grabbed in "tbl_sql.php"
if (!defined('PMA_IDX_INCLUDED')) { $fields_rs = PMA_DBI_query('SHOW FIELDS FROM '
$fields_rs = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
. PMA_backquote($table) . ';'); $save_row = array();
$save_row = array(); while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
while ($row = PMA_DBI_fetch_assoc($fields_rs)) { $save_row[] = $row;
$save_row[] = $row;
}
} }
$fields_names = array(); $fields_names = array();
@@ -109,9 +102,7 @@ if ($fields_rs) {
* Do run the query to build the new index and moves back to * Do run the query to build the new index and moves back to
* "tbl_sql.php" * "tbl_sql.php"
*/ */
if (!defined('PMA_IDX_INCLUDED') if (isset($index) && isset($do_save_data)) {
&& (isset($index) && isset($do_save_data))) {
$err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table); $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
if (empty($old_index)) { if (empty($old_index)) {
$err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields; $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
@@ -188,8 +179,7 @@ if (!defined('PMA_IDX_INCLUDED')
/** /**
* Edits an index or defines a new one * Edits an index or defines a new one
*/ */
elseif (!defined('PMA_IDX_INCLUDED') elseif (isset($index) || isset($create_index)) {
&& (isset($index) || isset($create_index))) {
// Prepares the form values // Prepares the form values
if (!isset($index)) { if (!isset($index)) {
@@ -256,8 +246,9 @@ elseif (!defined('PMA_IDX_INCLUDED')
<form action="./tbl_indexes.php" method="post" name="index_frm" <form action="./tbl_indexes.php" method="post" name="index_frm"
onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') { onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {
this.elements['index'].disabled = false}"> this.elements['index'].disabled = false}">
<?php echo PMA_generate_common_hidden_inputs($db, $table); ?> <?php
<?php echo PMA_generate_common_hidden_inputs($db, $table);
if (isset($create_index)) { if (isset($create_index)) {
echo '<input type="hidden" name="create_index" value="1" />' . "\n"; echo '<input type="hidden" name="create_index" value="1" />' . "\n";
} }
@@ -380,97 +371,11 @@ PMA_Message::warning('strPrimaryKeyWarning')->display();
</fieldset> </fieldset>
</form> </form>
</div> </div>
<?php
} else {
/**
* Display indexes
*/
?>
<form action="./tbl_indexes.php" method="post"
onsubmit="return checkFormElementInRange(this, 'idx_num_fields',
'<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
1)">
<?php <?php
echo PMA_generate_common_hidden_inputs($db, $table); }
?>
<table id="table_indexes" class="data">
<caption class="tblHeaders">
<?php
echo $strIndexes . ':' . "\n";
echo ' ' . PMA_showMySQLDocu('optimization',
'optimizing-database-structure');
?>
</caption>
<?php
if (count($ret_keys) > 0) {
$edit_link_text = '';
$drop_link_text = '';
if ($cfg['PropertiesIconic'] === true || $cfg['PropertiesIconic'] === 'both') {
$edit_link_text = '<img class="icon" src="' . $pmaThemeImage
. 'b_edit.png" width="16" height="16" title="' . $strEdit
. '" alt="' . $strEdit . '" />';
$drop_link_text = '<img class="icon" src="' . $pmaThemeImage
. 'b_drop.png" width="16" height="16" title="' . $strDrop
. '" alt="' . $strDrop . '" />';
}
if ($cfg['PropertiesIconic'] === false || $cfg['PropertiesIconic'] === 'both') {
$edit_link_text .= $strEdit;
$drop_link_text .= $strDrop;
}
if ($cfg['PropertiesIconic'] === 'both') {
$edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
$drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
}
?>
<thead>
<tr><th><?php echo $strKeyname; ?></th>
<th><?php echo $strType; ?></th>
<th><?php echo $strCardinality; ?></th>
<th colspan="2"><?php echo $strAction; ?></th>
<th colspan="2"><?php echo $strField; ?></th>
</tr>
</thead>
<tbody>
<?php
$idx_collection = PMA_show_indexes($table, $indexes, $indexes_info,
$indexes_data, true);
echo PMA_check_indexes($ret_keys);
} // end display indexes
else {
// none indexes
echo '<tbody>'
.'<tr><td colspan="7">';
PMA_Message::warning('strNoIndex')->display();
echo '</td></tr>' . "\n";
}
?>
<tr class="tblFooters"><td colspan="7">
<?php echo sprintf($strCreateIndex,
'<input type="text" size="2" name="idx_num_fields" value="1" />'); ?>
<input type="submit" name="create_index" value="<?php echo $strGo; ?>"
onclick="return checkFormElementInRange(this.form,
'idx_num_fields',
'<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
1)" />
</td></tr>
</tbody>
</table>
</form>
<?php
} // end display indexes
/** /**
* Displays the footer * Displays the footer
*/ */
echo "\n"; require_once './libraries/footer.inc.php';
if (!defined('PMA_IDX_INCLUDED')){
require_once './libraries/footer.inc.php';
}
?> ?>

View File

@@ -32,6 +32,7 @@ if (! isset($the_tables) || ! is_array($the_tables)) {
require_once './libraries/relation.lib.php'; require_once './libraries/relation.lib.php';
require_once './libraries/transformations.lib.php'; require_once './libraries/transformations.lib.php';
require_once './libraries/tbl_indexes.lib.php'; require_once './libraries/tbl_indexes.lib.php';
require_once './libraries/Index.class.php';
$cfgRelation = PMA_getRelationsParam(); $cfgRelation = PMA_getRelationsParam();
@@ -101,14 +102,6 @@ foreach ($the_tables as $key => $table) {
$tbl_is_view = PMA_Table::isView($db, $table); $tbl_is_view = PMA_Table::isView($db, $table);
// Gets table keys and store them in arrays
$indexes = array();
$indexes_info = array();
$indexes_data = array();
$ret_keys = PMA_get_indexes($table, $err_url_0);
PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
/** /**
* Gets fields properties * Gets fields properties
*/ */
@@ -281,41 +274,12 @@ foreach ($the_tables as $key => $table) {
?> ?>
</tbody> </tbody>
</table> </table>
<?php <?php
if (! $tbl_is_view && $db != 'information_schema') { if (! $tbl_is_view && $db != 'information_schema') {
/** /**
* Displays indexes * Displays indexes
*/ */
$index_count = (isset($indexes)) echo PMA_Index::getView($table, $db, true);
? count($indexes)
: 0;
if ($index_count > 0) {
echo "\n";
?>
<br /><br />
<!-- Indexes -->
<big><?php echo $strIndexes . ':'; ?></big>
<table>
<tr>
<th><?php echo $strKeyname; ?></th>
<th><?php echo $strType; ?></th>
<th><?php echo $strCardinality; ?></th>
<th colspan="2"><?php echo $strField; ?></th>
</tr>
<?php
echo "\n";
PMA_show_indexes($table, $indexes, $indexes_info, $indexes_data, true, true);
echo "\n";
?>
</table>
<?php
echo "\n";
} // end display indexes
/** /**
* Displays Space usage and row statistics * Displays Space usage and row statistics
@@ -522,7 +486,7 @@ foreach ($the_tables as $key => $table) {
} // end if ($cfg['ShowStats']) } // end if ($cfg['ShowStats'])
} }
if ($multi_tables) { if ($multi_tables) {
unset($ret_keys, $num_rows, $show_comment); unset($num_rows, $show_comment);
echo '<hr />' . "\n"; echo '<hr />' . "\n";
} // end if } // end if
echo '</div>' . "\n"; echo '</div>' . "\n";

View File

@@ -87,6 +87,8 @@ $cfgRelation = PMA_getRelationsParam();
*/ */
require_once './libraries/tbl_common.php'; require_once './libraries/tbl_common.php';
$url_query .= '&amp;goto=tbl_structure.php&amp;back=tbl_structure.php'; $url_query .= '&amp;goto=tbl_structure.php&amp;back=tbl_structure.php';
$url_params['goto'] = 'tbl_structure.php';
$url_params['back'] = 'tbl_structure.php';
/** /**
* Prepares the table structure display * Prepares the table structure display
@@ -102,26 +104,18 @@ require_once './libraries/tbl_info.inc.php';
* Displays top menu links * Displays top menu links
*/ */
require_once './libraries/tbl_links.inc.php'; require_once './libraries/tbl_links.inc.php';
require_once './libraries/Index.class.php';
// 2. Gets table keys and retains them // 2. Gets table keys and retains them
$result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ';'); // @todo should be: $server->db($db)->table($table)->primary()
$primary = ''; $primary = PMA_Index::getPrimary($table, $db);
$ret_keys = array();
$pk_array = array(); // will be use to emphasis prim. keys in the table view
while ($row = PMA_DBI_fetch_assoc($result)) {
$ret_keys[] = $row;
// Backups the list of primary keys
if ($row['Key_name'] == 'PRIMARY') {
$primary .= $row['Column_name'] . ', ';
$pk_array[$row['Column_name']] = 1;
}
} // end while
PMA_DBI_free_result($result);
// 3. Get fields // 3. Get fields
$fields_rs = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE); $fields_rs = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
$fields_cnt = PMA_DBI_num_rows($fields_rs); $fields_cnt = PMA_DBI_num_rows($fields_rs);
// Get more complete field information // Get more complete field information
// For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
// but later, if the analyser returns more information, it // but later, if the analyser returns more information, it
@@ -327,7 +321,7 @@ while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
$field_name = '<span style="border-bottom: 1px dashed black;" title="' . htmlspecialchars($comments_map[$row['Field']]) . '">' . $field_name . '</span>'; $field_name = '<span style="border-bottom: 1px dashed black;" title="' . htmlspecialchars($comments_map[$row['Field']]) . '">' . $field_name . '</span>';
} }
if (isset($pk_array[$row['Field']])) { if ($primary && $primary->hasColumn($field_name)) {
$field_name = '<u>' . $field_name . '</u>'; $field_name = '<u>' . $field_name . '</u>';
} }
echo "\n"; echo "\n";
@@ -364,8 +358,8 @@ while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
} else { } else {
echo "\n"; echo "\n";
?> ?>
<a href="sql.php?<?php echo $url_query; ?>&amp;sql_query=<?php echo urlencode('ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY(' . PMA_backquote($row['Field']) . ')'); ?>&amp;zero_rows=<?php echo urlencode(sprintf($strAPrimaryKey, htmlspecialchars($row['Field']))); ?>" <a href="sql.php?<?php echo $url_query; ?>&amp;sql_query=<?php echo urlencode('ALTER TABLE ' . PMA_backquote($table) . ($primary ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY(' . PMA_backquote($row['Field']) . ')'); ?>&amp;zero_rows=<?php echo urlencode(sprintf($strAPrimaryKey, htmlspecialchars($row['Field']))); ?>"
onclick="return confirmLink(this, 'ALTER TABLE <?php echo PMA_jsFormat($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,'); ?> ADD PRIMARY KEY(<?php echo PMA_jsFormat($row['Field']); ?>)')"> onclick="return confirmLink(this, 'ALTER TABLE <?php echo PMA_jsFormat($table) . ($primary ? '' : ' DROP PRIMARY KEY,'); ?> ADD PRIMARY KEY(<?php echo PMA_jsFormat($row['Field']); ?>)')">
<?php echo $titles['Primary']; ?></a> <?php echo $titles['Primary']; ?></a>
<?php <?php
} }
@@ -531,17 +525,40 @@ if (! $tbl_is_view && ! $db_is_information_schema) {
if ($fields_cnt > 20) { if ($fields_cnt > 20) {
require './libraries/tbl_links.inc.php'; require './libraries/tbl_links.inc.php';
} // end if ($fields_cnt > 20) } // end if ($fields_cnt > 20)
echo "\n\n";
/** /**
* Displays indexes * Displays indexes
*/ */
PMA_generate_slider_effect('tablestatistics', $strDetails); PMA_generate_slider_effect('tablestatistics_indexes', $strDetails);
echo '<div id="tablestatistics">' . "\n"; echo '<div id="tablestatistics_indexes">' . "\n";
if (! $tbl_is_view && ! $db_is_information_schema) { if (! $tbl_is_view && ! $db_is_information_schema) {
define('PMA_IDX_INCLUDED', 1); /**
require './tbl_indexes.php'; * Display indexes
*/
echo PMA_Index::getView($table, $db);
?>
<br />
<form action="./tbl_indexes.php" method="post"
onsubmit="return checkFormElementInRange(this, 'idx_num_fields',
'<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
1)">
<fieldset>
<?php
echo PMA_generate_common_hidden_inputs($db, $table);
echo sprintf($strCreateIndex,
'<input type="text" size="2" name="idx_num_fields" value="1" />');
?>
<input type="submit" name="create_index" value="<?php echo $strGo; ?>"
onclick="return checkFormElementInRange(this.form,
'idx_num_fields',
'<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>',
1)" />
</fieldset>
</form>
<br />
<?php
} }
echo '<div id="tablestatistics">' . "\n";
/** /**
* Displays Space usage and row statistics * Displays Space usage and row statistics
@@ -769,6 +786,7 @@ require './libraries/tbl_triggers.lib.php';
echo '<div class="clearfloat"></div>' . "\n"; echo '<div class="clearfloat"></div>' . "\n";
echo '</div>' . "\n"; echo '</div>' . "\n";
echo '</div>' . "\n";
/** /**
* Displays the footer * Displays the footer