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();
25 require 'libraries/tbl_common.inc.php';
26 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
27 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
30 * Gets relation settings
32 $cfgRelation = PMA_getRelationsParam();
35 * Gets available MySQL charsets and storage engines
37 require_once 'libraries/mysql_charsets.inc.php';
38 require_once 'libraries/StorageEngine.class.php';
41 * Class for partition management
43 require_once 'libraries/Partition.class.php';
45 // reselect current db (needed in some cases probably due to
46 // the calling of relation.lib.php)
47 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
50 * Gets tables informations
52 require 'libraries/tbl_info.inc.php';
54 // define some variables here, for improved syntax in the conditionals
55 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
56 $is_aria = $is_pbxt = false;
57 // set initial value of these variables, based on the current table engine
58 list($is_myisam_or_aria, $is_innodb, $is_isam,
59 $is_berkeleydb, $is_aria, $is_pbxt
60 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
63 // the value for transactional can be implicit
64 // (no create option found, in this case it means 1)
65 // or explicit (option found with a value of 0 or 1)
66 // ($transactional may have been set by libraries/tbl_info.inc.php,
67 // from the $create_options)
68 $transactional = (isset($transactional) && $transactional == '0')
71 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
75 $table_alters = array();
78 * If the table has to be moved to some other database
80 if (isset($_REQUEST['submit_move']) ||
isset($_REQUEST['submit_copy'])) {
82 include_once 'tbl_move_copy.php';
85 * If the table has to be maintained
87 if (isset($_REQUEST['table_maintenance'])) {
88 include_once 'sql.php';
92 * Updates table comment, type and options if required
94 if (isset($_REQUEST['submitoptions'])) {
96 $warning_messages = array();
98 if (isset($_REQUEST['new_name'])) {
99 if ($pma_table->rename($_REQUEST['new_name'])) {
100 $_message .= $pma_table->getLastMessage();
102 $GLOBALS['table'] = $pma_table->getName();
106 $_message .= $pma_table->getLastError();
111 if (! empty($_REQUEST['new_tbl_storage_engine'])
112 && strtolower($_REQUEST['new_tbl_storage_engine']) !== strtolower($tbl_storage_engine)
114 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
115 // reset the globals for the new engine
116 list($is_myisam_or_aria, $is_innodb, $is_isam,
117 $is_berkeleydb, $is_aria, $is_pbxt
118 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
121 $transactional = (isset($transactional) && $transactional == '0')
124 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
127 $new_tbl_storage_engine = '';
130 $table_alters = PMA_getTableAltersArray(
131 $is_myisam_or_aria, $is_isam, $pack_keys,
132 (empty($checksum) ?
'0' : '1'),
134 ((isset($page_checksum)) ?
$page_checksum : ''),
135 (empty($delay_key_write) ?
'0' : '1'),
136 $is_innodb, $is_pbxt, $row_format,
137 $new_tbl_storage_engine,
138 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
142 if (count($table_alters) > 0) {
143 $sql_query = 'ALTER TABLE '
144 . PMA_Util
::backquote($GLOBALS['table']);
145 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
147 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
149 unset($table_alters);
150 $warning_messages = PMA_getWarningMessagesArray();
154 * Reordering the table has been requested by the user
156 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
157 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
161 * A partition operation has been requested by the user
163 if (isset($_REQUEST['submit_partition'])
164 && ! empty($_REQUEST['partition_operation'])
166 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
170 // to avoid showing the old value (for example the AUTO_INCREMENT) after
171 // a change, clear the cache
172 PMA_Table
::$cache = array();
173 $page_checksum = $checksum = $delay_key_write = 0;
174 include 'libraries/tbl_info.inc.php';
178 if (isset($result) && empty($message_to_show)) {
179 // set to success by default, because result set could be empty
180 // (for example, a table rename)
182 if (empty($_message)) {
184 ? PMA_Message
::success(
185 __('Your SQL query has been executed successfully')
187 : PMA_Message
::error(__('Error'));
188 // $result should exist, regardless of $_message
189 $_type = $result ?
'success' : 'error';
191 if (isset($GLOBALS['ajax_request'])
192 && $GLOBALS['ajax_request'] == true
194 $response = PMA_Response
::getInstance();
195 $response->isSuccess($_message->isSuccess());
196 $response->addJSON('message', $_message);
198 'sql_query', PMA_Util
::getMessage(null, $sql_query)
203 if (! empty($warning_messages)) {
204 $_message = new PMA_Message
;
205 $_message->addMessages($warning_messages);
206 $_message->isError(true);
207 if ($GLOBALS['ajax_request'] == true) {
208 $response = PMA_Response
::getInstance();
209 $response->isSuccess(false);
210 $response->addJSON('message', $_message);
213 unset($warning_messages);
217 PMA_Util
::getMessage($_message, $sql_query, $_type)
219 unset($_message, $_type);
223 = $url_params['back']
224 = 'tbl_operations.php';
229 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
234 $response->addHTML('<div id="boxContainer" data-box-width="300">');
239 $hideOrderTable = false;
240 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
241 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
242 // InnoDB always orders table rows according to such an index if one is present.
243 if ($tbl_storage_engine == 'INNODB') {
244 include_once 'libraries/Index.class.php';
245 $indexes = PMA_Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
246 foreach ($indexes as $name => $idx) {
247 if ($name == 'PRIMARY') {
248 $hideOrderTable = true;
250 } elseif (! $idx->getNonUnique()) {
252 foreach ($idx->getColumns() as $column) {
253 if ($column->getNull()) {
259 $hideOrderTable = true;
265 if (! $hideOrderTable) {
266 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
272 $response->addHTML(PMA_getHtmlForMoveTable());
274 if (strstr($show_comment, '; InnoDB free') === false) {
275 if (strstr($show_comment, 'InnoDB free') === false) {
276 // only user entered comment
277 $comment = $show_comment;
279 // here we have just InnoDB generated part
283 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
284 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
287 // PACK_KEYS: MyISAM or ISAM
288 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
289 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
291 // Here should be version check for InnoDB, however it is supported
292 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
296 PMA_getTableOptionDiv(
297 $comment, $tbl_collation, $tbl_storage_engine,
298 $is_myisam_or_aria, $is_isam, $pack_keys,
300 (empty($delay_key_write) ?
'0' : '1'),
301 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
302 ((isset($page_checksum)) ?
$page_checksum : ''),
303 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ?
'0' : '1')
310 $response->addHTML(PMA_getHtmlForCopytable());
316 PMA_getHtmlForTableMaintenance(
324 if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
325 $truncate_table_url_params = array();
326 $drop_table_url_params = array();
329 && ! (isset($db_is_information_schema) && $db_is_information_schema)
331 $this_sql_query = 'TRUNCATE TABLE '
332 . PMA_Util
::backquote($GLOBALS['table']);
333 $truncate_table_url_params = array_merge(
336 'sql_query' => $this_sql_query,
337 'goto' => 'tbl_structure.php',
339 'message_to_show' => sprintf(
340 __('Table %s has been emptied'),
341 htmlspecialchars($table)
346 if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
347 $this_sql_query = 'DROP TABLE '
348 . PMA_Util
::backquote($GLOBALS['table']);
349 $drop_table_url_params = array_merge(
352 'sql_query' => $this_sql_query,
353 'goto' => 'db_operations.php',
356 'message_to_show' => sprintf(
358 ?
__('View %s has been dropped')
359 : __('Table %s has been dropped')
361 htmlspecialchars($table)
363 // table name is needed to avoid running
364 // PMA_relationsCleanupDatabase() on the whole db later
365 'table' => $GLOBALS['table'],
370 PMA_getHtmlForDeleteDataOrTable(
371 $truncate_table_url_params,
372 $drop_table_url_params
377 if (PMA_Partition
::havePartitioning()) {
378 $partition_names = PMA_Partition
::getPartitionNames($db, $table);
379 // show the Partition maintenance section only if we detect a partition
380 if (! is_null($partition_names[0])) {
382 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
386 unset($partition_names);
388 // Referential integrity check
389 // The Referential integrity check was intended for the non-InnoDB
390 // tables for which the relations are defined in pmadb
391 // so I assume that if the current table is InnoDB, I don't display
392 // this choice (InnoDB maintains integrity by itself)
394 if ($cfgRelation['relwork'] && ! $is_innodb) {
395 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
396 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table']);
400 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
402 } // end if ($foreign)
404 } // end if (!empty($cfg['Server']['relation']))
406 $response->addHTML('</div>');