2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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
13 * Gets some core libraries
15 require_once './libraries/common.inc.php';
16 require_once './libraries/Table.class.php';
18 $GLOBALS['js_include'][] = 'functions.js';
19 require_once './libraries/header.inc.php';
22 PMA_checkParameters(array('db', 'table'));
25 * Gets tables informations
27 require_once './libraries/tbl_common.php';
28 require_once './libraries/tbl_info.inc.php';
30 $active_page = 'tbl_structure.php';
33 * Defines the url to return to in case of error in a sql statement
35 $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
39 * Modifications have been submitted -> updates the table
42 if (isset($_REQUEST['do_save_data'])) {
43 $field_cnt = count($_REQUEST['field_orig']);
44 $key_fields = array();
47 for ($i = 0; $i < $field_cnt; $i++
) {
48 $changes[] = 'CHANGE ' . PMA_Table
::generateAlter(
49 $_REQUEST['field_orig'][$i],
50 $_REQUEST['field_name'][$i],
51 $_REQUEST['field_type'][$i],
52 $_REQUEST['field_length'][$i],
53 $_REQUEST['field_attribute'][$i],
54 isset($_REQUEST['field_collation'][$i])
55 ?
$_REQUEST['field_collation'][$i]
57 isset($_REQUEST['field_null'][$i])
58 ?
$_REQUEST['field_null'][$i]
60 $_REQUEST['field_default_type'][$i],
61 $_REQUEST['field_default_value'][$i],
62 isset($_REQUEST['field_extra'][$i])
63 ?
$_REQUEST['field_extra'][$i]
65 isset($_REQUEST['field_comments'][$i])
66 ?
$_REQUEST['field_comments'][$i]
70 $_REQUEST['field_default_orig'][$i]
74 // Builds the primary keys statements and updates the table
77 * this is a little bit more complex
79 * @todo if someone selects A_I when altering a column we need to check:
80 * - no other column with A_I
81 * - the column has an index, if not create one
83 if (count($key_fields)) {
85 foreach ($key_fields as $each_field) {
86 if (isset($_REQUEST['field_name'][$each_field]) && strlen($_REQUEST['field_name'][$each_field])) {
87 $fields[] = PMA_backquote($_REQUEST['field_name'][$each_field]);
90 $key_query = ', ADD KEY (' . implode(', ', $fields) . ') ';
94 // To allow replication, we first select the db to use and then run queries
96 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
97 // Optimization fix - 2 May 2001 - Robbat2
98 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $changes) . $key_query;
99 $result = PMA_DBI_try_query($sql_query);
101 if ($result !== false) {
102 $message = PMA_Message
::success('strTableAlteredSuccessfully');
103 $message->addParam($table);
107 * If comments were sent, enable relation stuff
109 require_once './libraries/relation.lib.php';
110 require_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]);
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],
131 $_REQUEST['field_transformation'][$fieldindex],
132 $_REQUEST['field_transformation_options'][$fieldindex]);
137 $active_page = 'tbl_structure.php';
138 require './tbl_structure.php';
140 PMA_mysqlDie('', '', '', $err_url, false);
141 // garvin: 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'];
153 * No modifications yet required -> displays the table fields
155 * $selected comes from multi_submits.inc.php
157 if ($abort == false) {
158 if (! isset($selected)) {
159 PMA_checkParameters(array('field'));
160 $selected[] = $_REQUEST['field'];
162 } else { // from a multiple submit
163 $selected_cnt = count($selected);
167 * @todo optimize in case of multiple fields to modify
169 for ($i = 0; $i < $selected_cnt; $i++
) {
170 $_REQUEST['field'] = PMA_sqlAddslashes($selected[$i], true);
171 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' LIKE \'' . $_REQUEST['field'] . '\';');
172 $fields_meta[] = PMA_DBI_fetch_assoc($result);
173 PMA_DBI_free_result($result);
175 $num_fields = count($fields_meta);
176 $action = 'tbl_alter.php';
178 // Get more complete field information
179 // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
180 // but later, if the analyser returns more information, it
181 // could be executed for any MySQL version and replace
182 // the info given by SHOW FULL FIELDS FROM.
184 * @todo put this code into a require()
185 * or maybe make it part of PMA_DBI_get_fields();
188 if (PMA_MYSQL_INT_VERSION
< 50025) {
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
193 $show_create_table = PMA_DBI_fetch_value(
194 'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table),
196 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
200 * Form for changing properties.
202 require './libraries/tbl_properties.inc.php';
207 * Displays the footer
209 require_once './libraries/footer.inc.php';