2 /* vim: set expandtab sw=4 ts=4 sts=4: */
7 if (! defined('PHPMYADMIN')) {
12 * Prepares the work and runs some other scripts if required
14 if (! empty($submit_mult)
15 && $submit_mult != __('With selected:')
16 && (! empty($selected_db)
17 ||
! empty($selected_tbl)
18 ||
! empty($selected_fld)
19 ||
! empty($rows_to_delete))) {
20 define('PMA_SUBMIT_MULT', 1);
21 if (isset($selected_db) && !empty($selected_db)) {
22 // coming from server database view - do something with selected databases
23 $selected = $selected_db;
25 } elseif (isset($selected_tbl) && !empty($selected_tbl)) {
26 // coming from database structure view - do something with selected tables
27 if ($submit_mult == 'print') {
28 include './tbl_printview.php';
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
32 case 'add_prefix_tbl':
33 case 'replace_prefix_tbl':
34 case 'copy_tbl_change_prefix':
44 $query_type = $submit_mult;
46 $mult_btn = __('Yes');
50 include 'db_export.php';
55 } elseif (isset($selected_fld) && !empty($selected_fld)) {
56 // coming from table structure view - do something with selected columns/fileds
57 $selected = $selected_fld;
58 switch ($submit_mult) {
63 // Gets table primary key
64 PMA_DBI_select_db($db);
65 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
67 while ($row = PMA_DBI_fetch_assoc($result)) {
68 // Backups the list of primary keys
69 if ($row['Key_name'] == 'PRIMARY') {
70 $primary .= $row['Column_name'] . ', ';
73 PMA_DBI_free_result($result);
74 if (empty($primary)) {
75 // no primary key, so we can safely create new
77 $query_type = 'primary_fld';
78 $mult_btn = __('Yes');
80 // primary key exists, so lets as user
81 $what = 'primary_fld';
86 $query_type = 'index_fld';
87 $mult_btn = __('Yes');
91 $query_type = 'unique_fld';
92 $mult_btn = __('Yes');
96 $query_type = 'spatial_fld';
97 $mult_btn = __('Yes');
101 $query_type = 'fulltext_fld';
102 $mult_btn = __('Yes');
105 include './tbl_alter.php';
108 // this should already be handled by tbl_structure.php
111 // coming from browsing - do something with selected rows
112 $what = 'row_delete';
113 $selected = $rows_to_delete;
119 * Displays the confirmation form if required
121 if (!empty($submit_mult) && !empty($what)) {
124 include_once './libraries/header.inc.php';
125 if (strlen($table)) {
126 include './libraries/tbl_common.php';
127 $url_query .= '&goto=tbl_sql.php&back=tbl_sql.php';
128 include './libraries/tbl_info.inc.php';
129 include_once './libraries/tbl_links.inc.php';
130 } elseif (strlen($db)) {
131 include './libraries/db_common.inc.php';
132 include './libraries/db_info.inc.php';
134 include_once './libraries/server_common.inc.php';
135 include_once './libraries/server_links.inc.php';
140 if ($what == 'drop_tbl') {
141 $full_query_views = '';
143 $selected_cnt = count($selected);
145 foreach ($selected AS $idx => $sval) {
148 $full_query .= htmlspecialchars($sval)
152 $full_query .= 'DROP DATABASE '
153 . PMA_backquote(htmlspecialchars($sval))
160 if (!empty($views) && in_array($current, $views)) {
161 $full_query_views .= (empty($full_query_views) ?
'DROP VIEW ' : ', ')
162 . PMA_backquote(htmlspecialchars($current));
164 $full_query .= (empty($full_query) ?
'DROP TABLE ' : ', ')
165 . PMA_backquote(htmlspecialchars($current));
170 $full_query .= 'TRUNCATE ';
171 $full_query .= PMA_backquote(htmlspecialchars($sval))
176 if ($full_query == '') {
177 $full_query .= 'ALTER TABLE '
178 . PMA_backquote(htmlspecialchars($table))
179 . '<br /> DROP PRIMARY KEY,'
180 . '<br /> ADD PRIMARY KEY('
181 . '<br /> '
182 . PMA_backquote(htmlspecialchars($sval))
185 $full_query .= '<br /> '
186 . PMA_backquote(htmlspecialchars($sval))
189 if ($i == $selected_cnt-1) {
190 $full_query = preg_replace('@,$@', ');<br />', $full_query);
195 if ($full_query == '') {
196 $full_query .= 'ALTER TABLE '
197 . PMA_backquote(htmlspecialchars($table));
199 $full_query .= '<br /> DROP '
200 . PMA_backquote(htmlspecialchars($sval))
202 if ($i == $selected_cnt - 1) {
203 $full_query = preg_replace('@,$@', ';<br />', $full_query);
209 if ($what == 'drop_tbl') {
210 if (!empty($full_query)) {
211 $full_query .= ';<br />' . "\n";
213 if (!empty($full_query_views)) {
214 $full_query .= $full_query_views . ';<br />' . "\n";
216 unset($full_query_views);
219 // Displays the confirmation form
220 $_url_params = array(
221 'query_type' => $what,
222 'reload' => (! empty($reload) ?
1 : 0),
224 if (strpos(' ' . $action, 'db_') == 1) {
225 $_url_params['db']= $db;
226 } elseif (strpos(' ' . $action, 'tbl_') == 1 ||
$what == 'row_delete') {
227 $_url_params['db']= $db;
228 $_url_params['table']= $table;
230 foreach ($selected as $idx => $sval) {
231 $_url_params['selected'][] = $sval;
233 if ($what == 'drop_tbl' && !empty($views)) {
234 foreach ($views as $current) {
235 $_url_params['views'][] = $current;
238 if ($what == 'row_delete') {
239 $_url_params['original_sql_query'] = $original_sql_query;
240 if (! empty($original_url_query)) {
241 $_url_params['original_url_query'] = $original_url_query;
245 <form action
="<?php echo $action; ?>" method
="post">
247 echo PMA_generate_common_hidden_inputs($_url_params);
249 <?php
if ($what == 'replace_prefix_tbl' ||
$what == 'copy_tbl_change_prefix') { ?
>
250 <fieldset
class = "input">
251 <legend
><?php
echo ($what == 'replace_prefix_tbl' ?
__('Replace table prefix') : __('Copy table with prefix')) ?
>:</legend
>
254 <td
><?php
echo __('From'); ?
></td
><td
><input type
="text" name
="from_prefix" id
="initialPrefix"</td
>
257 <td
><?php
echo __('To'); ?
> </td
><td
><input type
="text" name
="to_prefix" id
="newPrefix"</td
>
261 <fieldset
class="tblFooters">
262 <button type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes"><?php
echo __('Submit'); ?
></button
>
265 } elseif ($what == 'add_prefix_tbl') { ?
>
266 <fieldset
class = "input">
267 <legend
><?php
echo __('Add table prefix') ?
>:</legend
>
270 <td
><?php
echo __('Add prefix'); ?
></td
> <td
><input type
="text" name
="add_prefix" id
="txtPrefix"</td
>
274 <fieldset
class="tblFooters">
275 <button type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes"><?php
echo __('Submit'); ?
></button
>
279 <fieldset
class="confirmation">
280 <legend
><?php
echo ($what == 'drop_db' ?
__('You are about to DESTROY a complete database!') . ' ' : '') . __('Do you really want to '); ?
>:</legend
>
281 <tt
><?php
echo $full_query; ?
></tt
>
283 <fieldset
class="tblFooters">
284 <input type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes" />
285 <input type
="submit" name
="mult_btn" value
="<?php echo __('No'); ?>" id
="buttonNo" />
289 include './libraries/footer.inc.php';
291 } elseif ($mult_btn == __('Yes')) {
293 * Executes the query - dropping rows, columns/fields, tables or dbs
295 if ($query_type == 'drop_db' ||
$query_type == 'drop_tbl' ||
$query_type == 'drop_fld') {
296 include_once './libraries/relation_cleanup.lib.php';
300 if ($query_type == 'drop_tbl') {
301 $sql_query_views = '';
303 $selected_cnt = count($selected);
304 $run_parts = false; // whether to run query after each pass
305 $use_sql = false; // whether to include sql.php at the end (to display results)
307 if ($query_type == 'primary_fld') {
308 // Gets table primary key
309 PMA_DBI_select_db($db);
310 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
312 while ($row = PMA_DBI_fetch_assoc($result)) {
313 // Backups the list of primary keys
314 if ($row['Key_name'] == 'PRIMARY') {
315 $primary .= $row['Column_name'] . ', ';
318 PMA_DBI_free_result($result);
321 $rebuild_database_list = false;
323 for ($i = 0; $i < $selected_cnt; $i++
) {
324 switch ($query_type) {
326 $a_query = $selected[$i];
331 PMA_relationsCleanupDatabase($selected[$i]);
332 $a_query = 'DROP DATABASE '
333 . PMA_backquote($selected[$i]);
336 $rebuild_database_list = true;
340 PMA_relationsCleanupTable($db, $selected[$i]);
341 $current = $selected[$i];
342 if (!empty($views) && in_array($current, $views)) {
343 $sql_query_views .= (empty($sql_query_views) ?
'DROP VIEW ' : ', ')
344 . PMA_backquote($current);
346 $sql_query .= (empty($sql_query) ?
'DROP TABLE ' : ', ')
347 . PMA_backquote($current);
353 $sql_query .= (empty($sql_query) ?
'CHECK TABLE ' : ', ')
354 . PMA_backquote($selected[$i]);
359 $sql_query .= (empty($sql_query) ?
'OPTIMIZE TABLE ' : ', ')
360 . PMA_backquote($selected[$i]);
365 $sql_query .= (empty($sql_query) ?
'ANALYZE TABLE ' : ', ')
366 . PMA_backquote($selected[$i]);
371 $sql_query .= (empty($sql_query) ?
'REPAIR TABLE ' : ', ')
372 . PMA_backquote($selected[$i]);
377 $a_query = 'TRUNCATE ';
378 $a_query .= PMA_backquote($selected[$i]);
383 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
384 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) : ',')
385 . ' DROP ' . PMA_backquote($selected[$i])
386 . (($i == $selected_cnt-1) ?
';' : '');
390 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ?
'' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
391 . PMA_backquote($selected[$i])
392 . (($i == $selected_cnt-1) ?
');' : '');
396 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
397 . PMA_backquote($selected[$i])
398 . (($i == $selected_cnt-1) ?
');' : '');
402 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
403 . PMA_backquote($selected[$i])
404 . (($i == $selected_cnt-1) ?
');' : '');
408 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD SPATIAL( ' : ', ')
409 . PMA_backquote($selected[$i])
410 . (($i == $selected_cnt-1) ?
');' : '');
414 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
415 . PMA_backquote($selected[$i])
416 . (($i == $selected_cnt-1) ?
');' : '');
419 case 'add_prefix_tbl':
420 $newtablename = $add_prefix . $selected[$i];
421 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // ADD PREFIX TO TABLE NAME
425 case 'replace_prefix_tbl':
426 $current = $selected[$i];
427 $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
428 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // CHANGE PREFIX PATTERN
432 case 'copy_tbl_change_prefix':
433 $current = $selected[$i];
434 $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
435 $a_query = 'CREATE TABLE ' . PMA_backquote($newtablename) . ' SELECT * FROM ' . PMA_backquote($selected[$i]) ; // COPY TABLE AND CHANGE PREFIX PATTERN
441 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
442 // statements will be run at once below
444 $sql_query .= $a_query . ';' . "\n";
445 if ($query_type != 'drop_db') {
446 PMA_DBI_select_db($db);
448 $result = PMA_DBI_query($a_query);
452 if ($query_type == 'drop_tbl') {
453 if (!empty($sql_query)) {
455 } elseif (!empty($sql_query_views)) {
456 $sql_query = $sql_query_views . ';';
457 unset($sql_query_views);
463 } elseif (!$run_parts) {
464 PMA_DBI_select_db($db);
465 $result = PMA_DBI_try_query($sql_query);
466 if ($result && !empty($sql_query_views)) {
467 $sql_query .= ' ' . $sql_query_views . ';';
468 $result = PMA_DBI_try_query($sql_query_views);
469 unset($sql_query_views);
473 $message = PMA_Message
::error(PMA_DBI_getError());
476 if ($rebuild_database_list) {
477 // avoid a problem with the database list navigator
478 // when dropping a db from server_databases
479 $GLOBALS['pma']->databases
->build();
482 $message = PMA_Message
::success(__('No change'));