diff --git a/libraries/Index.class.php b/libraries/Index.class.php index a677bb326..bf97de36f 100644 --- a/libraries/Index.class.php +++ b/libraries/Index.class.php @@ -20,40 +20,40 @@ class PMA_Index protected static $_registry = array(); /** - * @var string The name of the database + * @var string The name of the schema */ - protected $_database = ''; + protected $_schema = ''; /** * @var string The name of the table */ - protected $_table = ''; + protected $_table = ''; /** * @var string The name of the index */ - protected $_name = ''; + protected $_name = ''; /** * Columns in index * * @var array */ - protected $_columns = array(); + protected $_columns = array(); /** * The index method used (BTREE, FULLTEXT, HASH, RTREE). * * @var string */ - protected $_type = ''; + protected $_type = ''; /** * Various remarks. * * @var string */ - protected $_remarks = ''; + protected $_remarks = ''; /** * Any comment provided for the index with a COMMENT attribute when the @@ -61,19 +61,19 @@ class PMA_Index * * @var string */ - protected $_comment = ''; + protected $_comment = ''; /** * @var integer 0 if the index cannot contain duplicates, 1 if it can. */ - protected $_non_unique = 0; + protected $_non_unique = 0; /** * Indicates how the key is packed. NULL if it is not. * * @var string */ - protected $_packed = null; + protected $_packed = null; /** * Constructor @@ -86,21 +86,36 @@ class PMA_Index $this->set($params); } + static public function singleton($schema, $table, $index_name = '') + { + PMA_Index::_loadIndexes($table, $schema); + if (! isset(PMA_Index::$_registry[$schema][$table][$index_name])) { + $index = new PMA_Index; + if (strlen($index_name)) { + $index->setName($index_name); + PMA_Index::$_registry[$schema][$table][$index->getName()] = $index; + } + return $index; + } else { + return PMA_Index::$_registry[$schema][$table][$index_name]; + } + } + /** * returns an array with all indexes from the given table * * @uses PMA_Index::_loadIndexes() * @uses PMA_Index::$_registry * @param string $table - * @param string $database + * @param string $schema * @return array */ - static public function getFromTable($table, $database) + static public function getFromTable($table, $schema) { - PMA_Index::_loadIndexes($table, $database); + PMA_Index::_loadIndexes($table, $schema); - if (isset(PMA_Index::$_registry[$database][$table])) { - return PMA_Index::$_registry[$database][$table]; + if (isset(PMA_Index::$_registry[$schema][$table])) { + return PMA_Index::$_registry[$schema][$table]; } else { return array(); } @@ -112,15 +127,15 @@ class PMA_Index * @uses PMA_Index::_loadIndexes() * @uses PMA_Index::$_registry * @param string $table - * @param string $database + * @param string $schema * @return mixed primary index or false if no one exists */ - static public function getPrimary($table, $database) + static public function getPrimary($table, $schema) { - PMA_Index::_loadIndexes($table, $database); + PMA_Index::_loadIndexes($table, $schema); - if (isset(PMA_Index::$_registry[$database][$table]['PRIMARY'])) { - return PMA_Index::$_registry[$database][$table]['PRIMARY']; + if (isset(PMA_Index::$_registry[$schema][$table]['PRIMARY'])) { + return PMA_Index::$_registry[$schema][$table]['PRIMARY']; } else { return false; } @@ -135,22 +150,23 @@ class PMA_Index * @uses PMA_Index * @uses PMA_Index->addColumn() * @param string $table - * @param string $database + * @param string $schema * @return boolean */ - static protected function _loadIndexes($table, $database) + static protected function _loadIndexes($table, $schema) { - if (isset(PMA_Index::$_registry[$database][$table])) { + if (isset(PMA_Index::$_registry[$schema][$table])) { return true; } - $_raw_indexes = PMA_DBI_fetch_result('SHOW INDEX FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table)); + $_raw_indexes = PMA_DBI_fetch_result('SHOW INDEX FROM ' . PMA_backquote($schema) . '.' . PMA_backquote($table)); foreach ($_raw_indexes as $_each_index) { - if (! isset(PMA_Index::$_registry[$database][$table][$_each_index['Key_name']])) { + $_each_index['Schema'] = $schema; + if (! isset(PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']])) { $key = new PMA_Index($_each_index); - PMA_Index::$_registry[$database][$table][$_each_index['Key_name']] = $key; + PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']] = $key; } else { - $key = PMA_Index::$_registry[$database][$table][$_each_index['Key_name']]; + $key = PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']]; } $key->addColumn($_each_index); @@ -168,7 +184,36 @@ class PMA_Index */ public function addColumn($params) { - $this->_columns[$params['Column_name']] = new PMA_Index_Column($params); + if (strlen($params['Column_name'])) { + $this->_columns[$params['Column_name']] = new PMA_Index_Column($params); + } + } + + public function addColumns($columns) + { + $_columns = array(); + + if (isset($columns['names'])) { + // coming from form + // $columns[names][] + // $columns[sub_parts][] + foreach ($columns['names'] as $key => $name) { + $_columns[] = array( + 'Column_name' => $name, + 'Sub_part' => $columns['sub_parts'][$key], + ); + } + } else { + // coming from SHOW INDEXES + // $columns[][name] + // $columns[][sub_part] + // ... + $_columns = $columns; + } + + foreach ($_columns as $column) { + $this->addColumn($column); + } } /** @@ -185,8 +230,11 @@ class PMA_Index public function set($params) { - if (isset($params['Column_name'])) { - $this->_database = $params['Column_name']; + if (isset($params['columns'])) { + $this->addColumns($params['columns']); + } + if (isset($params['Schema'])) { + $this->_schema = $params['Schema']; } if (isset($params['Table'])) { $this->_table = $params['Table']; @@ -295,6 +343,11 @@ class PMA_Index return $this->_name; } + public function setName($name) + { + $this->_name = (string) $name; + } + public function getColumns() { return $this->_columns; diff --git a/tbl_indexes.php b/tbl_indexes.php index f0cc49b3c..68f280eb9 100644 --- a/tbl_indexes.php +++ b/tbl_indexes.php @@ -12,49 +12,7 @@ require_once './libraries/common.inc.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 - * informations. - * Skipped if this script is called by "tbl_sql.php" - */ -// Not a valid db name -> back to the welcome page -if (strlen($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; - } - PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php' - . PMA_generate_common_url($uri_params, '&')); - exit; -} -// Not a valid table name -> back to the default db sub-page -if (strlen($table)) { - $is_table = PMA_DBI_query('SHOW TABLES LIKE \'' - . PMA_sqlAddslashes($table, TRUE) . '\'', null, PMA_DBI_QUERY_STORE); -} -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); -} - -// Displays headers (if needed) -$GLOBALS['js_include'][] = 'functions.js'; -$GLOBALS['js_include'][] = 'indexes.js'; -require_once './libraries/header.inc.php'; - +require_once './libraries/tbl_common.php'; /** * Gets fields and indexes informations @@ -68,232 +26,176 @@ $ret_keys = PMA_get_indexes($table, $err_url_0); PMA_extract_indexes($ret_keys, $indexes_info, $indexes_data); -$indexes = PMA_Index::getFromTable($table, $db); - // Get fields and stores their name/type -// fields had already been grabbed in "tbl_sql.php" -$fields_rs = PMA_DBI_query('SHOW FIELDS FROM ' - . PMA_backquote($table) . ';'); -$save_row = array(); -while ($row = PMA_DBI_fetch_assoc($fields_rs)) { - $save_row[] = $row; -} - -$fields_names = array(); -$fields_types = array(); -foreach ($save_row AS $saved_row_key => $row) { - $fields_names[] = $row['Field']; - // loic1: set or enum types: slashes single quotes inside options +$fields = array(); +foreach (PMA_DBI_get_fields($db, $table) as $row) { if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) { $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1); - $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; + $fields[$row['Field']] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; } else { - $fields_types[] = $row['Type']; + $fields[$row['Field']] = $row['Type']; } } // end while -if ($fields_rs) { - PMA_DBI_free_result($fields_rs); +// Prepares the form values +if (isset($_REQUEST['index'])) { + if (is_array($_REQUEST['index'])) { + // coming already from form + $index = new PMA_Index($_REQUEST['index']); + } else { + $index = PMA_Index::singleton($db, $table, $_REQUEST['index']); + } +} else { + $index = new PMA_Index; } - /** - * Do run the query to build the new index and moves back to - * "tbl_sql.php" + * Process the data from the edit/create index form, + * run the query to build the new index + * and moves back to "tbl_sql.php" */ -if (isset($index) && isset($do_save_data)) { - $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table); - if (empty($old_index)) { - $err_url .= '&create_index=1&idx_num_fields=' . $idx_num_fields; - } else { - $err_url .= '&index=' . urlencode($old_index); - } - - if ($index_type == 'PRIMARY') { - if ($index == '') { - $index = 'PRIMARY'; - } elseif ($index != 'PRIMARY') { - PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url); - } - } elseif ($index == 'PRIMARY') { - PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url); - } - - +if (isset($_REQUEST['do_save_data'])) { + $error = false; + // $sql_query is the one displayed in the query box - $sql_query = 'ALTER TABLE ' . PMA_backquote($table); + $sql_query = 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table); // Drops the old index - if (!empty($old_index)) { - if ($old_index == 'PRIMARY') { + if (! empty($_REQUEST['old_index'])) { + if ($_REQUEST['old_index'] == 'PRIMARY') { $sql_query .= ' DROP PRIMARY KEY,'; } else { - $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .','; + $sql_query .= ' DROP INDEX ' . PMA_backquote($_REQUEST['old_index']) . ','; } } // end if // Builds the new one - switch ($index_type) { + switch ($index->getType()) { case 'PRIMARY': - $sql_query .= ' ADD PRIMARY KEY ('; + if ($index->getName() == '') { + $index->setName('PRIMARY'); + } elseif ($index->getName() != 'PRIMARY') { + $error = PMA_Message::error('strPrimaryKeyName'); + } + $sql_query .= ' ADD PRIMARY KEY'; break; case 'FULLTEXT': - $sql_query .= ' ADD FULLTEXT ' - . (empty($index) ? '' : PMA_backquote($index)) . ' ('; - break; case 'UNIQUE': - $sql_query .= ' ADD UNIQUE ' - . (empty($index) ? '' : PMA_backquote($index)) . ' ('; - break; case 'INDEX': - $sql_query .= ' ADD INDEX ' - . (empty($index) ? '' : PMA_backquote($index)) . ' ('; + if ($index->getName() == 'PRIMARY') { + $error = PMA_Message::error('strCantRenameIdxToPrimary'); + } + $sql_query .= ' ADD ' . $index->getType() . ' ' + . ($index->getName() ? PMA_backquote($index->getName()) : ''); break; } // end switch - $index_fields = ''; - foreach ($column AS $i => $name) { - if ($name != '--ignore--') { - $index_fields .= (empty($index_fields) ? '' : ',') - . PMA_backquote($name) - . (empty($sub_part[$i]) - ? '' - : '(' . $sub_part[$i] . ')'); + + $index_fields = array(); + foreach ($index->getColumns() as $key => $column) { + $index_fields[$key] = PMA_backquote($column->getName()); + if ($column->getSubPart()) { + $index_fields[$key] .= '(' . $column->getSubPart() . ')'; } } // end while + if (empty($index_fields)){ - PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url); + $error = PMA_Message::error('strNoIndexPartsDefined'); } else { - $sql_query .= $index_fields . ')'; + $sql_query .= ' (' . implode(', ', $index_fields) . ')'; + } + + if (! $error) { + PMA_DBI_query($sql_query); + $message = PMA_Message::success('strTableAlteredSuccessfully'); + $message->addParam($table); + + $active_page = 'tbl_structure.php'; + require './tbl_structure.php'; + exit; + } else { + $error->display(); } - - $result = PMA_DBI_query($sql_query); - $message = PMA_Message::success('strTableAlteredSuccessfully'); - $message->addParam($table); - - $active_page = 'tbl_structure.php'; - require './tbl_structure.php'; } // end builds the new index /** - * Edits an index or defines a new one + * Display the form to edit/create an index */ -elseif (isset($index) || isset($create_index)) { - // Prepares the form values - if (!isset($index)) { - $index = ''; +// Displays headers (if needed) +$GLOBALS['js_include'][] = 'functions.js'; +$GLOBALS['js_include'][] = 'indexes.js'; + +require_once './libraries/tbl_info.inc.php'; +require_once './libraries/tbl_links.inc.php'; + +if (is_array($_REQUEST['index'])) { + // coming already from form + $add_fields = + count($_REQUEST['index']['columns']['names']) - $index->getColumnCount(); + if (isset($_REQUEST['add_fields'])) { + $add_fields += $_REQUEST['added_fields']; } - if (!isset($old_index)){ - $old_index = $index; - } - if (!isset($index_type)) { - $index_type = ''; - } - if ($old_index == '' || !isset($indexes_info[$old_index])) { - $edited_index_info['Sequences'] = array(); - $edited_index_data = array(); - for ($i = 1; $i <= $idx_num_fields; $i++) { - $edited_index_info['Sequences'][] = $i; - $edited_index_data[$i] = array('Column_name' => '', - 'Sub_part' => ''); - } // end for - if ($old_index == '' - && !isset($indexes_info['PRIMARY']) - && ($index_type == '' || $index_type == 'PRIMARY')) { - $old_index = 'PRIMARY'; - } - } else { - $edited_index_info = $indexes_info[$old_index]; - $edited_index_data = $indexes_data[$old_index]; +} elseif (isset($_REQUEST['create_index'])) { + $add_fields = $_REQUEST['added_fields']; +} else { + $add_fields = 1; +} +// end preparing form values +?> - if ($edited_index_info['Index_type'] == 'FULLTEXT') { - $index_type = 'FULLTEXT'; - } elseif ($index == 'PRIMARY') { - $index_type = 'PRIMARY'; - } elseif ($edited_index_info['Non_unique'] == '0') { - $index_type = 'UNIQUE'; - } else { - $index_type = 'INDEX'; - } - } // end if... else... - - if (isset($add_fields)) { - if (isset($prev_add_fields)) { - $added_fields += $prev_add_fields; - } - $field_cnt = count($edited_index_info['Sequences']) + 1; - for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) { - $edited_index_info['Sequences'][] = $i; - $edited_index_data[$i] = array('Column_name' => '', - 'Sub_part' => ''); - } // end for - - // Restore entered values - foreach ($column AS $i => $name) { - if ($name != '--ignore--'){ - $edited_index_data[$i+1]['Column_name'] = $name; - $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i]; - } - } // end while - } // end if - // end preparing form values - ?> - -
- ' . "\n"; - } - if (isset($added_fields)) { - echo ' ' . "\n"; - } - if (isset($idx_num_fields)) { - echo ' ' . "\n"; - } - ?> - + $db, + 'table' => $table, +); +if (isset($_REQUEST['create_index'])) { + $form_params['create_index'] = 1; +} elseif (isset($_REQUEST['old_index'])) { + $form_params['old_index'] = $_REQUEST['old_index']; +} elseif (isset($_REQUEST['index'])) { + $form_params['old_index'] = $_REQUEST['index']; +} + +echo PMA_generate_common_hidden_inputs($form_params); +?>
- +
- +
- +getName() !== 'PRIMARY' + && isset($indexes_info['PRIMARY'])) { + // skip PRIMARY if there is already one in the table + continue; } - ?> + echo '' . "\n"; +} +?>
@@ -311,68 +213,74 @@ PMA_Message::warning('strPrimaryKeyWarning')->display(); - - +getColumns() as $column) { + ?> - + + $field_type) { + if ($index->getType() != 'FULLTEXT' + || preg_match('/(char|text)/i', $field_type)) { + echo '' . "\n"; + } + } // end foreach $fields + ?> /> + name="index[columns][sub_parts][]" value="getSubPart(); ?>" /> - + + + + + + +
- ') . "\n"; - echo ' ' . "\n"; - ?> +') . "\n"; +echo '' . "\n"; +?>
-
- '); + ''); ?>