From 05c5ba753cd9f67019aa3f3ad125cc6be4296550 Mon Sep 17 00:00:00 2001 From: Madhura Jayaratne Date: Wed, 28 Jan 2015 11:50:21 +0530 Subject: [PATCH 1/3] Distinguish between index type and index choice Signed-off-by: Madhura Jayaratne --- js/functions.js | 4 +- js/indexes.js | 64 ++++++++++++------------ libraries/Index.class.php | 58 +++++++++++++++------ libraries/create_addfield.lib.php | 6 +-- libraries/tbl_indexes.lib.php | 19 +++---- libraries/tracking.lib.php | 2 +- tbl_indexes.php | 2 +- test/classes/PMA_Index_test.php | 6 +-- test/libraries/PMA_tbl_tracking_test.php | 4 +- 9 files changed, 96 insertions(+), 69 deletions(-) diff --git a/js/functions.js b/js/functions.js index df9697df7d..4091ecefac 100644 --- a/js/functions.js +++ b/js/functions.js @@ -3219,10 +3219,10 @@ function checkIndexName(form_id) // Gets the elements pointers var $the_idx_name = $("#input_index_name"); - var $the_idx_type = $("#select_index_type"); + var $the_idx_choice = $("#select_index_choice"); // Index is a primary key - if ($the_idx_type.find("option:selected").val() == 'PRIMARY') { + if ($the_idx_choice.find("option:selected").val() == 'PRIMARY') { $the_idx_name.val('PRIMARY'); $the_idx_name.prop("disabled", true); } diff --git a/js/indexes.js b/js/indexes.js index fbca67e9ac..c92218ee57 100644 --- a/js/indexes.js +++ b/js/indexes.js @@ -15,9 +15,9 @@ function checkIndexType() { /** - * @var Object Dropdown to select the index type. + * @var Object Dropdown to select the index choice. */ - $select_index_type = $('#select_index_type'); + $select_index_choice = $('#select_index_choice'); /** * @var Object Table header for the size column. */ @@ -35,7 +35,7 @@ function checkIndexType() */ $add_more = $('#index_frm .tblFooters'); - if ($select_index_type.val() == 'SPATIAL') { + if ($select_index_choice.val() == 'SPATIAL') { // Disable and hide the size column $size_header.hide(); $size_inputs.each(function () { @@ -84,16 +84,16 @@ function checkIndexType() * Sets current index information into form parameters. * * @param array source_array Array containing index columns - * @param string index_type Type of index + * @param string index_choice Choice of index * * @return void */ -function PMA_setIndexFormParameters(source_array, index_type) +function PMA_setIndexFormParameters(source_array, index_choice) { - if (index_type == 'index') { + if (index_choice == 'index') { $('input[name="indexes"]').val(JSON.stringify(source_array)); } else { - $('input[name="' + index_type + '_indexes"]').val(JSON.stringify(source_array)); + $('input[name="' + index_choice + '_indexes"]').val(JSON.stringify(source_array)); } } @@ -153,12 +153,12 @@ function PMA_removeColumnFromIndex(col_index) * * @param array source_array Array holding corresponding indexes * @param string array_index Index of an INDEX in array - * @param string index_type Type of Index + * @param string index_choice Choice of Index * @param string col_index Index of column on form * * @return void */ -function PMA_addColumnToIndex(source_array, array_index, index_type, col_index) +function PMA_addColumnToIndex(source_array, array_index, index_choice, col_index) { // Remove column from other indexes (if any). PMA_removeColumnFromIndex(col_index); @@ -179,20 +179,20 @@ function PMA_addColumnToIndex(source_array, array_index, index_type, col_index) source_array[array_index] = { 'Key_name': index_name, 'Index_comment': index_comment, - 'Index_type': index_type.toUpperCase(), + 'Index_choice': index_choice.toUpperCase(), 'columns': columns }; // Update index details on form. $('select[name="field_key[' + col_index + ']"]') - .attr('data-index', index_type + ',' + array_index); - PMA_setIndexFormParameters(source_array, index_type.toLowerCase()); + .attr('data-index', index_choice + ',' + array_index); + PMA_setIndexFormParameters(source_array, index_choice.toLowerCase()); } /** * Get choices list for a column to create a composite index with. * - * @param string index_type Type of index + * @param string index_choice Choice of index * @param array source_array Array hodling columns for particular index * * @return jQuery Object @@ -288,7 +288,7 @@ function PMA_showAddIndexDialog(source_array, array_index, target_columns, col_i PMA_addColumnToIndex( source_array, array_index, - index.Index_type, + index.Index_choice, col_index ); } else { @@ -354,21 +354,21 @@ function PMA_showAddIndexDialog(source_array, array_index, target_columns, col_i * Creates a advanced index type selection dialog. * * @param array source_array Array holding a particular type of indexes - * @param string index_type Type of index + * @param string index_choice Choice of index * @param string col_index Index of new column on form * * @return void */ -function PMA_indexTypeSelectionDialog(source_array, index_type, col_index) +function PMA_indexTypeSelectionDialog(source_array, index_choice, col_index) { - var $single_column_radio = $('' + ''); var $composite_index_radio = $('' + + ' name="index_choice">' + ''); var $dialog_content = $('
'); - $dialog_content.append('' + index_type.toUpperCase() + ''); + $dialog_content.append('' + index_choice.toUpperCase() + ''); // For UNIQUE/INDEX type, show choice for single-column and composite index. @@ -380,8 +380,8 @@ function PMA_indexTypeSelectionDialog(source_array, index_type, col_index) button_options[PMA_messages.strGo] = function () { if ($('#single_column').is(':checked')) { var index = { - 'Key_name': (index_type == 'primary' ? 'PRIMARY' : ''), - 'Index_type': index_type.toUpperCase() + 'Key_name': (index_choice == 'primary' ? 'PRIMARY' : ''), + 'Index_choice': index_choice.toUpperCase() }; PMA_showAddIndexDialog(source_array, (source_array.length), [col_index], col_index, index); } @@ -458,7 +458,7 @@ function PMA_indexTypeSelectionDialog(source_array, index_type, col_index) * Unbind all event handlers before tearing down a page */ AJAX.registerTeardown('indexes.js', function () { - $(document).off('change', '#select_index_type'); + $(document).off('change', '#select_index_choice'); $(document).off('click', 'a.drop_primary_key_index_anchor.ajax'); $(document).off('click', "#table_index tbody tr td.edit_index.ajax, #indexes .add_index.ajax"); $(document).off('click', '#index_frm input[type=submit]'); @@ -483,7 +483,7 @@ AJAX.registerOnload('indexes.js', function () { checkIndexType(); checkIndexName("index_frm"); - $(document).on('change', '#select_index_type', function (event) { + $(document).on('change', '#select_index_choice', function (event) { event.preventDefault(); checkIndexType(); checkIndexName("index_frm"); @@ -596,19 +596,19 @@ AJAX.registerOnload('indexes.js', function () { // Index of column on Table edit and create page. var col_index = /\d/.exec($(this).attr('name')); col_index = col_index[0]; - // Type of selected index. - var index_type = /[a-z]+/.exec($(this).val()); - index_type = index_type[0]; + // Choice of selected index. + var index_choice = /[a-z]+/.exec($(this).val()); + index_choice = index_choice[0]; // Array containing corresponding indexes. var source_array = null; - if (index_type == 'none') { + if (index_choice == 'none') { PMA_removeColumnFromIndex(col_index); return false; } // Select a source array. - switch (index_type) { + switch (index_choice) { case 'primary': source_array = primary_indexes; break; @@ -625,12 +625,12 @@ AJAX.registerOnload('indexes.js', function () { if (source_array.length === 0) { var index = { - 'Key_name': (index_type == 'primary' ? 'PRIMARY' : ''), - 'Index_type': index_type.toUpperCase() + 'Key_name': (index_choice == 'primary' ? 'PRIMARY' : ''), + 'Index_choice': index_choice.toUpperCase() }; PMA_showAddIndexDialog(source_array, 0, [col_index], col_index, index); } else { - if (index_type == 'primary') { + if (index_choice == 'primary') { var array_index = 0; var source_length = source_array[array_index].columns.length; var target_columns = []; @@ -643,7 +643,7 @@ AJAX.registerOnload('indexes.js', function () { source_array[array_index]); } else { // If there are multiple columns selected for an index, show advanced dialog. - PMA_indexTypeSelectionDialog(source_array, index_type, col_index); + PMA_indexTypeSelectionDialog(source_array, index_choice, col_index); } } }); diff --git a/libraries/Index.class.php b/libraries/Index.class.php index 9917473750..043c2c1b18 100644 --- a/libraries/Index.class.php +++ b/libraries/Index.class.php @@ -47,7 +47,7 @@ class PMA_Index private $_columns = array(); /** - * The index method used (BTREE, SPATIAL, FULLTEXT, HASH, RTREE). + * The index method used (BTREE, HASH, RTREE). * * @var string */ @@ -290,16 +290,20 @@ class PMA_Index if (isset($params['Packed'])) { $this->_packed = $params['Packed']; } - if ('PRIMARY' == $this->_name) { - $this->_choice = 'PRIMARY'; - } elseif ('FULLTEXT' == $this->_type) { - $this->_choice = 'FULLTEXT'; - } elseif ('SPATIAL' == $this->_type) { - $this->_choice = 'SPATIAL'; - } elseif ('0' == $this->_non_unique) { - $this->_choice = 'UNIQUE'; + if (isset($params['Index_choice'])) { + $this->_choice = $params['Index_choice']; } else { - $this->_choice = 'INDEX'; + if ('PRIMARY' == $this->_name) { + $this->_choice = 'PRIMARY'; + } elseif ('FULLTEXT' == $this->_type) { + $this->_choice = 'FULLTEXT'; + } elseif ('SPATIAL' == $this->_type) { + $this->_choice = 'SPATIAL'; + } elseif ('0' == $this->_non_unique) { + $this->_choice = 'UNIQUE'; + } else { + $this->_choice = 'INDEX'; + } } } @@ -350,7 +354,7 @@ class PMA_Index } /** - * Returns index type ((BTREE, SPATIAL, FULLTEXT, HASH, RTREE) + * Returns index type (BTREE, HASH, RTREE) * * @return string index type */ @@ -385,14 +389,31 @@ class PMA_Index ); } + /** + * Returns a lit of all index types + * + * @return string[] index types + */ + static public function getIndexTypes() + { + return array( + 'BTREE', + 'HASH' + ); + } + /** * Returns HTML for the index choice selector * + * @param $edit_table whether this is table editing + * * @return string HTML for the index choice selector */ - public function generateIndexSelector() + public function generateIndexChoiceSelector($edit_table) { - $html_options = ''; + $html_options = ''; return $html_options; } + public function generateIndexTypeSelector() + { + return PMA_Util::getDropdown( + "index[Index_type]", PMA_Index::getIndexTypes(), + $this->_type, "select_index_type", "", "--" + ); + } + /** * Returns how the index is packed * @@ -691,7 +721,7 @@ class PMA_Index $data = array( // 'Non_unique' => $this->_non_unique, 'Packed' => $this->_packed, - 'Index_type' => $this->_type, + 'Index_choice' => $this->_choice, ); foreach ($this->_columns as $column) { diff --git a/libraries/create_addfield.lib.php b/libraries/create_addfield.lib.php index 25ee55f9da..d40376caa8 100644 --- a/libraries/create_addfield.lib.php +++ b/libraries/create_addfield.lib.php @@ -121,14 +121,14 @@ function PMA_setColumnCreationStatementSuffix($current_field_num, * Create relevant index statements * * @param array $index an array of index columns - * @param string $index_type index type that which represents + * @param string $index_choice index choice that which represents * the index type of $indexed_fields * @param boolean $is_create_tbl true if requirement is to get the statement * for table creation * * @return array an array of sql statements for indexes */ -function PMA_buildIndexStatements($index, $index_type, +function PMA_buildIndexStatements($index, $index_choice, $is_create_tbl = true ) { $statement = array(); @@ -143,7 +143,7 @@ function PMA_buildIndexStatements($index, $index_type, . (! empty($field['size']) ? '(' . $field['size'] . ')' : ''); } $statement[] = PMA_getStatementPrefix($is_create_tbl) - . ' ' . $index_type + . ' ' . $index_choice . (! empty($index['Key_name']) && $index['Key_name'] != 'PRIMARY' ? PMA_Util::backquote($index['Key_name']) : '') diff --git a/libraries/tbl_indexes.lib.php b/libraries/tbl_indexes.lib.php index 7b11e1e27f..61813dcf4a 100644 --- a/libraries/tbl_indexes.lib.php +++ b/libraries/tbl_indexes.lib.php @@ -109,7 +109,7 @@ function PMA_getSqlQueryForIndexCreateOrEdit($db, $table, $index, &$error) } // end if // Builds the new one - switch ($index->getType()) { + switch ($index->getChoice()) { case 'PRIMARY': if ($index->getName() == '') { $index->setName('PRIMARY'); @@ -127,7 +127,7 @@ function PMA_getSqlQueryForIndexCreateOrEdit($db, $table, $index, &$error) if ($index->getName() == 'PRIMARY') { $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!')); } - $sql_query .= ' ADD ' . $index->getType() . ' ' + $sql_query .= ' ADD ' . $index->getChoice() . ' ' . ($index->getName() ? PMA_Util::backquote($index->getName()) : ''); break; } // end switch @@ -295,16 +295,13 @@ function PMA_getHtmlForIndexForm($fields, $index, $form_params, $add_fields) $html .= '
' . '
' . '' - . '' . '
' - . '' + . $index->generateIndexChoiceSelector(isset($_REQUEST['create_edit_table'])) . '
'; $html .= '
'; @@ -336,9 +333,9 @@ function PMA_getHtmlForIndexForm($fields, $index, $form_params, $add_fields) $html .= '