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 require './tbl_printview.php';
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
41 $query_type = $submit_mult;
43 $mult_btn = __('Yes');
47 require('db_export.php');
52 } elseif (isset($selected_fld) && !empty($selected_fld)) {
53 // coming from table structure view - do something with selected columns/fileds
54 $selected = $selected_fld;
55 switch ($submit_mult) {
60 // Gets table primary key
61 PMA_DBI_select_db($db);
62 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
64 while ($row = PMA_DBI_fetch_assoc($result)) {
65 // Backups the list of primary keys
66 if ($row['Key_name'] == 'PRIMARY') {
67 $primary .= $row['Column_name'] . ', ';
70 PMA_DBI_free_result($result);
71 if (empty($primary)) {
72 // no primary key, so we can safely create new
74 $query_type = 'primary_fld';
75 $mult_btn = __('Yes');
77 // primary key exists, so lets as user
78 $what = 'primary_fld';
83 $query_type = 'index_fld';
84 $mult_btn = __('Yes');
88 $query_type = 'unique_fld';
89 $mult_btn = __('Yes');
93 $query_type = 'fulltext_fld';
94 $mult_btn = __('Yes');
97 require './tbl_alter.php';
100 // this should already be handled by tbl_structure.php
103 // coming from browsing - do something with selected rows
104 $what = 'row_delete';
105 $selected = $rows_to_delete;
111 * Displays the confirmation form if required
113 if (!empty($submit_mult) && !empty($what)) {
116 require_once './libraries/header.inc.php';
117 if (strlen($table)) {
118 require './libraries/tbl_common.php';
119 $url_query .= '&goto=tbl_sql.php&back=tbl_sql.php';
120 require './libraries/tbl_info.inc.php';
121 require_once './libraries/tbl_links.inc.php';
122 } elseif (strlen($db)) {
123 require './libraries/db_common.inc.php';
124 require './libraries/db_info.inc.php';
126 require_once './libraries/server_common.inc.php';
127 require_once './libraries/server_links.inc.php';
132 if ($what == 'drop_tbl') {
133 $full_query_views = '';
135 $selected_cnt = count($selected);
137 foreach ($selected AS $idx => $sval) {
140 $full_query .= htmlspecialchars($sval)
144 $full_query .= 'DROP DATABASE '
145 . PMA_backquote(htmlspecialchars($sval))
152 if (!empty($views) && in_array($current, $views)) {
153 $full_query_views .= (empty($full_query_views) ?
'DROP VIEW ' : ', ')
154 . PMA_backquote(htmlspecialchars($current));
156 $full_query .= (empty($full_query) ?
'DROP TABLE ' : ', ')
157 . PMA_backquote(htmlspecialchars($current));
162 $full_query .= 'TRUNCATE ';
163 $full_query .= PMA_backquote(htmlspecialchars($sval))
168 if ($full_query == '') {
169 $full_query .= 'ALTER TABLE '
170 . PMA_backquote(htmlspecialchars($table))
171 . '<br /> DROP PRIMARY KEY,'
172 . '<br /> ADD PRIMARY KEY('
173 . '<br /> '
174 . PMA_backquote(htmlspecialchars($sval))
177 $full_query .= '<br /> '
178 . PMA_backquote(htmlspecialchars($sval))
181 if ($i == $selected_cnt-1) {
182 $full_query = preg_replace('@,$@', ');<br />', $full_query);
187 if ($full_query == '') {
188 $full_query .= 'ALTER TABLE '
189 . PMA_backquote(htmlspecialchars($table));
191 $full_query .= '<br /> DROP '
192 . PMA_backquote(htmlspecialchars($sval))
194 if ($i == $selected_cnt - 1) {
195 $full_query = preg_replace('@,$@', ';<br />', $full_query);
201 if ($what == 'drop_tbl') {
202 if (!empty($full_query)) {
203 $full_query .= ';<br />' . "\n";
205 if (!empty($full_query_views)) {
206 $full_query .= $full_query_views . ';<br />' . "\n";
208 unset($full_query_views);
211 // Displays the confirmation form
212 $_url_params = array(
213 'query_type' => $what,
214 'reload' => (! empty($reload) ?
1 : 0),
216 if (strpos(' ' . $action, 'db_') == 1) {
217 $_url_params['db']= $db;
218 } elseif (strpos(' ' . $action, 'tbl_') == 1 ||
$what == 'row_delete') {
219 $_url_params['db']= $db;
220 $_url_params['table']= $table;
222 foreach ($selected as $idx => $sval) {
223 $_url_params['selected'][] = $sval;
225 if ($what == 'drop_tbl' && !empty($views)) {
226 foreach ($views as $current) {
227 $_url_params['views'][] = $current;
230 if ($what == 'row_delete') {
231 $_url_params['original_sql_query'] = $original_sql_query;
232 if (! empty($original_url_query)) {
233 $_url_params['original_url_query'] = $original_url_query;
237 <form action
="<?php echo $action; ?>" method
="post">
239 echo PMA_generate_common_hidden_inputs($_url_params);
241 <fieldset
class="confirmation">
242 <legend
><?php
echo ($what == 'drop_db' ?
__('You are about to DESTROY a complete database!') . ' ' : '') . __('Do you really want to '); ?
>:</legend
>
243 <tt
><?php
echo $full_query; ?
></tt
>
245 <fieldset
class="tblFooters">
246 <input type
="submit" name
="mult_btn" value
="<?php echo __('Yes'); ?>" id
="buttonYes" />
247 <input type
="submit" name
="mult_btn" value
="<?php echo __('No'); ?>" id
="buttonNo" />
250 require './libraries/footer.inc.php';
255 * Executes the query - dropping rows, columns/fields, tables or dbs
257 elseif ($mult_btn == __('Yes')) {
259 if ($query_type == 'drop_db' ||
$query_type == 'drop_tbl' ||
$query_type == 'drop_fld') {
260 require_once './libraries/relation_cleanup.lib.php';
264 if ($query_type == 'drop_tbl') {
265 $sql_query_views = '';
267 $selected_cnt = count($selected);
268 $run_parts = FALSE; // whether to run query after each pass
269 $use_sql = FALSE; // whether to include sql.php at the end (to display results)
271 if ($query_type == 'primary_fld') {
272 // Gets table primary key
273 PMA_DBI_select_db($db);
274 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
276 while ($row = PMA_DBI_fetch_assoc($result)) {
277 // Backups the list of primary keys
278 if ($row['Key_name'] == 'PRIMARY') {
279 $primary .= $row['Column_name'] . ', ';
282 PMA_DBI_free_result($result);
285 $rebuild_database_list = false;
287 for ($i = 0; $i < $selected_cnt; $i++
) {
288 switch ($query_type) {
290 $a_query = $selected[$i];
295 PMA_relationsCleanupDatabase($selected[$i]);
296 $a_query = 'DROP DATABASE '
297 . PMA_backquote($selected[$i]);
300 $rebuild_database_list = true;
304 PMA_relationsCleanupTable($db, $selected[$i]);
305 $current = $selected[$i];
306 if (!empty($views) && in_array($current, $views)) {
307 $sql_query_views .= (empty($sql_query_views) ?
'DROP VIEW ' : ', ')
308 . PMA_backquote($current);
310 $sql_query .= (empty($sql_query) ?
'DROP TABLE ' : ', ')
311 . PMA_backquote($current);
317 $sql_query .= (empty($sql_query) ?
'CHECK TABLE ' : ', ')
318 . PMA_backquote($selected[$i]);
323 $sql_query .= (empty($sql_query) ?
'OPTIMIZE TABLE ' : ', ')
324 . PMA_backquote($selected[$i]);
329 $sql_query .= (empty($sql_query) ?
'ANALYZE TABLE ' : ', ')
330 . PMA_backquote($selected[$i]);
335 $sql_query .= (empty($sql_query) ?
'REPAIR TABLE ' : ', ')
336 . PMA_backquote($selected[$i]);
341 $a_query = 'TRUNCATE ';
342 $a_query .= PMA_backquote(htmlspecialchars($selected[$i]));
347 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
348 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) : ',')
349 . ' DROP ' . PMA_backquote($selected[$i])
350 . (($i == $selected_cnt-1) ?
';' : '');
354 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ?
'' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
355 . PMA_backquote($selected[$i])
356 . (($i == $selected_cnt-1) ?
');' : '');
360 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
361 . PMA_backquote($selected[$i])
362 . (($i == $selected_cnt-1) ?
');' : '');
366 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
367 . PMA_backquote($selected[$i])
368 . (($i == $selected_cnt-1) ?
');' : '');
372 $sql_query .= (empty($sql_query) ?
'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
373 . PMA_backquote($selected[$i])
374 . (($i == $selected_cnt-1) ?
');' : '');
378 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
379 // statements will be run at once below
381 $sql_query .= $a_query . ';' . "\n";
382 if ($query_type != 'drop_db') {
383 PMA_DBI_select_db($db);
385 $result = PMA_DBI_query($a_query);
389 if ($query_type == 'drop_tbl') {
390 if (!empty($sql_query)) {
392 } elseif (!empty($sql_query_views)) {
393 $sql_query = $sql_query_views . ';';
394 unset($sql_query_views);
400 } elseif (!$run_parts) {
401 PMA_DBI_select_db($db);
402 $result = PMA_DBI_try_query($sql_query);
403 if ($result && !empty($sql_query_views)) {
404 $sql_query .= ' ' . $sql_query_views . ';';
405 $result = PMA_DBI_try_query($sql_query_views);
406 unset($sql_query_views);
410 $message = PMA_Message
::error(PMA_DBI_getError());
413 if ($rebuild_database_list) {
414 // avoid a problem with the database list navigator
415 // when dropping a db from server_databases
416 $GLOBALS['pma']->databases
->build();
419 $message = PMA_Message
::success(__('No change'));