diff options
author | Jo Michael <me@mynetx.net> | 2012-04-15 01:35:44 +0400 |
---|---|---|
committer | Jo Michael <me@mynetx.net> | 2012-04-15 01:35:44 +0400 |
commit | 37ac76c46124b6694a26949df9dd5df38c393f57 (patch) | |
tree | 869b9a26b0e0c66ff9d78a7b02cff7f12e85aaa3 /tbl_alter.php | |
parent | 2760d808b4076f30ec1afa940fbfbc570d3b86f4 (diff) |
rfe #3517835 [structure] Move columns easily
Diffstat (limited to 'tbl_alter.php')
-rw-r--r-- | tbl_alter.php | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tbl_alter.php b/tbl_alter.php index 91b39f0bcd..612a53544d 100644 --- a/tbl_alter.php +++ b/tbl_alter.php @@ -36,6 +36,83 @@ $active_page = 'tbl_structure.php'; */ $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table); +/** + * Moving columns + */ +if (isset($_REQUEST['move_columns']) + && is_array($_REQUEST['move_columns']) + && $GLOBALS['is_ajax_request']) { + /* + * first, load the definitions for all columns + */ + $result = PMA_DBI_try_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)); + // an error can happen, for example the table is crashed + $tmp_error = PMA_DBI_getError(); + if ($tmp_error) { + PMA_ajaxResponse(PMA_Message::error($tmp_error), false); + } + + if ($result != false && ($row = PMA_DBI_fetch_row($result))) { + $create_query = $row[1]; + unset($row); + + // Convert end of line chars to one that we want (note that MySQL doesn't return query it will accept in all cases) + if (strpos($create_query, "(\r\n ")) { + $create_query = str_replace("\r\n", "\n", $create_query); + } elseif (strpos($create_query, "(\r ")) { + $create_query = str_replace("\r", "\n", $create_query); + } + } + $create_query = explode("\n", $create_query); + $definitions = array(); + $columns = array(); + foreach($create_query as $row) { + $row = trim($row); + // trim any trailing commas + if (substr($row, -1) == ',') { + $row = substr($row, 0, -1); + } + // columns can be identified by the leading backtick + if (substr($row, 0, 1) == '`') { + $column = substr($row, 1, strpos($row, '`', 1) - 1); + $definitions[$column] = $row; + $columns[] = $column; + } + } + // 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 ($columns[$i] == $column) { + continue; + } + // it is not, let's move it to index $i + $move_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' + . 'CHANGE ' . PMA_backquote($column) . ' ' . $definitions[$column]; + // to become first column? + if ($i == 0) { + $move_query .= ' FIRST'; + } + else { + $move_query .= ' AFTER ' . PMA_backquote($columns[$i - 1]); + } + // move column here + $result = PMA_DBI_query($move_query); + $tmp_error = PMA_DBI_getError(); + if ($tmp_error) { + PMA_ajaxResponse(PMA_Message::error($tmp_error), false); + } + // update current columns array, first delete old position + for($j = 0, $ll = count($columns); $j < $ll; $j++) { + if ($columns[$j] == $column) { + unset($columns[$j]); + } + } + // insert moved column + array_splice($columns, $i, 0, $column); + } + PMA_ajaxResponse(PMA_Message::success(__('The columns have been moved successfully.')), true); +} /** * Modifications have been submitted -> updates the table |