Revert initial commit
[phpmyadmin/blinky.git] / tbl_alter.php
blob4a2a208c07d1f2170f5cdb1c1077e4017886ee43
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
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
9 * @version $Id$
10 * @package phpMyAdmin
13 /**
14 * Gets some core libraries
16 require_once './libraries/common.inc.php';
17 require_once './libraries/Table.class.php';
19 $GLOBALS['js_include'][] = 'functions.js';
20 require_once './libraries/header.inc.php';
22 // Check parameters
23 PMA_checkParameters(array('db', 'table'));
25 /**
26 * Gets tables informations
28 require_once './libraries/tbl_common.php';
29 require_once './libraries/tbl_info.inc.php';
31 $active_page = 'tbl_structure.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($_REQUEST['do_save_data'])) {
44 $field_cnt = count($_REQUEST['field_orig']);
45 $key_fields = array();
46 $changes = array();
48 for ($i = 0; $i < $field_cnt; $i++) {
49 $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
50 $_REQUEST['field_orig'][$i],
51 $_REQUEST['field_name'][$i],
52 $_REQUEST['field_type'][$i],
53 $_REQUEST['field_length'][$i],
54 $_REQUEST['field_attribute'][$i],
55 isset($_REQUEST['field_collation'][$i])
56 ? $_REQUEST['field_collation'][$i]
57 : '',
58 isset($_REQUEST['field_null'][$i])
59 ? $_REQUEST['field_null'][$i]
60 : 'NOT NULL',
61 $_REQUEST['field_default_type'][$i],
62 $_REQUEST['field_default_value'][$i],
63 isset($_REQUEST['field_extra'][$i])
64 ? $_REQUEST['field_extra'][$i]
65 : false,
66 isset($_REQUEST['field_comments'][$i])
67 ? $_REQUEST['field_comments'][$i]
68 : '',
69 $key_fields,
70 $i,
71 $_REQUEST['field_default_orig'][$i]
73 } // end for
75 // Builds the primary keys statements and updates the table
76 $key_query = '';
77 /**
78 * this is a little bit more complex
80 * @todo if someone selects A_I when altering a column we need to check:
81 * - no other column with A_I
82 * - the column has an index, if not create one
84 if (count($key_fields)) {
85 $fields = array();
86 foreach ($key_fields as $each_field) {
87 if (isset($_REQUEST['field_name'][$each_field]) && strlen($_REQUEST['field_name'][$each_field])) {
88 $fields[] = PMA_backquote($_REQUEST['field_name'][$each_field]);
90 } // end for
91 $key_query = ', ADD KEY (' . implode(', ', $fields) . ') ';
95 // To allow replication, we first select the db to use and then run queries
96 // on this db.
97 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
98 // Optimization fix - 2 May 2001 - Robbat2
99 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $changes) . $key_query;
100 $result = PMA_DBI_try_query($sql_query);
102 if ($result !== false) {
103 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
104 $message->addParam($table);
105 $btnDrop = 'Fake';
108 * If comments were sent, enable relation stuff
110 require_once './libraries/relation.lib.php';
111 require_once './libraries/transformations.lib.php';
113 // updaet field names in relation
114 if (isset($_REQUEST['field_orig']) && is_array($_REQUEST['field_orig'])) {
115 foreach ($_REQUEST['field_orig'] as $fieldindex => $fieldcontent) {
116 if ($_REQUEST['field_name'][$fieldindex] != $fieldcontent) {
117 PMA_REL_renameField($db, $table, $fieldcontent,
118 $_REQUEST['field_name'][$fieldindex]);
123 // update mime types
124 if (isset($_REQUEST['field_mimetype'])
125 && is_array($_REQUEST['field_mimetype'])
126 && $cfg['BrowseMIME']) {
127 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
128 if (isset($_REQUEST['field_name'][$fieldindex])
129 && strlen($_REQUEST['field_name'][$fieldindex])) {
130 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex],
131 $mimetype,
132 $_REQUEST['field_transformation'][$fieldindex],
133 $_REQUEST['field_transformation_options'][$fieldindex]);
138 $active_page = 'tbl_structure.php';
139 require './tbl_structure.php';
140 } else {
141 PMA_mysqlDie('', '', '', $err_url, false);
142 // An error happened while inserting/updating a table definition.
143 // to prevent total loss of that data, we embed the form once again.
144 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
145 if (isset($_REQUEST['orig_field'])) {
146 $_REQUEST['field'] = $_REQUEST['orig_field'];
149 $regenerate = true;
154 * No modifications yet required -> displays the table fields
156 * $selected comes from multi_submits.inc.php
158 if ($abort == false) {
159 if (! isset($selected)) {
160 PMA_checkParameters(array('field'));
161 $selected[] = $_REQUEST['field'];
162 $selected_cnt = 1;
163 } else { // from a multiple submit
164 $selected_cnt = count($selected);
168 * @todo optimize in case of multiple fields to modify
170 for ($i = 0; $i < $selected_cnt; $i++) {
171 $_REQUEST['field'] = PMA_sqlAddslashes($selected[$i], true);
172 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' LIKE \'' . $_REQUEST['field'] . '\';');
173 $fields_meta[] = PMA_DBI_fetch_assoc($result);
174 PMA_DBI_free_result($result);
176 $num_fields = count($fields_meta);
177 $action = 'tbl_alter.php';
179 // Get more complete field information.
180 // For now, this is done to obtain MySQL 4.1.2+ new TIMESTAMP options
181 // and to know when there is an empty DEFAULT value.
182 // Later, if the analyser returns more information, it
183 // could be executed to replace 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();
189 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
190 // SHOW FULL FIELDS says NULL and SHOW CREATE TABLE says NOT NULL (tested
191 // in MySQL 4.0.25).
193 $show_create_table = PMA_DBI_fetch_value('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0, 1);
194 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
195 unset($show_create_table);
197 * Form for changing properties.
199 require './libraries/tbl_properties.inc.php';
204 * Displays the footer
206 require_once './libraries/footer.inc.php';