2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
8 use PMA\libraries\Partition
;
9 use PMA\libraries\PMA_String
;
10 use PMA\libraries\Table
;
15 require_once 'libraries/common.inc.php';
18 * functions implementation for this script
20 require_once 'libraries/check_user_privileges.lib.php';
21 require_once 'libraries/operations.lib.php';
23 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
26 * Load JavaScript files
28 $response = PMA\libraries\Response
::getInstance();
29 $header = $response->getHeader();
30 $scripts = $header->getScripts();
31 $scripts->addFile('functions.js');
32 $scripts->addFile('tbl_operations.js');
37 require 'libraries/tbl_common.inc.php';
38 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
39 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
42 * Gets relation settings
44 $cfgRelation = PMA_getRelationsParam();
47 * Gets available MySQL charsets and storage engines
49 require_once 'libraries/mysql_charsets.inc.php';
51 // reselect current db (needed in some cases probably due to
52 // the calling of relation.lib.php)
53 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
56 * Gets tables information
58 require 'libraries/tbl_info.inc.php';
60 // define some variables here, for improved syntax in the conditionals
61 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
62 $is_aria = $is_pbxt = false;
63 // set initial value of these variables, based on the current table engine
64 list($is_myisam_or_aria, $is_innodb, $is_isam,
65 $is_berkeleydb, $is_aria, $is_pbxt
66 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
69 // the value for transactional can be implicit
70 // (no create option found, in this case it means 1)
71 // or explicit (option found with a value of 0 or 1)
72 // ($transactional may have been set by libraries/tbl_info.inc.php,
73 // from the $create_options)
74 $transactional = (isset($transactional) && $transactional == '0')
77 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
81 $table_alters = array();
83 /** @var String $pmaString */
84 $pmaString = $GLOBALS['PMA_String'];
87 * If the table has to be moved to some other database
89 if (isset($_REQUEST['submit_move']) ||
isset($_REQUEST['submit_copy'])) {
91 PMA_moveOrCopyTable($db, $table);
92 // This was ended in an Ajax call
96 * If the table has to be maintained
98 if (isset($_REQUEST['table_maintenance'])) {
99 include_once 'sql.php';
103 * Updates table comment, type and options if required
105 if (isset($_REQUEST['submitoptions'])) {
107 $warning_messages = array();
109 if (isset($_REQUEST['new_name'])) {
110 // Get original names before rename operation
111 $oldTable = $pma_table->getName();
112 $oldDb = $pma_table->getDbName();
114 if ($pma_table->rename($_REQUEST['new_name'])) {
115 if (isset($_REQUEST['adjust_privileges'])
116 && ! empty($_REQUEST['adjust_privileges'])
118 PMA_AdjustPrivileges_renameOrMoveTable(
119 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
123 // Reselect the original DB
124 $GLOBALS['db'] = $oldDb;
125 $GLOBALS['dbi']->selectDb($oldDb);
127 $_message .= $pma_table->getLastMessage();
129 $GLOBALS['table'] = $pma_table->getName();
133 $_message .= $pma_table->getLastError();
138 if (! empty($_REQUEST['new_tbl_storage_engine'])
139 && /*overload*/mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== /*overload*/mb_strtolower($tbl_storage_engine)
141 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
142 // reset the globals for the new engine
143 list($is_myisam_or_aria, $is_innodb, $is_isam,
144 $is_berkeleydb, $is_aria, $is_pbxt
145 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
148 $transactional = (isset($transactional) && $transactional == '0')
151 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
154 $new_tbl_storage_engine = '';
157 $table_alters = PMA_getTableAltersArray(
158 $is_myisam_or_aria, $is_isam, $pack_keys,
159 (empty($checksum) ?
'0' : '1'),
161 ((isset($page_checksum)) ?
$page_checksum : ''),
162 (empty($delay_key_write) ?
'0' : '1'),
163 $is_innodb, $is_pbxt, $row_format,
164 $new_tbl_storage_engine,
165 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
169 if (count($table_alters) > 0) {
170 $sql_query = 'ALTER TABLE '
171 . PMA\libraries\Util
::backquote($GLOBALS['table']);
172 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
174 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
176 unset($table_alters);
177 $warning_messages = PMA_getWarningMessagesArray();
180 if (isset($_REQUEST['tbl_collation'])
181 && ! empty($_REQUEST['tbl_collation'])
182 && isset($_REQUEST['change_all_collations'])
183 && ! empty($_REQUEST['change_all_collations'])
185 PMA_changeAllColumnsCollation(
186 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
191 * Reordering the table has been requested by the user
193 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
194 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
198 * A partition operation has been requested by the user
202 if (isset($_REQUEST['submit_partition'])
203 && ! empty($_REQUEST['partition_operation'])
205 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
209 // to avoid showing the old value (for example the AUTO_INCREMENT) after
210 // a change, clear the cache
211 $GLOBALS['dbi']->clearTableCache();
212 $page_checksum = $checksum = $delay_key_write = 0;
213 include 'libraries/tbl_info.inc.php';
217 if (isset($result) && empty($message_to_show)) {
218 // set to success by default, because result set could be empty
219 // (for example, a table rename)
221 if (empty($_message)) {
223 ? PMA\libraries\Message
::success(
224 __('Your SQL query has been executed successfully.')
226 : PMA\libraries\Message
::error(__('Error'));
227 // $result should exist, regardless of $_message
228 $_type = $result ?
'success' : 'error';
230 if (isset($GLOBALS['ajax_request'])
231 && $GLOBALS['ajax_request'] == true
233 $response = PMA\libraries\Response
::getInstance();
234 $response->setRequestStatus($_message->isSuccess());
235 $response->addJSON('message', $_message);
237 'sql_query', PMA\libraries\Util
::getMessage(null, $sql_query)
242 if (! empty($warning_messages)) {
243 $_message = new PMA\libraries\Message
;
244 $_message->addMessages($warning_messages);
245 $_message->isError(true);
246 if ($GLOBALS['ajax_request'] == true) {
247 $response = PMA\libraries\Response
::getInstance();
248 $response->setRequestStatus(false);
249 $response->addJSON('message', $_message);
252 unset($warning_messages);
256 PMA\libraries\Util
::getMessage($_message, $sql_query, $_type)
258 unset($_message, $_type);
262 = $url_params['back']
263 = 'tbl_operations.php';
268 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
273 $response->addHTML('<div id="boxContainer" data-box-width="300">');
278 $hideOrderTable = false;
279 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
280 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
281 // InnoDB always orders table rows according to such an index if one is present.
282 if ($tbl_storage_engine == 'INNODB') {
283 $indexes = PMA\libraries\Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
284 foreach ($indexes as $name => $idx) {
285 if ($name == 'PRIMARY') {
286 $hideOrderTable = true;
288 } elseif (! $idx->getNonUnique()) {
290 foreach ($idx->getColumns() as $column) {
291 if ($column->getNull()) {
297 $hideOrderTable = true;
303 if (! $hideOrderTable) {
304 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
310 $response->addHTML(PMA_getHtmlForMoveTable());
312 if (/*overload*/mb_strstr($show_comment, '; InnoDB free') === false) {
313 if (/*overload*/mb_strstr($show_comment, 'InnoDB free') === false) {
314 // only user entered comment
315 $comment = $show_comment;
317 // here we have just InnoDB generated part
321 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
322 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
325 // PACK_KEYS: MyISAM or ISAM
326 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
327 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
329 // Here should be version check for InnoDB, however it is supported
330 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
334 PMA_getTableOptionDiv(
335 $comment, $tbl_collation, $tbl_storage_engine,
336 $is_myisam_or_aria, $is_isam, $pack_keys,
338 (empty($delay_key_write) ?
'0' : '1'),
339 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
340 ((isset($page_checksum)) ?
$page_checksum : ''),
341 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ?
'0' : '1')
348 $response->addHTML(PMA_getHtmlForCopytable());
354 PMA_getHtmlForTableMaintenance(
362 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
363 $truncate_table_url_params = array();
364 $drop_table_url_params = array();
367 && ! (isset($db_is_system_schema) && $db_is_system_schema)
369 $this_sql_query = 'TRUNCATE TABLE '
370 . PMA\libraries\Util
::backquote($GLOBALS['table']);
371 $truncate_table_url_params = array_merge(
374 'sql_query' => $this_sql_query,
375 'goto' => 'tbl_structure.php',
377 'message_to_show' => sprintf(
378 __('Table %s has been emptied.'),
379 htmlspecialchars($table)
384 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
385 $this_sql_query = 'DROP TABLE '
386 . PMA\libraries\Util
::backquote($GLOBALS['table']);
387 $drop_table_url_params = array_merge(
390 'sql_query' => $this_sql_query,
391 'goto' => 'db_operations.php',
394 'message_to_show' => sprintf(
396 ?
__('View %s has been dropped.')
397 : __('Table %s has been dropped.')
399 htmlspecialchars($table)
401 // table name is needed to avoid running
402 // PMA_relationsCleanupDatabase() on the whole db later
403 'table' => $GLOBALS['table'],
408 PMA_getHtmlForDeleteDataOrTable(
409 $truncate_table_url_params,
410 $drop_table_url_params
415 if (Partition
::havePartitioning()) {
416 $partition_names = Partition
::getPartitionNames($db, $table);
417 // show the Partition maintenance section only if we detect a partition
418 if (! is_null($partition_names[0])) {
420 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
424 unset($partition_names);
426 // Referential integrity check
427 // The Referential integrity check was intended for the non-InnoDB
428 // tables for which the relations are defined in pmadb
429 // so I assume that if the current table is InnoDB, I don't display
430 // this choice (InnoDB maintains integrity by itself)
432 if ($cfgRelation['relwork'] && ! $is_innodb) {
433 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
434 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
436 if (! empty($foreign)) {
438 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
440 } // end if ($foreign)
442 } // end if (!empty($cfg['Server']['relation']))
444 $response->addHTML('</div>');