The world's most popular open source database
#include "mysql_priv.h"#include "sql_trigger.h"Include dependency graph for sql_rename.cc:

Go to the source code of this file.
Functions | |
| static TABLE_LIST * | rename_tables (THD *thd, TABLE_LIST *table_list, bool skip_error) |
| static TABLE_LIST * | reverse_table_list (TABLE_LIST *table_list) |
| bool | mysql_rename_tables (THD *thd, TABLE_LIST *table_list, bool silent) |
| bool mysql_rename_tables | ( | THD * | thd, | |
| TABLE_LIST * | table_list, | |||
| bool | silent | |||
| ) |
Definition at line 35 of file sql_rename.cc.
References DBUG_ENTER, DBUG_RETURN, ER, ER_LOCK_OR_ACTIVE_TRANSACTION, err, error, FALSE, MYSQL_LOG::is_open(), LOCK_open, lock_table_names(), my_message(), MYF, mysql_bin_log, st_table_list::next_local, pthread_mutex_lock, pthread_mutex_unlock, rename_tables(), reverse_table_list(), send_ok(), start_waiting_global_read_lock(), unlock_table_names(), VOID, and wait_if_global_read_lock().
Referenced by mysql_execute_command(), and mysql_rename_db().
00036 { 00037 bool error= 1; 00038 TABLE_LIST *ren_table= 0; 00039 DBUG_ENTER("mysql_rename_tables"); 00040 00041 /* 00042 Avoid problems with a rename on a table that we have locked or 00043 if the user is trying to to do this in a transcation context 00044 */ 00045 00046 if (thd->locked_tables || thd->active_transaction()) 00047 { 00048 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, 00049 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0)); 00050 DBUG_RETURN(1); 00051 } 00052 00053 if (wait_if_global_read_lock(thd,0,1)) 00054 DBUG_RETURN(1); 00055 VOID(pthread_mutex_lock(&LOCK_open)); 00056 if (lock_table_names(thd, table_list)) 00057 goto err; 00058 00059 error=0; 00060 if ((ren_table=rename_tables(thd,table_list,0))) 00061 { 00062 /* Rename didn't succeed; rename back the tables in reverse order */ 00063 TABLE_LIST *table; 00064 00065 /* Reverse the table list */ 00066 table_list= reverse_table_list(table_list); 00067 00068 /* Find the last renamed table */ 00069 for (table= table_list; 00070 table->next_local != ren_table ; 00071 table= table->next_local->next_local) ; 00072 table= table->next_local->next_local; // Skip error table 00073 /* Revert to old names */ 00074 rename_tables(thd, table, 1); 00075 00076 /* Revert the table list (for prepared statements) */ 00077 table_list= reverse_table_list(table_list); 00078 00079 error= 1; 00080 } 00081 00082 /* Lets hope this doesn't fail as the result will be messy */ 00083 if (!silent && !error) 00084 { 00085 if (mysql_bin_log.is_open()) 00086 { 00087 thd->clear_error(); 00088 thd->binlog_query(THD::STMT_QUERY_TYPE, 00089 thd->query, thd->query_length, FALSE, FALSE); 00090 } 00091 send_ok(thd); 00092 } 00093 00094 unlock_table_names(thd, table_list, (TABLE_LIST*) 0); 00095 00096 err: 00097 pthread_mutex_unlock(&LOCK_open); 00098 start_waiting_global_read_lock(thd); 00099 DBUG_RETURN(error); 00100 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static TABLE_LIST * rename_tables | ( | THD * | thd, | |
| TABLE_LIST * | table_list, | |||
| bool | skip_error | |||
| ) | [static] |
Definition at line 134 of file sql_rename.cc.
References access, st_table_list::alias, build_table_filename(), Table_triggers_list::change_table_name(), st_table_list::db, DB_TYPE_UNKNOWN, DBUG_ASSERT, DBUG_ENTER, DBUG_RETURN, ER_FILE_NOT_FOUND, ER_FORBID_SCHEMA_CHANGE, ER_TABLE_EXISTS_ERROR, F_OK, FN_REFLEN, FRMTYPE_ERROR, FRMTYPE_TABLE, FRMTYPE_VIEW, ha_resolve_by_legacy_type(), lower_case_table_names, my_errno, my_error(), MYF, mysql_frm_type(), mysql_rename_table(), mysql_rename_view(), name, st_table_list::next_local, reg_ext, strcmp(), and st_table_list::table_name.
Referenced by mysql_rename_tables().
00135 { 00136 TABLE_LIST *ren_table,*new_table; 00137 frm_type_enum frm_type; 00138 enum legacy_db_type table_type; 00139 00140 DBUG_ENTER("rename_tables"); 00141 00142 for (ren_table= table_list; ren_table; ren_table= new_table->next_local) 00143 { 00144 int rc= 1; 00145 char name[FN_REFLEN]; 00146 const char *new_alias, *old_alias; 00147 00148 new_table= ren_table->next_local; 00149 if (lower_case_table_names == 2) 00150 { 00151 old_alias= ren_table->alias; 00152 new_alias= new_table->alias; 00153 } 00154 else 00155 { 00156 old_alias= ren_table->table_name; 00157 new_alias= new_table->table_name; 00158 } 00159 build_table_filename(name, sizeof(name), 00160 new_table->db, new_alias, reg_ext, 0); 00161 if (!access(name,F_OK)) 00162 { 00163 my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); 00164 DBUG_RETURN(ren_table); // This can't be skipped 00165 } 00166 build_table_filename(name, sizeof(name), 00167 ren_table->db, old_alias, reg_ext, 0); 00168 00169 frm_type= mysql_frm_type(thd, name, &table_type); 00170 switch (frm_type) 00171 { 00172 case FRMTYPE_TABLE: 00173 { 00174 if (table_type == DB_TYPE_UNKNOWN) 00175 my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno); 00176 else 00177 { 00178 if (!(rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, 00179 table_type), 00180 ren_table->db, old_alias, 00181 new_table->db, new_alias, 0))) 00182 { 00183 if ((rc= Table_triggers_list::change_table_name(thd, ren_table->db, 00184 old_alias, 00185 new_table->db, 00186 new_alias))) 00187 { 00188 /* 00189 We've succeeded in renaming table's .frm and in updating 00190 corresponding handler data, but have failed to update table's 00191 triggers appropriately. So let us revert operations on .frm 00192 and handler's data and report about failure to rename table. 00193 */ 00194 (void) mysql_rename_table(ha_resolve_by_legacy_type(thd, 00195 table_type), 00196 new_table->db, new_alias, 00197 ren_table->db, old_alias, 0); 00198 } 00199 } 00200 } 00201 break; 00202 } 00203 case FRMTYPE_VIEW: 00204 /* change of schema is not allowed */ 00205 if (strcmp(ren_table->db, new_table->db)) 00206 my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db, 00207 new_table->db); 00208 else 00209 rc= mysql_rename_view(thd, new_alias, ren_table); 00210 break; 00211 default: 00212 DBUG_ASSERT(0); // should never happen 00213 case FRMTYPE_ERROR: 00214 my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno); 00215 break; 00216 } 00217 if (rc && !skip_error) 00218 DBUG_RETURN(ren_table); 00219 } 00220 DBUG_RETURN(0); 00221 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static TABLE_LIST * reverse_table_list | ( | TABLE_LIST * | table_list | ) | [static] |
Definition at line 113 of file sql_rename.cc.
References st_table_list::next_local.
Referenced by mysql_rename_tables().
00114 { 00115 TABLE_LIST *prev= 0; 00116 00117 while (table_list) 00118 { 00119 TABLE_LIST *next= table_list->next_local; 00120 table_list->next_local= prev; 00121 prev= table_list; 00122 table_list= next; 00123 } 00124 return (prev); 00125 }
Here is the caller graph for this function:

1.4.7

