Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / classes / Operations.php
blobfd575e04c921bf8e4c28b3fad592c8ad2bf494dd
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use PhpMyAdmin\Engines\Innodb;
8 use PhpMyAdmin\Plugins\Export\ExportSql;
10 use function array_merge;
11 use function count;
12 use function explode;
13 use function mb_strtolower;
14 use function str_replace;
15 use function strlen;
16 use function strtolower;
17 use function urldecode;
19 /**
20 * Set of functions with the operations section in phpMyAdmin
22 class Operations
24 /** @var Relation */
25 private $relation;
27 /** @var DatabaseInterface */
28 private $dbi;
30 /**
31 * @param DatabaseInterface $dbi DatabaseInterface object
32 * @param Relation $relation Relation object
34 public function __construct(DatabaseInterface $dbi, Relation $relation)
36 $this->dbi = $dbi;
37 $this->relation = $relation;
40 /**
41 * Run the Procedure definitions and function definitions
43 * to avoid selecting alternatively the current and new db
44 * we would need to modify the CREATE definitions to qualify
45 * the db name
47 * @param string $db database name
49 * @return void
51 public function runProcedureAndFunctionDefinitions($db)
53 $procedure_names = $this->dbi->getProceduresOrFunctions($db, 'PROCEDURE');
54 if ($procedure_names) {
55 foreach ($procedure_names as $procedure_name) {
56 $this->dbi->selectDb($db);
57 $tmp_query = $this->dbi->getDefinition(
58 $db,
59 'PROCEDURE',
60 $procedure_name
62 if ($tmp_query === null) {
63 continue;
66 // collect for later display
67 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
68 $this->dbi->selectDb($_POST['newname']);
69 $this->dbi->query($tmp_query);
73 $function_names = $this->dbi->getProceduresOrFunctions($db, 'FUNCTION');
74 if (! $function_names) {
75 return;
78 foreach ($function_names as $function_name) {
79 $this->dbi->selectDb($db);
80 $tmp_query = $this->dbi->getDefinition(
81 $db,
82 'FUNCTION',
83 $function_name
85 if ($tmp_query === null) {
86 continue;
89 // collect for later display
90 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
91 $this->dbi->selectDb($_POST['newname']);
92 $this->dbi->query($tmp_query);
96 /**
97 * Create database before copy
99 * @return void
101 public function createDbBeforeCopy()
103 $local_query = 'CREATE DATABASE IF NOT EXISTS '
104 . Util::backquote($_POST['newname']);
105 if (isset($_POST['db_collation'])) {
106 $local_query .= ' DEFAULT'
107 . Util::getCharsetQueryPart($_POST['db_collation'] ?? '');
110 $local_query .= ';';
111 $GLOBALS['sql_query'] .= $local_query;
113 // save the original db name because Tracker.php which
114 // may be called under $this->dbi->query() changes $GLOBALS['db']
115 // for some statements, one of which being CREATE DATABASE
116 $original_db = $GLOBALS['db'];
117 $this->dbi->query($local_query);
118 $GLOBALS['db'] = $original_db;
120 // Set the SQL mode to NO_AUTO_VALUE_ON_ZERO to prevent MySQL from creating
121 // export statements it cannot import
122 $sql_set_mode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'";
123 $this->dbi->query($sql_set_mode);
125 // rebuild the database list because Table::moveCopy
126 // checks in this list if the target db exists
127 $GLOBALS['dblist']->databases->build();
131 * Get views as an array and create SQL view stand-in
133 * @param array $tables_full array of all tables in given db or dbs
134 * @param ExportSql $export_sql_plugin export plugin instance
135 * @param string $db database name
137 * @return array
139 public function getViewsAndCreateSqlViewStandIn(
140 array $tables_full,
141 $export_sql_plugin,
144 $views = [];
145 foreach ($tables_full as $each_table => $tmp) {
146 // to be able to rename a db containing views,
147 // first all the views are collected and a stand-in is created
148 // the real views are created after the tables
149 if (! $this->dbi->getTable($db, (string) $each_table)->isView()) {
150 continue;
153 // If view exists, and 'add drop view' is selected: Drop it!
154 if (
155 $_POST['what'] !== 'nocopy'
156 && isset($_POST['drop_if_exists'])
157 && $_POST['drop_if_exists'] === 'true'
159 $drop_query = 'DROP VIEW IF EXISTS '
160 . Util::backquote($_POST['newname']) . '.'
161 . Util::backquote($each_table);
162 $this->dbi->query($drop_query);
164 $GLOBALS['sql_query'] .= "\n" . $drop_query . ';';
167 $views[] = $each_table;
168 // Create stand-in definition to resolve view dependencies
169 $sql_view_standin = $export_sql_plugin->getTableDefStandIn(
170 $db,
171 $each_table,
172 "\n"
174 $this->dbi->selectDb($_POST['newname']);
175 $this->dbi->query($sql_view_standin);
176 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
179 return $views;
183 * Get sql query for copy/rename table and boolean for whether copy/rename or not
185 * @param array $tables_full array of all tables in given db or dbs
186 * @param bool $move whether database name is empty or not
187 * @param string $db database name
189 * @return array SQL queries for the constraints
191 public function copyTables(array $tables_full, $move, $db)
193 $sqlContraints = [];
194 foreach ($tables_full as $each_table => $tmp) {
195 // skip the views; we have created stand-in definitions
196 if ($this->dbi->getTable($db, (string) $each_table)->isView()) {
197 continue;
200 // value of $what for this table only
201 $this_what = $_POST['what'];
203 // do not copy the data from a Merge table
204 // note: on the calling FORM, 'data' means 'structure and data'
205 if ($this->dbi->getTable($db, (string) $each_table)->isMerge()) {
206 if ($this_what === 'data') {
207 $this_what = 'structure';
210 if ($this_what === 'dataonly') {
211 $this_what = 'nocopy';
215 if ($this_what === 'nocopy') {
216 continue;
219 // keep the triggers from the original db+table
220 // (third param is empty because delimiters are only intended
221 // for importing via the mysql client or our Import feature)
222 $triggers = $this->dbi->getTriggers($db, (string) $each_table, '');
224 if (
225 ! Table::moveCopy(
226 $db,
227 $each_table,
228 $_POST['newname'],
229 $each_table,
230 ($this_what ?? 'data'),
231 $move,
232 'db_copy'
235 $GLOBALS['_error'] = true;
236 break;
239 // apply the triggers to the destination db+table
240 if ($triggers) {
241 $this->dbi->selectDb($_POST['newname']);
242 foreach ($triggers as $trigger) {
243 $this->dbi->query($trigger['create']);
244 $GLOBALS['sql_query'] .= "\n" . $trigger['create'] . ';';
248 // this does not apply to a rename operation
249 if (
250 ! isset($_POST['add_constraints'])
251 || empty($GLOBALS['sql_constraints_query'])
253 continue;
256 $sqlContraints[] = $GLOBALS['sql_constraints_query'];
257 unset($GLOBALS['sql_constraints_query']);
260 return $sqlContraints;
264 * Run the EVENT definition for selected database
266 * to avoid selecting alternatively the current and new db
267 * we would need to modify the CREATE definitions to qualify
268 * the db name
270 * @param string $db database name
272 * @return void
274 public function runEventDefinitionsForDb($db)
276 $event_names = $this->dbi->fetchResult(
277 'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \''
278 . $this->dbi->escapeString($db) . '\';'
280 if (! $event_names) {
281 return;
284 foreach ($event_names as $event_name) {
285 $this->dbi->selectDb($db);
286 $tmp_query = $this->dbi->getDefinition($db, 'EVENT', $event_name);
287 // collect for later display
288 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
289 $this->dbi->selectDb($_POST['newname']);
290 $this->dbi->query($tmp_query);
295 * Handle the views, return the boolean value whether table rename/copy or not
297 * @param array $views views as an array
298 * @param bool $move whether database name is empty or not
299 * @param string $db database name
301 * @return void
303 public function handleTheViews(array $views, $move, $db)
305 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
306 // to remove stand-in VIEW that was created earlier
307 // ( $_POST['drop_if_exists'] is used in moveCopy() )
308 if (isset($_POST['drop_if_exists'])) {
309 $temp_drop_if_exists = $_POST['drop_if_exists'];
312 $_POST['drop_if_exists'] = 'true';
313 foreach ($views as $view) {
314 $copying_succeeded = Table::moveCopy(
315 $db,
316 $view,
317 $_POST['newname'],
318 $view,
319 'structure',
320 $move,
321 'db_copy'
323 if (! $copying_succeeded) {
324 $GLOBALS['_error'] = true;
325 break;
329 unset($_POST['drop_if_exists']);
331 if (! isset($temp_drop_if_exists)) {
332 return;
335 // restore previous value
336 $_POST['drop_if_exists'] = $temp_drop_if_exists;
340 * Adjust the privileges after Renaming the db
342 * @param string $oldDb Database name before renaming
343 * @param string $newname New Database name requested
345 * @return void
347 public function adjustPrivilegesMoveDb($oldDb, $newname)
349 if (
350 ! $GLOBALS['db_priv'] || ! $GLOBALS['table_priv']
351 || ! $GLOBALS['col_priv'] || ! $GLOBALS['proc_priv']
352 || ! $GLOBALS['is_reload_priv']
354 return;
357 $this->dbi->selectDb('mysql');
358 $newname = str_replace('_', '\_', $newname);
359 $oldDb = str_replace('_', '\_', $oldDb);
361 // For Db specific privileges
362 $query_db_specific = 'UPDATE ' . Util::backquote('db')
363 . 'SET Db = \'' . $this->dbi->escapeString($newname)
364 . '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
365 $this->dbi->query($query_db_specific);
367 // For table specific privileges
368 $query_table_specific = 'UPDATE ' . Util::backquote('tables_priv')
369 . 'SET Db = \'' . $this->dbi->escapeString($newname)
370 . '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
371 $this->dbi->query($query_table_specific);
373 // For column specific privileges
374 $query_col_specific = 'UPDATE ' . Util::backquote('columns_priv')
375 . 'SET Db = \'' . $this->dbi->escapeString($newname)
376 . '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
377 $this->dbi->query($query_col_specific);
379 // For procedures specific privileges
380 $query_proc_specific = 'UPDATE ' . Util::backquote('procs_priv')
381 . 'SET Db = \'' . $this->dbi->escapeString($newname)
382 . '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
383 $this->dbi->query($query_proc_specific);
385 // Finally FLUSH the new privileges
386 $flush_query = 'FLUSH PRIVILEGES;';
387 $this->dbi->query($flush_query);
391 * Adjust the privileges after Copying the db
393 * @param string $oldDb Database name before copying
394 * @param string $newname New Database name requested
396 * @return void
398 public function adjustPrivilegesCopyDb($oldDb, $newname)
400 if (
401 ! $GLOBALS['db_priv'] || ! $GLOBALS['table_priv']
402 || ! $GLOBALS['col_priv'] || ! $GLOBALS['proc_priv']
403 || ! $GLOBALS['is_reload_priv']
405 return;
408 $this->dbi->selectDb('mysql');
409 $newname = str_replace('_', '\_', $newname);
410 $oldDb = str_replace('_', '\_', $oldDb);
412 $query_db_specific_old = 'SELECT * FROM '
413 . Util::backquote('db') . ' WHERE '
414 . 'Db = "' . $oldDb . '";';
416 $old_privs_db = $this->dbi->fetchResult($query_db_specific_old, 0);
418 foreach ($old_privs_db as $old_priv) {
419 $newDb_db_privs_query = 'INSERT INTO ' . Util::backquote('db')
420 . ' VALUES("' . $old_priv[0] . '", "' . $newname . '"';
421 $privCount = count($old_priv);
422 for ($i = 2; $i < $privCount; $i++) {
423 $newDb_db_privs_query .= ', "' . $old_priv[$i] . '"';
426 $newDb_db_privs_query .= ')';
428 $this->dbi->query($newDb_db_privs_query);
431 // For Table Specific privileges
432 $query_table_specific_old = 'SELECT * FROM '
433 . Util::backquote('tables_priv') . ' WHERE '
434 . 'Db = "' . $oldDb . '";';
436 $old_privs_table = $this->dbi->fetchResult(
437 $query_table_specific_old,
441 foreach ($old_privs_table as $old_priv) {
442 $newDb_table_privs_query = 'INSERT INTO ' . Util::backquote(
443 'tables_priv'
444 ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
445 . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
446 . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
447 . $old_priv[7] . '");';
449 $this->dbi->query($newDb_table_privs_query);
452 // For Column Specific privileges
453 $query_col_specific_old = 'SELECT * FROM '
454 . Util::backquote('columns_priv') . ' WHERE '
455 . 'Db = "' . $oldDb . '";';
457 $old_privs_col = $this->dbi->fetchResult(
458 $query_col_specific_old,
462 foreach ($old_privs_col as $old_priv) {
463 $newDb_col_privs_query = 'INSERT INTO ' . Util::backquote(
464 'columns_priv'
465 ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
466 . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
467 . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '");';
469 $this->dbi->query($newDb_col_privs_query);
472 // For Procedure Specific privileges
473 $query_proc_specific_old = 'SELECT * FROM '
474 . Util::backquote('procs_priv') . ' WHERE '
475 . 'Db = "' . $oldDb . '";';
477 $old_privs_proc = $this->dbi->fetchResult(
478 $query_proc_specific_old,
482 foreach ($old_privs_proc as $old_priv) {
483 $newDb_proc_privs_query = 'INSERT INTO ' . Util::backquote(
484 'procs_priv'
485 ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
486 . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
487 . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
488 . $old_priv[7] . '");';
490 $this->dbi->query($newDb_proc_privs_query);
493 // Finally FLUSH the new privileges
494 $flush_query = 'FLUSH PRIVILEGES;';
495 $this->dbi->query($flush_query);
499 * Create all accumulated constraints
501 * @param array $sqlConstratints array of sql constraints for the database
503 * @return void
505 public function createAllAccumulatedConstraints(array $sqlConstratints)
507 $this->dbi->selectDb($_POST['newname']);
508 foreach ($sqlConstratints as $one_query) {
509 $this->dbi->query($one_query);
510 // and prepare to display them
511 $GLOBALS['sql_query'] .= "\n" . $one_query;
516 * Duplicate the bookmarks for the db (done once for each db)
518 * @param bool $_error whether table rename/copy or not
519 * @param string $db database name
521 * @return void
523 public function duplicateBookmarks($_error, $db)
525 if ($_error || $db == $_POST['newname']) {
526 return;
529 $get_fields = [
530 'user',
531 'label',
532 'query',
534 $where_fields = ['dbase' => $db];
535 $new_fields = ['dbase' => $_POST['newname']];
536 Table::duplicateInfo(
537 'bookmarkwork',
538 'bookmark',
539 $get_fields,
540 $where_fields,
541 $new_fields
546 * Get array of possible row formats
548 * @return array
550 public function getPossibleRowFormat()
552 // the outer array is for engines, the inner array contains the dropdown
553 // option values as keys then the dropdown option labels
555 $possible_row_formats = [
556 'ARCHIVE' => ['COMPRESSED' => 'COMPRESSED'],
557 'ARIA' => [
558 'FIXED' => 'FIXED',
559 'DYNAMIC' => 'DYNAMIC',
560 'PAGE' => 'PAGE',
562 'MARIA' => [
563 'FIXED' => 'FIXED',
564 'DYNAMIC' => 'DYNAMIC',
565 'PAGE' => 'PAGE',
567 'MYISAM' => [
568 'FIXED' => 'FIXED',
569 'DYNAMIC' => 'DYNAMIC',
571 'PBXT' => [
572 'FIXED' => 'FIXED',
573 'DYNAMIC' => 'DYNAMIC',
575 'INNODB' => [
576 'COMPACT' => 'COMPACT',
577 'REDUNDANT' => 'REDUNDANT',
581 /** @var Innodb $innodbEnginePlugin */
582 $innodbEnginePlugin = StorageEngine::getEngine('Innodb');
583 $innodbPluginVersion = $innodbEnginePlugin->getInnodbPluginVersion();
584 if (! empty($innodbPluginVersion)) {
585 $innodb_file_format = $innodbEnginePlugin->getInnodbFileFormat();
586 } else {
587 $innodb_file_format = '';
591 * Newer MySQL/MariaDB always return empty a.k.a '' on $innodb_file_format otherwise
592 * old versions of MySQL/MariaDB must be returning something or not empty.
593 * This patch is to support newer MySQL/MariaDB while also for backward compatibilities.
595 if (
596 ($innodb_file_format === 'Barracuda') || ($innodb_file_format == '')
597 && $innodbEnginePlugin->supportsFilePerTable()
599 $possible_row_formats['INNODB']['DYNAMIC'] = 'DYNAMIC';
600 $possible_row_formats['INNODB']['COMPRESSED'] = 'COMPRESSED';
603 return $possible_row_formats;
607 * @return array<string, string>
609 public function getPartitionMaintenanceChoices(): array
611 global $db, $table;
613 $choices = [
614 'ANALYZE' => __('Analyze'),
615 'CHECK' => __('Check'),
616 'OPTIMIZE' => __('Optimize'),
617 'REBUILD' => __('Rebuild'),
618 'REPAIR' => __('Repair'),
619 'TRUNCATE' => __('Truncate'),
622 $partitionMethod = Partition::getPartitionMethod($db, $table);
624 // add COALESCE or DROP option to choices array depending on Partition method
625 if (
626 $partitionMethod === 'RANGE'
627 || $partitionMethod === 'RANGE COLUMNS'
628 || $partitionMethod === 'LIST'
629 || $partitionMethod === 'LIST COLUMNS'
631 $choices['DROP'] = __('Drop');
632 } else {
633 $choices['COALESCE'] = __('Coalesce');
636 return $choices;
640 * @param array $urlParams Array of url parameters.
641 * @param bool $hasRelationFeature If relation feature is enabled.
643 * @return array
645 public function getForeignersForReferentialIntegrityCheck(
646 array $urlParams,
647 $hasRelationFeature
648 ): array {
649 global $db, $table;
651 if (! $hasRelationFeature) {
652 return [];
655 $foreigners = [];
656 $this->dbi->selectDb($db);
657 $foreign = $this->relation->getForeigners($db, $table, '', 'internal');
659 foreach ($foreign as $master => $arr) {
660 $joinQuery = 'SELECT '
661 . Util::backquote($table) . '.*'
662 . ' FROM ' . Util::backquote($table)
663 . ' LEFT JOIN '
664 . Util::backquote($arr['foreign_db'])
665 . '.'
666 . Util::backquote($arr['foreign_table']);
668 if ($arr['foreign_table'] == $table) {
669 $foreignTable = $table . '1';
670 $joinQuery .= ' AS ' . Util::backquote($foreignTable);
671 } else {
672 $foreignTable = $arr['foreign_table'];
675 $joinQuery .= ' ON '
676 . Util::backquote($table) . '.'
677 . Util::backquote($master)
678 . ' = '
679 . Util::backquote($arr['foreign_db'])
680 . '.'
681 . Util::backquote($foreignTable) . '.'
682 . Util::backquote($arr['foreign_field'])
683 . ' WHERE '
684 . Util::backquote($arr['foreign_db'])
685 . '.'
686 . Util::backquote($foreignTable) . '.'
687 . Util::backquote($arr['foreign_field'])
688 . ' IS NULL AND '
689 . Util::backquote($table) . '.'
690 . Util::backquote($master)
691 . ' IS NOT NULL';
692 $thisUrlParams = array_merge(
693 $urlParams,
695 'sql_query' => $joinQuery,
696 'sql_signature' => Core::signSqlQuery($joinQuery),
700 $foreigners[] = [
701 'params' => $thisUrlParams,
702 'master' => $master,
703 'db' => $arr['foreign_db'],
704 'table' => $arr['foreign_table'],
705 'field' => $arr['foreign_field'],
709 return $foreigners;
713 * Get table alters array
715 * @param Table $pma_table The Table object
716 * @param string $pack_keys pack keys
717 * @param string $checksum value of checksum
718 * @param string $page_checksum value of page checksum
719 * @param string $delay_key_write delay key write
720 * @param string $row_format row format
721 * @param string $newTblStorageEngine table storage engine
722 * @param string $transactional value of transactional
723 * @param string $tbl_collation collation of the table
725 * @return array
727 public function getTableAltersArray(
728 $pma_table,
729 $pack_keys,
730 $checksum,
731 $page_checksum,
732 $delay_key_write,
733 $row_format,
734 $newTblStorageEngine,
735 $transactional,
736 $tbl_collation
738 global $auto_increment;
740 $table_alters = [];
742 if (
743 isset($_POST['comment'])
744 && urldecode($_POST['prev_comment']) !== $_POST['comment']
746 $table_alters[] = 'COMMENT = \''
747 . $this->dbi->escapeString($_POST['comment']) . '\'';
750 if (
751 ! empty($newTblStorageEngine)
752 && mb_strtolower($newTblStorageEngine) !== mb_strtolower($GLOBALS['tbl_storage_engine'])
754 $table_alters[] = 'ENGINE = ' . $newTblStorageEngine;
757 if (
758 ! empty($_POST['tbl_collation'])
759 && $_POST['tbl_collation'] !== $tbl_collation
761 $table_alters[] = 'DEFAULT '
762 . Util::getCharsetQueryPart($_POST['tbl_collation'] ?? '');
765 if (
766 $pma_table->isEngine(['MYISAM', 'ARIA', 'ISAM'])
767 && isset($_POST['new_pack_keys'])
768 && $_POST['new_pack_keys'] != (string) $pack_keys
770 $table_alters[] = 'pack_keys = ' . $_POST['new_pack_keys'];
773 $_POST['new_checksum'] = empty($_POST['new_checksum']) ? '0' : '1';
774 if (
775 $pma_table->isEngine(['MYISAM', 'ARIA'])
776 && $_POST['new_checksum'] !== $checksum
778 $table_alters[] = 'checksum = ' . $_POST['new_checksum'];
781 $_POST['new_transactional'] = empty($_POST['new_transactional']) ? '0' : '1';
782 if (
783 $pma_table->isEngine('ARIA')
784 && $_POST['new_transactional'] !== $transactional
786 $table_alters[] = 'TRANSACTIONAL = ' . $_POST['new_transactional'];
789 $_POST['new_page_checksum'] = empty($_POST['new_page_checksum']) ? '0' : '1';
790 if (
791 $pma_table->isEngine('ARIA')
792 && $_POST['new_page_checksum'] !== $page_checksum
794 $table_alters[] = 'PAGE_CHECKSUM = ' . $_POST['new_page_checksum'];
797 $_POST['new_delay_key_write'] = empty($_POST['new_delay_key_write']) ? '0' : '1';
798 if (
799 $pma_table->isEngine(['MYISAM', 'ARIA'])
800 && $_POST['new_delay_key_write'] !== $delay_key_write
802 $table_alters[] = 'delay_key_write = ' . $_POST['new_delay_key_write'];
805 if (
806 $pma_table->isEngine(['MYISAM', 'ARIA', 'INNODB', 'PBXT', 'ROCKSDB'])
807 && ! empty($_POST['new_auto_increment'])
808 && (! isset($auto_increment)
809 || $_POST['new_auto_increment'] !== $auto_increment)
810 && $_POST['new_auto_increment'] !== $_POST['hidden_auto_increment']
812 $table_alters[] = 'auto_increment = '
813 . $this->dbi->escapeString($_POST['new_auto_increment']);
816 if (! empty($_POST['new_row_format'])) {
817 $newRowFormat = $_POST['new_row_format'];
818 $newRowFormatLower = mb_strtolower($newRowFormat);
819 if (
820 $pma_table->isEngine(['MYISAM', 'ARIA', 'INNODB', 'PBXT'])
821 && (strlen($row_format) === 0
822 || $newRowFormatLower !== mb_strtolower($row_format))
824 $table_alters[] = 'ROW_FORMAT = '
825 . $this->dbi->escapeString($newRowFormat);
829 return $table_alters;
833 * Get warning messages array
835 * @return array
837 public function getWarningMessagesArray()
839 $warning_messages = [];
840 foreach ($this->dbi->getWarnings() as $warning) {
841 // In MariaDB 5.1.44, when altering a table from Maria to MyISAM
842 // and if TRANSACTIONAL was set, the system reports an error;
843 // I discussed with a Maria developer and he agrees that this
844 // should not be reported with a Level of Error, so here
845 // I just ignore it. But there are other 1478 messages
846 // that it's better to show.
847 if (
848 isset($_POST['new_tbl_storage_engine'])
849 && $_POST['new_tbl_storage_engine'] === 'MyISAM'
850 && $warning['Code'] == '1478'
851 && $warning['Level'] === 'Error'
853 continue;
856 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
857 . ' ' . $warning['Message'];
860 return $warning_messages;
864 * Adjust the privileges after renaming/moving a table
866 * @param string $oldDb Database name before table renaming/moving table
867 * @param string $oldTable Table name before table renaming/moving table
868 * @param string $newDb Database name after table renaming/ moving table
869 * @param string $newTable Table name after table renaming/moving table
871 * @return void
873 public function adjustPrivilegesRenameOrMoveTable($oldDb, $oldTable, $newDb, $newTable)
875 if (
876 ! $GLOBALS['table_priv'] || ! $GLOBALS['col_priv']
877 || ! $GLOBALS['is_reload_priv']
879 return;
882 $this->dbi->selectDb('mysql');
884 // For table specific privileges
885 $query_table_specific = 'UPDATE ' . Util::backquote('tables_priv')
886 . 'SET Db = \'' . $this->dbi->escapeString($newDb)
887 . '\', Table_name = \'' . $this->dbi->escapeString($newTable)
888 . '\' where Db = \'' . $this->dbi->escapeString($oldDb)
889 . '\' AND Table_name = \'' . $this->dbi->escapeString($oldTable)
890 . '\';';
891 $this->dbi->query($query_table_specific);
893 // For column specific privileges
894 $query_col_specific = 'UPDATE ' . Util::backquote('columns_priv')
895 . 'SET Db = \'' . $this->dbi->escapeString($newDb)
896 . '\', Table_name = \'' . $this->dbi->escapeString($newTable)
897 . '\' where Db = \'' . $this->dbi->escapeString($oldDb)
898 . '\' AND Table_name = \'' . $this->dbi->escapeString($oldTable)
899 . '\';';
900 $this->dbi->query($query_col_specific);
902 // Finally FLUSH the new privileges
903 $flush_query = 'FLUSH PRIVILEGES;';
904 $this->dbi->query($flush_query);
908 * Adjust the privileges after copying a table
910 * @param string $oldDb Database name before table copying
911 * @param string $oldTable Table name before table copying
912 * @param string $newDb Database name after table copying
913 * @param string $newTable Table name after table copying
915 * @return void
917 public function adjustPrivilegesCopyTable($oldDb, $oldTable, $newDb, $newTable)
919 if (
920 ! $GLOBALS['table_priv'] || ! $GLOBALS['col_priv']
921 || ! $GLOBALS['is_reload_priv']
923 return;
926 $this->dbi->selectDb('mysql');
928 // For Table Specific privileges
929 $query_table_specific_old = 'SELECT * FROM '
930 . Util::backquote('tables_priv') . ' where '
931 . 'Db = "' . $oldDb . '" AND Table_name = "' . $oldTable . '";';
933 $old_privs_table = $this->dbi->fetchResult(
934 $query_table_specific_old,
938 foreach ($old_privs_table as $old_priv) {
939 $newDb_table_privs_query = 'INSERT INTO '
940 . Util::backquote('tables_priv') . ' VALUES("'
941 . $old_priv[0] . '", "' . $newDb . '", "' . $old_priv[2] . '", "'
942 . $newTable . '", "' . $old_priv[4] . '", "' . $old_priv[5]
943 . '", "' . $old_priv[6] . '", "' . $old_priv[7] . '");';
945 $this->dbi->query($newDb_table_privs_query);
948 // For Column Specific privileges
949 $query_col_specific_old = 'SELECT * FROM '
950 . Util::backquote('columns_priv') . ' WHERE '
951 . 'Db = "' . $oldDb . '" AND Table_name = "' . $oldTable . '";';
953 $old_privs_col = $this->dbi->fetchResult(
954 $query_col_specific_old,
958 foreach ($old_privs_col as $old_priv) {
959 $newDb_col_privs_query = 'INSERT INTO '
960 . Util::backquote('columns_priv') . ' VALUES("'
961 . $old_priv[0] . '", "' . $newDb . '", "' . $old_priv[2] . '", "'
962 . $newTable . '", "' . $old_priv[4] . '", "' . $old_priv[5]
963 . '", "' . $old_priv[6] . '");';
965 $this->dbi->query($newDb_col_privs_query);
968 // Finally FLUSH the new privileges
969 $flush_query = 'FLUSH PRIVILEGES;';
970 $this->dbi->query($flush_query);
974 * Change all collations and character sets of all columns in table
976 * @param string $db Database name
977 * @param string $table Table name
978 * @param string $tbl_collation Collation Name
980 * @return void
982 public function changeAllColumnsCollation($db, $table, $tbl_collation)
984 $this->dbi->selectDb($db);
986 $change_all_collations_query = 'ALTER TABLE '
987 . Util::backquote($table)
988 . ' CONVERT TO';
990 [$charset] = explode('_', $tbl_collation);
992 $change_all_collations_query .= ' CHARACTER SET ' . $charset
993 . ($charset == $tbl_collation ? '' : ' COLLATE ' . $tbl_collation);
995 $this->dbi->query($change_all_collations_query);
999 * Move or copy a table
1001 * @param string $db current database name
1002 * @param string $table current table name
1004 public function moveOrCopyTable($db, $table): Message
1007 * Selects the database to work with
1009 $this->dbi->selectDb($db);
1012 * $_POST['target_db'] could be empty in case we came from an input field
1013 * (when there are many databases, no drop-down)
1015 if (empty($_POST['target_db'])) {
1016 $_POST['target_db'] = $db;
1020 * A target table name has been sent to this script -> do the work
1022 if (Core::isValid($_POST['new_name'])) {
1023 if ($db == $_POST['target_db'] && $table == $_POST['new_name']) {
1024 if (isset($_POST['submit_move'])) {
1025 $message = Message::error(__('Can\'t move table to same one!'));
1026 } else {
1027 $message = Message::error(__('Can\'t copy table to same one!'));
1029 } else {
1030 Table::moveCopy(
1031 $db,
1032 $table,
1033 $_POST['target_db'],
1034 $_POST['new_name'],
1035 $_POST['what'],
1036 isset($_POST['submit_move']),
1037 'one_table'
1040 if (
1041 isset($_POST['adjust_privileges'])
1042 && ! empty($_POST['adjust_privileges'])
1044 if (isset($_POST['submit_move'])) {
1045 $this->adjustPrivilegesRenameOrMoveTable(
1046 $db,
1047 $table,
1048 $_POST['target_db'],
1049 $_POST['new_name']
1051 } else {
1052 $this->adjustPrivilegesCopyTable(
1053 $db,
1054 $table,
1055 $_POST['target_db'],
1056 $_POST['new_name']
1060 if (isset($_POST['submit_move'])) {
1061 $message = Message::success(
1063 'Table %s has been moved to %s. Privileges have been '
1064 . 'adjusted.'
1067 } else {
1068 $message = Message::success(
1070 'Table %s has been copied to %s. Privileges have been '
1071 . 'adjusted.'
1075 } else {
1076 if (isset($_POST['submit_move'])) {
1077 $message = Message::success(
1078 __('Table %s has been moved to %s.')
1080 } else {
1081 $message = Message::success(
1082 __('Table %s has been copied to %s.')
1087 $old = Util::backquote($db) . '.'
1088 . Util::backquote($table);
1089 $message->addParam($old);
1091 $new_name = $_POST['new_name'];
1092 if ($this->dbi->getLowerCaseNames() === '1') {
1093 $new_name = strtolower($new_name);
1096 $GLOBALS['table'] = $new_name;
1098 $new = Util::backquote($_POST['target_db']) . '.'
1099 . Util::backquote($new_name);
1100 $message->addParam($new);
1102 } else {
1104 * No new name for the table!
1106 $message = Message::error(__('The table name is empty!'));
1109 return $message;