From 95fa48fc8e479f8063876005e8e58093862093c8 Mon Sep 17 00:00:00 2001 From: Marc Delisle Date: Tue, 18 Dec 2012 15:44:39 -0500 Subject: [PATCH] Remove remaining code in tbl_alter.php --- libraries/common.inc.php | 1 - .../auth/AuthenticationCookie.class.php | 4 - libraries/relation.lib.php | 2 +- libraries/structure.lib.php | 98 +++++++++++++- tbl_alter.php | 128 ------------------ tbl_structure.php | 11 ++ .../core/PMA_checkPageValidity_test.php | 1 - 7 files changed, 108 insertions(+), 137 deletions(-) delete mode 100644 tbl_alter.php diff --git a/libraries/common.inc.php b/libraries/common.inc.php index 3c06ee66cb..7d7fce1024 100644 --- a/libraries/common.inc.php +++ b/libraries/common.inc.php @@ -398,7 +398,6 @@ $goto_whitelist = array( 'server_variables.php', 'sql.php', 'tbl_addfield.php', - 'tbl_alter.php', 'tbl_change.php', 'tbl_create.php', 'tbl_import.php', diff --git a/libraries/plugins/auth/AuthenticationCookie.class.php b/libraries/plugins/auth/AuthenticationCookie.class.php index 688190d4e6..f9588b0977 100644 --- a/libraries/plugins/auth/AuthenticationCookie.class.php +++ b/libraries/plugins/auth/AuthenticationCookie.class.php @@ -21,10 +21,6 @@ if (! empty($_REQUEST['target'])) { $GLOBALS['target'] = $_REQUEST['target']; } else if (PMA_getenv('SCRIPT_NAME')) { $GLOBALS['target'] = basename(PMA_getenv('SCRIPT_NAME')); - // avoid "missing parameter: field" on re-entry - if ('tbl_alter.php' == $GLOBALS['target']) { - $GLOBALS['target'] = 'tbl_structure.php'; - } } /** diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index c51f50bc00..584fcdf8ea 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -1209,7 +1209,7 @@ function PMA_getRelatives($all_tables, $master) /** * Rename a field in relation tables * - * usually called after a field in a table was renamed in tbl_alter.php + * usually called after a column in a table was renamed * * @param string $db databse name * @param string $table table name diff --git a/libraries/structure.lib.php b/libraries/structure.lib.php index e3dcb38af0..827236f6b4 100644 --- a/libraries/structure.lib.php +++ b/libraries/structure.lib.php @@ -1432,7 +1432,7 @@ function PMA_getHtmlDivForMoveColumnsDialog() $html_output .= '

' . __('Move the columns by dragging them up and down.') . '

'; - $html_output .= '
' + $html_output .= '' . '
' . PMA_generate_common_hidden_inputs($GLOBALS['db'], $GLOBALS['table']) . '
    ' @@ -2274,7 +2274,7 @@ function PMA_displayHtmlForColumnChange($db, $table, $selected, $action) $num_fields = count($fields_meta); // set these globals because tbl_properties.inc.php verifies them // @todo: refactor tbl_properties.inc.php so that it uses function params - $GLOBALS['action'] = 'tbl_alter.php'; + $GLOBALS['action'] = 'tbl_structure.php'; $GLOBALS['num_fields'] = $num_fields; // Get more complete field information. @@ -2451,4 +2451,98 @@ function PMA_updateColumns($db, $table) return $regenerate; } +/** + * Moves columns in the table's structure based on $_REQUEST + * + * @param string $db database name + * @param string $table table name + */ +function PMA_moveColumns($db, $table) +{ + PMA_DBI_select_db($db); + + /* + * load the definitions for all columns + */ + $columns = PMA_DBI_get_columns_full($db, $table); + $column_names = array_keys($columns); + $changes = array(); + $we_dont_change_keys = array(); + + // move columns from first to last + for ($i = 0, $l = count($_REQUEST['move_columns']); $i < $l; $i++) { + $column = $_REQUEST['move_columns'][$i]; + // is this column already correctly placed? + if ($column_names[$i] == $column) { + continue; + } + + // it is not, let's move it to index $i + $data = $columns[$column]; + $extracted_columnspec = PMA_Util::extractColumnSpec($data['Type']); + if (isset($data['Extra']) && $data['Extra'] == 'on update CURRENT_TIMESTAMP') { + $extracted_columnspec['attribute'] = $data['Extra']; + unset($data['Extra']); + } + $current_timestamp = false; + if ($data['Type'] == 'timestamp' && $data['Default'] == 'CURRENT_TIMESTAMP') { + $current_timestamp = true; + } + $default_type + = $data['Null'] === 'YES' && $data['Default'] === null + ? 'NULL' + : ($current_timestamp + ? 'CURRENT_TIMESTAMP' + : ($data['Default'] == '' + ? 'NONE' + : 'USER_DEFINED')); + + $changes[] = 'CHANGE ' . PMA_Table::generateAlter( + $column, + $column, + strtoupper($extracted_columnspec['type']), + $extracted_columnspec['spec_in_brackets'], + $extracted_columnspec['attribute'], + isset($data['Collation']) ? $data['Collation'] : '', + $data['Null'] === 'YES' ? 'NULL' : 'NOT NULL', + $default_type, + $current_timestamp ? '' : $data['Default'], + isset($data['Extra']) && $data['Extra'] !== '' ? $data['Extra'] : false, + isset($data['Comments']) && $data['Comments'] !== '' + ? $data['Comments'] : false, + $we_dont_change_keys, + $i, + $i === 0 ? '-first' : $column_names[$i - 1] + ); + // update current column_names array, first delete old position + for ($j = 0, $ll = count($column_names); $j < $ll; $j++) { + if ($column_names[$j] == $column) { + unset($column_names[$j]); + } + } + // insert moved column + array_splice($column_names, $i, 0, $column); + } + $response = PMA_Response::getInstance(); + if (empty($changes)) { // should never happen + $response->isSuccess(false); + exit; + } + $move_query = 'ALTER TABLE ' . PMA_Util::backquote($table) . ' '; + $move_query .= implode(', ', $changes); + // move columns + $result = PMA_DBI_try_query($move_query); + $tmp_error = PMA_DBI_getError(); + if ($tmp_error) { + $response->isSuccess(false); + $response->addJSON('message', PMA_Message::error($tmp_error)); + } else { + $message = PMA_Message::success( + __('The columns have been moved successfully.') + ); + $response->addJSON('message', $message); + $response->addJSON('columns', $column_names); + } + exit; +} ?> diff --git a/tbl_alter.php b/tbl_alter.php deleted file mode 100644 index 292b381ed8..0000000000 --- a/tbl_alter.php +++ /dev/null @@ -1,128 +0,0 @@ -isSuccess(false); - exit; - } - $move_query = 'ALTER TABLE ' . PMA_Util::backquote($table) . ' '; - $move_query .= implode(', ', $changes); - // move columns - $result = PMA_DBI_try_query($move_query); - $tmp_error = PMA_DBI_getError(); - if ($tmp_error) { - $response->isSuccess(false); - $response->addJSON('message', PMA_Message::error($tmp_error)); - } else { - $message = PMA_Message::success( - __('The columns have been moved successfully.') - ); - $response->addJSON('message', $message); - $response->addJSON('columns', $column_names); - } - exit; -} -?> diff --git a/tbl_structure.php b/tbl_structure.php index 63a77dedde..a9d6408487 100644 --- a/tbl_structure.php +++ b/tbl_structure.php @@ -24,6 +24,17 @@ $scripts = $header->getScripts(); $scripts->addFile('tbl_structure.js'); $scripts->addFile('indexes.js'); +/** + * Handle column moving + */ +if (isset($_REQUEST['move_columns']) + && is_array($_REQUEST['move_columns']) + && $response->isAjax() +) { + PMA_moveColumns($db, $table); + exit; +} + /** * A click on Change has been made for one column */ diff --git a/test/libraries/core/PMA_checkPageValidity_test.php b/test/libraries/core/PMA_checkPageValidity_test.php index 31b39a18df..5bf04ad39b 100644 --- a/test/libraries/core/PMA_checkPageValidity_test.php +++ b/test/libraries/core/PMA_checkPageValidity_test.php @@ -28,7 +28,6 @@ class PMA_checkPageValidity_test extends PHPUnit_Framework_TestCase 'server_binlog.php', 'server_variables.php', 'sql.php', - 'tbl_alter.php', 'tbl_select.php', 'transformation_overview.php', 'transformation_wrapper.php',