/** * Duplicate the bookmarks for the db (done once for each db) * * @param boolean $_error whether table rename/copy or not * @param string $db database name * * @return void */ function PMA_duplicateBookmarks($_error, $db) { if (!$_error && $db != $_REQUEST['newname']) { $get_fields = array('user', 'label', 'query'); $where_fields = array('dbase' => $db); $new_fields = array('dbase' => $_REQUEST['newname']); PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields, $where_fields, $new_fields); } }
/** * Copies or renames table * @todo use RENAME for move operations * - would work only if the databases are on the same filesystem, * how can we check that? try the operation and * catch an error? * - for views, only if MYSQL > 50013 * - still have to handle pmadb synch. * * @author Michal Cihar <*****@*****.**> */ function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move, $mode) { global $err_url; // set export settings we need $GLOBALS['sql_backquotes'] = 1; $GLOBALS['asfile'] = 1; // Ensure the target is valid if (!$GLOBALS['PMA_List_Database']->exists($source_db, $target_db)) { /** * @todo exit really needed here? or just a return? */ exit; } $source = PMA_backquote($source_db) . '.' . PMA_backquote($source_table); if (!isset($target_db) || !strlen($target_db)) { $target_db = $source_db; } // Doing a select_db could avoid some problems with replicated databases, // when moving table from replicated one to not replicated one PMA_DBI_select_db($target_db); $target = PMA_backquote($target_db) . '.' . PMA_backquote($target_table); // do not create the table if dataonly if ($what != 'dataonly') { require_once './libraries/export/sql.php'; $no_constraints_comments = true; $GLOBALS['sql_constraints_query'] = ''; $sql_structure = PMA_getTableDef($source_db, $source_table, "\n", $err_url); unset($no_constraints_comments); $parsed_sql = PMA_SQP_parse($sql_structure); $analyzed_sql = PMA_SQP_analyze($parsed_sql); $i = 0; if (empty($analyzed_sql[0]['create_table_fields'])) { // this is not a CREATE TABLE, so find the first VIEW $target_for_view = PMA_backquote($target_db); while (true) { if ($parsed_sql[$i]['type'] == 'alpha_reservedWord' && $parsed_sql[$i]['data'] == 'VIEW') { break; } $i++; } } unset($analyzed_sql); $server_sql_mode = PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'sql_mode'", 0, 1); if ('ANSI_QUOTES' == $server_sql_mode) { $table_delimiter = 'quote_double'; } else { $table_delimiter = 'quote_backtick'; } unset($server_sql_mode); /* nijel: Find table name in query and replace it */ while ($parsed_sql[$i]['type'] != $table_delimiter) { $i++; } /* no need to PMA_backquote() */ if (isset($target_for_view)) { // this a view definition; we just found the first db name // that follows DEFINER VIEW // so change it for the new db name $parsed_sql[$i]['data'] = $target_for_view; // then we have to find all references to the source db // and change them to the target db, ensuring we stay into // the $parsed_sql limits $last = $parsed_sql['len'] - 1; $backquoted_source_db = PMA_backquote($source_db); for (++$i; $i <= $last; $i++) { if ($parsed_sql[$i]['type'] == $table_delimiter && $parsed_sql[$i]['data'] == $backquoted_source_db) { $parsed_sql[$i]['data'] = $target_for_view; } } unset($last, $backquoted_source_db); } else { $parsed_sql[$i]['data'] = $target; } /* Generate query back */ $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only'); // If table exists, and 'add drop table' is selected: Drop it! $drop_query = ''; if (isset($GLOBALS['drop_if_exists']) && $GLOBALS['drop_if_exists'] == 'true') { if (PMA_Table::_isView($target_db, $target_table)) { $drop_query = 'DROP VIEW'; } else { $drop_query = 'DROP TABLE'; } $drop_query .= ' IF EXISTS ' . PMA_backquote($target_db) . '.' . PMA_backquote($target_table); PMA_DBI_query($drop_query); $GLOBALS['sql_query'] .= "\n" . $drop_query . ';'; // garvin: If an existing table gets deleted, maintain any // entries for the PMA_* tables $maintain_relations = true; } @PMA_DBI_query($sql_structure); $GLOBALS['sql_query'] .= "\n" . $sql_structure . ';'; if (($move || isset($GLOBALS['add_constraints'])) && !empty($GLOBALS['sql_constraints_query'])) { $parsed_sql = PMA_SQP_parse($GLOBALS['sql_constraints_query']); $i = 0; // find the first $table_delimiter, it must be the source table name while ($parsed_sql[$i]['type'] != $table_delimiter) { $i++; // maybe someday we should guard against going over limit //if ($i == $parsed_sql['len']) { // break; //} } // replace it by the target table name, no need to PMA_backquote() $parsed_sql[$i]['data'] = $target; // now we must remove all $table_delimiter that follow a CONSTRAINT // keyword, because a constraint name must be unique in a db $cnt = $parsed_sql['len'] - 1; for ($j = $i; $j < $cnt; $j++) { if ($parsed_sql[$j]['type'] == 'alpha_reservedWord' && strtoupper($parsed_sql[$j]['data']) == 'CONSTRAINT') { if ($parsed_sql[$j + 1]['type'] == $table_delimiter) { $parsed_sql[$j + 1]['data'] = ''; } } } // Generate query back $GLOBALS['sql_constraints_query'] = PMA_SQP_formatHtml($parsed_sql, 'query_only'); if ($mode == 'one_table') { PMA_DBI_query($GLOBALS['sql_constraints_query']); } $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query']; if ($mode == 'one_table') { unset($GLOBALS['sql_constraints_query']); } } } else { $GLOBALS['sql_query'] = ''; } // Copy the data unless this is a VIEW if (($what == 'data' || $what == 'dataonly') && !PMA_Table::_isView($target_db, $target_table)) { $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source; PMA_DBI_query($sql_insert_data); $GLOBALS['sql_query'] .= "\n\n" . $sql_insert_data . ';'; } require_once './libraries/relation.lib.php'; $GLOBALS['cfgRelation'] = PMA_getRelationsParam(); // Drops old table if the user has requested to move it if ($move) { // This could avoid some problems with replicated databases, when // moving table from replicated one to not replicated one PMA_DBI_select_db($source_db); if (PMA_Table::_isView($source_db, $source_table)) { $sql_drop_query = 'DROP VIEW'; } else { $sql_drop_query = 'DROP TABLE'; } $sql_drop_query .= ' ' . $source; PMA_DBI_query($sql_drop_query); // garvin: Move old entries from PMA-DBs to new table if ($GLOBALS['cfgRelation']['commwork']) { $remove_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . ' SET table_name = \'' . PMA_sqlAddslashes($target_table) . '\', ' . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\'' . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\''; PMA_query_as_cu($remove_query); unset($remove_query); } // garvin: updating bookmarks is not possible since only a single table is moved, // and not the whole DB. if ($GLOBALS['cfgRelation']['displaywork']) { $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['table_info']) . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\', ' . ' table_name = \'' . PMA_sqlAddslashes($target_table) . '\'' . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\'' . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\''; PMA_query_as_cu($table_query); unset($table_query); } if ($GLOBALS['cfgRelation']['relwork']) { $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . ' SET foreign_table = \'' . PMA_sqlAddslashes($target_table) . '\',' . ' foreign_db = \'' . PMA_sqlAddslashes($target_db) . '\'' . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($source_db) . '\'' . ' AND foreign_table = \'' . PMA_sqlAddslashes($source_table) . '\''; PMA_query_as_cu($table_query); unset($table_query); $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . ' SET master_table = \'' . PMA_sqlAddslashes($target_table) . '\',' . ' master_db = \'' . PMA_sqlAddslashes($target_db) . '\'' . ' WHERE master_db = \'' . PMA_sqlAddslashes($source_db) . '\'' . ' AND master_table = \'' . PMA_sqlAddslashes($source_table) . '\''; PMA_query_as_cu($table_query); unset($table_query); } /** * @todo garvin: Can't get moving PDFs the right way. The page numbers * always get screwed up independently from duplication because the * numbers do not seem to be stored on a per-database basis. Would * the author of pdf support please have a look at it? */ if ($GLOBALS['cfgRelation']['pdfwork']) { $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['table_coords']) . ' SET table_name = \'' . PMA_sqlAddslashes($target_table) . '\',' . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\'' . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\''; PMA_query_as_cu($table_query); unset($table_query); /* $pdf_query = 'SELECT pdf_page_number ' . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['table_coords']) . ' WHERE db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' . ' AND table_name = \'' . PMA_sqlAddslashes($target_table) . '\''; $pdf_rs = PMA_query_as_cu($pdf_query); while ($pdf_copy_row = PMA_DBI_fetch_assoc($pdf_rs)) { $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['pdf_pages']) . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\'' . ' AND page_nr = \'' . PMA_sqlAddslashes($pdf_copy_row['pdf_page_number']) . '\''; $tb_rs = PMA_query_as_cu($table_query); unset($table_query); unset($tb_rs); } */ } if ($GLOBALS['cfgRelation']['designerwork']) { $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['designer_coords']) . ' SET table_name = \'' . PMA_sqlAddslashes($target_table) . '\',' . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\'' . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\''; PMA_query_as_cu($table_query); unset($table_query); } $GLOBALS['sql_query'] .= "\n\n" . $sql_drop_query . ';'; // end if ($move) } else { // we are copying // garvin: Create new entries as duplicates from old PMA DBs if ($what != 'dataonly' && !isset($maintain_relations)) { if ($GLOBALS['cfgRelation']['commwork']) { // Get all comments and MIME-Types for current table $comments_copy_query = 'SELECT column_name, ' . PMA_backquote('comment') . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\''; $comments_copy_rs = PMA_query_as_cu($comments_copy_query); // Write every comment as new copied entry. [MIME] while ($comments_copy_row = PMA_DBI_fetch_assoc($comments_copy_rs)) { $new_comment_query = 'REPLACE INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') ' . ' VALUES(' . '\'' . PMA_sqlAddslashes($target_db) . '\',' . '\'' . PMA_sqlAddslashes($target_table) . '\',' . '\'' . PMA_sqlAddslashes($comments_copy_row['column_name']) . '\'' . ($GLOBALS['cfgRelation']['mimework'] ? ',\'' . PMA_sqlAddslashes($comments_copy_row['comment']) . '\',' . '\'' . PMA_sqlAddslashes($comments_copy_row['mimetype']) . '\',' . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation']) . '\',' . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation_options']) . '\'' : '') . ')'; PMA_query_as_cu($new_comment_query); } // end while PMA_DBI_free_result($comments_copy_rs); unset($comments_copy_rs); } // duplicating the bookmarks must not be done here, but // just once per db $get_fields = array('display_field'); $where_fields = array('db_name' => $source_db, 'table_name' => $source_table); $new_fields = array('db_name' => $target_db, 'table_name' => $target_table); PMA_Table::duplicateInfo('displaywork', 'table_info', $get_fields, $where_fields, $new_fields); /** * @todo revise this code when we support cross-db relations */ $get_fields = array('master_field', 'foreign_table', 'foreign_field'); $where_fields = array('master_db' => $source_db, 'master_table' => $source_table); $new_fields = array('master_db' => $target_db, 'foreign_db' => $target_db, 'master_table' => $target_table); PMA_Table::duplicateInfo('relwork', 'relation', $get_fields, $where_fields, $new_fields); $get_fields = array('foreign_field', 'master_table', 'master_field'); $where_fields = array('foreign_db' => $source_db, 'foreign_table' => $source_table); $new_fields = array('master_db' => $target_db, 'foreign_db' => $target_db, 'foreign_table' => $target_table); PMA_Table::duplicateInfo('relwork', 'relation', $get_fields, $where_fields, $new_fields); $get_fields = array('x', 'y', 'v', 'h'); $where_fields = array('db_name' => $source_db, 'table_name' => $source_table); $new_fields = array('db_name' => $target_db, 'table_name' => $target_table); PMA_Table::duplicateInfo('designerwork', 'designer_coords', $get_fields, $where_fields, $new_fields); /** * @todo garvin: Can't get duplicating PDFs the right way. The * page numbers always get screwed up independently from * duplication because the numbers do not seem to be stored on a * per-database basis. Would the author of pdf support please * have a look at it? * $get_fields = array('page_descr'); $where_fields = array('db_name' => $source_db); $new_fields = array('db_name' => $target_db); $last_id = PMA_Table::duplicateInfo('pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields); if (isset($last_id) && $last_id >= 0) { $get_fields = array('x', 'y'); $where_fields = array('db_name' => $source_db, 'table_name' => $source_table); $new_fields = array('db_name' => $target_db, 'table_name' => $target_table, 'pdf_page_number' => $last_id); PMA_Table::duplicateInfo('pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields); } */ } } }
$tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name); // collect for later display $GLOBALS['sql_query'] .= "\n" . $tmp_query; PMA_DBI_select_db($newname); PMA_DBI_query($tmp_query); } } } // go back to current db, just in case PMA_DBI_select_db($db); // Duplicate the bookmarks for this db (done once for each db) if (!$_error && $db != $newname) { $get_fields = array('user', 'label', 'query'); $where_fields = array('dbase' => $db); $new_fields = array('dbase' => $newname); PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields, $where_fields, $new_fields); } if (!$_error && $move) { /** * cleanup pmadb stuff for this db */ require_once './libraries/relation_cleanup.lib.php'; PMA_relationsCleanupDatabase($db); // if someday the RENAME DATABASE reappears, do not DROP $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';'; $sql_query .= "\n" . $local_query; PMA_DBI_query($local_query); $message = PMA_Message::success('strRenameDatabaseOK'); $message->addParam($db); $message->addParam($newname); } elseif (!$_error) {
/** * Test for duplicateInfo * * @return void */ public function testDuplicateInfo() { $work = "PMA_work"; $pma_table = "pma_table"; $get_fields = array("filed0", "field6"); $where_fields = array("field2", "filed5"); $new_fields = array("field3", "filed4"); $GLOBALS['cfgRelation'][$work] = true; $GLOBALS['cfgRelation']['db'] = "PMA_db"; $GLOBALS['cfgRelation'][$pma_table] = "pma_table"; $ret = PMA_Table::duplicateInfo($work, $pma_table, $get_fields, $where_fields, $new_fields); $this->assertEquals(true, $ret); }
/** * Copies or renames table * * @param string $source_db source database * @param string $source_table source table * @param string $target_db target database * @param string $target_table target table * @param string $what what to be moved or copied (data, dataonly) * @param bool $move whether to move * @param string $mode mode * * @return bool true if success, false otherwise */ public static function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move, $mode) { global $err_url; /* Try moving table directly */ if ($move && $what == 'data') { $tbl = new PMA_Table($source_table, $source_db); $result = $tbl->rename($target_table, $target_db); if ($result) { $GLOBALS['message'] = $tbl->getLastMessage(); return true; } } // set export settings we need $GLOBALS['sql_backquotes'] = 1; $GLOBALS['asfile'] = 1; // Ensure the target is valid if (!$GLOBALS['pma']->databases->exists($source_db, $target_db)) { if (!$GLOBALS['pma']->databases->exists($source_db)) { $GLOBALS['message'] = PMA_Message::rawError(sprintf(__('Source database `%s` was not found!'), htmlspecialchars($source_db))); } if (!$GLOBALS['pma']->databases->exists($target_db)) { $GLOBALS['message'] = PMA_Message::rawError(sprintf(__('Target database `%s` was not found!'), htmlspecialchars($target_db))); } return false; } $source = PMA_Util::backquote($source_db) . '.' . PMA_Util::backquote($source_table); if (!isset($target_db) || !strlen($target_db)) { $target_db = $source_db; } // Doing a select_db could avoid some problems with replicated databases, // when moving table from replicated one to not replicated one PMA_DBI_select_db($target_db); $target = PMA_Util::backquote($target_db) . '.' . PMA_Util::backquote($target_table); // do not create the table if dataonly if ($what != 'dataonly') { include_once "libraries/plugin_interface.lib.php"; // get Export SQL instance $export_sql_plugin = PMA_getPlugin("export", "sql", 'libraries/plugins/export/', array('export_type' => 'table', 'single_table' => isset($single_table))); $no_constraints_comments = true; $GLOBALS['sql_constraints_query'] = ''; $sql_structure = $export_sql_plugin->getTableDef($source_db, $source_table, "\n", $err_url, false, false); unset($no_constraints_comments); $parsed_sql = PMA_SQP_parse($sql_structure); $analyzed_sql = PMA_SQP_analyze($parsed_sql); $i = 0; if (empty($analyzed_sql[0]['create_table_fields'])) { // this is not a CREATE TABLE, so find the first VIEW $target_for_view = PMA_Util::backquote($target_db); while (true) { if ($parsed_sql[$i]['type'] == 'alpha_reservedWord' && $parsed_sql[$i]['data'] == 'VIEW') { break; } $i++; } } unset($analyzed_sql); if (PMA_DRIZZLE) { $table_delimiter = 'quote_backtick'; } else { $server_sql_mode = PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'sql_mode'", 0, 1); // ANSI_QUOTES might be a subset of sql_mode, for example // REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ANSI if (false !== strpos($server_sql_mode, 'ANSI_QUOTES')) { $table_delimiter = 'quote_double'; } else { $table_delimiter = 'quote_backtick'; } unset($server_sql_mode); } /* Find table name in query and replace it */ while ($parsed_sql[$i]['type'] != $table_delimiter) { $i++; } /* no need to backquote() */ if (isset($target_for_view)) { // this a view definition; we just found the first db name // that follows DEFINER VIEW // so change it for the new db name $parsed_sql[$i]['data'] = $target_for_view; // then we have to find all references to the source db // and change them to the target db, ensuring we stay into // the $parsed_sql limits $last = $parsed_sql['len'] - 1; $backquoted_source_db = PMA_Util::backquote($source_db); for (++$i; $i <= $last; $i++) { if ($parsed_sql[$i]['type'] == $table_delimiter && $parsed_sql[$i]['data'] == $backquoted_source_db && $parsed_sql[$i - 1]['type'] != 'punct_qualifier') { $parsed_sql[$i]['data'] = $target_for_view; } } unset($last, $backquoted_source_db); } else { $parsed_sql[$i]['data'] = $target; } /* Generate query back */ $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only'); // If table exists, and 'add drop table' is selected: Drop it! $drop_query = ''; if (isset($_REQUEST['drop_if_exists']) && $_REQUEST['drop_if_exists'] == 'true') { if (PMA_Table::isView($target_db, $target_table)) { $drop_query = 'DROP VIEW'; } else { $drop_query = 'DROP TABLE'; } $drop_query .= ' IF EXISTS ' . PMA_Util::backquote($target_db) . '.' . PMA_Util::backquote($target_table); PMA_DBI_query($drop_query); $GLOBALS['sql_query'] .= "\n" . $drop_query . ';'; // If an existing table gets deleted, maintain any // entries for the PMA_* tables $maintain_relations = true; } @PMA_DBI_query($sql_structure); $GLOBALS['sql_query'] .= "\n" . $sql_structure . ';'; if (($move || isset($GLOBALS['add_constraints'])) && !empty($GLOBALS['sql_constraints_query'])) { $parsed_sql = PMA_SQP_parse($GLOBALS['sql_constraints_query']); $i = 0; // find the first $table_delimiter, it must be the source // table name while ($parsed_sql[$i]['type'] != $table_delimiter) { $i++; // maybe someday we should guard against going over limit //if ($i == $parsed_sql['len']) { // break; //} } // replace it by the target table name, no need // to backquote() $parsed_sql[$i]['data'] = $target; // now we must remove all $table_delimiter that follow a // CONSTRAINT keyword, because a constraint name must be // unique in a db $cnt = $parsed_sql['len'] - 1; for ($j = $i; $j < $cnt; $j++) { if ($parsed_sql[$j]['type'] == 'alpha_reservedWord' && strtoupper($parsed_sql[$j]['data']) == 'CONSTRAINT') { if ($parsed_sql[$j + 1]['type'] == $table_delimiter) { $parsed_sql[$j + 1]['data'] = ''; } } } // Generate query back $GLOBALS['sql_constraints_query'] = PMA_SQP_formatHtml($parsed_sql, 'query_only'); if ($mode == 'one_table') { PMA_DBI_query($GLOBALS['sql_constraints_query']); } $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query']; if ($mode == 'one_table') { unset($GLOBALS['sql_constraints_query']); } } } else { $GLOBALS['sql_query'] = ''; } // Copy the data unless this is a VIEW if (($what == 'data' || $what == 'dataonly') && !PMA_Table::isView($target_db, $target_table)) { $sql_set_mode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'"; PMA_DBI_query($sql_set_mode); $GLOBALS['sql_query'] .= "\n\n" . $sql_set_mode . ';'; $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source; PMA_DBI_query($sql_insert_data); $GLOBALS['sql_query'] .= "\n\n" . $sql_insert_data . ';'; } $GLOBALS['cfgRelation'] = PMA_getRelationsParam(); // Drops old table if the user has requested to move it if ($move) { // This could avoid some problems with replicated databases, when // moving table from replicated one to not replicated one PMA_DBI_select_db($source_db); if (PMA_Table::isView($source_db, $source_table)) { $sql_drop_query = 'DROP VIEW'; } else { $sql_drop_query = 'DROP TABLE'; } $sql_drop_query .= ' ' . $source; PMA_DBI_query($sql_drop_query); // Renable table in configuration storage PMA_REL_renameTable($source_db, $target_db, $source_table, $target_table); $GLOBALS['sql_query'] .= "\n\n" . $sql_drop_query . ';'; // end if ($move) } else { // we are copying // Create new entries as duplicates from old PMA DBs if ($what != 'dataonly' && !isset($maintain_relations)) { if ($GLOBALS['cfgRelation']['commwork']) { // Get all comments and MIME-Types for current table $comments_copy_query = 'SELECT column_name, comment' . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ' FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($GLOBALS['cfgRelation']['column_info']) . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($source_db) . '\' AND table_name = \'' . PMA_Util::sqlAddSlashes($source_table) . '\''; $comments_copy_rs = PMA_queryAsControlUser($comments_copy_query); // Write every comment as new copied entry. [MIME] while ($comments_copy_row = PMA_DBI_fetch_assoc($comments_copy_rs)) { $new_comment_query = 'REPLACE INTO ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($GLOBALS['cfgRelation']['column_info']) . ' (db_name, table_name, column_name, comment' . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') ' . ' VALUES(' . '\'' . PMA_Util::sqlAddSlashes($target_db) . '\',' . '\'' . PMA_Util::sqlAddSlashes($target_table) . '\',' . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['column_name']) . '\'' . ($GLOBALS['cfgRelation']['mimework'] ? ',\'' . PMA_Util::sqlAddSlashes($comments_copy_row['comment']) . '\',' . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['mimetype']) . '\',' . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['transformation']) . '\',' . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['transformation_options']) . '\'' : '') . ')'; PMA_queryAsControlUser($new_comment_query); } // end while PMA_DBI_free_result($comments_copy_rs); unset($comments_copy_rs); } // duplicating the bookmarks must not be done here, but // just once per db $get_fields = array('display_field'); $where_fields = array('db_name' => $source_db, 'table_name' => $source_table); $new_fields = array('db_name' => $target_db, 'table_name' => $target_table); PMA_Table::duplicateInfo('displaywork', 'table_info', $get_fields, $where_fields, $new_fields); /** * @todo revise this code when we support cross-db relations */ $get_fields = array('master_field', 'foreign_table', 'foreign_field'); $where_fields = array('master_db' => $source_db, 'master_table' => $source_table); $new_fields = array('master_db' => $target_db, 'foreign_db' => $target_db, 'master_table' => $target_table); PMA_Table::duplicateInfo('relwork', 'relation', $get_fields, $where_fields, $new_fields); $get_fields = array('foreign_field', 'master_table', 'master_field'); $where_fields = array('foreign_db' => $source_db, 'foreign_table' => $source_table); $new_fields = array('master_db' => $target_db, 'foreign_db' => $target_db, 'foreign_table' => $target_table); PMA_Table::duplicateInfo('relwork', 'relation', $get_fields, $where_fields, $new_fields); $get_fields = array('x', 'y', 'v', 'h'); $where_fields = array('db_name' => $source_db, 'table_name' => $source_table); $new_fields = array('db_name' => $target_db, 'table_name' => $target_table); PMA_Table::duplicateInfo('designerwork', 'designer_coords', $get_fields, $where_fields, $new_fields); /** * @todo Can't get duplicating PDFs the right way. The * page numbers always get screwed up independently from * duplication because the numbers do not seem to be stored on a * per-database basis. Would the author of pdf support please * have a look at it? * $get_fields = array('page_descr'); $where_fields = array('db_name' => $source_db); $new_fields = array('db_name' => $target_db); $last_id = PMA_Table::duplicateInfo( 'pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields ); if (isset($last_id) && $last_id >= 0) { $get_fields = array('x', 'y'); $where_fields = array( 'db_name' => $source_db, 'table_name' => $source_table ); $new_fields = array( 'db_name' => $target_db, 'table_name' => $target_table, 'pdf_page_number' => $last_id ); PMA_Table::duplicateInfo( 'pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields ); } */ } } return true; }
/** * Copies or renames table * * @param string $source_db source database * @param string $source_table source table * @param string $target_db target database * @param string $target_table target table * @param string $what what to be moved or copied (data, dataonly) * @param bool $move whether to move * @param string $mode mode * * @return bool true if success, false otherwise */ public static function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move, $mode) { global $err_url; // Try moving the tables directly, using native `RENAME` statement. if ($move && $what == 'data') { $tbl = new PMA_Table($source_table, $source_db); if ($tbl->rename($target_table, $target_db)) { $GLOBALS['message'] = $tbl->getLastMessage(); return true; } } // Setting required export settings. $GLOBALS['sql_backquotes'] = 1; $GLOBALS['asfile'] = 1; // Ensuring the target database is valid. if (!$GLOBALS['pma']->databases->exists($source_db, $target_db)) { if (!$GLOBALS['pma']->databases->exists($source_db)) { $GLOBALS['message'] = PMA_Message::rawError(sprintf(__('Source database `%s` was not found!'), htmlspecialchars($source_db))); } if (!$GLOBALS['pma']->databases->exists($target_db)) { $GLOBALS['message'] = PMA_Message::rawError(sprintf(__('Target database `%s` was not found!'), htmlspecialchars($target_db))); } return false; } /** * The full name of source table, quoted. * @var string $source */ $source = PMA_Util::backquote($source_db) . '.' . PMA_Util::backquote($source_table); // If the target database is not specified, the operation is taking // place in the same database. if (!isset($target_db) || !mb_strlen($target_db)) { $target_db = $source_db; } // Selecting the database could avoid some problems with replicated // databases, when moving table from replicated one to not replicated one. $GLOBALS['dbi']->selectDb($target_db); /** * The full name of target table, quoted. * @var string $target */ $target = PMA_Util::backquote($target_db) . '.' . PMA_Util::backquote($target_table); // No table is created when this is a data-only operation. if ($what != 'dataonly') { include_once "libraries/plugin_interface.lib.php"; /** * Instance used for exporting the current structure of the table. * * @var ExportSql */ $export_sql_plugin = PMA_getPlugin("export", "sql", 'libraries/plugins/export/', array('export_type' => 'table', 'single_table' => false)); $no_constraints_comments = true; $GLOBALS['sql_constraints_query'] = ''; // set the value of global sql_auto_increment variable if (isset($_POST['sql_auto_increment'])) { $GLOBALS['sql_auto_increment'] = $_POST['sql_auto_increment']; } /** * The old structure of the table.. * @var string $sql_structure */ $sql_structure = $export_sql_plugin->getTableDef($source_db, $source_table, "\n", $err_url, false, false); unset($no_constraints_comments); // ----------------------------------------------------------------- // Phase 0: Preparing structures used. /** * The destination where the table is moved or copied to. * @var SqlParser\Components\Expression */ $destination = new SqlParser\Components\Expression($target_db, $target_table, ''); // Find server's SQL mode so the builder can generate correct // queries. // One of the options that alters the behaviour is `ANSI_QUOTES`. // This is not availabile for Drizzle. if (!PMA_DRIZZLE) { SqlParser\Context::setMode($GLOBALS['dbi']->fetchValue("SELECT @@sql_mode")); } // ----------------------------------------------------------------- // Phase 1: Dropping existent element of the same name (if exists // and required). if (isset($_REQUEST['drop_if_exists']) && $_REQUEST['drop_if_exists'] == 'true') { /** * Drop statement used for building the query. * @var SqlParser\Statements\DropStatement $statement */ $statement = new SqlParser\Statements\DropStatement(); $tbl = new PMA_Table($target_db, $target_table); $statement->options = new SqlParser\Components\OptionsArray(array($tbl->isView() ? 'VIEW' : 'TABLE', 'IF EXISTS')); $statement->fields = array($destination); // Building the query. $drop_query = $statement->build() . ';'; // Executing it. $GLOBALS['dbi']->query($drop_query); $GLOBALS['sql_query'] .= "\n" . $drop_query; // If an existing table gets deleted, maintain any entries for // the PMA_* tables. $maintain_relations = true; } // ----------------------------------------------------------------- // Phase 2: Generating the new query of this structure. /** * The parser responsible for parsing the old queries. * @var SqlParser\Parser $parser */ $parser = new SqlParser\Parser($sql_structure); if (!empty($parser->statements[0])) { /** * The CREATE statement of this structure. * @var SqlParser\Statements\CreateStatement $statement */ $statement = $parser->statements[0]; // Changing the destination. $statement->name = $destination; // Building back the query. $sql_structure = $statement->build() . ';'; // Executing it. $GLOBALS['dbi']->query($sql_structure); $GLOBALS['sql_query'] .= "\n" . $sql_structure; } // ----------------------------------------------------------------- // Phase 3: Adding constraints. // All constraint names are removed because they must be unique. if (($move || isset($GLOBALS['add_constraints'])) && !empty($GLOBALS['sql_constraints_query'])) { $parser = new SqlParser\Parser($GLOBALS['sql_constraints_query']); /** * The ALTER statement that generates the constraints. * @var SqlParser\Statements\AlterStatement $statement */ $statement = $parser->statements[0]; // Changing the altered table to the destination. $statement->table = $destination; // Removing the name of the constraints. foreach ($statement->altered as $idx => $altered) { // All constraint names are removed because they must be unique. if ($altered->options->has('CONSTRAINT')) { $altered->field = null; } } // Building back the query. $GLOBALS['sql_constraints_query'] = $statement->build() . ';'; // Executing it. if ($mode == 'one_table') { $GLOBALS['dbi']->query($GLOBALS['sql_constraints_query']); } $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query']; if ($mode == 'one_table') { unset($GLOBALS['sql_constraints_query']); } } // ----------------------------------------------------------------- // Phase 4: Adding indexes. // View phase 3. if (!empty($GLOBALS['sql_indexes'])) { $parser = new SqlParser\Parser($GLOBALS['sql_indexes']); /** * The ALTER statement that generates the indexes. * @var SqlParser\Statements\AlterStatement $statement */ $statement = $parser->statements[0]; // Changing the altered table to the destination. $statement->table = $destination; // Removing the name of the constraints. foreach ($statement->altered as $idx => $altered) { // All constraint names are removed because they must be unique. if ($altered->options->has('CONSTRAINT')) { $altered->field = null; } } // Building back the query. $GLOBALS['sql_indexes'] = $statement->build() . ';'; // Executing it. if ($mode == 'one_table' || $mode == 'db_copy') { $GLOBALS['dbi']->query($GLOBALS['sql_indexes']); } $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_indexes']; if ($mode == 'one_table' || $mode == 'db_copy') { unset($GLOBALS['sql_indexes']); } } // ----------------------------------------------------------------- // Phase 5: Adding AUTO_INCREMENT. if (!empty($GLOBALS['sql_auto_increments'])) { if ($mode == 'one_table' || $mode == 'db_copy') { $parser = new SqlParser\Parser($GLOBALS['sql_auto_increments']); /** * The ALTER statement that alters the AUTO_INCREMENT value. * @var SqlParser\Statements\AlterStatement $statement */ $statement = $parser->statements[0]; // Changing the altered table to the destination. $statement->table = $destination; // Building back the query. $GLOBALS['sql_auto_increments'] = $statement->build() . ';'; // Executing it. $GLOBALS['dbi']->query($GLOBALS['sql_auto_increments']); $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_auto_increments']; unset($GLOBALS['sql_auto_increments']); } } } else { $GLOBALS['sql_query'] = ''; } $_table = new PMA_Table($target_table, $target_db); // Copy the data unless this is a VIEW if (($what == 'data' || $what == 'dataonly') && !$_table->isView()) { if (!PMA_DRIZZLE) { $sql_set_mode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'"; $GLOBALS['dbi']->query($sql_set_mode); $GLOBALS['sql_query'] .= "\n\n" . $sql_set_mode . ';'; } $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source; $GLOBALS['dbi']->query($sql_insert_data); $GLOBALS['sql_query'] .= "\n\n" . $sql_insert_data . ';'; } PMA_getRelationsParam(); // Drops old table if the user has requested to move it if ($move) { // This could avoid some problems with replicated databases, when // moving table from replicated one to not replicated one $GLOBALS['dbi']->selectDb($source_db); $_source_table = new PMA_Table($source_table, $source_db); if ($_source_table->isView()) { $sql_drop_query = 'DROP VIEW'; } else { $sql_drop_query = 'DROP TABLE'; } $sql_drop_query .= ' ' . $source; $GLOBALS['dbi']->query($sql_drop_query); // Renable table in configuration storage PMA_REL_renameTable($source_db, $target_db, $source_table, $target_table); $GLOBALS['sql_query'] .= "\n\n" . $sql_drop_query . ';'; // end if ($move) return true; } // we are copying // Create new entries as duplicates from old PMA DBs if ($what == 'dataonly' || isset($maintain_relations)) { return true; } if ($GLOBALS['cfgRelation']['commwork']) { // Get all comments and MIME-Types for current table $comments_copy_rs = PMA_queryAsControlUser('SELECT column_name, comment' . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ' FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($GLOBALS['cfgRelation']['column_info']) . ' WHERE ' . ' db_name = \'' . PMA_Util::sqlAddSlashes($source_db) . '\'' . ' AND ' . ' table_name = \'' . PMA_Util::sqlAddSlashes($source_table) . '\''); // Write every comment as new copied entry. [MIME] while ($comments_copy_row = $GLOBALS['dbi']->fetchAssoc($comments_copy_rs)) { $new_comment_query = 'REPLACE INTO ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($GLOBALS['cfgRelation']['column_info']) . ' (db_name, table_name, column_name, comment' . ($GLOBALS['cfgRelation']['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') ' . ' VALUES(' . '\'' . PMA_Util::sqlAddSlashes($target_db) . '\',\'' . PMA_Util::sqlAddSlashes($target_table) . '\',\'' . PMA_Util::sqlAddSlashes($comments_copy_row['column_name']) . '\'' . ($GLOBALS['cfgRelation']['mimework'] ? ',\'' . PMA_Util::sqlAddSlashes($comments_copy_row['comment']) . '\',' . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['mimetype']) . '\',' . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['transformation']) . '\',' . '\'' . PMA_Util::sqlAddSlashes($comments_copy_row['transformation_options']) . '\'' : '') . ')'; PMA_queryAsControlUser($new_comment_query); } // end while $GLOBALS['dbi']->freeResult($comments_copy_rs); unset($comments_copy_rs); } // duplicating the bookmarks must not be done here, but // just once per db $get_fields = array('display_field'); $where_fields = array('db_name' => $source_db, 'table_name' => $source_table); $new_fields = array('db_name' => $target_db, 'table_name' => $target_table); PMA_Table::duplicateInfo('displaywork', 'table_info', $get_fields, $where_fields, $new_fields); /** * @todo revise this code when we support cross-db relations */ $get_fields = array('master_field', 'foreign_table', 'foreign_field'); $where_fields = array('master_db' => $source_db, 'master_table' => $source_table); $new_fields = array('master_db' => $target_db, 'foreign_db' => $target_db, 'master_table' => $target_table); PMA_Table::duplicateInfo('relwork', 'relation', $get_fields, $where_fields, $new_fields); $get_fields = array('foreign_field', 'master_table', 'master_field'); $where_fields = array('foreign_db' => $source_db, 'foreign_table' => $source_table); $new_fields = array('master_db' => $target_db, 'foreign_db' => $target_db, 'foreign_table' => $target_table); PMA_Table::duplicateInfo('relwork', 'relation', $get_fields, $where_fields, $new_fields); /** * @todo Can't get duplicating PDFs the right way. The * page numbers always get screwed up independently from * duplication because the numbers do not seem to be stored on a * per-database basis. Would the author of pdf support please * have a look at it? * $get_fields = array('page_descr'); $where_fields = array('db_name' => $source_db); $new_fields = array('db_name' => $target_db); $last_id = PMA_Table::duplicateInfo( 'pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields ); if (isset($last_id) && $last_id >= 0) { $get_fields = array('x', 'y'); $where_fields = array( 'db_name' => $source_db, 'table_name' => $source_table ); $new_fields = array( 'db_name' => $target_db, 'table_name' => $target_table, 'pdf_page_number' => $last_id ); PMA_Table::duplicateInfo( 'pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields ); } */ return true; }