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/operations.lib.php';
19 $pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
20 $response = PMA_Response
::getInstance();
24 require 'libraries/tbl_common.inc.php';
25 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
26 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
29 * Gets relation settings
31 $cfgRelation = PMA_getRelationsParam();
34 * Gets available MySQL charsets and storage engines
36 require_once 'libraries/mysql_charsets.inc.php';
37 require_once 'libraries/StorageEngine.class.php';
40 * Class for partition management
42 require_once 'libraries/Partition.class.php';
44 // reselect current db (needed in some cases probably due to
45 // the calling of relation.lib.php)
46 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
49 * Gets tables informations
51 require 'libraries/tbl_info.inc.php';
53 // define some variables here, for improved syntax in the conditionals
54 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
55 $is_aria = $is_pbxt = false;
56 // set initial value of these variables, based on the current table engine
57 list($is_myisam_or_aria, $is_innodb, $is_isam,
58 $is_berkeleydb, $is_aria, $is_pbxt
59 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
62 // the value for transactional can be implicit
63 // (no create option found, in this case it means 1)
64 // or explicit (option found with a value of 0 or 1)
65 // ($transactional may have been set by libraries/tbl_info.inc.php,
66 // from the $create_options)
67 $transactional = (isset($transactional) && $transactional == '0')
70 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
74 $table_alters = array();
76 /** @var PMA_String $pmaString */
77 $pmaString = $GLOBALS['PMA_String'];
80 * If the table has to be moved to some other database
82 if (isset($_REQUEST['submit_move']) ||
isset($_REQUEST['submit_copy'])) {
84 PMA_moveOrCopyTable($db, $table);
85 // This was ended in an Ajax call
89 * If the table has to be maintained
91 if (isset($_REQUEST['table_maintenance'])) {
92 include_once 'sql.php';
96 * Updates table comment, type and options if required
98 if (isset($_REQUEST['submitoptions'])) {
100 $warning_messages = array();
102 if (isset($_REQUEST['new_name'])) {
103 if ($pma_table->rename($_REQUEST['new_name'])) {
104 $_message .= $pma_table->getLastMessage();
106 $GLOBALS['table'] = $pma_table->getName();
110 $_message .= $pma_table->getLastError();
115 if (! empty($_REQUEST['new_tbl_storage_engine'])
116 && /*overload*/mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== /*overload*/mb_strtolower($tbl_storage_engine)
118 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
119 // reset the globals for the new engine
120 list($is_myisam_or_aria, $is_innodb, $is_isam,
121 $is_berkeleydb, $is_aria, $is_pbxt
122 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
125 $transactional = (isset($transactional) && $transactional == '0')
128 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
131 $new_tbl_storage_engine = '';
134 $table_alters = PMA_getTableAltersArray(
135 $is_myisam_or_aria, $is_isam, $pack_keys,
136 (empty($checksum) ?
'0' : '1'),
138 ((isset($page_checksum)) ?
$page_checksum : ''),
139 (empty($delay_key_write) ?
'0' : '1'),
140 $is_innodb, $is_pbxt, $row_format,
141 $new_tbl_storage_engine,
142 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
146 if (count($table_alters) > 0) {
147 $sql_query = 'ALTER TABLE '
148 . PMA_Util
::backquote($GLOBALS['table']);
149 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
151 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
153 unset($table_alters);
154 $warning_messages = PMA_getWarningMessagesArray();
158 * Reordering the table has been requested by the user
160 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
161 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
165 * A partition operation has been requested by the user
169 if (isset($_REQUEST['submit_partition'])
170 && ! empty($_REQUEST['partition_operation'])
172 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
176 // to avoid showing the old value (for example the AUTO_INCREMENT) after
177 // a change, clear the cache
178 PMA_Table
::$cache = array();
179 $page_checksum = $checksum = $delay_key_write = 0;
180 include 'libraries/tbl_info.inc.php';
184 if (isset($result) && empty($message_to_show)) {
185 // set to success by default, because result set could be empty
186 // (for example, a table rename)
188 if (empty($_message)) {
190 ? PMA_Message
::success(
191 __('Your SQL query has been executed successfully.')
193 : PMA_Message
::error(__('Error'));
194 // $result should exist, regardless of $_message
195 $_type = $result ?
'success' : 'error';
197 if (isset($GLOBALS['ajax_request'])
198 && $GLOBALS['ajax_request'] == true
200 $response = PMA_Response
::getInstance();
201 $response->isSuccess($_message->isSuccess());
202 $response->addJSON('message', $_message);
204 'sql_query', PMA_Util
::getMessage(null, $sql_query)
209 if (! empty($warning_messages)) {
210 $_message = new PMA_Message
;
211 $_message->addMessages($warning_messages);
212 $_message->isError(true);
213 if ($GLOBALS['ajax_request'] == true) {
214 $response = PMA_Response
::getInstance();
215 $response->isSuccess(false);
216 $response->addJSON('message', $_message);
219 unset($warning_messages);
223 PMA_Util
::getMessage($_message, $sql_query, $_type)
225 unset($_message, $_type);
229 = $url_params['back']
230 = 'tbl_operations.php';
235 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
240 $response->addHTML('<div id="boxContainer" data-box-width="300">');
245 $hideOrderTable = false;
246 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
247 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
248 // InnoDB always orders table rows according to such an index if one is present.
249 if ($tbl_storage_engine == 'INNODB') {
250 include_once 'libraries/Index.class.php';
251 $indexes = PMA_Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
252 foreach ($indexes as $name => $idx) {
253 if ($name == 'PRIMARY') {
254 $hideOrderTable = true;
256 } elseif (! $idx->getNonUnique()) {
258 foreach ($idx->getColumns() as $column) {
259 if ($column->getNull()) {
265 $hideOrderTable = true;
271 if (! $hideOrderTable) {
272 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
278 $response->addHTML(PMA_getHtmlForMoveTable());
280 if (/*overload*/mb_strstr($show_comment, '; InnoDB free') === false) {
281 if (/*overload*/mb_strstr($show_comment, 'InnoDB free') === false) {
282 // only user entered comment
283 $comment = $show_comment;
285 // here we have just InnoDB generated part
289 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
290 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
293 // PACK_KEYS: MyISAM or ISAM
294 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
295 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
297 // Here should be version check for InnoDB, however it is supported
298 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
302 PMA_getTableOptionDiv(
303 $comment, $tbl_collation, $tbl_storage_engine,
304 $is_myisam_or_aria, $is_isam, $pack_keys,
306 (empty($delay_key_write) ?
'0' : '1'),
307 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
308 ((isset($page_checksum)) ?
$page_checksum : ''),
309 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ?
'0' : '1')
316 $response->addHTML(PMA_getHtmlForCopytable());
322 PMA_getHtmlForTableMaintenance(
330 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
331 $truncate_table_url_params = array();
332 $drop_table_url_params = array();
335 && ! (isset($db_is_system_schema) && $db_is_system_schema)
337 $this_sql_query = 'TRUNCATE TABLE '
338 . PMA_Util
::backquote($GLOBALS['table']);
339 $truncate_table_url_params = array_merge(
342 'sql_query' => $this_sql_query,
343 'goto' => 'tbl_structure.php',
345 'message_to_show' => sprintf(
346 __('Table %s has been emptied.'),
347 htmlspecialchars($table)
352 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
353 $this_sql_query = 'DROP TABLE '
354 . PMA_Util
::backquote($GLOBALS['table']);
355 $drop_table_url_params = array_merge(
358 'sql_query' => $this_sql_query,
359 'goto' => 'db_operations.php',
362 'message_to_show' => sprintf(
364 ?
__('View %s has been dropped.')
365 : __('Table %s has been dropped.')
367 htmlspecialchars($table)
369 // table name is needed to avoid running
370 // PMA_relationsCleanupDatabase() on the whole db later
371 'table' => $GLOBALS['table'],
376 PMA_getHtmlForDeleteDataOrTable(
377 $truncate_table_url_params,
378 $drop_table_url_params
383 if (PMA_Partition
::havePartitioning()) {
384 $partition_names = PMA_Partition
::getPartitionNames($db, $table);
385 // show the Partition maintenance section only if we detect a partition
386 if (! is_null($partition_names[0])) {
388 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
392 unset($partition_names);
394 // Referential integrity check
395 // The Referential integrity check was intended for the non-InnoDB
396 // tables for which the relations are defined in pmadb
397 // so I assume that if the current table is InnoDB, I don't display
398 // this choice (InnoDB maintains integrity by itself)
400 if ($cfgRelation['relwork'] && ! $is_innodb) {
401 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
402 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
406 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
408 } // end if ($foreign)
410 } // end if (!empty($cfg['Server']['relation']))
412 $response->addHTML('</div>');