Translated using Weblate.
[phpmyadmin.git] / tbl_alter.php
blobc9262ea036642119eea8b5a0cc7f55197bf3b223
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 if (isset($_REQUEST['field'])) {
20 $GLOBALS['field'] = $_REQUEST['field'];
23 // Check parameters
24 PMA_checkParameters(array('db', 'table'));
26 /**
27 * Gets tables informations
29 require_once './libraries/tbl_common.php';
30 require_once './libraries/tbl_info.inc.php';
32 $active_page = 'tbl_structure.php';
34 /**
35 * Defines the url to return to in case of error in a sql statement
37 $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
40 /**
41 * Modifications have been submitted -> updates the table
43 $abort = false;
44 if (isset($_REQUEST['do_save_data'])) {
45 $field_cnt = count($_REQUEST['field_orig']);
46 $key_fields = array();
47 $changes = array();
49 for ($i = 0; $i < $field_cnt; $i++) {
50 $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
51 $_REQUEST['field_orig'][$i],
52 $_REQUEST['field_name'][$i],
53 $_REQUEST['field_type'][$i],
54 $_REQUEST['field_length'][$i],
55 $_REQUEST['field_attribute'][$i],
56 isset($_REQUEST['field_collation'][$i])
57 ? $_REQUEST['field_collation'][$i]
58 : '',
59 isset($_REQUEST['field_null'][$i])
60 ? $_REQUEST['field_null'][$i]
61 : 'NOT NULL',
62 $_REQUEST['field_default_type'][$i],
63 $_REQUEST['field_default_value'][$i],
64 isset($_REQUEST['field_extra'][$i])
65 ? $_REQUEST['field_extra'][$i]
66 : false,
67 isset($_REQUEST['field_comments'][$i])
68 ? $_REQUEST['field_comments'][$i]
69 : '',
70 $key_fields,
71 $i,
72 $_REQUEST['field_default_orig'][$i]
74 } // end for
76 // Builds the primary keys statements and updates the table
77 $key_query = '';
78 /**
79 * this is a little bit more complex
81 * @todo if someone selects A_I when altering a column we need to check:
82 * - no other column with A_I
83 * - the column has an index, if not create one
85 if (count($key_fields)) {
86 $fields = array();
87 foreach ($key_fields as $each_field) {
88 if (isset($_REQUEST['field_name'][$each_field]) && strlen($_REQUEST['field_name'][$each_field])) {
89 $fields[] = PMA_backquote($_REQUEST['field_name'][$each_field]);
91 } // end for
92 $key_query = ', ADD KEY (' . implode(', ', $fields) . ') ';
96 // To allow replication, we first select the db to use and then run queries
97 // on this db.
98 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
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 include_once './libraries/transformations.lib.php';
112 // updaet field names in relation
113 if (isset($_REQUEST['field_orig']) && is_array($_REQUEST['field_orig'])) {
114 foreach ($_REQUEST['field_orig'] as $fieldindex => $fieldcontent) {
115 if ($_REQUEST['field_name'][$fieldindex] != $fieldcontent) {
116 PMA_REL_renameField($db, $table, $fieldcontent,
117 $_REQUEST['field_name'][$fieldindex]);
122 // update mime types
123 if (isset($_REQUEST['field_mimetype'])
124 && is_array($_REQUEST['field_mimetype'])
125 && $cfg['BrowseMIME']) {
126 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
127 if (isset($_REQUEST['field_name'][$fieldindex])
128 && strlen($_REQUEST['field_name'][$fieldindex])) {
129 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex],
130 $mimetype,
131 $_REQUEST['field_transformation'][$fieldindex],
132 $_REQUEST['field_transformation_options'][$fieldindex]);
137 if ( $_REQUEST['ajax_request'] == true) {
138 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
139 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
142 $active_page = 'tbl_structure.php';
143 include './tbl_structure.php';
144 } else {
145 PMA_mysqlDie('', '', '', $err_url, false);
146 // An error happened while inserting/updating a table definition.
147 // to prevent total loss of that data, we embed the form once again.
148 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
149 if (isset($_REQUEST['orig_field'])) {
150 $_REQUEST['field'] = $_REQUEST['orig_field'];
153 $regenerate = true;
158 * No modifications yet required -> displays the table fields
160 * $selected comes from multi_submits.inc.php
162 if ($abort == false) {
163 if ($_REQUEST['ajax_request'] != true) {
164 include_once './libraries/tbl_links.inc.php';
167 if (! isset($selected)) {
168 PMA_checkParameters(array('field'));
169 $selected[] = $_REQUEST['field'];
170 $selected_cnt = 1;
171 } else { // from a multiple submit
172 $selected_cnt = count($selected);
176 * @todo optimize in case of multiple fields to modify
178 for ($i = 0; $i < $selected_cnt; $i++) {
179 $fields_meta[] = PMA_DBI_get_columns($db, $table, $selected[$i], true);
181 $num_fields = count($fields_meta);
182 $action = 'tbl_alter.php';
184 // Get more complete field information.
185 // For now, this is done to obtain MySQL 4.1.2+ new TIMESTAMP options
186 // and to know when there is an empty DEFAULT value.
187 // Later, if the analyser returns more information, it
188 // could be executed to replace the info given by SHOW FULL COLUMNS FROM.
190 * @todo put this code into a require()
191 * or maybe make it part of PMA_DBI_get_columns();
194 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
195 // SHOW FULL COLUMNS says NULL and SHOW CREATE TABLE says NOT NULL (tested
196 // in MySQL 4.0.25).
198 $show_create_table = PMA_DBI_fetch_value('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0, 1);
199 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
200 unset($show_create_table);
202 * Form for changing properties.
204 include './libraries/tbl_properties.inc.php';
209 * Displays the footer
211 require './libraries/footer.inc.php';