From b8b9334da98677983744f1bdedf211cee5ac8d99 Mon Sep 17 00:00:00 2001 From: Marc Delisle Date: Wed, 16 Mar 2005 17:23:58 +0000 Subject: [PATCH] experimental support in table structure editing for MySQL 4.1.2+ TIMESTAMP options --- ChangeLog | 6 +++++ config.inc.php | 3 +++ libraries/common.lib.php | 14 ++++++++--- tbl_alter.php | 21 ++++++++++++++-- tbl_properties.inc.php | 49 ++++++++++++++++++++++++++++++------ tbl_properties_structure.php | 21 ++++++++++------ 6 files changed, 92 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6cb46e17a..a99ac52a7 100755 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,12 @@ phpMyAdmin - Changelog $Id$ $Source$ +2005-03-16 Marc Delisle + * 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 * tbl_row_delete.php renamed to tbl_row_action.php (RFE 1097729) diff --git a/config.inc.php b/config.inc.php index 0b29ea521..c28c69bed 100644 --- a/config.inc.php +++ b/config.inc.php @@ -670,6 +670,9 @@ $cfg['ColumnTypes'] = array( ); // Attributes +// Note: the "ON UPDATE CURRENT_TIMESTAMP" attribute is added dynamically +// for MySQL >= 4.1.2, in tbl_properties.inc.php + $cfg['AttributeTypes'] = array( '', 'BINARY', diff --git a/libraries/common.lib.php b/libraries/common.lib.php index ec89ef4c9..187f69913 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -2517,24 +2517,30 @@ if (typeof(document.getElementById) != 'undefined' } // 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) . ' ' . $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); } - if (!empty($default)) { + + if ($default_current_timestamp) { + $query .= ' DEFAULT CURRENT_TIMESTAMP'; + } elseif (!empty($default)) { if (strtoupper($default) == 'NULL') { $query .= ' DEFAULT NULL'; } else { $query .= ' DEFAULT \'' . PMA_sqlAddslashes($default) . '\''; } } + if (!empty($null)) { $query .= ' NOT NULL'; - //} else { - // $query .= ' NULL'; } if (!empty($extra)) { $query .= ' ' . $extra; diff --git a/tbl_alter.php b/tbl_alter.php index b7c50cc40..22dafd2c9 100644 --- a/tbl_alter.php +++ b/tbl_alter.php @@ -2,7 +2,6 @@ /* $Id$ */ // vim: expandtab sw=4 ts=4 sts=4: - /** * Gets some core libraries */ @@ -67,7 +66,7 @@ if (isset($do_save_data)) { $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], $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 // 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); $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'); } diff --git a/tbl_properties.inc.php b/tbl_properties.inc.php index cfd138a16..a3b7ba543 100644 --- a/tbl_properties.inc.php +++ b/tbl_properties.inc.php @@ -116,7 +116,6 @@ if (!$is_backup) { $header_cells[] = $cfg['PropertiesIconic'] ? '' . $strIdxFulltext . '' : $strIdxFulltext; } - require_once('./libraries/relation.lib.php'); require_once('./libraries/transformations.lib.php'); $cfgRelation = PMA_getRelationsParam(); @@ -302,30 +301,48 @@ for ($i = 0 ; $i < $num_fields; $i++) { $ci++; } - $content_cells[$i][$ci] = '' . "\n"; - $strAttribute = ''; + $attribute = ''; if ($binary) { - $strAttribute = 'BINARY'; + $attribute = 'BINARY'; } if ($unsigned) { - $strAttribute = 'UNSIGNED'; + $attribute = 'UNSIGNED'; } if ($zerofill) { - $strAttribute = 'UNSIGNED ZEROFILL'; + $attribute = 'UNSIGNED ZEROFILL'; } 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']); for ($j = 0;$j < $cnt_attribute_types; $j++) { if (PMA_MYSQL_INT_VERSION >= 40100 && $cfg['AttributeTypes'][$j] == 'BINARY') { continue; } $content_cells[$i][$ci] .= '