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