18
18
#include <drizzled/server_includes.h>
19
19
#include <storage/myisam/myisam.h>
20
#include <drizzled/sql_show.h>
21
#include <drizzled/drizzled_error_messages.h>
22
#include <libdrizzle/gettext.h>
20
#include <drizzled/show.h>
21
#include <drizzled/error.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/data_home.h>
24
#include <drizzled/sql_parse.h>
25
#include <mysys/hash.h>
26
#include <drizzled/sql_lex.h>
27
#include <drizzled/session.h>
28
#include <drizzled/sql_base.h>
29
#include <drizzled/db.h>
30
#include <drizzled/replicator.h>
31
#include <drizzled/lock.h>
32
#include <drizzled/unireg.h>
33
#include <drizzled/item/int.h>
34
#include <drizzled/item/empty_string.h>
38
extern HASH lock_db_cache;
24
40
int creating_table= 0; // How many mysql_create_table are running
26
const char *primary_key_name="PRIMARY";
43
bool is_primary_key(KEY *key_info)
45
static const char * primary_key_name="PRIMARY";
46
return (strcmp(key_info->name, primary_key_name)==0);
49
const char* is_primary_key_name(const char* key_name)
51
static const char * primary_key_name="PRIMARY";
52
if (strcmp(key_name, primary_key_name)==0)
28
58
static bool check_if_keyname_exists(const char *name,KEY *start, KEY *end);
29
59
static char *make_unique_key_name(const char *field_name,KEY *start,KEY *end);
34
64
enum enum_enable_or_disable keys_onoff,
35
65
bool error_if_not_empty);
37
static bool prepare_blob_field(THD *thd, Create_field *sql_field);
38
static bool check_engine(THD *, const char *, HA_CREATE_INFO *);
67
static bool prepare_blob_field(Session *session, Create_field *sql_field);
68
static bool check_engine(Session *, const char *, HA_CREATE_INFO *);
40
mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
70
mysql_prepare_create_table(Session *session, HA_CREATE_INFO *create_info,
41
71
Alter_info *alter_info,
43
73
uint32_t *db_options,
44
74
handler *file, KEY **key_info_buffer,
45
75
uint32_t *key_count, int select_field_count);
47
mysql_prepare_alter_table(THD *thd, Table *table,
77
mysql_prepare_alter_table(Session *session, Table *table,
48
78
HA_CREATE_INFO *create_info,
49
79
Alter_info *alter_info);
102
132
uint32_t tablename_to_filename(const char *from, char *to, uint32_t to_length)
104
134
uint32_t errors, length;
106
136
if (from[0] == '#' && !strncmp(from, MYSQL50_TABLE_NAME_PREFIX,
107
137
MYSQL50_TABLE_NAME_PREFIX_LENGTH))
108
return((uint) (strmake(to, from+MYSQL50_TABLE_NAME_PREFIX_LENGTH,
110
(from + MYSQL50_TABLE_NAME_PREFIX_LENGTH)));
138
return((uint) (strncpy(to, from+MYSQL50_TABLE_NAME_PREFIX_LENGTH,
140
(from + MYSQL50_TABLE_NAME_PREFIX_LENGTH)));
111
141
length= strconvert(system_charset_info, from,
112
142
&my_charset_filename, to, to_length, &errors);
113
143
if (check_if_legal_tablename(to) &&
150
180
build_tmptable_filename() for them.
183
path length on success, 0 on failure
156
186
uint32_t build_table_filename(char *buff, size_t bufflen, const char *db,
157
187
const char *table_name, const char *ext, uint32_t flags)
159
190
char dbbuff[FN_REFLEN];
160
191
char tbbuff[FN_REFLEN];
192
int rootdir_len= strlen(FN_ROOTDIR);
162
194
if (flags & FN_IS_TMP) // FN_FROM_IS_TMP | FN_TO_IS_TMP
163
my_stpncpy(tbbuff, table_name, sizeof(tbbuff));
195
strncpy(tbbuff, table_name, sizeof(tbbuff));
165
197
tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
167
199
tablename_to_filename(db, dbbuff, sizeof(dbbuff));
169
char *end = buff + bufflen;
170
/* Don't add FN_ROOTDIR if mysql_data_home already includes it */
171
char *pos = my_stpncpy(buff, mysql_data_home, bufflen);
172
int rootdir_len= strlen(FN_ROOTDIR);
173
if (pos - rootdir_len >= buff &&
174
memcmp(pos - rootdir_len, FN_ROOTDIR, rootdir_len) != 0)
175
pos= my_stpncpy(pos, FN_ROOTDIR, end - pos);
176
pos= strxnmov(pos, end - pos, dbbuff, FN_ROOTDIR, NULL);
200
table_path= drizzle_data_home;
201
int without_rootdir= table_path.length()-rootdir_len;
203
/* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
204
if (without_rootdir >= 0)
206
char *tmp= (char*)table_path.c_str()+without_rootdir;
207
if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
208
table_path.append(FN_ROOTDIR);
211
table_path.append(dbbuff);
212
table_path.append(FN_ROOTDIR);
177
213
#ifdef USE_SYMDIR
178
unpack_dirname(buff, buff);
181
pos= strxnmov(pos, end - pos, tbbuff, ext, NULL);
216
table_path.append(tbbuff);
217
table_path.append(ext);
219
if (bufflen < table_path.length())
222
strcpy(buff, table_path.c_str());
223
return table_path.length();
188
Creates path to a file: mysql_tmpdir/#sql1234_12_1.ext
228
Creates path to a file: drizzle_tmpdir/#sql1234_12_1.ext
191
231
build_tmptable_filename()
192
thd The thread handle.
232
session The thread handle.
193
233
buff Where to write result in my_charset_filename.
194
234
bufflen buff size
198
238
Uses current_pid, thread_id, and tmp_table counter to create
199
a file name in mysql_tmpdir.
239
a file name in drizzle_tmpdir.
242
path length on success, 0 on failure
205
uint32_t build_tmptable_filename(THD* thd, char *buff, size_t bufflen)
245
uint32_t build_tmptable_filename(Session* session, char *buff, size_t bufflen)
248
ostringstream path_str, post_tmpdir_str;
208
char *p= my_stpncpy(buff, mysql_tmpdir, bufflen);
209
snprintf(p, bufflen - (p - buff), "/%s%lx_%lx_%x%s",
210
tmp_file_prefix, current_pid,
211
thd->thread_id, thd->tmp_table++, reg_ext);
251
path_str << drizzle_tmpdir;
252
post_tmpdir_str << "/" << TMP_FILE_PREFIX << current_pid;
253
post_tmpdir_str << session->thread_id << session->tmp_table++ << reg_ext;
254
tmp= post_tmpdir_str.str();
213
256
if (lower_case_table_names)
215
/* Convert all except tmpdir to lower case */
216
my_casedn_str(files_charset_info, p);
219
uint32_t length= unpack_filename(buff, buff);
257
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
261
if (bufflen < path_str.str().length())
264
length= unpack_filename(buff, path_str.str().c_str());
272
session Thread object
227
273
clear_error is clear_error to be called
228
274
query Query to log
229
275
query_length Length of query
438
474
if (!drop_temporary)
440
476
Table *locked_table;
441
abort_locked_tables(thd, db, table->table_name);
442
remove_table_from_cache(thd, db, table->table_name,
477
abort_locked_tables(session, db, table->table_name);
478
remove_table_from_cache(session, db, table->table_name,
443
479
RTFC_WAIT_OTHER_THREAD_FLAG |
444
480
RTFC_CHECK_KILLED_FLAG);
446
482
If the table was used in lock tables, remember it so that
447
483
unlock_table_names can free it
449
if ((locked_table= drop_locked_tables(thd, db, table->table_name)))
485
if ((locked_table= drop_locked_tables(session, db, table->table_name)))
450
486
table->table= locked_table;
455
491
goto err_with_placeholders;
478
if (table_type == NULL)
480
mysql_frm_type(thd, path, &frm_db_type);
481
table_type= ha_resolve_by_legacy_type(thd, frm_db_type);
483
514
// Remove extension for delete
484
515
*(end= path + path_length - reg_ext_length)= '\0';
485
error= ha_delete_table(thd, table_type, path, db, table->table_name,
516
error= ha_delete_table(session, path, db, table->table_name,
486
517
!dont_log_query);
487
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) &&
488
(if_exists || table_type == NULL))
518
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) &&
522
session->clear_error();
493
524
if (error == HA_ERR_ROW_IS_REFERENCED)
1642
Preparation of Create_field for SP function return values.
1643
Based on code used in the inner loop of mysql_prepare_create_table()
1647
sp_prepare_create_field()
1649
sql_field Field to prepare
1652
Prepares the field structures for field creation.
1656
void sp_prepare_create_field(THD *thd, Create_field *sql_field)
1658
if (sql_field->sql_type == DRIZZLE_TYPE_ENUM)
1660
uint32_t field_length, dummy;
1661
/* DRIZZLE_TYPE_ENUM */
1663
calculate_interval_lengths(sql_field->charset,
1664
sql_field->interval,
1665
&field_length, &dummy);
1666
sql_field->length= field_length;
1668
set_if_smaller(sql_field->length, MAX_FIELD_WIDTH-1);
1671
sql_field->create_length_to_internal_length();
1672
assert(sql_field->def == 0);
1673
/* Can't go wrong as sql_field->def is not defined */
1674
(void) prepare_blob_field(thd, sql_field);
1682
1718
mysql_create_table_no_lock()
1719
session Thread object
1685
1721
table_name Table name
1686
1722
create_info Create information (like MAX_ROWS)
1732
if (check_engine(thd, table_name, create_info))
1769
if (check_engine(session, table_name, create_info))
1734
1771
db_options= create_info->table_options;
1735
1772
if (create_info->row_type == ROW_TYPE_DYNAMIC)
1736
1773
db_options|=HA_OPTION_PACK_RECORD;
1737
1774
alias= table_case_name(create_info, table_name);
1738
if (!(file= get_new_handler((TABLE_SHARE*) 0, thd->mem_root,
1775
if (!(file= get_new_handler((TABLE_SHARE*) 0, session->mem_root,
1739
1776
create_info->db_type)))
1741
1778
my_error(ER_OUTOFMEMORY, MYF(0), sizeof(handler));
1745
set_table_default_charset(thd, create_info, (char*) db);
1782
set_table_default_charset(session, create_info, (char*) db);
1747
if (mysql_prepare_create_table(thd, create_info, alter_info,
1784
if (mysql_prepare_create_table(session, create_info, alter_info,
1748
1785
internal_tmp_table,
1749
1786
&db_options, file,
1750
1787
&key_info_buffer, &key_count,
1864
1902
#endif /* HAVE_READLINK */
1866
1904
if (create_info->data_file_name)
1867
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1905
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1868
1906
"DATA DIRECTORY option ignored");
1869
1907
if (create_info->index_file_name)
1870
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1908
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1871
1909
"INDEX DIRECTORY option ignored");
1872
1910
create_info->data_file_name= create_info->index_file_name= 0;
1874
1912
create_info->table_options=db_options;
1876
1914
path[path_length - reg_ext_length]= '\0'; // Remove .frm extension
1877
if (rea_create_table(thd, path, db, table_name,
1915
if (rea_create_table(session, path, db, table_name,
1878
1916
create_info, alter_info->create_list,
1879
1917
key_count, key_info_buffer, file))
1880
1918
goto unlock_and_end;
1882
1920
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1884
1922
/* Open table and put in temporary table list */
1885
if (!(open_temporary_table(thd, path, db, table_name, 1, OTM_OPEN)))
1923
if (!(open_temporary_table(session, path, db, table_name, 1, OTM_OPEN)))
1887
(void) rm_temporary_table(create_info->db_type, path, false);
1925
(void) rm_temporary_table(create_info->db_type, path);
1888
1926
goto unlock_and_end;
1890
thd->thread_specific_used= true;
1928
session->thread_specific_used= true;
1898
1936
Otherwise, the statement shall be binlogged.
1900
1938
if (!internal_tmp_table &&
1901
(!thd->current_stmt_binlog_row_based ||
1902
(thd->current_stmt_binlog_row_based &&
1903
!(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
1904
write_bin_log(thd, true, thd->query, thd->query_length);
1939
((!(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
1940
write_bin_log(session, true, session->query, session->query_length);
1906
1942
unlock_and_end:
1907
pthread_mutex_unlock(&LOCK_open);
1944
pthread_mutex_unlock(&LOCK_open);
1910
thd_proc_info(thd, "After create");
1947
session->set_proc_info("After create");
1916
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1953
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1917
1954
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1919
1956
create_info->table_existed= 1; // Mark that table existed
2090
2129
if (lower_case_table_names == 2 && file &&
2091
2130
!(file->ha_table_flags() & HA_FILE_BASED))
2093
my_stpcpy(tmp_name, old_name);
2132
strcpy(tmp_name, old_name);
2094
2133
my_casedn_str(files_charset_info, tmp_name);
2095
2134
build_table_filename(lc_from, sizeof(lc_from), old_db, tmp_name, "",
2096
2135
flags & FN_FROM_IS_TMP);
2097
2136
from_base= lc_from;
2099
my_stpcpy(tmp_name, new_name);
2138
strcpy(tmp_name, new_name);
2100
2139
my_casedn_str(files_charset_info, tmp_name);
2101
2140
build_table_filename(lc_to, sizeof(lc_to), new_db, tmp_name, "",
2102
2141
flags & FN_TO_IS_TMP);
2174
2222
Win32 clients must also have a WRITE LOCK on the table !
2177
void close_cached_table(THD *thd, Table *table)
2225
void close_cached_table(Session *session, Table *table)
2180
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
2228
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
2181
2229
/* Close lock if this is not got with LOCK TABLES */
2184
mysql_unlock_tables(thd, thd->lock);
2185
thd->lock=0; // Start locked threads
2232
mysql_unlock_tables(session, session->lock);
2233
session->lock=0; // Start locked threads
2187
2235
/* Close all copies of 'table'. This also frees all LOCK TABLES lock */
2188
unlink_open_table(thd, table, true);
2236
unlink_open_table(session, table, true);
2190
2238
/* When lock on LOCK_open is freed other threads can continue */
2191
2239
broadcast_refresh();
2195
static int send_check_errmsg(THD *thd, TableList* table,
2243
static int send_check_errmsg(Session *session, TableList* table,
2196
2244
const char* operator_name, const char* errmsg)
2199
Protocol *protocol= thd->protocol;
2247
Protocol *protocol= session->protocol;
2200
2248
protocol->prepare_for_resend();
2201
2249
protocol->store(table->alias, system_charset_info);
2202
2250
protocol->store((char*) operator_name, system_charset_info);
2203
2251
protocol->store(STRING_WITH_LEN("error"), system_charset_info);
2204
2252
protocol->store(errmsg, system_charset_info);
2253
session->clear_error();
2206
2254
if (protocol->write())
2212
static int prepare_for_repair(THD *thd, TableList *table_list,
2260
static int prepare_for_repair(Session *session, TableList *table_list,
2213
2261
HA_CHECK_OPT *check_opt)
2227
2275
char key[MAX_DBKEY_LENGTH];
2228
2276
uint32_t key_length;
2230
key_length= create_table_def_key(thd, key, table_list, 0);
2278
key_length= create_table_def_key(session, key, table_list, 0);
2231
2279
pthread_mutex_lock(&LOCK_open);
2232
if (!(share= (get_table_share(thd, table_list, key, key_length, 0,
2280
if (!(share= (get_table_share(session, table_list, key, key_length, 0,
2235
2283
pthread_mutex_unlock(&LOCK_open);
2236
2284
return(0); // Can't open frm file
2239
if (open_table_from_share(thd, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2287
if (open_table_from_share(session, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2241
2289
release_table_share(share, RELEASE_NORMAL);
2242
2290
pthread_mutex_unlock(&LOCK_open);
2270
2318
Check if this is a table type that stores index and data separately,
2271
2319
like ISAM or MyISAM. We assume fixed order of engine file name
2272
2320
extentions array. First element of engine file name extentions array
2273
is meta/index file extention. Second element - data file extention.
2321
is meta/index file extention. Second element - data file extention.
2275
2323
ext= table->file->bas_ext();
2276
2324
if (!ext[0] || !ext[1])
2277
2325
goto end; // No data file
2279
2327
// Name of data file
2280
strxmov(from, table->s->normalized_path.str, ext[1], NULL);
2328
sprintf(from,"%s%s", table->s->normalized_path.str, ext[1]);
2281
2329
if (stat(from, &stat_info))
2282
2330
goto end; // Can't use USE_FRM flag
2284
snprintf(tmp, sizeof(tmp), "%s-%lx_%lx",
2285
from, current_pid, thd->thread_id);
2332
snprintf(tmp, sizeof(tmp), "%s-%lx_%"PRIx64,
2333
from, (unsigned long)current_pid, session->thread_id);
2287
2335
/* If we could open the table, close it */
2288
2336
if (table_list->table)
2290
2338
pthread_mutex_lock(&LOCK_open);
2291
close_cached_table(thd, table);
2339
close_cached_table(session, table);
2292
2340
pthread_mutex_unlock(&LOCK_open);
2294
if (lock_and_wait_for_table_name(thd,table_list))
2342
if (lock_and_wait_for_table_name(session,table_list))
2299
2347
if (my_rename(from, tmp, MYF(MY_WME)))
2301
2349
pthread_mutex_lock(&LOCK_open);
2302
unlock_table_name(thd, table_list);
2350
unlock_table_name(session, table_list);
2303
2351
pthread_mutex_unlock(&LOCK_open);
2304
error= send_check_errmsg(thd, table_list, "repair",
2352
error= send_check_errmsg(session, table_list, "repair",
2305
2353
"Failed renaming data file");
2308
if (mysql_truncate(thd, table_list, 1))
2356
if (mysql_truncate(session, table_list, 1))
2310
2358
pthread_mutex_lock(&LOCK_open);
2311
unlock_table_name(thd, table_list);
2359
unlock_table_name(session, table_list);
2312
2360
pthread_mutex_unlock(&LOCK_open);
2313
error= send_check_errmsg(thd, table_list, "repair",
2361
error= send_check_errmsg(session, table_list, "repair",
2314
2362
"Failed generating table from .frm file");
2317
2365
if (my_rename(tmp, from, MYF(MY_WME)))
2319
2367
pthread_mutex_lock(&LOCK_open);
2320
unlock_table_name(thd, table_list);
2368
unlock_table_name(session, table_list);
2321
2369
pthread_mutex_unlock(&LOCK_open);
2322
error= send_check_errmsg(thd, table_list, "repair",
2370
error= send_check_errmsg(session, table_list, "repair",
2323
2371
"Failed restoring .MYD file");
2329
2377
to finish the repair in the handler later on.
2331
2379
pthread_mutex_lock(&LOCK_open);
2332
if (reopen_name_locked_table(thd, table_list, true))
2380
if (reopen_name_locked_table(session, table_list, true))
2334
unlock_table_name(thd, table_list);
2382
unlock_table_name(session, table_list);
2335
2383
pthread_mutex_unlock(&LOCK_open);
2336
error= send_check_errmsg(thd, table_list, "repair",
2384
error= send_check_errmsg(session, table_list, "repair",
2337
2385
"Failed to open partially repaired table");
2356
2404
false Message sent to net (admin operation went ok)
2357
true Message should be sent by caller
2405
true Message should be sent by caller
2358
2406
(admin operation or network communication failed)
2360
static bool mysql_admin_table(THD* thd, TableList* tables,
2408
static bool mysql_admin_table(Session* session, TableList* tables,
2361
2409
HA_CHECK_OPT* check_opt,
2362
2410
const char *operator_name,
2363
2411
thr_lock_type lock_type,
2364
2412
bool open_for_modify,
2365
2413
bool no_warnings_for_error,
2366
2414
uint32_t extra_open_options,
2367
int (*prepare_func)(THD *, TableList *,
2415
int (*prepare_func)(Session *, TableList *,
2368
2416
HA_CHECK_OPT *),
2369
int (handler::*operator_func)(THD *,
2417
int (handler::*operator_func)(Session *,
2370
2418
HA_CHECK_OPT *))
2372
2420
TableList *table;
2373
SELECT_LEX *select= &thd->lex->select_lex;
2421
SELECT_LEX *select= &session->lex->select_lex;
2374
2422
List<Item> field_list;
2376
Protocol *protocol= thd->protocol;
2424
Protocol *protocol= session->protocol;
2425
LEX *lex= session->lex;
2378
2426
int result_code= 0;
2379
2427
const CHARSET_INFO * const cs= system_charset_info;
2381
if (end_active_trans(thd))
2429
if (end_active_trans(session))
2383
2431
field_list.push_back(item = new Item_empty_string("Table",
2384
2432
NAME_CHAR_LEN * 2,
2422
2470
lex->query_tables= table;
2423
2471
lex->query_tables_last= &table->next_global;
2424
2472
lex->query_tables_own_last= 0;
2425
thd->no_warnings_for_error= no_warnings_for_error;
2473
session->no_warnings_for_error= no_warnings_for_error;
2427
open_and_lock_tables(thd, table);
2428
thd->no_warnings_for_error= 0;
2475
open_and_lock_tables(session, table);
2476
session->no_warnings_for_error= 0;
2429
2477
table->next_global= save_next_global;
2430
2478
table->next_local= save_next_local;
2431
thd->open_options&= ~extra_open_options;
2479
session->open_options&= ~extra_open_options;
2434
2482
if (prepare_func)
2436
switch ((*prepare_func)(thd, table, check_opt)) {
2484
switch ((*prepare_func)(session, table, check_opt)) {
2437
2485
case 1: // error, message written to net
2438
ha_autocommit_or_rollback(thd, 1);
2439
end_trans(thd, ROLLBACK);
2440
close_thread_tables(thd);
2486
ha_autocommit_or_rollback(session, 1);
2487
end_trans(session, ROLLBACK);
2488
close_thread_tables(session);
2442
2490
case -1: // error, message could be written to net
2443
2491
/* purecov: begin inspected */
2491
2539
if (lock_type == TL_WRITE && table->table->s->version)
2493
2541
pthread_mutex_lock(&LOCK_open);
2494
const char *old_message=thd->enter_cond(&COND_refresh, &LOCK_open,
2542
const char *old_message=session->enter_cond(&COND_refresh, &LOCK_open,
2495
2543
"Waiting to get writelock");
2496
mysql_lock_abort(thd,table->table, true);
2497
remove_table_from_cache(thd, table->table->s->db.str,
2544
mysql_lock_abort(session,table->table, true);
2545
remove_table_from_cache(session, table->table->s->db.str,
2498
2546
table->table->s->table_name.str,
2499
2547
RTFC_WAIT_OTHER_THREAD_FLAG |
2500
2548
RTFC_CHECK_KILLED_FLAG);
2501
thd->exit_cond(old_message);
2549
session->exit_cond(old_message);
2550
if (session->killed)
2504
2552
open_for_modify= 0;
2525
2573
(table->table->file->ha_check_for_upgrade(check_opt) ==
2526
2574
HA_ADMIN_NEEDS_ALTER))
2528
ha_autocommit_or_rollback(thd, 1);
2529
close_thread_tables(thd);
2530
tmp_disable_binlog(thd); // binlogging is done by caller if wanted
2531
result_code= mysql_recreate_table(thd, table);
2532
reenable_binlog(thd);
2576
ha_autocommit_or_rollback(session, 1);
2577
close_thread_tables(session);
2578
tmp_disable_binlog(session); // binlogging is done by caller if wanted
2579
result_code= mysql_recreate_table(session, table);
2580
reenable_binlog(session);
2534
2582
mysql_recreate_table() can push OK or ERROR.
2535
2583
Clear 'OK' status. If there is an error, keep it:
2536
we will store the error message in a result set row
2584
we will store the error message in a result set row
2537
2585
and then clear.
2539
if (thd->main_da.is_ok())
2540
thd->main_da.reset_diagnostics_area();
2587
if (session->main_da.is_ok())
2588
session->main_da.reset_diagnostics_area();
2541
2589
goto send_result;
2545
result_code = (table->table->file->*operator_func)(thd, check_opt);
2593
result_code = (table->table->file->*operator_func)(session, check_opt);
2549
2597
lex->cleanup_after_one_table_open();
2550
thd->clear_error(); // these errors shouldn't get client
2598
session->clear_error(); // these errors shouldn't get client
2552
List_iterator_fast<DRIZZLE_ERROR> it(thd->warn_list);
2600
List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
2553
2601
DRIZZLE_ERROR *err;
2554
2602
while ((err= it++))
2635
2683
"try with alter", so here we close the table, do an ALTER Table,
2636
2684
reopen the table and do ha_innobase::analyze() on it.
2638
ha_autocommit_or_rollback(thd, 0);
2639
close_thread_tables(thd);
2686
ha_autocommit_or_rollback(session, 0);
2687
close_thread_tables(session);
2640
2688
TableList *save_next_local= table->next_local,
2641
2689
*save_next_global= table->next_global;
2642
2690
table->next_local= table->next_global= 0;
2643
tmp_disable_binlog(thd); // binlogging is done by caller if wanted
2644
result_code= mysql_recreate_table(thd, table);
2645
reenable_binlog(thd);
2691
tmp_disable_binlog(session); // binlogging is done by caller if wanted
2692
result_code= mysql_recreate_table(session, table);
2693
reenable_binlog(session);
2647
2695
mysql_recreate_table() can push OK or ERROR.
2648
2696
Clear 'OK' status. If there is an error, keep it:
2649
we will store the error message in a result set row
2697
we will store the error message in a result set row
2650
2698
and then clear.
2652
if (thd->main_da.is_ok())
2653
thd->main_da.reset_diagnostics_area();
2654
ha_autocommit_or_rollback(thd, 0);
2655
close_thread_tables(thd);
2700
if (session->main_da.is_ok())
2701
session->main_da.reset_diagnostics_area();
2702
ha_autocommit_or_rollback(session, 0);
2703
close_thread_tables(session);
2656
2704
if (!result_code) // recreation went ok
2658
if ((table->table= open_ltable(thd, table, lock_type, 0)) &&
2659
((result_code= table->table->file->ha_analyze(thd, check_opt)) > 0))
2706
if ((table->table= open_ltable(session, table, lock_type, 0)) &&
2707
((result_code= table->table->file->ha_analyze(session, check_opt)) > 0))
2660
2708
result_code= 0; // analyze went ok
2662
2710
if (result_code) // either mysql_recreate_table or analyze failed
2664
assert(thd->is_error());
2665
if (thd->is_error())
2712
assert(session->is_error());
2713
if (session->is_error())
2667
const char *err_msg= thd->main_da.message();
2715
const char *err_msg= session->main_da.message();
2716
if (!session->vio_ok())
2670
sql_print_error(err_msg);
2718
sql_print_error("%s",err_msg);
2734
2782
pthread_mutex_lock(&LOCK_open);
2735
remove_table_from_cache(thd, table->table->s->db.str,
2783
remove_table_from_cache(session, table->table->s->db.str,
2736
2784
table->table->s->table_name.str, RTFC_NO_FLAG);
2737
2785
pthread_mutex_unlock(&LOCK_open);
2741
ha_autocommit_or_rollback(thd, 0);
2742
end_trans(thd, COMMIT);
2743
close_thread_tables(thd);
2789
ha_autocommit_or_rollback(session, 0);
2790
end_trans(session, COMMIT);
2791
close_thread_tables(session);
2744
2792
table->table=0; // For query cache
2745
2793
if (protocol->write())
2753
ha_autocommit_or_rollback(thd, 1);
2754
end_trans(thd, ROLLBACK);
2755
close_thread_tables(thd); // Shouldn't be needed
2801
ha_autocommit_or_rollback(session, 1);
2802
end_trans(session, ROLLBACK);
2803
close_thread_tables(session); // Shouldn't be needed
2757
2805
table->table=0;
2762
bool mysql_repair_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
2810
bool mysql_repair_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2764
return(mysql_admin_table(thd, tables, check_opt,
2812
return(mysql_admin_table(session, tables, check_opt,
2765
2813
"repair", TL_WRITE, 1,
2766
2814
test(check_opt->sql_flags & TT_USEFRM),
2767
2815
HA_OPEN_FOR_REPAIR,
2877
2925
local_create_info.default_table_charset=default_charset_info;
2878
2926
alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
2879
2927
schema_table->table->use_all_columns();
2880
if (mysql_prepare_alter_table(thd, schema_table->table,
2928
if (mysql_prepare_alter_table(session, schema_table->table,
2881
2929
&local_create_info, &alter_info))
2883
if (mysql_prepare_create_table(thd, &local_create_info, &alter_info,
2931
if (mysql_prepare_create_table(session, &local_create_info, &alter_info,
2884
2932
tmp_table, &db_options,
2885
2933
schema_table->table->file,
2886
2934
&schema_table->table->s->key_info, &keys, 0))
2888
2936
local_create_info.max_rows= 0;
2889
if (mysql_create_frm(thd, dst_path, NULL, NULL,
2937
if (mysql_create_frm(session, dst_path, NULL, NULL,
2890
2938
&local_create_info, alter_info.create_list,
2891
2939
keys, schema_table->table->s->key_info,
2892
2940
schema_table->table->file))
2931
2979
we ensure that our statement is properly isolated from all concurrent
2932
2980
operations which matter.
2934
if (open_tables(thd, &src_table, ¬_used, 0))
2982
if (open_tables(session, &src_table, ¬_used, 0))
2937
strxmov(src_path, src_table->table->s->path.str, reg_ext, NULL);
2985
sprintf(src_path,"%s%s",src_table->table->s->path.str, reg_ext);
2940
2988
Check that destination tables does not exist. Note that its name
2941
2989
was already checked when it was added to the table list.
2943
2991
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
2945
if (find_temporary_table(thd, db, table_name))
2993
if (find_temporary_table(session, db, table_name))
2946
2994
goto table_exists;
2947
dst_path_length= build_tmptable_filename(thd, dst_path, sizeof(dst_path));
2995
dst_path_length= build_tmptable_filename(session, dst_path, sizeof(dst_path));
2948
2996
create_info->table_options|= HA_CREATE_DELAY_KEY_WRITE;
2952
if (lock_table_name_if_not_cached(thd, db, table_name, &name_lock))
3000
if (lock_table_name_if_not_cached(session, db, table_name, &name_lock))
2954
3002
if (!name_lock)
2955
3003
goto table_exists;
2997
3045
and temporary tables).
2999
3047
dst_path[dst_path_length - reg_ext_length]= '\0'; // Remove .frm
3000
if (thd->variables.keep_files_on_create)
3048
if (session->variables.keep_files_on_create)
3001
3049
create_info->options|= HA_CREATE_KEEP_FILES;
3002
err= ha_create_table(thd, dst_path, db, table_name, create_info, 1);
3050
err= ha_create_table(session, dst_path, db, table_name, create_info, 1);
3003
3051
pthread_mutex_unlock(&LOCK_open);
3005
3053
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
3007
if (err || !open_temporary_table(thd, dst_path, db, table_name, 1,
3055
if (err || !open_temporary_table(session, dst_path, db, table_name, 1,
3010
3058
(void) rm_temporary_table(create_info->db_type,
3011
dst_path, false); /* purecov: inspected */
3012
3060
goto err; /* purecov: inspected */
3057
3104
table->table= name_lock;
3058
3105
pthread_mutex_lock(&LOCK_open);
3059
if (reopen_name_locked_table(thd, table, false))
3106
if (reopen_name_locked_table(session, table, false))
3061
3108
pthread_mutex_unlock(&LOCK_open);
3064
3111
pthread_mutex_unlock(&LOCK_open);
3066
int result= store_create_info(thd, table, &query,
3113
int result= store_create_info(session, table, &query,
3069
3116
assert(result == 0); // store_create_info() always return 0
3070
write_bin_log(thd, true, query.ptr(), query.length());
3117
write_bin_log(session, true, query.ptr(), query.length());
3073
write_bin_log(thd, true, thd->query, thd->query_length);
3120
write_bin_log(session, true, session->query, session->query_length);
3076
3123
Case 3 and 4 does nothing under RBR
3080
write_bin_log(thd, true, thd->query, thd->query_length);
3101
3146
pthread_mutex_lock(&LOCK_open);
3102
unlink_open_table(thd, name_lock, false);
3147
unlink_open_table(session, name_lock, false);
3103
3148
pthread_mutex_unlock(&LOCK_open);
3109
bool mysql_analyze_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
3154
bool mysql_analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
3111
3156
thr_lock_type lock_type = TL_READ_NO_INSERT;
3113
return(mysql_admin_table(thd, tables, check_opt,
3158
return(mysql_admin_table(session, tables, check_opt,
3114
3159
"analyze", lock_type, 1, 0, 0, 0,
3115
3160
&handler::ha_analyze));
3119
bool mysql_check_table(THD* thd, TableList* tables,HA_CHECK_OPT* check_opt)
3164
bool mysql_check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
3121
3166
thr_lock_type lock_type = TL_READ_NO_INSERT;
3123
return(mysql_admin_table(thd, tables, check_opt,
3168
return(mysql_admin_table(session, tables, check_opt,
3124
3169
"check", lock_type,
3125
3170
0, 0, HA_OPEN_FOR_REPAIR, 0,
3126
3171
&handler::ha_check));
3150
3195
We set this flag so that ha_innobase::open and ::external_lock() do
3151
3196
not complain when we lock the table
3153
thd->tablespace_op= true;
3154
if (!(table=open_ltable(thd, table_list, TL_WRITE, 0)))
3198
session->tablespace_op= true;
3199
if (!(table=open_ltable(session, table_list, TL_WRITE, 0)))
3156
thd->tablespace_op=false;
3201
session->tablespace_op=false;
3160
3205
error= table->file->ha_discard_or_import_tablespace(discard);
3162
thd_proc_info(thd, "end");
3207
session->set_proc_info("end");
3167
3212
/* The ALTER Table is always in its own transaction */
3168
error = ha_autocommit_or_rollback(thd, 0);
3169
if (end_active_trans(thd))
3213
error = ha_autocommit_or_rollback(session, 0);
3214
if (end_active_trans(session))
3173
write_bin_log(thd, false, thd->query, thd->query_length);
3218
write_bin_log(session, false, session->query, session->query_length);
3176
ha_autocommit_or_rollback(thd, error);
3177
thd->tablespace_op=false;
3221
ha_autocommit_or_rollback(session, error);
3222
session->tablespace_op=false;
3179
3224
if (error == 0)
3185
3230
table->file->print_error(error, MYF(0));
3751
3790
char tmp_name[80];
3752
3791
char path[FN_REFLEN];
3754
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx",
3755
tmp_file_prefix, current_pid, thd->thread_id);
3793
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64,
3794
TMP_FILE_PREFIX, (unsigned long)current_pid, session->thread_id);
3756
3795
/* Safety fix for InnoDB */
3757
3796
if (lower_case_table_names)
3758
3797
my_casedn_str(files_charset_info, tmp_name);
3759
3798
altered_create_info.options&= ~HA_LEX_CREATE_TMP_TABLE;
3760
altered_create_info.frm_only= 1;
3761
if ((error= create_temporary_table(thd, table, new_db, tmp_name,
3800
if ((error= create_temporary_table(session, table, new_db, tmp_name,
3762
3801
&altered_create_info,
3763
3802
alter_info, db_change)))
3846
3885
and will be renamed to the original table name.
3848
3887
pthread_mutex_lock(&LOCK_open);
3849
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
3888
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
3850
3889
alter_table_manage_keys(table, table->file->indexes_are_disabled(),
3852
close_data_files_and_morph_locks(thd,
3891
close_data_files_and_morph_locks(session,
3853
3892
table->pos_in_table_list->db,
3854
3893
table->pos_in_table_list->table_name);
3855
3894
if (mysql_rename_table(NULL,
4361
bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
4409
bool mysql_alter_table(Session *session,char *new_db, char *new_name,
4362
4410
HA_CREATE_INFO *create_info,
4363
4411
TableList *table_list,
4364
4412
Alter_info *alter_info,
4365
4413
uint32_t order_num, order_st *order, bool ignore)
4367
4415
Table *table, *new_table=0, *name_lock= 0;;
4416
string new_name_str;
4369
4418
char tmp_name[80],old_name[32],new_name_buff[FN_REFLEN];
4370
4419
char new_alias_buff[FN_REFLEN], *table_name, *db, *new_alias, *alias;
4371
4420
char path[FN_REFLEN];
4372
4421
ha_rows copied= 0,deleted= 0;
4373
4422
handlerton *old_db_type, *new_db_type, *save_old_db_type;
4374
legacy_db_type table_type;
4424
new_name_buff[0]= '\0';
4376
4426
if (table_list && table_list->schema_table)
4378
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
4428
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
4393
4443
build_table_filename(path, sizeof(path), db, table_name, "", 0);
4395
mysql_ha_rm_tables(thd, table_list, false);
4445
mysql_ha_rm_tables(session, table_list, false);
4397
4447
/* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
4398
4448
if (alter_info->tablespace_op != NO_TABLESPACE_OP)
4399
4449
/* Conditionally writes to binlog. */
4400
return(mysql_discard_or_import_tablespace(thd,table_list,
4450
return(mysql_discard_or_import_tablespace(session,table_list,
4401
4451
alter_info->tablespace_op));
4402
strxnmov(new_name_buff, sizeof (new_name_buff) - 1, mysql_data_home, "/", db,
4403
"/", table_name, reg_ext, NULL);
4404
(void) unpack_filename(new_name_buff, new_name_buff);
4453
oss << drizzle_data_home << "/" << db << "/" << table_name << reg_ext;
4455
(void) unpack_filename(new_name_buff, oss.str().c_str());
4406
4457
If this is just a rename of a view, short cut to the
4407
4458
following scenario: 1) lock LOCK_open 2) do a RENAME
4415
4466
into the main table list, like open_tables does).
4416
4467
This code is wrong and will be removed, please do not copy.
4418
(void)mysql_frm_type(thd, new_name_buff, &table_type);
4420
if (!(table= open_n_lock_single_table(thd, table_list, TL_WRITE_ALLOW_READ)))
4470
if (!(table= open_n_lock_single_table(session, table_list, TL_WRITE_ALLOW_READ)))
4422
4472
table->use_all_columns();
4424
4474
/* Check that we are not trying to rename to an existing table */
4427
my_stpcpy(new_name_buff,new_name);
4428
my_stpcpy(new_alias= new_alias_buff, new_name);
4477
strcpy(new_name_buff,new_name);
4478
strcpy(new_alias= new_alias_buff, new_name);
4429
4479
if (lower_case_table_names)
4431
4481
if (lower_case_table_names != 2)
4433
my_casedn_str(files_charset_info, new_name_buff);
4434
new_alias= new_name; // Create lower case table name
4483
my_casedn_str(files_charset_info, new_name_buff);
4484
new_alias= new_name; // Create lower case table name
4436
4486
my_casedn_str(files_charset_info, new_name);
4502
4552
if (create_info->row_type == ROW_TYPE_NOT_USED)
4503
4553
create_info->row_type= table->s->row_type;
4505
if (ha_check_storage_engine_flag(old_db_type, HTON_ALTER_NOT_SUPPORTED) ||
4506
ha_check_storage_engine_flag(new_db_type, HTON_ALTER_NOT_SUPPORTED))
4555
if (ha_check_storage_engine_flag(old_db_type, HTON_BIT_ALTER_NOT_SUPPORTED) ||
4556
ha_check_storage_engine_flag(new_db_type, HTON_BIT_ALTER_NOT_SUPPORTED))
4508
4558
my_error(ER_ILLEGAL_HA, MYF(0), table_name);
4512
thd_proc_info(thd, "setup");
4562
session->set_proc_info("setup");
4513
4563
if (!(alter_info->flags & ~(ALTER_RENAME | ALTER_KEYS_ONOFF)) &&
4514
4564
!table->s->tmp_table) // no need to touch frm
4527
4577
from concurrent DDL statements.
4529
4579
pthread_mutex_lock(&LOCK_open);
4530
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4580
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4531
4581
pthread_mutex_unlock(&LOCK_open);
4532
4582
error= table->file->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4533
4583
/* COND_refresh will be signaled in close_thread_tables() */
4536
4586
pthread_mutex_lock(&LOCK_open);
4537
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4587
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4538
4588
pthread_mutex_unlock(&LOCK_open);
4539
4589
error=table->file->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4540
4590
/* COND_refresh will be signaled in close_thread_tables() */
4638
4688
new_db_type= create_info->db_type;
4640
if (mysql_prepare_alter_table(thd, table, create_info, alter_info))
4690
if (mysql_prepare_alter_table(session, table, create_info, alter_info))
4643
set_table_default_charset(thd, create_info, db);
4646
if (thd->variables.old_alter_table
4693
set_table_default_charset(session, create_info, db);
4696
if (session->variables.old_alter_table
4647
4697
|| (table->s->db_type() != create_info->db_type)
4650
4700
if (alter_info->build_method == HA_BUILD_ONLINE)
4652
my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
4702
my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
4655
4705
alter_info->build_method= HA_BUILD_OFFLINE;
4720
4770
case HA_ALTER_NOT_SUPPORTED:
4721
4771
if (alter_info->build_method == HA_BUILD_ONLINE)
4723
my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
4724
close_temporary_table(thd, altered_table, 1, 1);
4773
my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
4774
close_temporary_table(session, altered_table, 1, 1);
4727
4777
need_copy_table= true;
4729
4779
case HA_ALTER_ERROR:
4731
close_temporary_table(thd, altered_table, 1, 1);
4781
close_temporary_table(session, altered_table, 1, 1);
4772
4822
if (altered_table)
4773
close_temporary_table(thd, altered_table, 1, 1);
4823
close_temporary_table(session, altered_table, 1, 1);
4776
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx", tmp_file_prefix,
4777
current_pid, thd->thread_id);
4826
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, TMP_FILE_PREFIX,
4827
(unsigned long)current_pid, session->thread_id);
4778
4828
/* Safety fix for innodb */
4779
4829
if (lower_case_table_names)
4780
4830
my_casedn_str(files_charset_info, tmp_name);
4783
4833
/* Create a temporary table with the new format */
4784
if ((error= create_temporary_table(thd, table, new_db, tmp_name,
4785
create_info, alter_info,
4834
if ((error= create_temporary_table(session, table, new_db, tmp_name,
4835
create_info, alter_info,
4786
4836
!strcmp(db, new_db))))
4805
4855
build_table_filename(path, sizeof(path), new_db, tmp_name, "",
4807
4857
/* Open our intermediate table */
4808
new_table=open_temporary_table(thd, path, new_db, tmp_name, 0, OTM_OPEN);
4858
new_table=open_temporary_table(session, path, new_db, tmp_name, 0, OTM_OPEN);
4810
4860
if (!new_table)
4813
4863
/* Copy the data if necessary. */
4814
thd->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields
4815
thd->cuted_fields=0L;
4816
thd_proc_info(thd, "copy to tmp table");
4864
session->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields
4865
session->cuted_fields=0L;
4866
session->set_proc_info("copy to tmp table");
4817
4867
copied=deleted=0;
4819
4869
We do not copy data for MERGE tables. Only the children have data.
4835
4885
pthread_mutex_lock(&LOCK_open);
4836
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4886
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4837
4887
pthread_mutex_unlock(&LOCK_open);
4838
4888
alter_table_manage_keys(table, table->file->indexes_are_disabled(),
4839
4889
alter_info->keys_onoff);
4840
error= ha_autocommit_or_rollback(thd, 0);
4841
if (end_active_trans(thd))
4890
error= ha_autocommit_or_rollback(session, 0);
4891
if (end_active_trans(session))
4844
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
4894
session->count_cuted_fields= CHECK_FIELD_IGNORE;
4846
4896
if (table->s->tmp_table != NO_TMP_TABLE)
4851
4901
/* Close lock if this is a transactional table */
4854
mysql_unlock_tables(thd, thd->lock);
4904
mysql_unlock_tables(session, session->lock);
4857
4907
/* Remove link to old table and rename the new one */
4858
close_temporary_table(thd, table, 1, 1);
4908
close_temporary_table(session, table, 1, 1);
4859
4909
/* Should pass the 'new_name' as we store table name in the cache */
4860
if (rename_temporary_table(thd, new_table, new_db, new_name))
4910
if (rename_temporary_table(session, new_table, new_db, new_name))
4862
/* We don't replicate alter table statement on temporary tables */
4863
if (!thd->current_stmt_binlog_row_based)
4864
write_bin_log(thd, true, thd->query, thd->query_length);
4865
4912
goto end_temporary;
4898
4945
call to remove name-locks from table cache and list of open table.
4901
thd_proc_info(thd, "rename result table");
4902
snprintf(old_name, sizeof(old_name), "%s2-%lx-%lx", tmp_file_prefix,
4903
current_pid, thd->thread_id);
4948
session->set_proc_info("rename result table");
4949
snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, TMP_FILE_PREFIX,
4950
(unsigned long)current_pid, session->thread_id);
4904
4951
if (lower_case_table_names)
4905
4952
my_casedn_str(files_charset_info, old_name);
4907
wait_while_table_is_used(thd, table, HA_EXTRA_PREPARE_FOR_RENAME);
4908
close_data_files_and_morph_locks(thd, db, table_name);
4954
wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
4955
close_data_files_and_morph_locks(session, db, table_name);
4911
4958
save_old_db_type= old_db_type;
4949
4996
quick_rm_table(old_db_type, db, old_name, FN_IS_TMP);
4952
if (thd->locked_tables && new_name == table_name && new_db == db)
4999
if (session->locked_tables && new_name == table_name && new_db == db)
4954
thd->in_lock_tables= 1;
4955
error= reopen_tables(thd, 1, 1);
4956
thd->in_lock_tables= 0;
5001
session->in_lock_tables= 1;
5002
error= reopen_tables(session, 1, 1);
5003
session->in_lock_tables= 0;
4958
5005
goto err_with_placeholders;
4960
5007
pthread_mutex_unlock(&LOCK_open);
4962
thd_proc_info(thd, "end");
5009
session->set_proc_info("end");
4964
assert(!(mysql_bin_log.is_open() &&
4965
thd->current_stmt_binlog_row_based &&
5011
assert(!(drizzle_bin_log.is_open() &&
4966
5012
(create_info->options & HA_LEX_CREATE_TMP_TABLE)));
4967
write_bin_log(thd, true, thd->query, thd->query_length);
5013
write_bin_log(session, true, session->query, session->query_length);
4969
if (ha_check_storage_engine_flag(old_db_type, HTON_FLUSH_AFTER_RENAME))
5015
if (ha_check_storage_engine_flag(old_db_type, HTON_BIT_FLUSH_AFTER_RENAME))
4972
5018
For the alter table to be properly flushed to the logs, we
4998
5044
LOCK TABLES we can rely on close_thread_tables() doing this job.
5000
5046
pthread_mutex_lock(&LOCK_open);
5001
unlink_open_table(thd, table, false);
5002
unlink_open_table(thd, name_lock, false);
5047
unlink_open_table(session, table, false);
5048
unlink_open_table(session, name_lock, false);
5003
5049
pthread_mutex_unlock(&LOCK_open);
5007
5053
snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
5008
5054
(ulong) (copied + deleted), (ulong) deleted,
5009
(ulong) thd->cuted_fields);
5010
my_ok(thd, copied + deleted, 0L, tmp_name);
5011
thd->some_tables_deleted=0;
5055
(ulong) session->cuted_fields);
5056
my_ok(session, copied + deleted, 0L, tmp_name);
5057
session->some_tables_deleted=0;
5017
5063
/* close_temporary_table() frees the new_table pointer. */
5018
close_temporary_table(thd, new_table, 1, 1);
5064
close_temporary_table(session, new_table, 1, 1);
5021
5067
quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
5045
5091
/* Shouldn't get here. */
5048
bool save_abort_on_warning= thd->abort_on_warning;
5049
thd->abort_on_warning= true;
5050
make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
5094
bool save_abort_on_warning= session->abort_on_warning;
5095
session->abort_on_warning= true;
5096
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
5051
5097
f_val, strlength(f_val), t_type,
5052
5098
alter_info->datetime_field->field_name);
5053
thd->abort_on_warning= save_abort_on_warning;
5099
session->abort_on_warning= save_abort_on_warning;
5057
5103
pthread_mutex_lock(&LOCK_open);
5058
unlink_open_table(thd, name_lock, false);
5104
unlink_open_table(session, name_lock, false);
5059
5105
pthread_mutex_unlock(&LOCK_open);
5103
5149
Turn off recovery logging since rollback of an alter table is to
5104
5150
delete the new table so there is no need to log the changes to it.
5106
5152
This needs to be done before external_lock
5108
error= ha_enable_transaction(thd, false);
5154
error= ha_enable_transaction(session, false);
5112
5158
if (!(copy= new Copy_field[to->s->fields]))
5113
5159
return(-1); /* purecov: inspected */
5115
if (to->file->ha_external_lock(thd, F_WRLCK))
5161
if (to->file->ha_external_lock(session, F_WRLCK))
5118
5164
/* We need external lock before we can disable/enable keys */
5119
5165
alter_table_manage_keys(to, from->file->indexes_are_disabled(), keys_onoff);
5121
5167
/* We can abort alter table for any table type */
5122
thd->abort_on_warning= !ignore;
5168
session->abort_on_warning= !ignore;
5124
5170
from->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
5125
5171
to->file->ha_start_bulk_insert(from->file->stats.records);
5127
save_sql_mode= thd->variables.sql_mode;
5173
save_sql_mode= session->variables.sql_mode;
5129
5175
List_iterator<Create_field> it(create);
5130
5176
Create_field *def;
5149
5195
if (to->s->primary_key != MAX_KEY && to->file->primary_key_is_clustered())
5151
5197
char warn_buff[DRIZZLE_ERRMSG_SIZE];
5152
snprintf(warn_buff, sizeof(warn_buff),
5198
snprintf(warn_buff, sizeof(warn_buff),
5153
5199
_("order_st BY ignored because there is a user-defined clustered "
5154
5200
"index in the table '%-.192s'"),
5155
5201
from->s->table_name.str);
5156
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
5202
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
5161
from->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
5162
MYF(MY_FAE | MY_ZEROFILL));
5207
from->sort.io_cache= new IO_CACHE;
5208
memset(from->sort.io_cache, 0, sizeof(IO_CACHE));
5163
5210
memset(&tables, 0, sizeof(tables));
5164
5211
tables.table= from;
5165
5212
tables.alias= tables.table_name= from->s->table_name.str;
5166
5213
tables.db= from->s->db.str;
5169
if (thd->lex->select_lex.setup_ref_array(thd, order_num) ||
5170
setup_order(thd, thd->lex->select_lex.ref_pointer_array,
5216
if (session->lex->select_lex.setup_ref_array(session, order_num) ||
5217
setup_order(session, session->lex->select_lex.ref_pointer_array,
5171
5218
&tables, fields, all_fields, order) ||
5172
5219
!(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
5173
(from->sort.found_records= filesort(thd, from, sortorder, length,
5220
(from->sort.found_records= filesort(session, from, sortorder, length,
5174
5221
(SQL_SELECT *) 0, HA_POS_ERROR,
5175
5222
1, &examined_rows)) ==
5181
5228
/* Tell handler that we have values for all columns in the to table */
5182
5229
to->use_all_columns();
5183
init_read_record(&info, thd, from, (SQL_SELECT *) 0, 1,1);
5230
init_read_record(&info, session, from, (SQL_SELECT *) 0, 1,1);
5185
5232
to->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
5233
session->row_count= 0;
5187
5234
restore_record(to, s->default_values); // Create empty record
5188
5235
while (!(error=info.read_record(&info)))
5237
if (session->killed)
5192
thd->send_kill_message();
5239
session->send_kill_message();
5243
session->row_count++;
5197
5244
/* Return error if source table isn't empty. */
5198
5245
if (error_if_not_empty)
5265
5313
Ensure that the new table is saved properly to disk so that we
5266
5314
can do a rename
5268
if (ha_autocommit_or_rollback(thd, 0))
5316
if (ha_autocommit_or_rollback(session, 0))
5270
if (end_active_trans(thd))
5318
if (end_active_trans(session))
5274
thd->variables.sql_mode= save_sql_mode;
5275
thd->abort_on_warning= 0;
5322
session->variables.sql_mode= save_sql_mode;
5323
session->abort_on_warning= 0;
5276
5324
free_io_cache(from);
5277
5325
*copied= found_count;
5278
5326
*deleted=delete_count;
5279
5327
to->file->ha_release_auto_increment();
5280
if (to->file->ha_external_lock(thd,F_UNLCK))
5328
if (to->file->ha_external_lock(session,F_UNLCK))
5282
5330
return(error > 0 ? -1 : 0);
5311
5359
create_info.default_table_charset=default_charset_info;
5312
5360
/* Force alter table to recreate table */
5313
5361
alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
5314
return(mysql_alter_table(thd, NULL, NULL, &create_info,
5362
return(mysql_alter_table(session, NULL, NULL, &create_info,
5315
5363
table_list, &alter_info, 0,
5316
5364
(order_st *) 0, 0));
5320
bool mysql_checksum_table(THD *thd, TableList *tables,
5368
bool mysql_checksum_table(Session *session, TableList *tables,
5321
5369
HA_CHECK_OPT *check_opt)
5323
5371
TableList *table;
5324
5372
List<Item> field_list;
5326
Protocol *protocol= thd->protocol;
5374
Protocol *protocol= session->protocol;
5328
5376
field_list.push_back(item = new Item_empty_string("Table", NAME_LEN*2));
5329
5377
item->maybe_null= 1;
5340
5388
char table_name[NAME_LEN*2+2];
5343
strxmov(table_name, table->db ,".", table->table_name, NULL);
5391
sprintf(table_name,"%s.%s",table->db,table->table_name);
5345
t= table->table= open_n_lock_single_table(thd, table, TL_READ);
5346
thd->clear_error(); // these errors shouldn't get client
5393
t= table->table= open_n_lock_single_table(session, table, TL_READ);
5394
session->clear_error(); // these errors shouldn't get client
5348
5396
protocol->prepare_for_resend();
5349
5397
protocol->store(table_name, system_charset_info);
5415
5463
t->file->ha_rnd_end();
5419
close_thread_tables(thd);
5466
session->clear_error();
5467
close_thread_tables(session);
5420
5468
table->table=0; // For query cache
5422
5470
if (protocol->write())
5430
close_thread_tables(thd); // Shouldn't be needed
5478
close_thread_tables(session); // Shouldn't be needed
5432
5480
table->table=0;
5436
static bool check_engine(THD *thd, const char *table_name,
5484
static bool check_engine(Session *session, const char *table_name,
5437
5485
HA_CREATE_INFO *create_info)
5439
5487
handlerton **new_engine= &create_info->db_type;
5440
5488
handlerton *req_engine= *new_engine;
5441
5489
bool no_substitution= 1;
5442
if (!(*new_engine= ha_checktype(thd, ha_legacy_type(req_engine),
5490
if (!(*new_engine= ha_checktype(session, ha_legacy_type(req_engine),
5443
5491
no_substitution, 1)))
5446
5494
if (req_engine && req_engine != *new_engine)
5448
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
5496
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
5449
5497
ER_WARN_USING_OTHER_HANDLER,
5450
5498
ER(ER_WARN_USING_OTHER_HANDLER),
5451
5499
ha_resolve_storage_engine_name(*new_engine),
5454
5502
if (create_info->options & HA_LEX_CREATE_TMP_TABLE &&
5455
ha_check_storage_engine_flag(*new_engine, HTON_TEMPORARY_NOT_SUPPORTED))
5503
ha_check_storage_engine_flag(*new_engine, HTON_BIT_TEMPORARY_NOT_SUPPORTED))
5457
5505
if (create_info->used_fields & HA_CREATE_USED_ENGINE)