More concise descriptions for MySQL column types
[phpmyadmin.git] / db_operations.php
blobec399fc3258912a013af04be7cc3d837ef0f1097
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * handles miscellaneous db operations:
5 * - move/rename
6 * - copy
7 * - changing collation
8 * - changing comment
9 * - adding tables
10 * - viewing PDF schemas
12 * @package PhpMyAdmin
15 /**
16 * requirements
18 require_once 'libraries/common.inc.php';
19 require_once 'libraries/mysql_charsets.lib.php';
21 // add a javascript file for jQuery functions to handle Ajax actions
22 $GLOBALS['js_include'][] = 'db_operations.js';
24 /**
25 * Sets globals from $_REQUEST (we're using GET on ajax, POST otherwise)
27 $request_params = array(
28 'add_constraints',
29 'comment',
30 'create_database_before_copying',
31 'db_collation',
32 'db_copy',
33 'db_rename',
34 'drop_if_exists',
35 'newname',
36 'sql_auto_increment',
37 'submitcollation',
38 'switch_to_new',
39 'what'
41 foreach ($request_params as $one_request_param) {
42 if (isset($_REQUEST[$one_request_param])) {
43 $GLOBALS[$one_request_param] = $_REQUEST[$one_request_param];
47 /**
48 * Rename/move or copy database
50 if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) {
52 if (! empty($db_rename)) {
53 $move = true;
54 } else {
55 $move = false;
58 if (! isset($newname) || ! strlen($newname)) {
59 $message = PMA_Message::error(__('The database name is empty!'));
60 } else {
61 $sql_query = ''; // in case target db exists
62 $_error = false;
63 if ($move
64 || (isset($create_database_before_copying)
65 && $create_database_before_copying)
66 ) {
67 // lower_case_table_names=1 `DB` becomes `db`
68 if (! PMA_DRIZZLE) {
69 $lower_case_table_names = PMA_DBI_fetch_value(
70 'SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1
72 if ($lower_case_table_names === '1') {
73 $newname = PMA_strtolower($newname);
77 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
78 if (isset($db_collation)) {
79 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
81 $local_query .= ';';
82 $sql_query = $local_query;
83 // save the original db name because Tracker.class.php which
84 // may be called under PMA_DBI_query() changes $GLOBALS['db']
85 // for some statements, one of which being CREATE DATABASE
86 $original_db = $db;
87 PMA_DBI_query($local_query);
88 $db = $original_db;
89 unset($original_db);
91 // rebuild the database list because PMA_Table::moveCopy
92 // checks in this list if the target db exists
93 $GLOBALS['pma']->databases->build();
96 // here I don't use DELIMITER because it's not part of the
97 // language; I have to send each statement one by one
99 // to avoid selecting alternatively the current and new db
100 // we would need to modify the CREATE definitions to qualify
101 // the db name
102 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
103 if ($procedure_names) {
104 foreach ($procedure_names as $procedure_name) {
105 PMA_DBI_select_db($db);
106 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
107 // collect for later display
108 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
109 PMA_DBI_select_db($newname);
110 PMA_DBI_query($tmp_query);
114 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
115 if ($function_names) {
116 foreach ($function_names as $function_name) {
117 PMA_DBI_select_db($db);
118 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
119 // collect for later display
120 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
121 PMA_DBI_select_db($newname);
122 PMA_DBI_query($tmp_query);
126 // go back to current db, just in case
127 PMA_DBI_select_db($db);
129 $GLOBALS['sql_constraints_query_full_db'] = array();
131 $tables_full = PMA_DBI_get_tables_full($db);
132 $views = array();
134 // remove all foreign key constraints, otherwise we can get errors
135 include_once 'libraries/export/sql.php';
136 foreach ($tables_full as $each_table => $tmp) {
137 $sql_constraints = '';
138 $sql_drop_foreign_keys = '';
139 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
140 if ($move && ! empty($sql_drop_foreign_keys)) {
141 PMA_DBI_query($sql_drop_foreign_keys);
143 // keep the constraint we just dropped
144 if (! empty($sql_constraints)) {
145 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
148 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
150 foreach ($tables_full as $each_table => $tmp) {
151 // to be able to rename a db containing views,
152 // first all the views are collected and a stand-in is created
153 // the real views are created after the tables
154 if (PMA_Table::isView($db, $each_table)) {
155 $views[] = $each_table;
156 // Create stand-in definition to resolve view dependencies
157 $sql_view_standin = PMA_getTableDefStandIn($db, $each_table, "\n");
158 PMA_DBI_select_db($newname);
159 PMA_DBI_query($sql_view_standin);
160 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
164 foreach ($tables_full as $each_table => $tmp) {
165 // skip the views; we have creted stand-in definitions
166 if (PMA_Table::isView($db, $each_table)) {
167 continue;
169 $back = $sql_query;
170 $sql_query = '';
172 // value of $what for this table only
173 $this_what = $what;
175 // do not copy the data from a Merge table
176 // note: on the calling FORM, 'data' means 'structure and data'
177 if (PMA_Table::isMerge($db, $each_table)) {
178 if ($this_what == 'data') {
179 $this_what = 'structure';
181 if ($this_what == 'dataonly') {
182 $this_what = 'nocopy';
186 if ($this_what != 'nocopy') {
187 // keep the triggers from the original db+table
188 // (third param is empty because delimiters are only intended
189 // for importing via the mysql client or our Import feature)
190 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
192 if (! PMA_Table::moveCopy(
193 $db, $each_table, $newname, $each_table,
194 isset($this_what) ? $this_what : 'data',
195 $move, 'db_copy'
196 )) {
197 $_error = true;
198 // $sql_query is filled by PMA_Table::moveCopy()
199 $sql_query = $back . $sql_query;
200 break;
202 // apply the triggers to the destination db+table
203 if ($triggers) {
204 PMA_DBI_select_db($newname);
205 foreach ($triggers as $trigger) {
206 PMA_DBI_query($trigger['create']);
207 $GLOBALS['sql_query'] .= "\n" . $trigger['create'] . ';';
209 unset($trigger);
211 unset($triggers);
213 // this does not apply to a rename operation
214 if (isset($GLOBALS['add_constraints'])
215 && ! empty($GLOBALS['sql_constraints_query'])
217 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
218 unset($GLOBALS['sql_constraints_query']);
221 // $sql_query is filled by PMA_Table::moveCopy()
222 $sql_query = $back . $sql_query;
223 } // end (foreach)
224 unset($each_table);
226 // handle the views
227 if (! $_error) {
228 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
229 // to remove stand-in VIEW that was created earlier
230 if (isset($GLOBALS['drop_if_exists'])) {
231 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
233 $GLOBALS['drop_if_exists'] = 'true';
235 foreach ($views as $view) {
236 if (! PMA_Table::moveCopy($db, $view, $newname, $view, 'structure', $move, 'db_copy')) {
237 $_error = true;
238 break;
241 unset($GLOBALS['drop_if_exists']);
242 if (isset($temp_drop_if_exists)) {
243 // restore previous value
244 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
245 unset($temp_drop_if_exists);
248 unset($view, $views);
250 // now that all tables exist, create all the accumulated constraints
251 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
252 PMA_DBI_select_db($newname);
253 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
254 PMA_DBI_query($one_query);
255 // and prepare to display them
256 $GLOBALS['sql_query'] .= "\n" . $one_query;
259 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
262 if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
263 // here DELIMITER is not used because it's not part of the
264 // language; each statement is sent one by one
266 // to avoid selecting alternatively the current and new db
267 // we would need to modify the CREATE definitions to qualify
268 // the db name
269 $event_names = PMA_DBI_fetch_result(
270 'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \''
271 . PMA_sqlAddSlashes($db, true) . '\';'
273 if ($event_names) {
274 foreach ($event_names as $event_name) {
275 PMA_DBI_select_db($db);
276 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
277 // collect for later display
278 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
279 PMA_DBI_select_db($newname);
280 PMA_DBI_query($tmp_query);
285 // go back to current db, just in case
286 PMA_DBI_select_db($db);
288 // Duplicate the bookmarks for this db (done once for each db)
289 if (! $_error && $db != $newname) {
290 $get_fields = array('user', 'label', 'query');
291 $where_fields = array('dbase' => $db);
292 $new_fields = array('dbase' => $newname);
293 PMA_Table::duplicateInfo(
294 'bookmarkwork', 'bookmark', $get_fields,
295 $where_fields, $new_fields
299 if (! $_error && $move) {
301 * cleanup pmadb stuff for this db
303 include_once 'libraries/relation_cleanup.lib.php';
304 PMA_relationsCleanupDatabase($db);
306 // if someday the RENAME DATABASE reappears, do not DROP
307 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
308 $sql_query .= "\n" . $local_query;
309 PMA_DBI_query($local_query);
311 $message = PMA_Message::success(__('Database %1$s has been renamed to %2$s'));
312 $message->addParam($db);
313 $message->addParam($newname);
314 } elseif (! $_error) {
315 $message = PMA_Message::success(__('Database %1$s has been copied to %2$s'));
316 $message->addParam($db);
317 $message->addParam($newname);
319 $reload = true;
321 /* Change database to be used */
322 if (! $_error && $move) {
323 $db = $newname;
324 } elseif (! $_error) {
325 if (isset($switch_to_new) && $switch_to_new == 'true') {
326 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
327 $db = $newname;
328 } else {
329 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
333 if ($_error && ! isset($message)) {
334 $message = PMA_Message::error();
339 * Database has been successfully renamed/moved. If in an Ajax request,
340 * generate the output with {@link PMA_ajaxResponse} and exit
342 if ( $GLOBALS['is_ajax_request'] == true) {
343 $extra_data['newname'] = $newname;
344 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
345 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
351 * Settings for relations stuff
354 $cfgRelation = PMA_getRelationsParam();
357 * Check if comments were updated
358 * (must be done before displaying the menu tabs)
360 if (isset($_REQUEST['comment'])) {
361 PMA_setDbComment($db, $comment);
365 * Prepares the tables list if the user where not redirected to this script
366 * because there is no table in the database ($is_info is true)
368 if (empty($is_info)) {
369 include 'libraries/db_common.inc.php';
370 $url_query .= '&amp;goto=db_operations.php';
372 // Gets the database structure
373 $sub_part = '_structure';
374 include 'libraries/db_info.inc.php';
375 echo "\n";
377 if (isset($message)) {
378 PMA_showMessage($message, $sql_query);
379 unset($message);
383 $db_collation = PMA_getDbCollation($db);
384 $is_information_schema = PMA_is_system_schema($db);
386 if (!$is_information_schema) {
387 if ($cfgRelation['commwork']) {
389 * database comment
392 <div class="operations_half_width">
393 <form method="post" action="db_operations.php">
394 <?php echo PMA_generate_common_hidden_inputs($db); ?>
395 <fieldset>
396 <legend>
397 <?php
398 if ($cfg['PropertiesIconic']) {
399 echo '<img class="icon ic_b_comment" src="themes/dot.gif" alt="" />';
401 echo __('Database comment: ');
403 </legend>
404 <input type="text" name="comment" class="textfield" size="30"
405 value="<?php
406 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
407 </fieldset>
408 <fieldset class="tblFooters">
409 <input type="submit" value="<?php echo __('Go'); ?>" />
410 </fieldset>
411 </form>
412 </div>
413 <?php
416 <div class="operations_half_width">
417 <?php include 'libraries/display_create_table.lib.php'; ?>
418 </div>
419 <?php
421 * rename database
423 if ($db != 'mysql') {
425 <div class="operations_half_width">
426 <form id="rename_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
427 onsubmit="return emptyFormElements(this, 'newname')">
428 <?php
429 if (isset($db_collation)) {
430 echo '<input type="hidden" name="db_collation" value="' . $db_collation
431 .'" />' . "\n";
434 <input type="hidden" name="what" value="data" />
435 <input type="hidden" name="db_rename" value="true" />
436 <?php echo PMA_generate_common_hidden_inputs($db); ?>
437 <fieldset>
438 <legend>
439 <?php
440 if ($cfg['PropertiesIconic']) {
441 echo PMA_getImage('b_edit.png');
443 echo __('Rename database to') . ':';
445 </legend>
446 <input id="new_db_name" type="text" name="newname" size="30" class="textfield" value="" />
447 </fieldset>
448 <fieldset class="tblFooters">
449 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
450 </fieldset>
451 </form>
452 </div>
453 <?php
454 } // end if
456 // Drop link if allowed
457 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
458 // Don't allow to easily drop mysql database, RFE #1327514.
459 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
460 && ! $db_is_information_schema
461 && (PMA_DRIZZLE || $db != 'mysql')
464 <div class="operations_half_width">
465 <fieldset class="caution">
466 <legend><?php
467 if ($cfg['PropertiesIconic']) {
468 echo PMA_getImage('b_deltbl.png');
470 echo __('Remove database');
471 ?></legend>
473 <ul>
474 <?php
475 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
476 $this_url_params = array(
477 'sql_query' => $this_sql_query,
478 'back' => 'db_operations.php',
479 'goto' => 'main.php',
480 'reload' => '1',
481 'purge' => '1',
482 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
483 'db' => null,
486 <li><a href="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? 'id="drop_db_anchor"' : ''); ?>>
487 <?php echo __('Drop the database (DROP)'); ?></a>
488 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
489 </li>
490 </ul>
491 </fieldset>
492 </div>
493 <?php } ?>
494 <?php
496 * Copy database
499 <div class="operations_half_width clearfloat">
500 <form id="copy_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
501 onsubmit="return emptyFormElements(this, 'newname')">
502 <?php
503 if (isset($db_collation)) {
504 echo '<input type="hidden" name="db_collation" value="' . $db_collation
505 .'" />' . "\n";
507 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
508 echo PMA_generate_common_hidden_inputs($db);
510 <fieldset>
511 <legend>
512 <?php
513 if ($cfg['PropertiesIconic']) {
514 echo PMA_getImage('b_edit.png');
516 echo __('Copy database to') . ':';
517 $drop_clause = 'DROP TABLE / DROP VIEW';
519 </legend>
520 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
521 <?php
522 $choices = array(
523 'structure' => __('Structure only'),
524 'data' => __('Structure and data'),
525 'dataonly' => __('Data only'));
526 PMA_display_html_radio('what', $choices, 'data', true);
527 unset($choices);
529 <input type="checkbox" name="create_database_before_copying" value="1"
530 id="checkbox_create_database_before_copying"
531 checked="checked" />
532 <label for="checkbox_create_database_before_copying">
533 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
534 <input type="checkbox" name="drop_if_exists" value="true"
535 id="checkbox_drop" />
536 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
537 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
538 id="checkbox_auto_increment" />
539 <label for="checkbox_auto_increment">
540 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
541 <input type="checkbox" name="add_constraints" value="1"
542 id="checkbox_constraints" />
543 <label for="checkbox_constraints">
544 <?php echo __('Add constraints'); ?></label><br />
545 <?php
546 unset($drop_clause);
548 if (isset($_COOKIE)
549 && isset($_COOKIE['pma_switch_to_new'])
550 && $_COOKIE['pma_switch_to_new'] == 'true'
552 $pma_switch_to_new = 'true';
555 <input type="checkbox" name="switch_to_new" value="true"
556 id="checkbox_switch"
557 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
559 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
560 </fieldset>
561 <fieldset class="tblFooters">
562 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
563 </fieldset>
564 </form>
565 </div>
566 <?php
569 * Change database charset
571 echo '<div class="operations_half_width"><form id="change_db_charset_form" ';
572 if ($GLOBALS['cfg']['AjaxEnable']) {
573 echo ' class="ajax" ';
575 echo 'method="post" action="db_operations.php">'
576 . PMA_generate_common_hidden_inputs($db, $table)
577 . '<fieldset>' . "\n"
578 . ' <legend>';
579 if ($cfg['PropertiesIconic']) {
580 echo PMA_getImage('s_asci.png');
582 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
583 . ' </legend>' . "\n"
584 . PMA_generateCharsetDropdownBox(
585 PMA_CSDROPDOWN_COLLATION, 'db_collation',
586 'select_db_collation', $db_collation, false, 3
588 . '</fieldset>'
589 . '<fieldset class="tblFooters">'
590 . ' <input type="submit" name="submitcollation"'
591 . ' value="' . __('Go') . '" />' . "\n"
592 . '</fieldset>' . "\n"
593 . '</form></div>' . "\n";
595 if ($num_tables > 0
596 && ! $cfgRelation['allworks']
597 && $cfg['PmaNoRelation_DisableWarning'] == false
599 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
600 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
601 $message->addParam('</a>', false);
602 /* Show error if user has configured something, notice elsewhere */
603 if (!empty($cfg['Servers'][$server]['pmadb'])) {
604 $message->isError(true);
606 echo '<div class="operations_full_width">';
607 $message->display();
608 echo '</div>';
609 } // end if
610 } // end if (!$is_information_schema)
613 // not sure about displaying the PDF dialog in case db is information_schema
614 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
615 <!-- Work on PDF Pages -->
617 <?php
618 // We only show this if we find something in the new pdf_pages table
620 $test_query = '
621 SELECT *
622 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
623 WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
624 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
627 * Export Relational Schema View
629 echo '<div class="operations_full_width"><fieldset><a href="schema_edit.php?' . $url_query . '">';
630 if ($cfg['PropertiesIconic']) {
631 echo PMA_getImage('b_edit.png');
633 echo __('Edit or export relational schema') . '</a></fieldset></div>';
634 } // end if
637 * Displays the footer
639 require 'libraries/footer.inc.php';