2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
12 require_once 'libraries/common.inc.php';
15 * functions implementation for this script
17 require_once 'libraries/check_user_privileges.lib.php';
18 require_once 'libraries/operations.lib.php';
20 $pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
23 * Load JavaScript files
25 $response = PMA_Response
::getInstance();
26 $header = $response->getHeader();
27 $scripts = $header->getScripts();
28 $scripts->addFile('functions.js');
29 $scripts->addFile('tbl_operations.js');
34 require 'libraries/tbl_common.inc.php';
35 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
36 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
39 * Gets relation settings
41 $cfgRelation = PMA_getRelationsParam();
44 * Gets available MySQL charsets and storage engines
46 require_once 'libraries/mysql_charsets.inc.php';
47 require_once 'libraries/StorageEngine.class.php';
50 * Class for partition management
52 require_once 'libraries/Partition.class.php';
54 // reselect current db (needed in some cases probably due to
55 // the calling of relation.lib.php)
56 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
59 * Gets tables information
61 require 'libraries/tbl_info.inc.php';
63 // define some variables here, for improved syntax in the conditionals
64 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
65 $is_aria = $is_pbxt = false;
66 // set initial value of these variables, based on the current table engine
67 list($is_myisam_or_aria, $is_innodb, $is_isam,
68 $is_berkeleydb, $is_aria, $is_pbxt
69 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
72 // the value for transactional can be implicit
73 // (no create option found, in this case it means 1)
74 // or explicit (option found with a value of 0 or 1)
75 // ($transactional may have been set by libraries/tbl_info.inc.php,
76 // from the $create_options)
77 $transactional = (isset($transactional) && $transactional == '0')
80 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
84 $table_alters = array();
86 /** @var PMA_String $pmaString */
87 $pmaString = $GLOBALS['PMA_String'];
90 * If the table has to be moved to some other database
92 if (isset($_REQUEST['submit_move']) ||
isset($_REQUEST['submit_copy'])) {
94 PMA_moveOrCopyTable($db, $table);
95 // This was ended in an Ajax call
99 * If the table has to be maintained
101 if (isset($_REQUEST['table_maintenance'])) {
102 include_once 'sql.php';
106 * Updates table comment, type and options if required
108 if (isset($_REQUEST['submitoptions'])) {
110 $warning_messages = array();
112 if (isset($_REQUEST['new_name'])) {
113 // Get original names before rename operation
114 $oldTable = $pma_table->getName();
115 $oldDb = $pma_table->getDbName();
117 if ($pma_table->rename($_REQUEST['new_name'])) {
118 if (isset($_REQUEST['adjust_privileges'])
119 && ! empty($_REQUEST['adjust_privileges'])
121 PMA_AdjustPrivileges_renameOrMoveTable(
122 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
126 // Reselect the original DB
127 $GLOBALS['db'] = $oldDb;
128 $GLOBALS['dbi']->selectDb($oldDb);
130 $_message .= $pma_table->getLastMessage();
132 $GLOBALS['table'] = $pma_table->getName();
136 $_message .= $pma_table->getLastError();
141 if (! empty($_REQUEST['new_tbl_storage_engine'])
142 && /*overload*/mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== /*overload*/mb_strtolower($tbl_storage_engine)
144 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
145 // reset the globals for the new engine
146 list($is_myisam_or_aria, $is_innodb, $is_isam,
147 $is_berkeleydb, $is_aria, $is_pbxt
148 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
151 $transactional = (isset($transactional) && $transactional == '0')
154 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
157 $new_tbl_storage_engine = '';
160 $table_alters = PMA_getTableAltersArray(
161 $is_myisam_or_aria, $is_isam, $pack_keys,
162 (empty($checksum) ?
'0' : '1'),
164 ((isset($page_checksum)) ?
$page_checksum : ''),
165 (empty($delay_key_write) ?
'0' : '1'),
166 $is_innodb, $is_pbxt, $row_format,
167 $new_tbl_storage_engine,
168 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
172 if (count($table_alters) > 0) {
173 $sql_query = 'ALTER TABLE '
174 . PMA_Util
::backquote($GLOBALS['table']);
175 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
177 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
179 unset($table_alters);
180 $warning_messages = PMA_getWarningMessagesArray();
183 if (isset($_REQUEST['tbl_collation'])
184 && ! empty($_REQUEST['tbl_collation'])
185 && isset($_REQUEST['change_all_collations'])
186 && ! empty($_REQUEST['change_all_collations'])
188 PMA_changeAllColumnsCollation(
189 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
194 * Reordering the table has been requested by the user
196 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
197 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
201 * A partition operation has been requested by the user
205 if (isset($_REQUEST['submit_partition'])
206 && ! empty($_REQUEST['partition_operation'])
208 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
212 // to avoid showing the old value (for example the AUTO_INCREMENT) after
213 // a change, clear the cache
214 $GLOBALS['dbi']->clearTableCache();
215 $page_checksum = $checksum = $delay_key_write = 0;
216 include 'libraries/tbl_info.inc.php';
220 if (isset($result) && empty($message_to_show)) {
221 // set to success by default, because result set could be empty
222 // (for example, a table rename)
224 if (empty($_message)) {
226 ? PMA_Message
::success(
227 __('Your SQL query has been executed successfully.')
229 : PMA_Message
::error(__('Error'));
230 // $result should exist, regardless of $_message
231 $_type = $result ?
'success' : 'error';
233 if (isset($GLOBALS['ajax_request'])
234 && $GLOBALS['ajax_request'] == true
236 $response = PMA_Response
::getInstance();
237 $response->isSuccess($_message->isSuccess());
238 $response->addJSON('message', $_message);
240 'sql_query', PMA_Util
::getMessage(null, $sql_query)
245 if (! empty($warning_messages)) {
246 $_message = new PMA_Message
;
247 $_message->addMessages($warning_messages);
248 $_message->isError(true);
249 if ($GLOBALS['ajax_request'] == true) {
250 $response = PMA_Response
::getInstance();
251 $response->isSuccess(false);
252 $response->addJSON('message', $_message);
255 unset($warning_messages);
259 PMA_Util
::getMessage($_message, $sql_query, $_type)
261 unset($_message, $_type);
265 = $url_params['back']
266 = 'tbl_operations.php';
271 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
276 $response->addHTML('<div id="boxContainer" data-box-width="300">');
281 $hideOrderTable = false;
282 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
283 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
284 // InnoDB always orders table rows according to such an index if one is present.
285 if ($tbl_storage_engine == 'INNODB') {
286 include_once 'libraries/Index.class.php';
287 $indexes = PMA_Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
288 foreach ($indexes as $name => $idx) {
289 if ($name == 'PRIMARY') {
290 $hideOrderTable = true;
292 } elseif (! $idx->getNonUnique()) {
294 foreach ($idx->getColumns() as $column) {
295 if ($column->getNull()) {
301 $hideOrderTable = true;
307 if (! $hideOrderTable) {
308 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
314 $response->addHTML(PMA_getHtmlForMoveTable());
316 if (/*overload*/mb_strstr($show_comment, '; InnoDB free') === false) {
317 if (/*overload*/mb_strstr($show_comment, 'InnoDB free') === false) {
318 // only user entered comment
319 $comment = $show_comment;
321 // here we have just InnoDB generated part
325 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
326 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
329 // PACK_KEYS: MyISAM or ISAM
330 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
331 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
333 // Here should be version check for InnoDB, however it is supported
334 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
338 PMA_getTableOptionDiv(
339 $comment, $tbl_collation, $tbl_storage_engine,
340 $is_myisam_or_aria, $is_isam, $pack_keys,
342 (empty($delay_key_write) ?
'0' : '1'),
343 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
344 ((isset($page_checksum)) ?
$page_checksum : ''),
345 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ?
'0' : '1')
352 $response->addHTML(PMA_getHtmlForCopytable());
358 PMA_getHtmlForTableMaintenance(
366 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
367 $truncate_table_url_params = array();
368 $drop_table_url_params = array();
371 && ! (isset($db_is_system_schema) && $db_is_system_schema)
373 $this_sql_query = 'TRUNCATE TABLE '
374 . PMA_Util
::backquote($GLOBALS['table']);
375 $truncate_table_url_params = array_merge(
378 'sql_query' => $this_sql_query,
379 'goto' => 'tbl_structure.php',
381 'message_to_show' => sprintf(
382 __('Table %s has been emptied.'),
383 htmlspecialchars($table)
388 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
389 $this_sql_query = 'DROP TABLE '
390 . PMA_Util
::backquote($GLOBALS['table']);
391 $drop_table_url_params = array_merge(
394 'sql_query' => $this_sql_query,
395 'goto' => 'db_operations.php',
398 'message_to_show' => sprintf(
400 ?
__('View %s has been dropped.')
401 : __('Table %s has been dropped.')
403 htmlspecialchars($table)
405 // table name is needed to avoid running
406 // PMA_relationsCleanupDatabase() on the whole db later
407 'table' => $GLOBALS['table'],
412 PMA_getHtmlForDeleteDataOrTable(
413 $truncate_table_url_params,
414 $drop_table_url_params
419 if (PMA_Partition
::havePartitioning()) {
420 $partition_names = PMA_Partition
::getPartitionNames($db, $table);
421 // show the Partition maintenance section only if we detect a partition
422 if (! is_null($partition_names[0])) {
424 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
428 unset($partition_names);
430 // Referential integrity check
431 // The Referential integrity check was intended for the non-InnoDB
432 // tables for which the relations are defined in pmadb
433 // so I assume that if the current table is InnoDB, I don't display
434 // this choice (InnoDB maintains integrity by itself)
436 if ($cfgRelation['relwork'] && ! $is_innodb) {
437 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
438 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
440 if (! empty($foreign)) {
442 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
444 } // end if ($foreign)
446 } // end if (!empty($cfg['Server']['relation']))
448 $response->addHTML('</div>');