diff --git a/ChangeLog b/ChangeLog index cfa4abae5..9dcfc8c2c 100755 --- a/ChangeLog +++ b/ChangeLog @@ -7,8 +7,10 @@ $Source$ 2006-07-04 Marc Delisle * db_operations.php, tbl_properties_operations.php, - libraries/Table.class.php, libraries/export/sql.php: - a single-table copy did not copy the constraints + libraries/Table.class.php, libraries/export/sql.php, + tbl_move_copy.php: + a single-table copy did not copy the constraints; + also bug #1388048, contraints copied too early when copying a db 2006-07-04 Sebastian Mendel * js\querywindow.js, libraries\footer.inc.php: renamed JavaScript function diff --git a/db_operations.php b/db_operations.php index 6ec83fa79..77d1d7c58 100644 --- a/db_operations.php +++ b/db_operations.php @@ -46,6 +46,10 @@ if (isset($db) && PMA_DBI_query($local_query); } + if (isset($GLOBALS['add_constraints'])) { + $GLOBALS['sql_constraints_query_full_db'] = ''; + } + $tables_full = PMA_DBI_get_tables_full($db); foreach ($tables_full as $table => $tmp) { $back = $sql_query; @@ -70,13 +74,25 @@ if (isset($db) && if ($this_what != 'nocopy') { PMA_Table::moveCopy($db, $table, $newname, $table, - isset($this_what) ? $this_what : 'data', $move); + isset($this_what) ? $this_what : 'data', $move, 'db_copy'); + if (isset($GLOBALS['add_constraints'])) { + $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query']; + unset($GLOBALS['sql_constraints_query']); + } } $sql_query = $back . $sql_query; } unset($table); + // now that all tables exist, create all the accumulated constraints + if (isset($GLOBALS['add_constraints'])) { + PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']); + // and prepare to display them + $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db']; + unset($GLOBALS['sql_constraints_query_full_db']); + } + // Duplicate the bookmarks for this db (done once for each db) if ($db != $newname) { $get_fields = array('user', 'label', 'query'); diff --git a/libraries/Table.class.php b/libraries/Table.class.php index d7cde6f10..554db3243 100644 --- a/libraries/Table.class.php +++ b/libraries/Table.class.php @@ -505,7 +505,7 @@ class PMA_Table { * * @author Michal Cihar */ - function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move) + function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move, $mode) { global $dblist, $err_url; @@ -541,13 +541,14 @@ class PMA_Table { $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'])) { - // lem9: this is not a CREATE TABLE, so find the first VIEW + // 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') { @@ -597,13 +598,17 @@ class PMA_Table { $GLOBALS['sql_query'] .= "\n" . $sql_structure . ';'; if (($move || isset($GLOBALS['add_constraints'])) - && isset($GLOBALS['sql_constraints_query'])) { + && !empty($GLOBALS['sql_constraints_query'])) { $parsed_sql = PMA_SQP_parse($GLOBALS['sql_constraints_query']); $i = 0; // find the first quote_backtick, it must be the source table name while ($parsed_sql[$i]['type'] != 'quote_backtick') { $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() @@ -623,13 +628,16 @@ class PMA_Table { } } - // Generate query back $GLOBALS['sql_constraints_query'] = PMA_SQP_formatHtml($parsed_sql, 'query_only'); - PMA_DBI_query($GLOBALS['sql_constraints_query']); + if ($mode == 'one_table') { + PMA_DBI_query($GLOBALS['sql_constraints_query']); + } $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query']; - unset($GLOBALS['sql_constraints_query']); + if ($mode == 'one_table') { + unset($GLOBALS['sql_constraints_query']); + } } } else { diff --git a/tbl_move_copy.php b/tbl_move_copy.php index 3deb2f961..48f52af38 100644 --- a/tbl_move_copy.php +++ b/tbl_move_copy.php @@ -30,7 +30,7 @@ if (isset($new_name) && trim($new_name) != '') { if ($db == $target_db && $table == $new_name) { $message = (isset($submit_move) ? $strMoveTableSameNames : $strCopyTableSameNames); } else { - PMA_Table::moveCopy($db, $table, $target_db, $new_name, $what, isset($submit_move)); + PMA_Table::moveCopy($db, $table, $target_db, $new_name, $what, isset($submit_move), 'one_table'); $js_to_run = 'functions.js'; $message = (isset($submit_move) ? $strMoveTableOK : $strCopyTableOK); $message = sprintf($message, htmlspecialchars($table), htmlspecialchars($new_name));