From 458aa0e34b3f4f6f5ccac6b03502e88864a2a18b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 18 Sep 2005 21:43:28 +0200 Subject: [PATCH] Changes after discussion/review with Sanja sql/parse_file.cc: Fix after discussion/review with Sanja: removed rename_view from sql_view to general function rename_in_schema_file (so it can be used for other .frm types like triggers too) sql/parse_file.h: added prototype for rename_in_schema_file sql/sql_rename.cc: simplified code (thanks to Sanja) sql/sql_view.cc: moved rename_view_files to rename_in_schema_file (parse_file.cc) corrected MYF parameter from MY_WME to 0. --- sql/parse_file.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++ sql/parse_file.h | 3 +++ sql/sql_rename.cc | 50 +++++++++++++++++++++---------------------- sql/sql_view.cc | 54 +++++++++++++++-------------------------------- 4 files changed, 98 insertions(+), 62 deletions(-) diff --git a/sql/parse_file.cc b/sql/parse_file.cc index 82ce2f2d7b5..5c7053e6e2a 100644 --- a/sql/parse_file.cc +++ b/sql/parse_file.cc @@ -333,6 +333,59 @@ err_w_file: DBUG_RETURN(TRUE); } +/* + Renames a frm file (including backups) in same schema + + SYNOPSIS + rename_in_schema_file + schema name of given schema + old_name original file name + new_name new file name + revision revision number + num_view_backups number of backups + + RETURN + 0 - OK + 1 - Error (only if renaming of frm failed) + +*/ +my_bool rename_in_schema_file(const char *schema, const char *old_name, + const char *new_name, ulonglong revision, + uint num_view_backups) +{ + char old_path[FN_REFLEN], new_path[FN_REFLEN], arc_path[FN_REFLEN]; + + strxnmov(old_path, FN_REFLEN, mysql_data_home, "/", schema, "/", + old_name, reg_ext, NullS); + (void) unpack_filename(old_path, old_path); + + strxnmov(new_path, FN_REFLEN, mysql_data_home, "/", schema, "/", + new_name, reg_ext, NullS); + (void) unpack_filename(new_path, new_path); + + if (my_rename(old_path, new_path, MYF(MY_WME))) + return 1; + + /* check if arc_dir exists */ + strxnmov(arc_path, FN_REFLEN, mysql_data_home, "/", schema, "/arc", NullS); + (void) unpack_filename(arc_path, arc_path); + + if (revision > 0 && !access(arc_path, F_OK)) + { + ulonglong limit= (revision > num_view_backups) ? revision - num_view_backups : 0; + while (revision > limit) { + my_snprintf(old_path, FN_REFLEN, "%s/%s%s-%04lu", + arc_path, old_name, reg_ext, (ulong)revision); + (void) unpack_filename(old_path, old_path); + my_snprintf(new_path, FN_REFLEN, "%s/%s%s-%04lu", + arc_path, new_name, reg_ext, (ulong)revision); + (void) unpack_filename(new_path, new_path); + my_rename(old_path, new_path, MYF(0)); + revision--; + } + } + return 0; +} /* Prepare frm to parse (read to memory) diff --git a/sql/parse_file.h b/sql/parse_file.h index cc0aa6556f6..b4199e4fbf1 100644 --- a/sql/parse_file.h +++ b/sql/parse_file.h @@ -48,6 +48,9 @@ my_bool sql_create_definition_file(const LEX_STRING *dir, const LEX_STRING *file_name, const LEX_STRING *type, gptr base, File_option *parameters, uint versions); +my_bool rename_in_schema_file(const char *schema, const char *old_name, + const char *new_name, ulonglong revision, + uint num_view_backups); class File_parser: public Sql_alloc { diff --git a/sql/sql_rename.cc b/sql/sql_rename.cc index b848809ccc9..154e828b47e 100644 --- a/sql/sql_rename.cc +++ b/sql/sql_rename.cc @@ -138,7 +138,7 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error) for (ren_table= table_list; ren_table; ren_table= new_table->next_local) { - db_type table_type; + int rc= 1; char name[FN_REFLEN]; const char *new_alias, *old_alias; @@ -165,36 +165,36 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error) ren_table->db, old_alias, reg_ext); unpack_filename(name, name); - if ((frm_type= mysql_frm_type(name)) == FRMTYPE_TABLE && - (table_type= get_table_type(thd, name)) == DB_TYPE_UNKNOWN) + + frm_type= mysql_frm_type(name); + switch (frm_type) { - my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno); - if (!skip_error) - DBUG_RETURN(ren_table); - } - else { - int rc= 1; - switch (frm_type) + case FRMTYPE_TABLE: { - case FRMTYPE_TABLE: + db_type table_type; + if ((table_type= get_table_type(thd, name)) == DB_TYPE_UNKNOWN) + my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno); + else rc= mysql_rename_table(table_type, ren_table->db, old_alias, new_table->db, new_alias); - break; - case FRMTYPE_VIEW: - /* change of schema is not allowed */ - if (strcmp(ren_table->db, new_table->db)) - my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db, - new_table->db); - else - rc= mysql_rename_view(thd, new_alias, ren_table); - break; - case FRMTYPE_ERROR: - default: - my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno); + break; } - if (rc && !skip_error) - DBUG_RETURN(ren_table); + case FRMTYPE_VIEW: + /* change of schema is not allowed */ + if (strcmp(ren_table->db, new_table->db)) + my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db, + new_table->db); + else + rc= mysql_rename_view(thd, new_alias, ren_table); + break; + default: + DBUG_ASSERT(0); // should never happen + case FRMTYPE_ERROR: + my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno); + break; } + if (rc && !skip_error) + DBUG_RETURN(ren_table); } DBUG_RETURN(0); } diff --git a/sql/sql_view.cc b/sql/sql_view.cc index 2e4e9c54b01..b742ec49922 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -1167,7 +1167,7 @@ frm_type_enum mysql_frm_type(char *path) int length; DBUG_ENTER("mysql_frm_type"); - if ((file= my_open(path, O_RDONLY | O_SHARE, MYF(MY_WME))) < 0) + if ((file= my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0) { DBUG_RETURN(FRMTYPE_ERROR); } @@ -1372,43 +1372,21 @@ int view_checksum(THD *thd, TABLE_LIST *view) HA_ADMIN_OK); } -bool rename_view_files(const char *schema, const char *old_name, - const char *new_name, ulonglong revision) -{ - char old_path[FN_REFLEN], new_path[FN_REFLEN], arc_path[FN_REFLEN]; +/* + rename view - strxnmov(old_path, FN_REFLEN, mysql_data_home, "/", schema, "/", - old_name, reg_ext, NullS); - (void) unpack_filename(old_path, old_path); + Synopsis: + renames a view - strxnmov(new_path, FN_REFLEN, mysql_data_home, "/", schema, "/", - new_name, reg_ext, NullS); - (void) unpack_filename(new_path, new_path); - - if (my_rename(old_path, new_path, MYF(MY_WME))) - return 1; - - /* check if arc_dir exists */ - strxnmov(arc_path, FN_REFLEN, mysql_data_home, "/", schema, "/arc", NullS); - (void) unpack_filename(arc_path, arc_path); - - if (revision && !access(arc_path, F_OK)) - { - while (revision) { - my_snprintf(old_path, FN_REFLEN, "%s/%s%s-%04lu", - arc_path, old_name, reg_ext, (ulong)revision); - (void) unpack_filename(old_path, old_path); - my_snprintf(new_path, FN_REFLEN, "%s/%s%s-%04lu", - arc_path, new_name, reg_ext, (ulong)revision); - (void) unpack_filename(new_path, new_path); - if (my_rename(old_path, new_path, MYF(0))) - return 0; - revision--; - } - } - return 0; -} + Parameters: + thd thread handler + new_name new name of view + view view + Return values: + FALSE Ok + TRUE Error +*/ bool mysql_rename_view(THD *thd, const char *new_name, @@ -1438,7 +1416,8 @@ mysql_rename_view(THD *thd, DBUG_RETURN(1); /* rename view and it's backups */ - if (rename_view_files(view->db, view->table_name, new_name, view->revision - 1)) + if (rename_in_schema_file(view->db, view->table_name, new_name, + view->revision - 1, num_view_backups)) DBUG_RETURN(1); strxnmov(dir_buff, FN_REFLEN, mysql_data_home, "/", view->db, "/", NullS); @@ -1454,7 +1433,8 @@ mysql_rename_view(THD *thd, if (sql_create_definition_file(&pathstr, &file, view_file_type, (gptr)view, view_parameters, num_view_backups)) { /* restore renamed view in case of error */ - rename_view_files(view->db, new_name, view->table_name, view->revision - 1); + rename_in_schema_file(view->db, new_name, view->table_name, + view->revision - 1, num_view_backups); DBUG_RETURN(1); } } else