Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 311 lines (281 sloc) 10.192 kb
7ff8fed Initial revision
Olivier Müller authored
1 <?php
817e790 synced/fixed vim line
Sebastian Mendel authored
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
374abd5 fixed/added page level docblock
Sebastian Mendel authored
3 /**
caa9844 @lem9 conform to relational terminology regarding columns and rows
lem9 authored
4 * Alter one or more table columns
d7ecf57 replaced NULL/NOT NULL selectbox with checkbox (preserver space)
Sebastian Mendel authored
5 *
6 * linked from table_structure, uses libraries/tbl_properties.inc.php to display
7 * form and handles this form data
374abd5 fixed/added page level docblock
Sebastian Mendel authored
8 *
e56949f @nijel Use package name PhpMyAdmin
nijel authored
9 * @package PhpMyAdmin
374abd5 fixed/added page level docblock
Sebastian Mendel authored
10 */
7ff8fed Initial revision
Olivier Müller authored
11
0ab0ad6 @lem9 merge Loic's version
lem9 authored
12 /**
13 * Gets some core libraries
14 */
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
15 require_once 'libraries/common.inc.php';
fcf387b common.lib.php should REALLY REALLY and ALWAYS be the first include
Sebastian Mendel authored
16
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
17 require_once 'libraries/header.inc.php';
0ab0ad6 @lem9 merge Loic's version
lem9 authored
18
1c50bea @lem9 No longer depend on grab_globals.lib.php for $_GET variables
lem9 authored
19 if (isset($_REQUEST['field'])) {
20 $GLOBALS['field'] = $_REQUEST['field'];
21 }
22
fc1c463 @lem9 path disclosure
lem9 authored
23 // Check parameters
24 PMA_checkParameters(array('db', 'table'));
0ab0ad6 @lem9 merge Loic's version
lem9 authored
25
26 /**
3f258cf @nijel Cleanup of message displaying and navigation reloading.
nijel authored
27 * Gets tables informations
28 */
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
29 require_once 'libraries/tbl_common.php';
30 require_once 'libraries/tbl_info.inc.php';
3f258cf @nijel Cleanup of message displaying and navigation reloading.
nijel authored
31
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
32 $active_page = 'tbl_structure.php';
3f258cf @nijel Cleanup of message displaying and navigation reloading.
nijel authored
33
34 /**
e726fe7 Fixed bug #444352 (Data Missing/POST Error)
Loïc Chapeaux authored
35 * Defines the url to return to in case of error in a sql statement
36 */
ad06b4a @nijel Rename tbl_* files to drop useless _properties part.
nijel authored
37 $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
e726fe7 Fixed bug #444352 (Data Missing/POST Error)
Loïc Chapeaux authored
38
37ac76c rfe #3517835 [structure] Move columns easily
Jo Michael authored
39 /**
40 * Moving columns
41 */
42 if (isset($_REQUEST['move_columns'])
43 && is_array($_REQUEST['move_columns'])
44 && $GLOBALS['is_ajax_request']) {
45 /*
46 * first, load the definitions for all columns
47 */
75d296a Use PMA_DBI_get_columns_full for moving columns
Jo Michael authored
48 $columns = PMA_DBI_get_columns_full($db, $table);
cdfe73d Use PMA_Table::generateAlter for moving columns
Jo Michael authored
49 $column_names = array_keys($columns);
50 $changes = array();
51
37ac76c rfe #3517835 [structure] Move columns easily
Jo Michael authored
52 // move columns from first to last
53 for ($i = 0, $l = count($_REQUEST['move_columns']); $i < $l; $i++) {
54 $column = $_REQUEST['move_columns'][$i];
55 // is this column already correctly placed?
75d296a Use PMA_DBI_get_columns_full for moving columns
Jo Michael authored
56 if ($column_names[$i] == $column) {
37ac76c rfe #3517835 [structure] Move columns easily
Jo Michael authored
57 continue;
58 }
cdfe73d Use PMA_Table::generateAlter for moving columns
Jo Michael authored
59
37ac76c rfe #3517835 [structure] Move columns easily
Jo Michael authored
60 // it is not, let's move it to index $i
cdfe73d Use PMA_Table::generateAlter for moving columns
Jo Michael authored
61 $data = $columns[$column];
62 $extracted_fieldspec = PMA_extractFieldspec($data['Type']);
63 $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
64 $column,
65 $column,
66 strtoupper($extracted_fieldspec['type']),
67 $extracted_fieldspec['spec_in_brackets'],
68 $extracted_fieldspec['attribute'],
69 isset($data['Collation'])
70 ? $data['Collation']
71 : '',
72 $data['Null'] === 'YES'
73 ? 'NULL'
74 : 'NOT NULL',
75 $data['Null'] === 'YES' && $data['Default'] === null
76 ? 'NULL'
77 : ($data['Default'] != ''
78 ? 'USER_DEFINED'
79 : 'NONE'),
80 $data['Default'],
81 $data['Extra'] !== ''
82 ? $data['Extra']
83 : false,
84 $data['Comments'] !== ''
85 ? $data['Comments']
86 : false,
87 $key_fields,
88 $i,
89 '',
90 $i === 0
91 ? '-first'
92 : $column_names[$i - 1]
93 );
75d296a Use PMA_DBI_get_columns_full for moving columns
Jo Michael authored
94 // update current column_names array, first delete old position
95 for($j = 0, $ll = count($column_names); $j < $ll; $j++) {
96 if ($column_names[$j] == $column) {
97 unset($column_names[$j]);
37ac76c rfe #3517835 [structure] Move columns easily
Jo Michael authored
98 }
99 }
100 // insert moved column
75d296a Use PMA_DBI_get_columns_full for moving columns
Jo Michael authored
101 array_splice($column_names, $i, 0, $column);
37ac76c rfe #3517835 [structure] Move columns easily
Jo Michael authored
102 }
cdfe73d Use PMA_Table::generateAlter for moving columns
Jo Michael authored
103 if (empty($changes)) { // should never happen
104 PMA_ajaxResponse('', true);
105 }
106 $move_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ';
107 $move_query .= implode(', ', $changes);
108 // move columns
109 $result = PMA_DBI_try_query($move_query);
110 $tmp_error = PMA_DBI_getError();
111 if ($tmp_error) {
112 PMA_ajaxResponse(PMA_Message::error($tmp_error), false);
113 }
37ac76c rfe #3517835 [structure] Move columns easily
Jo Michael authored
114 PMA_ajaxResponse(PMA_Message::success(__('The columns have been moved successfully.')), true);
115 }
e726fe7 Fixed bug #444352 (Data Missing/POST Error)
Loïc Chapeaux authored
116
117 /**
0ab0ad6 @lem9 merge Loic's version
lem9 authored
118 * Modifications have been submitted -> updates the table
119 */
3ae253e Do not waste the data for table-field creation, if MySQL shows an error.
Garvin Hicking authored
120 $abort = false;
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
121 if (isset($_REQUEST['do_save_data'])) {
122 $field_cnt = count($_REQUEST['field_orig']);
123 $key_fields = array();
124 $changes = array();
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
125
bbef63f Merged part 2 of patch #458014: multi-column editing
Loïc Chapeaux authored
126 for ($i = 0; $i < $field_cnt; $i++) {
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
127 $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
128 $_REQUEST['field_orig'][$i],
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
129 $_REQUEST['field_name'][$i],
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
130 $_REQUEST['field_type'][$i],
131 $_REQUEST['field_length'][$i],
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
132 $_REQUEST['field_attribute'][$i],
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
133 isset($_REQUEST['field_collation'][$i])
134 ? $_REQUEST['field_collation'][$i]
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
135 : '',
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
136 isset($_REQUEST['field_null'][$i])
137 ? $_REQUEST['field_null'][$i]
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
138 : 'NOT NULL',
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
139 $_REQUEST['field_default_type'][$i],
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
140 $_REQUEST['field_default_value'][$i],
a20d062 fixed NOTICE
Sebastian Mendel authored
141 isset($_REQUEST['field_extra'][$i])
142 ? $_REQUEST['field_extra'][$i]
143 : false,
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
144 isset($_REQUEST['field_comments'][$i])
145 ? $_REQUEST['field_comments'][$i]
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
146 : '',
147 $key_fields,
148 $i,
b5bca9c Move columns in table columns editor
Jo Michael authored
149 $_REQUEST['field_default_orig'][$i],
150 isset($_REQUEST['field_move_to'][$i])
151 ? $_REQUEST['field_move_to'][$i]
152 : ''
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
153 );
bbef63f Merged part 2 of patch #458014: multi-column editing
Loïc Chapeaux authored
154 } // end for
0ab0ad6 @lem9 merge Loic's version
lem9 authored
155
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
156 // Builds the primary keys statements and updates the table
157 $key_query = '';
158 /**
159 * this is a little bit more complex
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
160 *
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
161 * @todo if someone selects A_I when altering a column we need to check:
162 * - no other column with A_I
163 * - the column has an index, if not create one
164 *
165 if (count($key_fields)) {
166 $fields = array();
167 foreach ($key_fields as $each_field) {
168 if (isset($_REQUEST['field_name'][$each_field]) && strlen($_REQUEST['field_name'][$each_field])) {
169 $fields[] = PMA_backquote($_REQUEST['field_name'][$each_field]);
170 }
171 } // end for
172 $key_query = ', ADD KEY (' . implode(', ', $fields) . ') ';
173 }
174 */
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
175
f7ef2d4 Fixed bug #548495 - Alter table not replicate
Loïc Chapeaux authored
176 // To allow replication, we first select the db to use and then run queries
177 // on this db.
091daa3 @ruleant clarify code
ruleant authored
178 if (! PMA_DBI_select_db($db)) {
179 PMA_mysqlDie(
180 PMA_DBI_getError(),
181 'USE ' . PMA_backquote($db) . ';',
182 '',
183 $err_url
184 );
185 }
186 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ';
187 $sql_query .= implode(', ', $changes) . $key_query;
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
188 $result = PMA_DBI_try_query($sql_query);
30b1873 Update and display column comments in Add/Edit Fieldmode and CREATE tabl...
Garvin Hicking authored
189
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
190 if ($result !== false) {
76ce885 @ruleant improve coding style
ruleant authored
191 $message = PMA_Message::success(
192 __('Table %1$s has been altered successfully')
193 );
26599a4 fixed call to PMA_Message::addParam()
Sebastian Mendel authored
194 $message->addParam($table);
65fea8c make use of PMA_Message
Sebastian Mendel authored
195 $btnDrop = 'Fake';
a1875b0 @derrabus Improved MySQL 4.1 support
derrabus authored
196
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
197 /**
198 * If comments were sent, enable relation stuff
199 */
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
200 include_once 'libraries/transformations.lib.php';
a1875b0 @derrabus Improved MySQL 4.1 support
derrabus authored
201
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
202 // updaet field names in relation
203 if (isset($_REQUEST['field_orig']) && is_array($_REQUEST['field_orig'])) {
204 foreach ($_REQUEST['field_orig'] as $fieldindex => $fieldcontent) {
205 if ($_REQUEST['field_name'][$fieldindex] != $fieldcontent) {
76ce885 @ruleant improve coding style
ruleant authored
206 PMA_REL_renameField(
207 $db, $table, $fieldcontent,
208 $_REQUEST['field_name'][$fieldindex]
209 );
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
210 }
211 }
212 }
213
214 // update mime types
215 if (isset($_REQUEST['field_mimetype'])
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
216 && is_array($_REQUEST['field_mimetype'])
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
217 && $cfg['BrowseMIME']) {
218 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
219 if (isset($_REQUEST['field_name'][$fieldindex])
76ce885 @ruleant improve coding style
ruleant authored
220 && strlen($_REQUEST['field_name'][$fieldindex])
221 ) {
222 PMA_setMIME(
223 $db, $table, $_REQUEST['field_name'][$fieldindex],
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
224 $mimetype,
225 $_REQUEST['field_transformation'][$fieldindex],
76ce885 @ruleant improve coding style
ruleant authored
226 $_REQUEST['field_transformation_options'][$fieldindex]
227 );
db21962 @nijel Do not try to set comment and MIME transformation if field not named.
nijel authored
228 }
3ae253e Do not waste the data for table-field creation, if MySQL shows an error.
Garvin Hicking authored
229 }
230 }
a1875b0 @derrabus Improved MySQL 4.1 support
derrabus authored
231
66fbb2d @lgtkaushalya Fixed the bugs in table copy of table operations
lgtkaushalya authored
232 if ( $_REQUEST['ajax_request'] == true) {
13adb5a @madhuracj Use lower case for true, false and null
madhuracj authored
233 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
66fbb2d @lgtkaushalya Fixed the bugs in table copy of table operations
lgtkaushalya authored
234 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
23c8cf4 This page can now handle ajax requests
ninadsp authored
235 }
236
ad06b4a @nijel Rename tbl_* files to drop useless _properties part.
nijel authored
237 $active_page = 'tbl_structure.php';
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
238 include 'tbl_structure.php';
3ae253e Do not waste the data for table-field creation, if MySQL shows an error.
Garvin Hicking authored
239 } else {
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
240 PMA_mysqlDie('', '', '', $err_url, false);
9785bbc @lem9 remove author names
lem9 authored
241 // An error happened while inserting/updating a table definition.
3ae253e Do not waste the data for table-field creation, if MySQL shows an error.
Garvin Hicking authored
242 // to prevent total loss of that data, we embed the form once again.
c40b663 @nijel Move tbl_properties{.inc,_links,_table_info}.php to libraries folder as ...
nijel authored
243 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
244 if (isset($_REQUEST['orig_field'])) {
245 $_REQUEST['field'] = $_REQUEST['orig_field'];
dba8ecc Display MIME types to transform any table cell. You can only choose from...
Garvin Hicking authored
246 }
247
3ae253e Do not waste the data for table-field creation, if MySQL shows an error.
Garvin Hicking authored
248 $regenerate = true;
249 }
7ff8fed Initial revision
Olivier Müller authored
250 }
0ab0ad6 @lem9 merge Loic's version
lem9 authored
251
252 /**
253 * No modifications yet required -> displays the table fields
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
254 *
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
255 * $selected comes from multi_submits.inc.php
0ab0ad6 @lem9 merge Loic's version
lem9 authored
256 */
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
257 if ($abort == false) {
4130b9b Fix PHP notices
Jo Michael authored
258 if (!isset($_REQUEST['ajax_request']) || $_REQUEST['ajax_request'] != true) {
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
259 include_once 'libraries/tbl_links.inc.php';
f607d8a @roccivic table structure: Do not send #topmenucontainer in ajax requests
roccivic authored
260 }
f2ebfe1 @nijel Show links only when editing
nijel authored
261
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
262 if (! isset($selected)) {
fc1c463 @lem9 path disclosure
lem9 authored
263 PMA_checkParameters(array('field'));
206f4ad refactored complete table/column creation altering;
Sebastian Mendel authored
264 $selected[] = $_REQUEST['field'];
bbef63f Merged part 2 of patch #458014: multi-column editing
Loïc Chapeaux authored
265 $selected_cnt = 1;
518aca0 Fixed bug #543365 - Can't change field properties when name contains loc...
Loïc Chapeaux authored
266 } else { // from a multiple submit
bbef63f Merged part 2 of patch #458014: multi-column editing
Loïc Chapeaux authored
267 $selected_cnt = count($selected);
0ab0ad6 @lem9 merge Loic's version
lem9 authored
268 }
bbef63f Merged part 2 of patch #458014: multi-column editing
Loïc Chapeaux authored
269
1eadc0a use @todo tag for todo items, to show up in generated documentation
Sebastian Mendel authored
270 /**
271 * @todo optimize in case of multiple fields to modify
272 */
bbef63f Merged part 2 of patch #458014: multi-column editing
Loïc Chapeaux authored
273 for ($i = 0; $i < $selected_cnt; $i++) {
be6e210 @Crack Use new PMA_DBI_get_columns in column alter form
Crack authored
274 $fields_meta[] = PMA_DBI_get_columns($db, $table, $selected[$i], true);
bbef63f Merged part 2 of patch #458014: multi-column editing
Loïc Chapeaux authored
275 }
276 $num_fields = count($fields_meta);
6884f97 @nijel no more support for php3
nijel authored
277 $action = 'tbl_alter.php';
b8b9334 @lem9 experimental support in table structure editing for MySQL 4.1.2+ TIMESTA...
lem9 authored
278
ad48d9f @lem9 bug #2895894 [structure] Empty default value not set properly
lem9 authored
279 // Get more complete field information.
280 // For now, this is done to obtain MySQL 4.1.2+ new TIMESTAMP options
281 // and to know when there is an empty DEFAULT value.
282 // Later, if the analyser returns more information, it
12f70e8 @Crack Replace all SHOW FIELDS calls with PMA_DBI_get_columns() or PMA_DBI_get_...
Crack authored
283 // could be executed to replace the info given by SHOW FULL COLUMNS FROM.
1eadc0a use @todo tag for todo items, to show up in generated documentation
Sebastian Mendel authored
284 /**
285 * @todo put this code into a require()
18c6832 @lem9 Comment adjusted to reflect a previous change of function name
lem9 authored
286 * or maybe make it part of PMA_DBI_get_columns();
1eadc0a use @todo tag for todo items, to show up in generated documentation
Sebastian Mendel authored
287 */
b8b9334 @lem9 experimental support in table structure editing for MySQL 4.1.2+ TIMESTA...
lem9 authored
288
ad48d9f @lem9 bug #2895894 [structure] Empty default value not set properly
lem9 authored
289 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
12f70e8 @Crack Replace all SHOW FIELDS calls with PMA_DBI_get_columns() or PMA_DBI_get_...
Crack authored
290 // SHOW FULL COLUMNS says NULL and SHOW CREATE TABLE says NOT NULL (tested
ad48d9f @lem9 bug #2895894 [structure] Empty default value not set properly
lem9 authored
291 // in MySQL 4.0.25).
b8b9334 @lem9 experimental support in table structure editing for MySQL 4.1.2+ TIMESTA...
lem9 authored
292
76ce885 @ruleant improve coding style
ruleant authored
293 $show_create_table = PMA_DBI_fetch_value(
294 'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table),
295 0, 1
296 );
ad48d9f @lem9 bug #2895894 [structure] Empty default value not set properly
lem9 authored
297 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
298 unset($show_create_table);
37315b0 @nijel Document some require/includes as phpdoc otherwise takes random block be...
nijel authored
299 /**
300 * Form for changing properties.
301 */
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
302 include 'libraries/tbl_properties.inc.php';
7ff8fed Initial revision
Olivier Müller authored
303 }
304
0ab0ad6 @lem9 merge Loic's version
lem9 authored
305
306 /**
307 * Displays the footer
308 */
b2fd866 Leading ./ paths for includes can be omitted, part 2
Jo Michael authored
309 require 'libraries/footer.inc.php';
37d50c1 @derrabus Huge set of optimizations, please test!
derrabus authored
310 ?>
Something went wrong with that request. Please try again.