Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: tbl_alter.php,v 2.29 2006/01/14 23:17:15 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * Gets some core libraries
7 */
8 require_once('./libraries/common.lib.php');
9  
10 $js_to_run = 'functions.js';
11 require_once('./libraries/header.inc.php');
12  
13 // Check parameters
14 PMA_checkParameters(array('db', 'table'));
15  
16 /**
17 * Gets tables informations
18 */
19 require_once('./libraries/tbl_properties_common.php');
20 require_once('./libraries/tbl_properties_table_info.inc.php');
21 /**
22 * Displays top menu links
23 */
24 $active_page = 'tbl_properties_structure.php';
25 // I don't see the need to display the links here, they will be displayed later
26 //require('./libraries/tbl_properties_links.inc.php');
27  
28  
29 /**
30 * Defines the url to return to in case of error in a sql statement
31 */
32 $err_url = 'tbl_properties_structure.php?' . PMA_generate_common_url($db, $table);
33  
34  
35 /**
36 * Modifications have been submitted -> updates the table
37 */
38 $abort = false;
39 if (isset($do_save_data)) {
40 $field_cnt = count($field_orig);
41 for ($i = 0; $i < $field_cnt; $i++) {
42 // to "&quot;" in tbl_properties.php
43 $field_orig[$i] = urldecode($field_orig[$i]);
44 if (strcmp(str_replace('"', '&quot;', $field_orig[$i]), $field_name[$i]) == 0) {
45 $field_name[$i] = $field_orig[$i];
46 }
47 $field_default_orig[$i] = urldecode($field_default_orig[$i]);
48 if (strcmp(str_replace('"', '&quot;', $field_default_orig[$i]), $field_default[$i]) == 0) {
49 $field_default[$i] = $field_default_orig[$i];
50 }
51 $field_length_orig[$i] = urldecode($field_length_orig[$i]);
52 if (strcmp(str_replace('"', '&quot;', $field_length_orig[$i]), $field_length[$i]) == 0) {
53 $field_length[$i] = $field_length_orig[$i];
54 }
55 if (!isset($query)) {
56 $query = '';
57 } else {
58 $query .= ', CHANGE ';
59 }
60  
61 $query .= PMA_generateAlterTable($field_orig[$i], $field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$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]);
62 } // end for
63  
64 // To allow replication, we first select the db to use and then run queries
65 // on this db.
66 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
67 // Optimization fix - 2 May 2001 - Robbat2
68 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . $query;
69 $error_create = FALSE;
70 $result = PMA_DBI_try_query($sql_query) or $error_create = TRUE;
71  
72 if ($error_create == FALSE) {
73 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
74 $btnDrop = 'Fake';
75  
76 // garvin: If comments were sent, enable relation stuff
77 require_once('./libraries/relation.lib.php');
78 require_once('./libraries/transformations.lib.php');
79  
80 $cfgRelation = PMA_getRelationsParam();
81  
82 // take care of pmadb internal comments here
83 // garvin: Update comment table, if a comment was set.
84 if (PMA_MYSQL_INT_VERSION < 40100 && isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
85 foreach ($field_comments AS $fieldindex => $fieldcomment) {
86 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
87 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, $field_orig[$fieldindex], 'pmadb');
88 }
89 }
90 }
91  
92 // garvin: Rename relations&display fields, if altered.
93 if (($cfgRelation['displaywork'] || $cfgRelation['relwork']) && isset($field_orig) && is_array($field_orig)) {
94 foreach ($field_orig AS $fieldindex => $fieldcontent) {
95 if ($field_name[$fieldindex] != $fieldcontent) {
96 if ($cfgRelation['displaywork']) {
97 $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
98 . ' SET display_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
99 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
100 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
101 . ' AND display_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
102 $tb_rs = PMA_query_as_cu($table_query);
103 unset($table_query);
104 unset($tb_rs);
105 }
106  
107 if ($cfgRelation['relwork']) {
108 $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
109 . ' SET master_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
110 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
111 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
112 . ' AND master_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
113 $tb_rs = PMA_query_as_cu($table_query);
114 unset($table_query);
115 unset($tb_rs);
116  
117 $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
118 . ' SET foreign_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
119 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
120 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
121 . ' AND foreign_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
122 $tb_rs = PMA_query_as_cu($table_query);
123 unset($table_query);
124 unset($tb_rs);
125 } // end if relwork
126 } // end if fieldname has changed
127 } // end while check fieldnames
128 } // end if relations/display has to be changed
129  
130 // garvin: Update comment table for mime types [MIME]
131 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
132 foreach ($field_mimetype AS $fieldindex => $mimetype) {
133 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
134 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
135 }
136 }
137 }
138  
139 $active_page = 'tbl_properties_structure.php';
140 require('./tbl_properties_structure.php');
141 } else {
142 PMA_mysqlDie('', '', '', $err_url, FALSE);
143 // garvin: An error happened while inserting/updating a table definition.
144 // to prevent total loss of that data, we embed the form once again.
145 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
146 if (isset($orig_field)) {
147 $field = $orig_field;
148 }
149  
150 $regenerate = true;
151 }
152 }
153  
154 /**
155 * No modifications yet required -> displays the table fields
156 */
157 if ($abort == FALSE) {
158 if (!isset($selected)) {
159 PMA_checkParameters(array('field'));
160 $selected[] = $field;
161 $selected_cnt = 1;
162 } else { // from a multiple submit
163 $selected_cnt = count($selected);
164 }
165  
166 // TODO: optimize in case of multiple fields to modify
167 for ($i = 0; $i < $selected_cnt; $i++) {
168 if (!empty($submit_mult)) {
169 $field = PMA_sqlAddslashes(urldecode($selected[$i]), TRUE);
170 } else {
171 $field = PMA_sqlAddslashes($selected[$i], TRUE);
172 }
173 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' LIKE \'' . $field . '\';');
174 $fields_meta[] = PMA_DBI_fetch_assoc($result);
175 PMA_DBI_free_result($result);
176 }
177 $num_fields = count($fields_meta);
178 $action = 'tbl_alter.php';
179  
180 // Get more complete field information
181 // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
182 // but later, if the analyser returns more information, it
183 // could be executed for any MySQL version and replace
184 // the info given by SHOW FULL FIELDS FROM.
185 // TODO: put this code into a require()
186 // or maybe make it part of PMA_DBI_get_fields();
187  
188 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
189 // SHOW FULL FIELDS says NULL and SHOW CREATE TABLE says NOT NULL (tested
190 // in MySQL 4.0.25).
191  
192 $show_create_table = PMA_DBI_fetch_value(
193 'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table),
194 0, 1 );
195 $analyzed_sql = PMA_SQP_analyze( PMA_SQP_parse( $show_create_table ) );
196  
197 require('./libraries/tbl_properties.inc.php');
198 }
199  
200  
201 /**
202 * Displays the footer
203 */
204 require_once('./libraries/footer.inc.php');
205 ?>