3 declare(strict_types
=1);
7 use PhpMyAdmin\Engines\Innodb
;
8 use PhpMyAdmin\Plugins\Export\ExportSql
;
10 use function array_merge
;
13 use function mb_strtolower
;
14 use function str_replace
;
16 use function strtolower
;
17 use function urldecode
;
20 * Set of functions with the operations section in phpMyAdmin
27 /** @var DatabaseInterface */
31 * @param DatabaseInterface $dbi DatabaseInterface object
32 * @param Relation $relation Relation object
34 public function __construct(DatabaseInterface
$dbi, Relation
$relation)
37 $this->relation
= $relation;
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
47 * @param string $db database name
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(
62 if ($tmp_query === null) {
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) {
78 foreach ($function_names as $function_name) {
79 $this->dbi
->selectDb($db);
80 $tmp_query = $this->dbi
->getDefinition(
85 if ($tmp_query === null) {
89 // collect for later display
90 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
91 $this->dbi
->selectDb($_POST['newname']);
92 $this->dbi
->query($tmp_query);
97 * Create database before copy
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'] ??
'');
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
139 public function getViewsAndCreateSqlViewStandIn(
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()) {
153 // If view exists, and 'add drop view' is selected: Drop it!
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(
174 $this->dbi
->selectDb($_POST['newname']);
175 $this->dbi
->query($sql_view_standin);
176 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
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)
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()) {
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') {
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, '');
230 ($this_what ??
'data'),
235 $GLOBALS['_error'] = true;
239 // apply the triggers to the destination db+table
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
250 ! isset($_POST['add_constraints'])
251 ||
empty($GLOBALS['sql_constraints_query'])
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
270 * @param string $db database name
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) {
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
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(
323 if (! $copying_succeeded) {
324 $GLOBALS['_error'] = true;
329 unset($_POST['drop_if_exists']);
331 if (! isset($temp_drop_if_exists)) {
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
347 public function adjustPrivilegesMoveDb($oldDb, $newname)
350 ! $GLOBALS['db_priv'] ||
! $GLOBALS['table_priv']
351 ||
! $GLOBALS['col_priv'] ||
! $GLOBALS['proc_priv']
352 ||
! $GLOBALS['is_reload_priv']
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
398 public function adjustPrivilegesCopyDb($oldDb, $newname)
401 ! $GLOBALS['db_priv'] ||
! $GLOBALS['table_priv']
402 ||
! $GLOBALS['col_priv'] ||
! $GLOBALS['proc_priv']
403 ||
! $GLOBALS['is_reload_priv']
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(
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(
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(
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
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
523 public function duplicateBookmarks($_error, $db)
525 if ($_error ||
$db == $_POST['newname']) {
534 $where_fields = ['dbase' => $db];
535 $new_fields = ['dbase' => $_POST['newname']];
536 Table
::duplicateInfo(
546 * Get array of possible row formats
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'],
559 'DYNAMIC' => 'DYNAMIC',
564 'DYNAMIC' => 'DYNAMIC',
569 'DYNAMIC' => 'DYNAMIC',
573 'DYNAMIC' => 'DYNAMIC',
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();
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.
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
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
626 $partitionMethod === 'RANGE'
627 ||
$partitionMethod === 'RANGE COLUMNS'
628 ||
$partitionMethod === 'LIST'
629 ||
$partitionMethod === 'LIST COLUMNS'
631 $choices['DROP'] = __('Drop');
633 $choices['COALESCE'] = __('Coalesce');
640 * @param array $urlParams Array of url parameters.
641 * @param bool $hasRelationFeature If relation feature is enabled.
645 public function getForeignersForReferentialIntegrityCheck(
651 if (! $hasRelationFeature) {
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)
664 . Util
::backquote($arr['foreign_db'])
666 . Util
::backquote($arr['foreign_table']);
668 if ($arr['foreign_table'] == $table) {
669 $foreignTable = $table . '1';
670 $joinQuery .= ' AS ' . Util
::backquote($foreignTable);
672 $foreignTable = $arr['foreign_table'];
676 . Util
::backquote($table) . '.'
677 . Util
::backquote($master)
679 . Util
::backquote($arr['foreign_db'])
681 . Util
::backquote($foreignTable) . '.'
682 . Util
::backquote($arr['foreign_field'])
684 . Util
::backquote($arr['foreign_db'])
686 . Util
::backquote($foreignTable) . '.'
687 . Util
::backquote($arr['foreign_field'])
689 . Util
::backquote($table) . '.'
690 . Util
::backquote($master)
692 $thisUrlParams = array_merge(
695 'sql_query' => $joinQuery,
696 'sql_signature' => Core
::signSqlQuery($joinQuery),
701 'params' => $thisUrlParams,
703 'db' => $arr['foreign_db'],
704 'table' => $arr['foreign_table'],
705 'field' => $arr['foreign_field'],
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
727 public function getTableAltersArray(
734 $newTblStorageEngine,
738 global $auto_increment;
743 isset($_POST['comment'])
744 && urldecode($_POST['prev_comment']) !== $_POST['comment']
746 $table_alters[] = 'COMMENT = \''
747 . $this->dbi
->escapeString($_POST['comment']) . '\'';
751 ! empty($newTblStorageEngine)
752 && mb_strtolower($newTblStorageEngine) !== mb_strtolower($GLOBALS['tbl_storage_engine'])
754 $table_alters[] = 'ENGINE = ' . $newTblStorageEngine;
758 ! empty($_POST['tbl_collation'])
759 && $_POST['tbl_collation'] !== $tbl_collation
761 $table_alters[] = 'DEFAULT '
762 . Util
::getCharsetQueryPart($_POST['tbl_collation'] ??
'');
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';
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';
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';
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';
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'];
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);
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
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.
848 isset($_POST['new_tbl_storage_engine'])
849 && $_POST['new_tbl_storage_engine'] === 'MyISAM'
850 && $warning['Code'] == '1478'
851 && $warning['Level'] === 'Error'
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
873 public function adjustPrivilegesRenameOrMoveTable($oldDb, $oldTable, $newDb, $newTable)
876 ! $GLOBALS['table_priv'] ||
! $GLOBALS['col_priv']
877 ||
! $GLOBALS['is_reload_priv']
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)
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)
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
917 public function adjustPrivilegesCopyTable($oldDb, $oldTable, $newDb, $newTable)
920 ! $GLOBALS['table_priv'] ||
! $GLOBALS['col_priv']
921 ||
! $GLOBALS['is_reload_priv']
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
982 public function changeAllColumnsCollation($db, $table, $tbl_collation)
984 $this->dbi
->selectDb($db);
986 $change_all_collations_query = 'ALTER TABLE '
987 . Util
::backquote($table)
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!'));
1027 $message = Message
::error(__('Can\'t copy table to same one!'));
1033 $_POST['target_db'],
1036 isset($_POST['submit_move']),
1041 isset($_POST['adjust_privileges'])
1042 && ! empty($_POST['adjust_privileges'])
1044 if (isset($_POST['submit_move'])) {
1045 $this->adjustPrivilegesRenameOrMoveTable(
1048 $_POST['target_db'],
1052 $this->adjustPrivilegesCopyTable(
1055 $_POST['target_db'],
1060 if (isset($_POST['submit_move'])) {
1061 $message = Message
::success(
1063 'Table %s has been moved to %s. Privileges have been '
1068 $message = Message
::success(
1070 'Table %s has been copied to %s. Privileges have been '
1076 if (isset($_POST['submit_move'])) {
1077 $message = Message
::success(
1078 __('Table %s has been moved to %s.')
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);
1104 * No new name for the table!
1106 $message = Message
::error(__('The table name is empty!'));