2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Alter one or more table columns/fields
6 * linked from table_structure, uses libraries/tbl_properties.inc.php to display
7 * form and handles this form data
13 * Gets some core libraries
15 require_once './libraries/common.inc.php';
16 require_once './libraries/Table.class.php';
18 $GLOBALS['js_include'][] = 'functions.js';
19 require_once './libraries/header.inc.php';
22 PMA_checkParameters(array('db', 'table'));
25 * Gets tables informations
27 require_once './libraries/tbl_common.php';
28 require_once './libraries/tbl_info.inc.php';
30 * Displays top menu links
32 $active_page = 'tbl_structure.php';
33 // I don't see the need to display the links here, they will be displayed later
34 //require './libraries/tbl_links.inc.php';
38 * Defines the url to return to in case of error in a sql statement
40 $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
44 * Modifications have been submitted -> updates the table
47 if (isset($do_save_data)) {
48 $field_cnt = count($field_orig);
49 for ($i = 0; $i < $field_cnt; $i++
) {
50 // to """ in tbl_sql.php
51 $field_orig[$i] = urldecode($field_orig[$i]);
52 if (strcmp(str_replace('"', '"', $field_orig[$i]), $field_name[$i]) == 0) {
53 $field_name[$i] = $field_orig[$i];
55 $field_default_orig[$i] = urldecode($field_default_orig[$i]);
56 if (strcmp(str_replace('"', '"', $field_default_orig[$i]), $field_default[$i]) == 0) {
57 $field_default[$i] = $field_default_orig[$i];
59 $field_length_orig[$i] = urldecode($field_length_orig[$i]);
60 if (strcmp(str_replace('"', '"', $field_length_orig[$i]), $field_length[$i]) == 0) {
61 $field_length[$i] = $field_length_orig[$i];
66 $query .= ', CHANGE ';
69 $query .= PMA_Table
::generateAlter($field_orig[$i], $field_name[$i],
70 $field_type[$i], $field_length[$i], $field_attribute[$i],
71 isset($field_collation[$i]) ?
$field_collation[$i] : '',
72 isset($field_null[$i]) ?
$field_null[$i] : 'NOT NULL',
74 isset($field_default_current_timestamp[$i]), $field_extra[$i],
75 isset($field_comments[$i]) ?
$field_comments[$i] : '',
76 $field_default_orig[$i]);
79 // To allow replication, we first select the db to use and then run queries
81 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
82 // Optimization fix - 2 May 2001 - Robbat2
83 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . $query;
84 $error_create = FALSE;
85 $result = PMA_DBI_try_query($sql_query) or $error_create = TRUE;
87 if ($error_create == FALSE) {
88 $message = PMA_Message
::success('strTableAlteredSuccessfully');
89 $message->addParam($table);
92 // garvin: If comments were sent, enable relation stuff
93 require_once './libraries/relation.lib.php';
94 require_once './libraries/transformations.lib.php';
96 $cfgRelation = PMA_getRelationsParam();
98 // garvin: Rename relations&display fields, if altered.
99 if (($cfgRelation['displaywork'] ||
$cfgRelation['relwork']) && isset($field_orig) && is_array($field_orig)) {
100 foreach ($field_orig AS $fieldindex => $fieldcontent) {
101 if ($field_name[$fieldindex] != $fieldcontent) {
102 if ($cfgRelation['displaywork']) {
103 $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
104 . ' SET display_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
105 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
106 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
107 . ' AND display_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
108 $tb_rs = PMA_query_as_cu($table_query);
113 if ($cfgRelation['relwork']) {
114 $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
115 . ' SET master_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
116 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
117 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
118 . ' AND master_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
119 $tb_rs = PMA_query_as_cu($table_query);
123 $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
124 . ' SET foreign_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
125 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
126 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
127 . ' AND foreign_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
128 $tb_rs = PMA_query_as_cu($table_query);
132 } // end if fieldname has changed
133 } // end while check fieldnames
134 } // end if relations/display has to be changed
136 // garvin: Update comment table for mime types [MIME]
137 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
138 foreach ($field_mimetype AS $fieldindex => $mimetype) {
139 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
140 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
145 $active_page = 'tbl_structure.php';
146 require './tbl_structure.php';
148 PMA_mysqlDie('', '', '', $err_url, FALSE);
149 // garvin: An error happened while inserting/updating a table definition.
150 // to prevent total loss of that data, we embed the form once again.
151 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
152 if (isset($orig_field)) {
153 $field = $orig_field;
161 * No modifications yet required -> displays the table fields
163 if ($abort == FALSE) {
164 if (!isset($selected)) {
165 PMA_checkParameters(array('field'));
166 $selected[] = $field;
168 } else { // from a multiple submit
169 $selected_cnt = count($selected);
173 * @todo optimize in case of multiple fields to modify
175 for ($i = 0; $i < $selected_cnt; $i++
) {
176 if (!empty($submit_mult)) {
177 $field = PMA_sqlAddslashes(urldecode($selected[$i]), TRUE);
179 $field = PMA_sqlAddslashes($selected[$i], TRUE);
181 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' LIKE \'' . $field . '\';');
182 $fields_meta[] = PMA_DBI_fetch_assoc($result);
183 PMA_DBI_free_result($result);
185 $num_fields = count($fields_meta);
186 $action = 'tbl_alter.php';
188 // Get more complete field information
189 // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
190 // but later, if the analyser returns more information, it
191 // could be executed for any MySQL version and replace
192 // the info given by SHOW FULL FIELDS FROM.
194 * @todo put this code into a require()
195 * or maybe make it part of PMA_DBI_get_fields();
198 if (PMA_MYSQL_INT_VERSION
< 50025) {
199 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
200 // SHOW FULL FIELDS says NULL and SHOW CREATE TABLE says NOT NULL (tested
203 $show_create_table = PMA_DBI_fetch_value(
204 'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table),
206 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
209 require './libraries/tbl_properties.inc.php';
214 * Displays the footer
216 require_once './libraries/footer.inc.php';