Move common code into a function
[phpmyadmin/ammaryasirr.git] / tbl_alter.php
blob1c2d7fbd80c2a87e16f7499890708380e0025629
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Alter one or more table columns
6 * linked from table_structure, uses libraries/tbl_properties.inc.php to display
7 * form and handles this form data
9 * @package phpMyAdmin
12 /**
13 * Gets some core libraries
15 require_once './libraries/common.inc.php';
17 require_once './libraries/header.inc.php';
19 // Check parameters
20 PMA_checkParameters(array('db', 'table'));
22 /**
23 * Gets tables informations
25 require_once './libraries/tbl_common.php';
26 require_once './libraries/tbl_info.inc.php';
28 $active_page = 'tbl_structure.php';
30 /**
31 * Defines the url to return to in case of error in a sql statement
33 $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
36 /**
37 * Modifications have been submitted -> updates the table
39 $abort = false;
40 if (isset($_REQUEST['do_save_data'])) {
41 $field_cnt = count($_REQUEST['field_orig']);
42 $key_fields = array();
43 $changes = array();
45 for ($i = 0; $i < $field_cnt; $i++) {
46 $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
47 $_REQUEST['field_orig'][$i],
48 $_REQUEST['field_name'][$i],
49 $_REQUEST['field_type'][$i],
50 $_REQUEST['field_length'][$i],
51 $_REQUEST['field_attribute'][$i],
52 isset($_REQUEST['field_collation'][$i])
53 ? $_REQUEST['field_collation'][$i]
54 : '',
55 isset($_REQUEST['field_null'][$i])
56 ? $_REQUEST['field_null'][$i]
57 : 'NOT NULL',
58 $_REQUEST['field_default_type'][$i],
59 $_REQUEST['field_default_value'][$i],
60 isset($_REQUEST['field_extra'][$i])
61 ? $_REQUEST['field_extra'][$i]
62 : false,
63 isset($_REQUEST['field_comments'][$i])
64 ? $_REQUEST['field_comments'][$i]
65 : '',
66 $key_fields,
67 $i,
68 $_REQUEST['field_default_orig'][$i]
70 } // end for
72 // Builds the primary keys statements and updates the table
73 $key_query = '';
74 /**
75 * this is a little bit more complex
77 * @todo if someone selects A_I when altering a column we need to check:
78 * - no other column with A_I
79 * - the column has an index, if not create one
81 if (count($key_fields)) {
82 $fields = array();
83 foreach ($key_fields as $each_field) {
84 if (isset($_REQUEST['field_name'][$each_field]) && strlen($_REQUEST['field_name'][$each_field])) {
85 $fields[] = PMA_backquote($_REQUEST['field_name'][$each_field]);
87 } // end for
88 $key_query = ', ADD KEY (' . implode(', ', $fields) . ') ';
92 // To allow replication, we first select the db to use and then run queries
93 // on this db.
94 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
95 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $changes) . $key_query;
96 $result = PMA_DBI_try_query($sql_query);
98 if ($result !== false) {
99 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
100 $message->addParam($table);
101 $btnDrop = 'Fake';
104 * If comments were sent, enable relation stuff
106 require_once './libraries/transformations.lib.php';
108 // updaet field names in relation
109 if (isset($_REQUEST['field_orig']) && is_array($_REQUEST['field_orig'])) {
110 foreach ($_REQUEST['field_orig'] as $fieldindex => $fieldcontent) {
111 if ($_REQUEST['field_name'][$fieldindex] != $fieldcontent) {
112 PMA_REL_renameField($db, $table, $fieldcontent,
113 $_REQUEST['field_name'][$fieldindex]);
118 // update mime types
119 if (isset($_REQUEST['field_mimetype'])
120 && is_array($_REQUEST['field_mimetype'])
121 && $cfg['BrowseMIME']) {
122 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
123 if (isset($_REQUEST['field_name'][$fieldindex])
124 && strlen($_REQUEST['field_name'][$fieldindex])) {
125 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex],
126 $mimetype,
127 $_REQUEST['field_transformation'][$fieldindex],
128 $_REQUEST['field_transformation_options'][$fieldindex]);
133 if( $GLOBALS['is_ajax_request'] == true) {
134 PMA_ajaxResponse($message, $message->isSuccess());
137 $active_page = 'tbl_structure.php';
138 require './tbl_structure.php';
139 } else {
140 PMA_mysqlDie('', '', '', $err_url, false);
141 // An error happened while inserting/updating a table definition.
142 // to prevent total loss of that data, we embed the form once again.
143 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
144 if (isset($_REQUEST['orig_field'])) {
145 $_REQUEST['field'] = $_REQUEST['orig_field'];
148 $regenerate = true;
153 * No modifications yet required -> displays the table fields
155 * $selected comes from multi_submits.inc.php
157 if ($abort == false) {
158 require_once './libraries/tbl_links.inc.php';
160 if (! isset($selected)) {
161 PMA_checkParameters(array('field'));
162 $selected[] = $_REQUEST['field'];
163 $selected_cnt = 1;
164 } else { // from a multiple submit
165 $selected_cnt = count($selected);
169 * @todo optimize in case of multiple fields to modify
171 for ($i = 0; $i < $selected_cnt; $i++) {
172 $_REQUEST['field'] = PMA_sqlAddslashes($selected[$i], true);
173 $result = PMA_DRIZZLE
174 ? PMA_DBI_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' WHERE Field = \'' . $_REQUEST['field'] . '\';')
175 : PMA_DBI_query('SHOW FULL COLUMNS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' LIKE \'' . $_REQUEST['field'] . '\';');
176 $fields_meta[] = PMA_DBI_fetch_assoc($result);
177 PMA_DBI_free_result($result);
179 $num_fields = count($fields_meta);
180 $action = 'tbl_alter.php';
182 // Get more complete field information.
183 // For now, this is done to obtain MySQL 4.1.2+ new TIMESTAMP options
184 // and to know when there is an empty DEFAULT value.
185 // Later, if the analyser returns more information, it
186 // could be executed to replace the info given by SHOW FULL FIELDS FROM.
188 * @todo put this code into a require()
189 * or maybe make it part of PMA_DBI_get_fields();
192 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
193 // SHOW FULL FIELDS says NULL and SHOW CREATE TABLE says NOT NULL (tested
194 // in MySQL 4.0.25).
196 $show_create_table = PMA_DBI_fetch_value('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0, 1);
197 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
198 unset($show_create_table);
200 * Form for changing properties.
202 require './libraries/tbl_properties.inc.php';
207 * Displays the footer
209 require './libraries/footer.inc.php';