Use common code for CREATE/ALTER TABLE, set comments on creating table, so that user can see it in the query.

This commit is contained in:
Michal Čihař
2005-10-08 17:28:31 +00:00
parent f029aea260
commit c62f975a47
6 changed files with 49 additions and 149 deletions

View File

@@ -43,6 +43,10 @@ $Source$
strFailure. strFailure.
* lang/*: Remove unused messages. * lang/*: Remove unused messages.
* sql.php: Use common code for superuser detection. * sql.php: Use common code for superuser detection.
* tbl_addfield.php, tbl_alter.php, tbl_create.php,
libraries/common.lib.php, libraries/relation.lib.php: Use common code
for CREATE/ALTER TABLE, set comments on creating table, so that user can
see it in the query.
2005-10-07 Marc Delisle <lem9@users.sourceforge.net> 2005-10-07 Marc Delisle <lem9@users.sourceforge.net>
* libraries/check_user_privileges.lib.php: bug #1313821, dbname containing a * libraries/check_user_privileges.lib.php: bug #1313821, dbname containing a

View File

@@ -2737,25 +2737,36 @@ if (typeof(document.getElementById) != 'undefined'
} // end function } // end function
function PMA_generateAlterTable($oldcol, $newcol, $full_field_type, $collation, $null, $default, $default_current_timestamp, $extra, $comment='', $default_orig) { function PMA_generateFieldSpec($name, $type, $length, $attribute, $collation, $null, $default, $default_current_timestamp, $extra, $comment='', &$field_primary, $index, $default_orig = FALSE) {
// $default_current_timestamp has priority over $default // $default_current_timestamp has priority over $default
// TODO: on the interface, some js to clear the default value // TODO: on the interface, some js to clear the default value
// when the default current_timestamp is checked // when the default current_timestamp is checked
$query = PMA_backquote($oldcol) . ' ' . PMA_backquote($newcol) . ' ' $query = PMA_backquote($name) . ' ' . $type;
. $full_field_type;
if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR\(\d+\)|CHAR\(\d+\))$@i', $full_field_type)) { if ($length != ''
&& !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $type)) {
$query .= '(' . $length . ')';
}
if ($attribute != '') {
$query .= ' ' . $attribute;
}
if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR)$@i', $type)) {
$query .= PMA_generateCharsetQueryPart($collation); $query .= PMA_generateCharsetQueryPart($collation);
} }
if (!($null === FALSE)) {
if (!empty($null)) { if (!empty($null)) {
$query .= ' NOT NULL'; $query .= ' NOT NULL';
} else { } else {
$query .= ' NULL'; $query .= ' NULL';
} }
}
if ($default_current_timestamp && strpos(' ' . strtoupper($full_field_type),'TIMESTAMP') == 1) { if ($default_current_timestamp && strpos(' ' . strtoupper($type),'TIMESTAMP') == 1) {
$query .= ' DEFAULT CURRENT_TIMESTAMP'; $query .= ' DEFAULT CURRENT_TIMESTAMP';
// 0 is empty in PHP // 0 is empty in PHP
} elseif (!empty($default) || $default == '0' || $default != $default_orig) { } elseif (!empty($default) || $default == '0' || $default != $default_orig) {
@@ -2768,6 +2779,17 @@ if (typeof(document.getElementById) != 'undefined'
if (!empty($extra)) { if (!empty($extra)) {
$query .= ' ' . $extra; $query .= ' ' . $extra;
// An auto_increment field must be use as a primary key
if ($extra == 'AUTO_INCREMENT' && isset($field_primary)) {
$primary_cnt = count($field_primary);
for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $index; $j++) {
// void
} // end for
if (isset($field_primary[$j]) && $field_primary[$j] == $index) {
$query .= ' PRIMARY KEY';
unset($field_primary[$j]);
} // end if
} // end if (auto_increment)
} }
if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($comment)) { if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($comment)) {
$query .= " COMMENT '" . PMA_sqlAddslashes($comment) . "'"; $query .= " COMMENT '" . PMA_sqlAddslashes($comment) . "'";
@@ -2775,6 +2797,11 @@ if (typeof(document.getElementById) != 'undefined'
return $query; return $query;
} // end function } // end function
function PMA_generateAlterTable($oldcol, $newcol, $type, $length, $attribute, $collation, $null, $default, $default_current_timestamp, $extra, $comment='', $default_orig) {
$empty_a = array();
return PMA_backquote($oldcol) . ' ' . PMA_generateFieldSpec($newcol, $type, $length, $attribute, $collation, $null, $default, $default_current_timestamp, $extra, $comment, $empty_a, -1, $default_orig);
} // end function
} // end if: minimal common.lib needed? } // end if: minimal common.lib needed?
?> ?>

View File

@@ -560,59 +560,8 @@ function PMA_setComment($db, $table, $col, $comment, $removekey = '', $mode='aut
// native mode is only for column comments so we need a table name // native mode is only for column comments so we need a table name
if ($mode == 'native' && !empty($table)) { if ($mode == 'native' && !empty($table)) {
$fields = PMA_DBI_get_fields($db, $table);
// Get more complete field information
// For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
// but later, if the analyser returns more information, it
// could be executed for any MySQL version and replace
// the info given by SHOW FULL FIELDS FROM.
// TODO: put this code into a require()
// or maybe make it part of PMA_DBI_get_fields();
if (PMA_MYSQL_INT_VERSION >= 40102) {
$show_create_table_query = 'SHOW CREATE TABLE '
. PMA_backquote($db) . '.' . PMA_backquote($table);
$show_create_table_res = PMA_DBI_query($show_create_table_query);
list(,$show_create_table) = PMA_DBI_fetch_row($show_create_table_res);
PMA_DBI_free_result($show_create_table_res);
unset($show_create_table_res, $show_create_table_query);
$analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
}
// TODO: get directly the information of $col
foreach($fields as $key=>$field) {
$tmp_col = $field['Field'];
$types[$tmp_col] = $field['Type'];
$collations[$tmp_col] = $field['Collation'];
$nulls[$tmp_col] = $field['Null'];
$defaults[$tmp_col] = $field['Default'];
$extras[$tmp_col] = $field['Extra'];
if (PMA_MYSQL_INT_VERSION >= 40102 && isset($analyzed_sql[0]['create_table_fields'][$tmp_col]['on_update_current_timestamp'])) {
$extras[$tmp_col] = 'ON UPDATE CURRENT_TIMESTAMP';
}
if (PMA_MYSQL_INT_VERSION >= 40102 && isset($analyzed_sql[0]['create_table_fields'][$tmp_col]['default_current_timestamp'])) {
$default_current_timestamps[$tmp_col] = TRUE;
} else {
$default_current_timestamps[$tmp_col] = FALSE;
}
if ($tmp_col == $col) {
break;
}
}
if ($nulls[$col] == 'YES') {
$nulls[$col] = '';
} else {
$nulls[$col] = 'NOT NULL';
}
$query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' $query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE '
. PMA_generateAlterTable($col, $col, $types[$col], $collations[$col], $nulls[$col], $defaults[$col], $default_current_timestamps[$col], $extras[$col], $comment, ''); . PMA_generateAlterTable($col, $col, '', '', '', '', FALSE, '', FALSE, '', $comment, '', '');
PMA_DBI_try_query($query, NULL, PMA_DBI_QUERY_STORE); PMA_DBI_try_query($query, NULL, PMA_DBI_QUERY_STORE);
return TRUE; return TRUE;
} }

View File

@@ -51,54 +51,13 @@ if (isset($submit_num_fields)) {
} // end for } // end for
// Builds the field creation statement and alters the table // Builds the field creation statement and alters the table
// TODO: check to see if this logic is exactly the same
// as in tbl_create.php, and move to an include file
for ($i = 0; $i < $field_cnt; ++$i) { for ($i = 0; $i < $field_cnt; ++$i) {
// '0' is also empty for php :-( // '0' is also empty for php :-(
if (empty($field_name[$i]) && $field_name[$i] != '0') { if (empty($field_name[$i]) && $field_name[$i] != '0') {
continue; continue;
} }
$query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i]; $query .= PMA_generateFieldSpec($field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], $field_collation[$i], $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_primary, $i);
if ($field_length[$i] != ''
&& !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) {
$query .= '(' . $field_length[$i] . ')';
}
if ($field_attribute[$i] != '') {
$query .= ' ' . $field_attribute[$i];
} else if (PMA_MYSQL_INT_VERSION >= 40100 && isset($field_collation[$i]) && $field_collation[$i] != '') {
list($tmp_charset) = explode('_', $field_collation[$i]);
$query .= ' CHARACTER SET ' . $tmp_charset . ' COLLATE ' . $field_collation[$i];
unset($tmp_charset);
}
if (isset($field_default_current_timestamp[$i]) && $field_default_current_timestamp[$i]) {
$query .= ' DEFAULT CURRENT_TIMESTAMP';
} elseif ($field_default[$i] != '') {
if (strtoupper($field_default[$i]) == 'NULL') {
$query .= ' DEFAULT NULL';
} else {
$query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
}
}
if ($field_null[$i] != '') {
$query .= ' ' . $field_null[$i];
}
if ($field_extra[$i] != '') {
$query .= ' ' . $field_extra[$i];
// An auto_increment field must be use as a primary key
if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
$primary_cnt = count($field_primary);
for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
// void
} // end for
if ($field_primary[$j] == $i) {
$query .= ' PRIMARY KEY';
unset($field_primary[$j]);
} // end if
} // end if (auto_increment)
}
if ($field_where != 'last') { if ($field_where != 'last') {
// Only the first field can be added somewhere other than at the end // Only the first field can be added somewhere other than at the end
@@ -205,13 +164,9 @@ if (isset($submit_num_fields)) {
$cfgRelation = PMA_getRelationsParam(); $cfgRelation = PMA_getRelationsParam();
// garvin: Update comment table, if a comment was set. // garvin: Update comment table, if a comment was set.
// lem9: FIXME: here we take care of native comments and if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
// pmadb-style comments, however, in the case of
// native comments, users do not see the COMMENT clause
// when SQL query is displayed
if (isset($field_comments) && is_array($field_comments) && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
foreach ($field_comments AS $fieldindex => $fieldcomment) { foreach ($field_comments AS $fieldindex => $fieldcomment) {
PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment); PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
} }
} }

View File

@@ -57,17 +57,7 @@ if (isset($do_save_data)) {
$query .= ', CHANGE '; $query .= ', CHANGE ';
} }
$full_field_type = $field_type[$i]; $query .= PMA_generateAlterTable($field_orig[$i], $field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], $field_collation[$i], $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], (isset($field_comments[$i]) ? $field_comments[$i] : ''), $field_default_orig[$i]);
if ($field_length[$i] != ''
&& !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) {
$full_field_type .= '(' . $field_length[$i] . ')';
}
if ($field_attribute[$i] != '') {
$full_field_type .= ' ' . $field_attribute[$i];
}
// take care of native MySQL comments here
$query .= PMA_generateAlterTable($field_orig[$i], $field_name[$i], $full_field_type, (PMA_MYSQL_INT_VERSION >= 40100 && $field_collation[$i] != '' ? $field_collation[$i] : ''), $field_null[$i], $field_default[$i], (isset($field_default_current_timestamp[$i]) ? $field_default_current_timestamp[$i] : ''), $field_extra[$i], (PMA_MYSQL_INT_VERSION >= 40100 && isset($field_comments[$i]) && $field_comments[$i] != '' ? $field_comments[$i] : ''), $field_default_orig[$i]);
} // end for } // end for
// To allow replication, we first select the db to use and then run queries // To allow replication, we first select the db to use and then run queries

View File

@@ -62,34 +62,9 @@ if (isset($submit_num_fields)) {
if (empty($field_name[$i]) && $field_name[$i] != '0') { if (empty($field_name[$i]) && $field_name[$i] != '0') {
continue; continue;
} }
// TODO: maybe move this logic and the one of PMA_generateAlterTable()
// to a central place
$query = PMA_backquote($field_name[$i]) . ' ' . $field_type[$i]; $query = PMA_generateFieldSpec($field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], $field_collation[$i], $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_primary, $i);
if ($field_length[$i] != ''
&& !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) {
$query .= '(' . $field_length[$i] . ')';
}
if ($field_attribute[$i] != '') {
$query .= ' ' . $field_attribute[$i];
} else if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($field_collation[$i])) {
$query .= PMA_generateCharsetQueryPart($field_collation[$i]);
}
if (isset($field_default_current_timestamp[$i]) && $field_default_current_timestamp[$i]) {
$query .= ' DEFAULT CURRENT_TIMESTAMP';
} elseif ($field_default[$i] != '') {
if (strtoupper($field_default[$i]) == 'NULL') {
$query .= ' DEFAULT NULL';
} else {
$query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
}
}
if ($field_null[$i] != '') {
$query .= ' ' . $field_null[$i];
}
if ($field_extra[$i] != '') {
$query .= ' ' . $field_extra[$i];
}
$query .= ', '; $query .= ', ';
$sql_query .= $query; $sql_query .= $query;
$query_cpy .= "\n" . ' ' . $query; $query_cpy .= "\n" . ' ' . $query;
@@ -202,11 +177,11 @@ if (isset($submit_num_fields)) {
$cfgRelation = PMA_getRelationsParam(); $cfgRelation = PMA_getRelationsParam();
// garvin: Update comment table, if a comment was set. // garvin: Update comment table, if a comment was set.
if (isset($field_comments) && is_array($field_comments) && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
foreach ($field_comments AS $fieldindex => $fieldcomment) { foreach ($field_comments AS $fieldindex => $fieldcomment) {
// do not try to set a comment if the field name is empty // do not try to set a comment if the field name is empty
if (!empty($field_name[$fieldindex])) { if (!empty($field_name[$fieldindex])) {
PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment); PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
} }
} }
} }