Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use PMA_DBI_get_columns_full for moving columns

  • Loading branch information...
commit 75d296af15d43553cbf1fa03ebfb079d0dd5ad7b 1 parent 315f2f7
Jo Michael authored
Showing with 24 additions and 38 deletions.
  1. +24 −38 tbl_alter.php
View
62 tbl_alter.php
@@ -45,45 +45,31 @@
/*
* 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);
+ $columns = PMA_DBI_get_columns_full($db, $table);
$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;
- }
+ $column_names = array();
+ foreach($columns as $column => $data) {
+ $definitions[$column] =
+ rtrim(
+ PMA_backquote($column) . ' '
+ . strtoupper($data['Type']) . ' '
+ . ($data['CHARACTER_SET_NAME'] !== false
+ ? 'CHARACTER SET ' . $data['CHARACTER_SET_NAME'] . ' '
+ . 'COLLATE ' . $data['Collation'] . ' '
+ : '')
+ . ($data['Null'] == 'NO' ? 'NOT NULL ' : 'NULL ')
+ . ($data['Extra'] != '' ? strtoupper($data['Extra']) . ' ' : '')
+ . ($data['Default'] === null && $data['Null'] == 'YES' ? 'DEFAULT NULL ' : '')
+ . ($data['Default'] !== null ? 'DEFAULT \''
+ . PMA_sqlAddSlashes($data['Default']) . '\' ' : '')
+ );
@ZweiSteinSoft Owner

Is the definition created here complete in all cases?

@lem9
lem9 added a note

Jo,
did you consider using PMA_Table::generateAlter() instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $column_names[] = $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) {
+ if ($column_names[$i] == $column) {
continue;
}
// it is not, let's move it to index $i
@@ -102,14 +88,14 @@
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]);
+ // 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($columns, $i, 0, $column);
+ array_splice($column_names, $i, 0, $column);
}
PMA_ajaxResponse(PMA_Message::success(__('The columns have been moved successfully.')), true);
}
Please sign in to comment.
Something went wrong with that request. Please try again.