~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_rename.cc

Put errmsg.c in sql-common since it can be built only once and used twice.
Put client.c and net_serv.c in libmysql so that we can only have one
link_sources section. 
Got rid of just about all copying and other weirdness, other than some stuff
in client and client.c/net_serv.c, which need to be reworked.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
/*
17
17
  Atomic rename of table;  RENAME TABLE t1 to t2, tmp to t1 [,...]
18
18
*/
19
 
#include <drizzled/server_includes.h>
20
 
#include <drizzled/drizzled_error_messages.h>
21
 
 
22
 
static TableList *rename_tables(THD *thd, TableList *table_list,
 
19
 
 
20
#include "mysql_priv.h"
 
21
 
 
22
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
23
23
                                 bool skip_error);
24
24
 
25
 
static TableList *reverse_table_list(TableList *table_list);
 
25
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
26
26
 
27
27
/*
28
28
  Every second entry in the table_list is the original name and every
29
29
  second entry is the new name.
30
30
*/
31
31
 
32
 
bool mysql_rename_tables(THD *thd, TableList *table_list, bool silent)
 
32
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
33
33
{
34
34
  bool error= 1;
35
 
  TableList *ren_table= 0;
 
35
  TABLE_LIST *ren_table= 0;
 
36
  DBUG_ENTER("mysql_rename_tables");
36
37
 
37
38
  /*
38
39
    Avoid problems with a rename on a table that we have locked or
39
40
    if the user is trying to to do this in a transcation context
40
41
  */
 
42
 
41
43
  if (thd->locked_tables || thd->active_transaction())
42
44
  {
43
45
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
44
46
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
45
 
    return(1);
 
47
    DBUG_RETURN(1);
46
48
  }
47
49
 
48
 
  mysql_ha_rm_tables(thd, table_list, false);
 
50
  mysql_ha_rm_tables(thd, table_list, FALSE);
49
51
 
50
52
  if (wait_if_global_read_lock(thd,0,1))
51
 
    return(1);
 
53
    DBUG_RETURN(1);
52
54
 
53
55
  pthread_mutex_lock(&LOCK_open);
54
56
  if (lock_table_names_exclusively(thd, table_list))
61
63
  if ((ren_table=rename_tables(thd,table_list,0)))
62
64
  {
63
65
    /* Rename didn't succeed;  rename back the tables in reverse order */
64
 
    TableList *table;
 
66
    TABLE_LIST *table;
65
67
 
66
68
    /* Reverse the table list */
67
69
    table_list= reverse_table_list(table_list);
91
93
  /* Lets hope this doesn't fail as the result will be messy */ 
92
94
  if (!silent && !error)
93
95
  {
94
 
    write_bin_log(thd, true, thd->query, thd->query_length);
 
96
    write_bin_log(thd, TRUE, thd->query, thd->query_length);
95
97
    my_ok(thd);
96
98
  }
97
99
 
98
100
  pthread_mutex_lock(&LOCK_open);
99
 
  unlock_table_names(thd, table_list, (TableList*) 0);
 
101
  unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
100
102
  pthread_mutex_unlock(&LOCK_open);
101
103
 
102
104
err:
103
105
  start_waiting_global_read_lock(thd);
104
 
  return(error);
 
106
  DBUG_RETURN(error);
105
107
}
106
108
 
107
109
 
115
117
  RETURN
116
118
    pointer to new (reversed) list
117
119
*/
118
 
static TableList *reverse_table_list(TableList *table_list)
 
120
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
119
121
{
120
 
  TableList *prev= 0;
 
122
  TABLE_LIST *prev= 0;
121
123
 
122
124
  while (table_list)
123
125
  {
124
 
    TableList *next= table_list->next_local;
 
126
    TABLE_LIST *next= table_list->next_local;
125
127
    table_list->next_local= prev;
126
128
    prev= table_list;
127
129
    table_list= next;
151
153
*/
152
154
 
153
155
bool
154
 
do_rename(THD *thd, TableList *ren_table, char *new_db, char *new_table_name,
 
156
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
155
157
          char *new_table_alias, bool skip_error)
156
158
{
157
159
  int rc= 1;
158
160
  char name[FN_REFLEN];
159
161
  const char *new_alias, *old_alias;
160
 
  /* TODO: What should this really be set to - it doesn't
161
 
     get set anywhere before it's used? */
162
 
  enum legacy_db_type table_type=DB_TYPE_UNKNOWN;
 
162
  enum legacy_db_type table_type;
 
163
 
 
164
  DBUG_ENTER("do_rename");
163
165
 
164
166
  if (lower_case_table_names == 2)
165
167
  {
176
178
  if (!access(name,F_OK))
177
179
  {
178
180
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
179
 
    return(1);                  // This can't be skipped
 
181
    DBUG_RETURN(1);                     // This can't be skipped
180
182
  }
181
183
  build_table_filename(name, sizeof(name),
182
184
                       ren_table->db, old_alias, reg_ext, 0);
183
185
 
184
 
  rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type),
 
186
  rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type), 
185
187
                               ren_table->db, old_alias,
186
188
                               new_db, new_alias, 0);
187
189
  if (rc && !skip_error)
188
 
    return(1);
 
190
    DBUG_RETURN(1);
189
191
 
190
 
  return(0);
 
192
  DBUG_RETURN(0);
191
193
 
192
194
}
193
195
/*
214
216
    true      rename failed
215
217
*/
216
218
 
217
 
static TableList *
218
 
rename_tables(THD *thd, TableList *table_list, bool skip_error)
 
219
static TABLE_LIST *
 
220
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
219
221
{
220
 
  TableList *ren_table, *new_table;
 
222
  TABLE_LIST *ren_table, *new_table;
 
223
 
 
224
  DBUG_ENTER("rename_tables");
221
225
 
222
226
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
223
227
  {
224
228
    new_table= ren_table->next_local;
225
229
    if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
226
230
                  new_table->alias, skip_error))
227
 
      return(ren_table);
 
231
      DBUG_RETURN(ren_table);
228
232
  }
229
 
  return(0);
 
233
  DBUG_RETURN(0);
230
234
}