experimental support in table structure editing for MySQL 4.1.2+ TIMESTAMP options

This commit is contained in:
Marc Delisle
2005-03-16 17:23:58 +00:00
parent dedb122182
commit b8b9334da9
6 changed files with 92 additions and 22 deletions

View File

@@ -5,6 +5,12 @@ phpMyAdmin - Changelog
$Id$ $Id$
$Source$ $Source$
2005-03-16 Marc Delisle <lem9@users.sourceforge.net>
* tbl_properties_structure.php, tbl_alter.php, tbl_properties.inc.php,
config.inc.php (comment only), libraries/common.lib.php:
experimental support for table structure editing with MySQL 4.1.2+
TIMESTAMP options
2005-03-13 Marc Delisle <lem9@users.sourceforge.net> 2005-03-13 Marc Delisle <lem9@users.sourceforge.net>
* tbl_row_delete.php renamed to tbl_row_action.php (RFE 1097729) * tbl_row_delete.php renamed to tbl_row_action.php (RFE 1097729)

View File

@@ -670,6 +670,9 @@ $cfg['ColumnTypes'] = array(
); );
// Attributes // Attributes
// Note: the "ON UPDATE CURRENT_TIMESTAMP" attribute is added dynamically
// for MySQL >= 4.1.2, in tbl_properties.inc.php
$cfg['AttributeTypes'] = array( $cfg['AttributeTypes'] = array(
'', '',
'BINARY', 'BINARY',

View File

@@ -2517,24 +2517,30 @@ if (typeof(document.getElementById) != 'undefined'
} // end function } // end function
function PMA_generateAlterTable($oldcol, $newcol, $full_field_type, $collation, $null, $default, $extra, $comment='') { function PMA_generateAlterTable($oldcol, $newcol, $full_field_type, $collation, $null, $default, $default_current_timestamp, $extra, $comment='') {
// $default_current_timestamp has priority over $default
// TODO: on the interface, some js to clear the default value
// when the default current_timestamp is checked
$query = PMA_backquote($oldcol) . ' ' . PMA_backquote($newcol) . ' ' $query = PMA_backquote($oldcol) . ' ' . PMA_backquote($newcol) . ' '
. $full_field_type; . $full_field_type;
if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR)$@i', $full_field_type)) { if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($collation) && $collation != 'NULL' && preg_match('@^(TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT|VARCHAR|CHAR)$@i', $full_field_type)) {
$query .= PMA_generateCharsetQueryPart($collation); $query .= PMA_generateCharsetQueryPart($collation);
} }
if (!empty($default)) {
if ($default_current_timestamp) {
$query .= ' DEFAULT CURRENT_TIMESTAMP';
} elseif (!empty($default)) {
if (strtoupper($default) == 'NULL') { if (strtoupper($default) == 'NULL') {
$query .= ' DEFAULT NULL'; $query .= ' DEFAULT NULL';
} else { } else {
$query .= ' DEFAULT \'' . PMA_sqlAddslashes($default) . '\''; $query .= ' DEFAULT \'' . PMA_sqlAddslashes($default) . '\'';
} }
} }
if (!empty($null)) { if (!empty($null)) {
$query .= ' NOT NULL'; $query .= ' NOT NULL';
//} else {
// $query .= ' NULL';
} }
if (!empty($extra)) { if (!empty($extra)) {
$query .= ' ' . $extra; $query .= ' ' . $extra;

View File

@@ -2,7 +2,6 @@
/* $Id$ */ /* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4: // vim: expandtab sw=4 ts=4 sts=4:
/** /**
* Gets some core libraries * Gets some core libraries
*/ */
@@ -67,7 +66,7 @@ if (isset($do_save_data)) {
$full_field_type .= ' ' . $field_attribute[$i]; $full_field_type .= ' ' . $field_attribute[$i];
} }
// take care of native MySQL comments here // 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], $field_extra[$i], (PMA_MYSQL_INT_VERSION >= 40100 && $field_comments[$i] != '' ? $field_comments[$i] : '')); $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], $field_default_current_timestamp[$i], $field_extra[$i], (PMA_MYSQL_INT_VERSION >= 40100 && $field_comments[$i] != '' ? $field_comments[$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
@@ -181,6 +180,24 @@ if ($abort == FALSE) {
} }
$num_fields = count($fields_meta); $num_fields = count($fields_meta);
$action = 'tbl_alter.php'; $action = 'tbl_alter.php';
// 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()
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));
}
require('./tbl_properties.inc.php'); require('./tbl_properties.inc.php');
} }

View File

@@ -116,7 +116,6 @@ if (!$is_backup) {
$header_cells[] = $cfg['PropertiesIconic'] ? '<img src="' . $pmaThemeImage . 'b_ftext.png" width="16" height="16" alt="' . $strIdxFulltext . '" title="' . $strIdxFulltext . '" />' : $strIdxFulltext; $header_cells[] = $cfg['PropertiesIconic'] ? '<img src="' . $pmaThemeImage . 'b_ftext.png" width="16" height="16" alt="' . $strIdxFulltext . '" title="' . $strIdxFulltext . '" />' : $strIdxFulltext;
} }
require_once('./libraries/relation.lib.php'); require_once('./libraries/relation.lib.php');
require_once('./libraries/transformations.lib.php'); require_once('./libraries/transformations.lib.php');
$cfgRelation = PMA_getRelationsParam(); $cfgRelation = PMA_getRelationsParam();
@@ -302,30 +301,48 @@ for ($i = 0 ; $i < $num_fields; $i++) {
$ci++; $ci++;
} }
$content_cells[$i][$ci] = '<select name="field_attribute[]" id="field_' . $i . '_' . ($ci - $ci_offset) . '">' . "\n"; $content_cells[$i][$ci] = '<select style="font-size: ' . $font_smallest . ';" name="field_attribute[]" id="field_' . $i . '_' . ($ci - $ci_offset) . '">' . "\n";
$strAttribute = ''; $attribute = '';
if ($binary) { if ($binary) {
$strAttribute = 'BINARY'; $attribute = 'BINARY';
} }
if ($unsigned) { if ($unsigned) {
$strAttribute = 'UNSIGNED'; $attribute = 'UNSIGNED';
} }
if ($zerofill) { if ($zerofill) {
$strAttribute = 'UNSIGNED ZEROFILL'; $attribute = 'UNSIGNED ZEROFILL';
} }
if (isset($submit_attribute) && $submit_attribute != FALSE) { if (isset($submit_attribute) && $submit_attribute != FALSE) {
$strAttribute = $submit_attribute; $attribute = $submit_attribute;
} }
// MySQL 4.1.2+ TIMESTAMP options
// (if on_update_current_timestamp is set, then it's TRUE)
if (isset($analyzed_sql[0]['create_table_fields'][$row['Field']]['on_update_current_timestamp'])) {
$attribute = 'ON UPDATE CURRENT_TIMESTAMP';
}
if (isset($analyzed_sql[0]['create_table_fields'][$row['Field']]['default_current_timestamp'])) {
$default_current_timestamp = TRUE;
} else {
$default_current_timestamp = FALSE;
}
// Dynamically add ON UPDATE CURRENT_TIMESTAMP to the possible attributes
if (PMA_MYSQL_INT_VERSION >= 40102) {
$cfg['AttributeTypes'][] = 'ON UPDATE CURRENT_TIMESTAMP';
}
$cnt_attribute_types = count($cfg['AttributeTypes']); $cnt_attribute_types = count($cfg['AttributeTypes']);
for ($j = 0;$j < $cnt_attribute_types; $j++) { for ($j = 0;$j < $cnt_attribute_types; $j++) {
if (PMA_MYSQL_INT_VERSION >= 40100 && $cfg['AttributeTypes'][$j] == 'BINARY') { if (PMA_MYSQL_INT_VERSION >= 40100 && $cfg['AttributeTypes'][$j] == 'BINARY') {
continue; continue;
} }
$content_cells[$i][$ci] .= ' <option value="'. $cfg['AttributeTypes'][$j] . '"'; $content_cells[$i][$ci] .= ' <option value="'. $cfg['AttributeTypes'][$j] . '"';
if (strtoupper($strAttribute) == strtoupper($cfg['AttributeTypes'][$j])) { if (strtoupper($attribute) == strtoupper($cfg['AttributeTypes'][$j])) {
$content_cells[$i][$ci] .= ' selected="selected"'; $content_cells[$i][$ci] .= ' selected="selected"';
} }
$content_cells[$i][$ci] .= '>' . $cfg['AttributeTypes'][$j] . '</option>' . "\n"; $content_cells[$i][$ci] .= '>' . $cfg['AttributeTypes'][$j] . '</option>' . "\n";
@@ -360,7 +377,23 @@ for ($i = 0 ; $i < $num_fields; $i++) {
$content_cells[$i][$ci] = "\n"; $content_cells[$i][$ci] = "\n";
} }
// for a TIMESTAMP, do not show CURRENT_TIMESTAMP as a default value
if (PMA_MYSQL_INT_VERSION >= 40102
&& $type == 'timestamp'
&& $default_current_timestamp
&& isset($row)
&& isset($row['Default'])) {
$row['Default'] = '';
}
$content_cells[$i][$ci] .= '<input id="field_' . $i . '_' . ($ci - $ci_offset) . '" type="text" name="field_default[]" size="12" value="' . (isset($row) && isset($row['Default']) ? str_replace('"', '&quot;', $row['Default']) : '') . '" class="textfield" />'; $content_cells[$i][$ci] .= '<input id="field_' . $i . '_' . ($ci - $ci_offset) . '" type="text" name="field_default[]" size="12" value="' . (isset($row) && isset($row['Default']) ? str_replace('"', '&quot;', $row['Default']) : '') . '" class="textfield" />';
if (PMA_MYSQL_INT_VERSION >= 40102 && $type == 'timestamp') {
$content_cells[$i][$ci] .= '<br /><input id="field_' . $i . '_' . ($ci - $ci_offset) . 'a" type="checkbox" name="field_default_current_timestamp[' . $i . ']"';
if ($default_current_timestamp) {
$content_cells[$i][$ci] .= ' checked="checked" ';
}
$content_cells[$i][$ci] .= ' /><label for="field_' . $i . '_' . ($ci - $ci_offset) . 'a" style="font-size: ' . $font_smallest . ';">CURRENT_TIMESTAMP</label>';
}
$ci++; $ci++;
$content_cells[$i][$ci] = '<select name="field_extra[]" id="field_' . $i . '_' . ($ci - $ci_offset) . '">'; $content_cells[$i][$ci] = '<select name="field_extra[]" id="field_' . $i . '_' . ($ci - $ci_offset) . '">';

View File

@@ -83,9 +83,7 @@ $fields_cnt = PMA_DBI_num_rows($fields_rs);
// For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
// but later, if the analyser returns more information, it // but later, if the analyser returns more information, it
// could be executed for any MySQL version and replace // could be executed for any MySQL version and replace
// the info given by the previous SHOW FULL FIELDS FROM query. // the info given by SHOW FULL FIELDS FROM.
// TODO: use this information for proper treatment of TIMESTAMPs
if (PMA_MYSQL_INT_VERSION >= 40102) { if (PMA_MYSQL_INT_VERSION >= 40102) {
$show_create_table_query = 'SHOW CREATE TABLE ' $show_create_table_query = 'SHOW CREATE TABLE '
@@ -232,16 +230,23 @@ while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
$type_mime = ''; $type_mime = '';
} }
$strAttribute = '&nbsp;'; $attribute = '&nbsp;';
if ($binary) { if ($binary) {
$strAttribute = 'BINARY'; $attribute = 'BINARY';
} }
if ($unsigned) { if ($unsigned) {
$strAttribute = 'UNSIGNED'; $attribute = 'UNSIGNED';
} }
if ($zerofill) { if ($zerofill) {
$strAttribute = 'UNSIGNED ZEROFILL'; $attribute = 'UNSIGNED ZEROFILL';
} }
// MySQL 4.1.2+ TIMESTAMP options
// (if on_update_current_timestamp is set, then it's TRUE)
if (isset($analyzed_sql[0]['create_table_fields'][$row['Field']]['on_update_current_timestamp'])) {
$attribute = 'ON UPDATE CURRENT_TIMESTAMP';
}
if (!isset($row['Default'])) { if (!isset($row['Default'])) {
if ($row['Null'] != '') { if ($row['Null'] != '') {
$row['Default'] = '<i>NULL</i>'; $row['Default'] = '<i>NULL</i>';
@@ -330,7 +335,7 @@ while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
<td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap">&nbsp;<label onclick="return (document.getElementById('checkbox_row_<?php echo $i; ?>') ? false : true)" for="checkbox_row_<?php echo $i; ?>"><?php echo $field_name; ?></label>&nbsp;</td> <td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap">&nbsp;<label onclick="return (document.getElementById('checkbox_row_<?php echo $i; ?>') ? false : true)" for="checkbox_row_<?php echo $i; ?>"><?php echo $field_name; ?></label>&nbsp;</td>
<td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>"<?php echo $type_nowrap; ?>><?php echo $type; echo $type_mime; ?><bdo dir="ltr"></bdo></td> <td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>"<?php echo $type_nowrap; ?>><?php echo $type; echo $type_mime; ?><bdo dir="ltr"></bdo></td>
<?php echo PMA_MYSQL_INT_VERSION >= 40100 ? ' <td bgcolor="' . $bgcolor . '" ' . $click_mouse . '>' . (empty($field_charset) ? '&nbsp;' : '<dfn title="' . PMA_getCollationDescr($field_charset) . '">' . $field_charset . '</dfn>') . '</td>' . "\n" : '' ?> <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? ' <td bgcolor="' . $bgcolor . '" ' . $click_mouse . '>' . (empty($field_charset) ? '&nbsp;' : '<dfn title="' . PMA_getCollationDescr($field_charset) . '">' . $field_charset . '</dfn>') . '</td>' . "\n" : '' ?>
<td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap"><?php echo $strAttribute; ?></td> <td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap" style="font-size: <?php echo $font_smallest; ?>"><?php echo $attribute; ?></td>
<td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>"><?php echo (($row['Null'] == '') ? $strNo : $strYes); ?>&nbsp;</td> <td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>"><?php echo (($row['Null'] == '') ? $strNo : $strYes); ?>&nbsp;</td>
<td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap"><?php if (isset($row['Default'])) echo $row['Default']; ?>&nbsp;</td> <td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap"><?php if (isset($row['Default'])) echo $row['Default']; ?>&nbsp;</td>
<td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap"><?php echo $row['Extra']; ?>&nbsp;</td> <td <?php echo $click_mouse; ?> bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap"><?php echo $row['Extra']; ?>&nbsp;</td>