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/lock.h>
31
#include <drizzled/unireg.h>
32
#include <drizzled/item/int.h>
33
#include <drizzled/item/empty_string.h>
34
#include <drizzled/replicator.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);
81
static void set_table_default_charset(Session *session,
82
HA_CREATE_INFO *create_info, char *db)
85
If the table character set was not given explicitly,
86
let's fetch the database default character set and
87
apply it to the table.
89
if (!create_info->default_table_charset)
91
HA_CREATE_INFO db_info;
93
load_db_opt_by_name(session, db, &db_info);
95
create_info->default_table_charset= db_info.default_table_charset;
52
100
Translate a file name to a table name (WL #1324).
102
150
uint32_t tablename_to_filename(const char *from, char *to, uint32_t to_length)
104
152
uint32_t errors, length;
106
154
if (from[0] == '#' && !strncmp(from, MYSQL50_TABLE_NAME_PREFIX,
107
155
MYSQL50_TABLE_NAME_PREFIX_LENGTH))
108
return((uint) (strmake(to, from+MYSQL50_TABLE_NAME_PREFIX_LENGTH,
110
(from + MYSQL50_TABLE_NAME_PREFIX_LENGTH)));
156
return((uint) (strncpy(to, from+MYSQL50_TABLE_NAME_PREFIX_LENGTH,
158
(from + MYSQL50_TABLE_NAME_PREFIX_LENGTH)));
111
159
length= strconvert(system_charset_info, from,
112
160
&my_charset_filename, to, to_length, &errors);
113
161
if (check_if_legal_tablename(to) &&
150
198
build_tmptable_filename() for them.
201
path length on success, 0 on failure
156
204
uint32_t build_table_filename(char *buff, size_t bufflen, const char *db,
157
205
const char *table_name, const char *ext, uint32_t flags)
159
208
char dbbuff[FN_REFLEN];
160
209
char tbbuff[FN_REFLEN];
210
int rootdir_len= strlen(FN_ROOTDIR);
162
212
if (flags & FN_IS_TMP) // FN_FROM_IS_TMP | FN_TO_IS_TMP
163
my_stpncpy(tbbuff, table_name, sizeof(tbbuff));
213
strncpy(tbbuff, table_name, sizeof(tbbuff));
165
215
tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
167
217
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);
218
table_path= drizzle_data_home;
219
int without_rootdir= table_path.length()-rootdir_len;
221
/* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
222
if (without_rootdir >= 0)
224
char *tmp= (char*)table_path.c_str()+without_rootdir;
225
if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
226
table_path.append(FN_ROOTDIR);
229
table_path.append(dbbuff);
230
table_path.append(FN_ROOTDIR);
177
231
#ifdef USE_SYMDIR
178
unpack_dirname(buff, buff);
181
pos= strxnmov(pos, end - pos, tbbuff, ext, NULL);
234
table_path.append(tbbuff);
235
table_path.append(ext);
237
if (bufflen < table_path.length())
240
strcpy(buff, table_path.c_str());
241
return table_path.length();
188
Creates path to a file: mysql_tmpdir/#sql1234_12_1.ext
246
Creates path to a file: drizzle_tmpdir/#sql1234_12_1.ext
191
249
build_tmptable_filename()
192
thd The thread handle.
250
session The thread handle.
193
251
buff Where to write result in my_charset_filename.
194
252
bufflen buff size
198
256
Uses current_pid, thread_id, and tmp_table counter to create
199
a file name in mysql_tmpdir.
257
a file name in drizzle_tmpdir.
260
path length on success, 0 on failure
205
uint32_t build_tmptable_filename(THD* thd, char *buff, size_t bufflen)
263
uint32_t build_tmptable_filename(Session* session, char *buff, size_t bufflen)
266
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);
269
path_str << drizzle_tmpdir;
270
post_tmpdir_str << "/" << TMP_FILE_PREFIX << current_pid;
271
post_tmpdir_str << session->thread_id << session->tmp_table++ << reg_ext;
272
tmp= post_tmpdir_str.str();
213
274
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);
275
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
279
if (bufflen < path_str.str().length())
282
length= unpack_filename(buff, path_str.str().c_str());
290
session Thread object
227
291
clear_error is clear_error to be called
228
292
query Query to log
229
293
query_length Length of query
338
396
-1 Thread was killed
341
int mysql_rm_table_part2(THD *thd, TableList *tables, bool if_exists,
342
bool drop_temporary, bool drop_view,
399
int mysql_rm_table_part2(Session *session, TableList *tables, bool if_exists,
400
bool drop_temporary, bool dont_log_query)
345
402
TableList *table;
346
403
char path[FN_REFLEN], *alias;
347
uint32_t path_length;
404
uint32_t path_length= 0;
348
405
String wrong_tables;
350
407
int non_temp_tables_count= 0;
351
408
bool some_tables_deleted=0, tmp_table_deleted=0, foreign_key_error=0;
352
409
String built_query;
354
if (thd->current_stmt_binlog_row_based && !dont_log_query)
356
413
built_query.set_charset(system_charset_info);
438
492
if (!drop_temporary)
440
494
Table *locked_table;
441
abort_locked_tables(thd, db, table->table_name);
442
remove_table_from_cache(thd, db, table->table_name,
495
abort_locked_tables(session, db, table->table_name);
496
remove_table_from_cache(session, db, table->table_name,
443
497
RTFC_WAIT_OTHER_THREAD_FLAG |
444
498
RTFC_CHECK_KILLED_FLAG);
446
500
If the table was used in lock tables, remember it so that
447
501
unlock_table_names can free it
449
if ((locked_table= drop_locked_tables(thd, db, table->table_name)))
503
if ((locked_table= drop_locked_tables(session, db, table->table_name)))
450
504
table->table= locked_table;
455
509
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
532
// Remove extension for delete
484
533
*(end= path + path_length - reg_ext_length)= '\0';
485
error= ha_delete_table(thd, table_type, path, db, table->table_name,
534
error= ha_delete_table(session, path, db, table->table_name,
486
535
!dont_log_query);
487
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) &&
488
(if_exists || table_type == NULL))
536
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) &&
540
session->clear_error();
493
542
if (error == HA_ERR_ROW_IS_REFERENCED)
1571
Set table default charset, if not set
1574
set_table_default_charset()
1575
create_info Table create information
1578
If the table character set was not given explicitely,
1579
let's fetch the database default character set and
1580
apply it to the table.
1583
static void set_table_default_charset(THD *thd,
1584
HA_CREATE_INFO *create_info, char *db)
1587
If the table character set was not given explicitly,
1588
let's fetch the database default character set and
1589
apply it to the table.
1591
if (!create_info->default_table_charset)
1593
HA_CREATE_INFO db_info;
1595
load_db_opt_by_name(thd, db, &db_info);
1597
create_info->default_table_charset= db_info.default_table_charset;
1603
1658
Extend long VARCHAR fields to blob & prepare field if it's a blob
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
1700
mysql_create_table_no_lock()
1701
session Thread object
1685
1703
table_name Table name
1686
1704
create_info Create information (like MAX_ROWS)
1732
if (check_engine(thd, table_name, create_info))
1751
if (check_engine(session, table_name, create_info))
1734
1753
db_options= create_info->table_options;
1735
1754
if (create_info->row_type == ROW_TYPE_DYNAMIC)
1736
1755
db_options|=HA_OPTION_PACK_RECORD;
1737
1756
alias= table_case_name(create_info, table_name);
1738
if (!(file= get_new_handler((TABLE_SHARE*) 0, thd->mem_root,
1757
if (!(file= get_new_handler((TABLE_SHARE*) 0, session->mem_root,
1739
1758
create_info->db_type)))
1741
1760
my_error(ER_OUTOFMEMORY, MYF(0), sizeof(handler));
1745
set_table_default_charset(thd, create_info, (char*) db);
1764
set_table_default_charset(session, create_info, (char*) db);
1747
if (mysql_prepare_create_table(thd, create_info, alter_info,
1766
if (mysql_prepare_create_table(session, create_info, alter_info,
1748
1767
internal_tmp_table,
1749
1768
&db_options, file,
1750
1769
&key_info_buffer, &key_count,
1864
1884
#endif /* HAVE_READLINK */
1866
1886
if (create_info->data_file_name)
1867
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1887
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1868
1888
"DATA DIRECTORY option ignored");
1869
1889
if (create_info->index_file_name)
1870
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1890
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1871
1891
"INDEX DIRECTORY option ignored");
1872
1892
create_info->data_file_name= create_info->index_file_name= 0;
1874
1894
create_info->table_options=db_options;
1876
1896
path[path_length - reg_ext_length]= '\0'; // Remove .frm extension
1877
if (rea_create_table(thd, path, db, table_name,
1897
if (rea_create_table(session, path, db, table_name,
1878
1898
create_info, alter_info->create_list,
1879
key_count, key_info_buffer, file))
1899
key_count, key_info_buffer, file, false))
1880
1900
goto unlock_and_end;
1882
1902
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1884
1904
/* Open table and put in temporary table list */
1885
if (!(open_temporary_table(thd, path, db, table_name, 1, OTM_OPEN)))
1905
if (!(open_temporary_table(session, path, db, table_name, 1, OTM_OPEN)))
1887
(void) rm_temporary_table(create_info->db_type, path, false);
1907
(void) rm_temporary_table(create_info->db_type, path);
1888
1908
goto unlock_and_end;
1890
thd->thread_specific_used= true;
1910
session->thread_specific_used= true;
1898
1918
Otherwise, the statement shall be binlogged.
1900
1920
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);
1921
((!(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
1922
write_bin_log(session, true, session->query, session->query_length);
1906
1924
unlock_and_end:
1907
pthread_mutex_unlock(&LOCK_open);
1926
pthread_mutex_unlock(&LOCK_open);
1910
thd_proc_info(thd, "After create");
1929
session->set_proc_info("After create");
1916
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1935
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1917
1936
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1919
1938
create_info->table_existed= 1; // Mark that table existed
2090
2111
if (lower_case_table_names == 2 && file &&
2091
2112
!(file->ha_table_flags() & HA_FILE_BASED))
2093
my_stpcpy(tmp_name, old_name);
2114
strcpy(tmp_name, old_name);
2094
2115
my_casedn_str(files_charset_info, tmp_name);
2095
2116
build_table_filename(lc_from, sizeof(lc_from), old_db, tmp_name, "",
2096
2117
flags & FN_FROM_IS_TMP);
2097
2118
from_base= lc_from;
2099
my_stpcpy(tmp_name, new_name);
2120
strcpy(tmp_name, new_name);
2100
2121
my_casedn_str(files_charset_info, tmp_name);
2101
2122
build_table_filename(lc_to, sizeof(lc_to), new_db, tmp_name, "",
2102
2123
flags & FN_TO_IS_TMP);
2174
2204
Win32 clients must also have a WRITE LOCK on the table !
2177
void close_cached_table(THD *thd, Table *table)
2207
void close_cached_table(Session *session, Table *table)
2180
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
2210
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
2181
2211
/* Close lock if this is not got with LOCK TABLES */
2184
mysql_unlock_tables(thd, thd->lock);
2185
thd->lock=0; // Start locked threads
2214
mysql_unlock_tables(session, session->lock);
2215
session->lock=0; // Start locked threads
2187
2217
/* Close all copies of 'table'. This also frees all LOCK TABLES lock */
2188
unlink_open_table(thd, table, true);
2218
unlink_open_table(session, table, true);
2190
2220
/* When lock on LOCK_open is freed other threads can continue */
2191
2221
broadcast_refresh();
2195
static int send_check_errmsg(THD *thd, TableList* table,
2225
static int send_check_errmsg(Session *session, TableList* table,
2196
2226
const char* operator_name, const char* errmsg)
2199
Protocol *protocol= thd->protocol;
2229
Protocol *protocol= session->protocol;
2200
2230
protocol->prepare_for_resend();
2201
2231
protocol->store(table->alias, system_charset_info);
2202
2232
protocol->store((char*) operator_name, system_charset_info);
2203
2233
protocol->store(STRING_WITH_LEN("error"), system_charset_info);
2204
2234
protocol->store(errmsg, system_charset_info);
2235
session->clear_error();
2206
2236
if (protocol->write())
2212
static int prepare_for_repair(THD *thd, TableList *table_list,
2242
static int prepare_for_repair(Session *session, TableList *table_list,
2213
2243
HA_CHECK_OPT *check_opt)
2227
2257
char key[MAX_DBKEY_LENGTH];
2228
2258
uint32_t key_length;
2230
key_length= create_table_def_key(thd, key, table_list, 0);
2260
key_length= create_table_def_key(session, key, table_list, 0);
2231
2261
pthread_mutex_lock(&LOCK_open);
2232
if (!(share= (get_table_share(thd, table_list, key, key_length, 0,
2262
if (!(share= (get_table_share(session, table_list, key, key_length, 0,
2235
2265
pthread_mutex_unlock(&LOCK_open);
2236
2266
return(0); // Can't open frm file
2239
if (open_table_from_share(thd, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2269
if (open_table_from_share(session, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2241
2271
release_table_share(share, RELEASE_NORMAL);
2242
2272
pthread_mutex_unlock(&LOCK_open);
2270
2300
Check if this is a table type that stores index and data separately,
2271
2301
like ISAM or MyISAM. We assume fixed order of engine file name
2272
2302
extentions array. First element of engine file name extentions array
2273
is meta/index file extention. Second element - data file extention.
2303
is meta/index file extention. Second element - data file extention.
2275
2305
ext= table->file->bas_ext();
2276
2306
if (!ext[0] || !ext[1])
2277
2307
goto end; // No data file
2279
2309
// Name of data file
2280
strxmov(from, table->s->normalized_path.str, ext[1], NULL);
2310
sprintf(from,"%s%s", table->s->normalized_path.str, ext[1]);
2281
2311
if (stat(from, &stat_info))
2282
2312
goto end; // Can't use USE_FRM flag
2284
snprintf(tmp, sizeof(tmp), "%s-%lx_%lx",
2285
from, current_pid, thd->thread_id);
2314
snprintf(tmp, sizeof(tmp), "%s-%lx_%"PRIx64,
2315
from, (unsigned long)current_pid, session->thread_id);
2287
2317
/* If we could open the table, close it */
2288
2318
if (table_list->table)
2290
2320
pthread_mutex_lock(&LOCK_open);
2291
close_cached_table(thd, table);
2321
close_cached_table(session, table);
2292
2322
pthread_mutex_unlock(&LOCK_open);
2294
if (lock_and_wait_for_table_name(thd,table_list))
2324
if (lock_and_wait_for_table_name(session,table_list))
2299
2329
if (my_rename(from, tmp, MYF(MY_WME)))
2301
2331
pthread_mutex_lock(&LOCK_open);
2302
unlock_table_name(thd, table_list);
2332
unlock_table_name(session, table_list);
2303
2333
pthread_mutex_unlock(&LOCK_open);
2304
error= send_check_errmsg(thd, table_list, "repair",
2334
error= send_check_errmsg(session, table_list, "repair",
2305
2335
"Failed renaming data file");
2308
if (mysql_truncate(thd, table_list, 1))
2338
if (mysql_truncate(session, table_list, 1))
2310
2340
pthread_mutex_lock(&LOCK_open);
2311
unlock_table_name(thd, table_list);
2341
unlock_table_name(session, table_list);
2312
2342
pthread_mutex_unlock(&LOCK_open);
2313
error= send_check_errmsg(thd, table_list, "repair",
2343
error= send_check_errmsg(session, table_list, "repair",
2314
2344
"Failed generating table from .frm file");
2317
2347
if (my_rename(tmp, from, MYF(MY_WME)))
2319
2349
pthread_mutex_lock(&LOCK_open);
2320
unlock_table_name(thd, table_list);
2350
unlock_table_name(session, table_list);
2321
2351
pthread_mutex_unlock(&LOCK_open);
2322
error= send_check_errmsg(thd, table_list, "repair",
2352
error= send_check_errmsg(session, table_list, "repair",
2323
2353
"Failed restoring .MYD file");
2329
2359
to finish the repair in the handler later on.
2331
2361
pthread_mutex_lock(&LOCK_open);
2332
if (reopen_name_locked_table(thd, table_list, true))
2362
if (reopen_name_locked_table(session, table_list, true))
2334
unlock_table_name(thd, table_list);
2364
unlock_table_name(session, table_list);
2335
2365
pthread_mutex_unlock(&LOCK_open);
2336
error= send_check_errmsg(thd, table_list, "repair",
2366
error= send_check_errmsg(session, table_list, "repair",
2337
2367
"Failed to open partially repaired table");
2356
2386
false Message sent to net (admin operation went ok)
2357
true Message should be sent by caller
2387
true Message should be sent by caller
2358
2388
(admin operation or network communication failed)
2360
static bool mysql_admin_table(THD* thd, TableList* tables,
2390
static bool mysql_admin_table(Session* session, TableList* tables,
2361
2391
HA_CHECK_OPT* check_opt,
2362
2392
const char *operator_name,
2363
2393
thr_lock_type lock_type,
2364
2394
bool open_for_modify,
2365
2395
bool no_warnings_for_error,
2366
2396
uint32_t extra_open_options,
2367
int (*prepare_func)(THD *, TableList *,
2397
int (*prepare_func)(Session *, TableList *,
2368
2398
HA_CHECK_OPT *),
2369
int (handler::*operator_func)(THD *,
2399
int (handler::*operator_func)(Session *,
2370
2400
HA_CHECK_OPT *))
2372
2402
TableList *table;
2373
SELECT_LEX *select= &thd->lex->select_lex;
2403
Select_Lex *select= &session->lex->select_lex;
2374
2404
List<Item> field_list;
2376
Protocol *protocol= thd->protocol;
2406
Protocol *protocol= session->protocol;
2407
LEX *lex= session->lex;
2378
2408
int result_code= 0;
2379
2409
const CHARSET_INFO * const cs= system_charset_info;
2381
if (end_active_trans(thd))
2411
if (end_active_trans(session))
2383
2413
field_list.push_back(item = new Item_empty_string("Table",
2384
2414
NAME_CHAR_LEN * 2,
2422
2452
lex->query_tables= table;
2423
2453
lex->query_tables_last= &table->next_global;
2424
2454
lex->query_tables_own_last= 0;
2425
thd->no_warnings_for_error= no_warnings_for_error;
2455
session->no_warnings_for_error= no_warnings_for_error;
2427
open_and_lock_tables(thd, table);
2428
thd->no_warnings_for_error= 0;
2457
open_and_lock_tables(session, table);
2458
session->no_warnings_for_error= 0;
2429
2459
table->next_global= save_next_global;
2430
2460
table->next_local= save_next_local;
2431
thd->open_options&= ~extra_open_options;
2461
session->open_options&= ~extra_open_options;
2434
2464
if (prepare_func)
2436
switch ((*prepare_func)(thd, table, check_opt)) {
2466
switch ((*prepare_func)(session, table, check_opt)) {
2437
2467
case 1: // error, message written to net
2438
ha_autocommit_or_rollback(thd, 1);
2439
end_trans(thd, ROLLBACK);
2440
close_thread_tables(thd);
2468
ha_autocommit_or_rollback(session, 1);
2469
end_trans(session, ROLLBACK);
2470
close_thread_tables(session);
2442
2472
case -1: // error, message could be written to net
2443
2473
/* purecov: begin inspected */
2491
2521
if (lock_type == TL_WRITE && table->table->s->version)
2493
2523
pthread_mutex_lock(&LOCK_open);
2494
const char *old_message=thd->enter_cond(&COND_refresh, &LOCK_open,
2524
const char *old_message=session->enter_cond(&COND_refresh, &LOCK_open,
2495
2525
"Waiting to get writelock");
2496
mysql_lock_abort(thd,table->table, true);
2497
remove_table_from_cache(thd, table->table->s->db.str,
2526
mysql_lock_abort(session,table->table, true);
2527
remove_table_from_cache(session, table->table->s->db.str,
2498
2528
table->table->s->table_name.str,
2499
2529
RTFC_WAIT_OTHER_THREAD_FLAG |
2500
2530
RTFC_CHECK_KILLED_FLAG);
2501
thd->exit_cond(old_message);
2531
session->exit_cond(old_message);
2532
if (session->killed)
2504
2534
open_for_modify= 0;
2518
2548
/* purecov: end */
2521
if (operator_func == &handler::ha_repair &&
2522
!(check_opt->sql_flags & TT_USEFRM))
2551
if (operator_func == &handler::ha_repair && !(check_opt->use_frm))
2524
2553
if ((table->table->file->check_old_types() == HA_ADMIN_NEEDS_ALTER) ||
2525
2554
(table->table->file->ha_check_for_upgrade(check_opt) ==
2526
2555
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);
2557
ha_autocommit_or_rollback(session, 1);
2558
close_thread_tables(session);
2559
result_code= mysql_recreate_table(session, table);
2534
2561
mysql_recreate_table() can push OK or ERROR.
2535
2562
Clear 'OK' status. If there is an error, keep it:
2536
we will store the error message in a result set row
2563
we will store the error message in a result set row
2537
2564
and then clear.
2539
if (thd->main_da.is_ok())
2540
thd->main_da.reset_diagnostics_area();
2566
if (session->main_da.is_ok())
2567
session->main_da.reset_diagnostics_area();
2541
2568
goto send_result;
2545
result_code = (table->table->file->*operator_func)(thd, check_opt);
2572
result_code = (table->table->file->*operator_func)(session, check_opt);
2549
2576
lex->cleanup_after_one_table_open();
2550
thd->clear_error(); // these errors shouldn't get client
2577
session->clear_error(); // these errors shouldn't get client
2552
List_iterator_fast<DRIZZLE_ERROR> it(thd->warn_list);
2579
List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
2553
2580
DRIZZLE_ERROR *err;
2554
2581
while ((err= it++))
2635
2652
"try with alter", so here we close the table, do an ALTER Table,
2636
2653
reopen the table and do ha_innobase::analyze() on it.
2638
ha_autocommit_or_rollback(thd, 0);
2639
close_thread_tables(thd);
2655
ha_autocommit_or_rollback(session, 0);
2656
close_thread_tables(session);
2640
2657
TableList *save_next_local= table->next_local,
2641
2658
*save_next_global= table->next_global;
2642
2659
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);
2660
result_code= mysql_recreate_table(session, table);
2647
2662
mysql_recreate_table() can push OK or ERROR.
2648
2663
Clear 'OK' status. If there is an error, keep it:
2649
we will store the error message in a result set row
2664
we will store the error message in a result set row
2650
2665
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);
2667
if (session->main_da.is_ok())
2668
session->main_da.reset_diagnostics_area();
2669
ha_autocommit_or_rollback(session, 0);
2670
close_thread_tables(session);
2656
2671
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))
2673
if ((table->table= open_ltable(session, table, lock_type, 0)) &&
2674
((result_code= table->table->file->ha_analyze(session, check_opt)) > 0))
2660
2675
result_code= 0; // analyze went ok
2662
2677
if (result_code) // either mysql_recreate_table or analyze failed
2664
assert(thd->is_error());
2665
if (thd->is_error())
2679
assert(session->is_error());
2680
if (session->is_error())
2667
const char *err_msg= thd->main_da.message();
2682
const char *err_msg= session->main_da.message();
2683
if (!session->drizzleclient_vio_ok())
2670
sql_print_error(err_msg);
2685
errmsg_printf(ERRMSG_LVL_ERROR, "%s", err_msg);
2734
2741
pthread_mutex_lock(&LOCK_open);
2735
remove_table_from_cache(thd, table->table->s->db.str,
2742
remove_table_from_cache(session, table->table->s->db.str,
2736
2743
table->table->s->table_name.str, RTFC_NO_FLAG);
2737
2744
pthread_mutex_unlock(&LOCK_open);
2741
ha_autocommit_or_rollback(thd, 0);
2742
end_trans(thd, COMMIT);
2743
close_thread_tables(thd);
2748
ha_autocommit_or_rollback(session, 0);
2749
end_trans(session, COMMIT);
2750
close_thread_tables(session);
2744
2751
table->table=0; // For query cache
2745
2752
if (protocol->write())
2753
ha_autocommit_or_rollback(thd, 1);
2754
end_trans(thd, ROLLBACK);
2755
close_thread_tables(thd); // Shouldn't be needed
2760
ha_autocommit_or_rollback(session, 1);
2761
end_trans(session, ROLLBACK);
2762
close_thread_tables(session); // Shouldn't be needed
2757
2764
table->table=0;
2762
bool mysql_repair_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
2769
bool mysql_repair_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2764
return(mysql_admin_table(thd, tables, check_opt,
2765
"repair", TL_WRITE, 1,
2766
test(check_opt->sql_flags & TT_USEFRM),
2768
&prepare_for_repair,
2769
&handler::ha_repair));
2771
return(mysql_admin_table(session, tables, check_opt,
2772
"repair", TL_WRITE, 1,
2775
&prepare_for_repair,
2776
&handler::ha_repair));
2773
bool mysql_optimize_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
2780
bool mysql_optimize_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2775
return(mysql_admin_table(thd, tables, check_opt,
2776
"optimize", TL_WRITE, 1,0,0,0,
2777
&handler::ha_optimize));
2782
return(mysql_admin_table(session, tables, check_opt,
2783
"optimize", TL_WRITE, 1,0,0,0,
2784
&handler::ha_optimize));
2877
2884
local_create_info.default_table_charset=default_charset_info;
2878
2885
alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
2879
2886
schema_table->table->use_all_columns();
2880
if (mysql_prepare_alter_table(thd, schema_table->table,
2887
if (mysql_prepare_alter_table(session, schema_table->table,
2881
2888
&local_create_info, &alter_info))
2883
if (mysql_prepare_create_table(thd, &local_create_info, &alter_info,
2891
if (mysql_prepare_create_table(session, &local_create_info, &alter_info,
2884
2892
tmp_table, &db_options,
2885
2893
schema_table->table->file,
2886
2894
&schema_table->table->s->key_info, &keys, 0))
2888
2897
local_create_info.max_rows= 0;
2889
if (mysql_create_frm(thd, dst_path, NULL, NULL,
2898
if (rea_create_table(session, dst_path, "system_tmp", "system_stupid_i_s_fix_nonsense",
2890
2899
&local_create_info, alter_info.create_list,
2891
2900
keys, schema_table->table->s->key_info,
2892
schema_table->table->file))
2901
schema_table->table->file, true))
2899
2908
Create a table identical to the specified table
2902
2911
mysql_create_like_table()
2912
session Thread object
2904
2913
table Table list element for target table
2905
2914
src_table Table list element for source table
2906
2915
create_info Create info
2931
2940
we ensure that our statement is properly isolated from all concurrent
2932
2941
operations which matter.
2934
if (open_tables(thd, &src_table, ¬_used, 0))
2943
if (open_tables(session, &src_table, ¬_used, 0))
2937
strxmov(src_path, src_table->table->s->path.str, reg_ext, NULL);
2946
sprintf(src_path,"%s%s",src_table->table->s->path.str, reg_ext);
2940
2949
Check that destination tables does not exist. Note that its name
2941
2950
was already checked when it was added to the table list.
2943
2952
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
2945
if (find_temporary_table(thd, db, table_name))
2954
if (find_temporary_table(session, db, table_name))
2946
2955
goto table_exists;
2947
dst_path_length= build_tmptable_filename(thd, dst_path, sizeof(dst_path));
2956
dst_path_length= build_tmptable_filename(session, dst_path, sizeof(dst_path));
2948
2957
create_info->table_options|= HA_CREATE_DELAY_KEY_WRITE;
2952
if (lock_table_name_if_not_cached(thd, db, table_name, &name_lock))
2961
if (lock_table_name_if_not_cached(session, db, table_name, &name_lock))
2954
2963
if (!name_lock)
2955
2964
goto table_exists;
2975
2984
pthread_mutex_lock(&LOCK_open);
2976
2985
if (src_table->schema_table)
2978
if (mysql_create_like_schema_frm(thd, src_table, dst_path, create_info))
2987
if (mysql_create_like_schema_frm(session, src_table, dst_path, create_info))
2980
2989
pthread_mutex_unlock(&LOCK_open);
2984
else if (my_copy(src_path, dst_path, MYF(MY_DONT_OVERWRITE_FILE)))
2986
if (my_errno == ENOENT)
2987
my_error(ER_BAD_DB_ERROR,MYF(0),db);
2989
my_error(ER_CANT_CREATE_FILE,MYF(0),dst_path,my_errno);
2990
pthread_mutex_unlock(&LOCK_open);
2995
int frmcopyr= my_copy(src_path, dst_path, MYF(MY_DONT_OVERWRITE_FILE));
2997
string dfesrc(src_path);
2998
string dfedst(dst_path);
3000
dfesrc.replace(dfesrc.find(".frm"), 4, ".dfe" );
3001
dfedst.replace(dfedst.find(".frm"), 4, ".dfe" );
3003
int dfecopyr= my_copy(dfesrc.c_str(), dfedst.c_str(),
3004
MYF(MY_DONT_OVERWRITE_FILE));
3006
if(frmcopyr || dfecopyr) // FIXME: should handle only one fail.
3008
if (my_errno == ENOENT)
3009
my_error(ER_BAD_DB_ERROR,MYF(0),db);
3011
my_error(ER_CANT_CREATE_FILE,MYF(0),dst_path,my_errno);
3012
pthread_mutex_unlock(&LOCK_open);
2997
3020
and temporary tables).
2999
3022
dst_path[dst_path_length - reg_ext_length]= '\0'; // Remove .frm
3000
if (thd->variables.keep_files_on_create)
3023
if (session->variables.keep_files_on_create)
3001
3024
create_info->options|= HA_CREATE_KEEP_FILES;
3002
err= ha_create_table(thd, dst_path, db, table_name, create_info, 1);
3025
err= ha_create_table(session, dst_path, db, table_name, create_info, 1);
3003
3026
pthread_mutex_unlock(&LOCK_open);
3005
3028
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
3007
if (err || !open_temporary_table(thd, dst_path, db, table_name, 1,
3030
if (err || !open_temporary_table(session, dst_path, db, table_name, 1,
3010
3033
(void) rm_temporary_table(create_info->db_type,
3011
dst_path, false); /* purecov: inspected */
3012
3035
goto err; /* purecov: inspected */
3057
3079
table->table= name_lock;
3058
3080
pthread_mutex_lock(&LOCK_open);
3059
if (reopen_name_locked_table(thd, table, false))
3081
if (reopen_name_locked_table(session, table, false))
3061
3083
pthread_mutex_unlock(&LOCK_open);
3064
3086
pthread_mutex_unlock(&LOCK_open);
3066
int result= store_create_info(thd, table, &query,
3088
int result= store_create_info(session, table, &query,
3069
3091
assert(result == 0); // store_create_info() always return 0
3070
write_bin_log(thd, true, query.ptr(), query.length());
3092
write_bin_log(session, true, query.ptr(), query.length());
3073
write_bin_log(thd, true, thd->query, thd->query_length);
3095
write_bin_log(session, true, session->query, session->query_length);
3076
3098
Case 3 and 4 does nothing under RBR
3080
write_bin_log(thd, true, thd->query, thd->query_length);
3101
3121
pthread_mutex_lock(&LOCK_open);
3102
unlink_open_table(thd, name_lock, false);
3122
unlink_open_table(session, name_lock, false);
3103
3123
pthread_mutex_unlock(&LOCK_open);
3109
bool mysql_analyze_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
3129
bool mysql_analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
3111
3131
thr_lock_type lock_type = TL_READ_NO_INSERT;
3113
return(mysql_admin_table(thd, tables, check_opt,
3133
return(mysql_admin_table(session, tables, check_opt,
3114
3134
"analyze", lock_type, 1, 0, 0, 0,
3115
3135
&handler::ha_analyze));
3119
bool mysql_check_table(THD* thd, TableList* tables,HA_CHECK_OPT* check_opt)
3139
bool mysql_check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
3121
3141
thr_lock_type lock_type = TL_READ_NO_INSERT;
3123
return(mysql_admin_table(thd, tables, check_opt,
3143
return(mysql_admin_table(session, tables, check_opt,
3124
3144
"check", lock_type,
3125
3145
0, 0, HA_OPEN_FOR_REPAIR, 0,
3126
3146
&handler::ha_check));
3150
3170
We set this flag so that ha_innobase::open and ::external_lock() do
3151
3171
not complain when we lock the table
3153
thd->tablespace_op= true;
3154
if (!(table=open_ltable(thd, table_list, TL_WRITE, 0)))
3173
session->tablespace_op= true;
3174
if (!(table=open_ltable(session, table_list, TL_WRITE, 0)))
3156
thd->tablespace_op=false;
3176
session->tablespace_op=false;
3160
3180
error= table->file->ha_discard_or_import_tablespace(discard);
3162
thd_proc_info(thd, "end");
3182
session->set_proc_info("end");
3167
3187
/* The ALTER Table is always in its own transaction */
3168
error = ha_autocommit_or_rollback(thd, 0);
3169
if (end_active_trans(thd))
3188
error = ha_autocommit_or_rollback(session, 0);
3189
if (end_active_trans(session))
3173
write_bin_log(thd, false, thd->query, thd->query_length);
3193
write_bin_log(session, false, session->query, session->query_length);
3176
ha_autocommit_or_rollback(thd, error);
3177
thd->tablespace_op=false;
3196
ha_autocommit_or_rollback(session, error);
3197
session->tablespace_op=false;
3179
3199
if (error == 0)
3185
3205
table->file->print_error(error, MYF(0));
3751
3763
char tmp_name[80];
3752
3764
char path[FN_REFLEN];
3754
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx",
3755
tmp_file_prefix, current_pid, thd->thread_id);
3766
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64,
3767
TMP_FILE_PREFIX, (unsigned long)current_pid, session->thread_id);
3756
3768
/* Safety fix for InnoDB */
3757
3769
if (lower_case_table_names)
3758
3770
my_casedn_str(files_charset_info, tmp_name);
3759
3771
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,
3773
if ((error= create_temporary_table(session, table, new_db, tmp_name,
3762
3774
&altered_create_info,
3763
3775
alter_info, db_change)))
3846
3858
and will be renamed to the original table name.
3848
3860
pthread_mutex_lock(&LOCK_open);
3849
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
3861
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
3850
3862
alter_table_manage_keys(table, table->file->indexes_are_disabled(),
3852
close_data_files_and_morph_locks(thd,
3864
close_data_files_and_morph_locks(session,
3853
3865
table->pos_in_table_list->db,
3854
3866
table->pos_in_table_list->table_name);
3855
3867
if (mysql_rename_table(NULL,
4361
bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
4382
bool mysql_alter_table(Session *session,char *new_db, char *new_name,
4362
4383
HA_CREATE_INFO *create_info,
4363
4384
TableList *table_list,
4364
4385
Alter_info *alter_info,
4365
4386
uint32_t order_num, order_st *order, bool ignore)
4367
4388
Table *table, *new_table=0, *name_lock= 0;;
4389
string new_name_str;
4369
4391
char tmp_name[80],old_name[32],new_name_buff[FN_REFLEN];
4370
4392
char new_alias_buff[FN_REFLEN], *table_name, *db, *new_alias, *alias;
4371
4393
char path[FN_REFLEN];
4372
4394
ha_rows copied= 0,deleted= 0;
4373
4395
handlerton *old_db_type, *new_db_type, *save_old_db_type;
4374
legacy_db_type table_type;
4397
new_name_buff[0]= '\0';
4376
4399
if (table_list && table_list->schema_table)
4378
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
4401
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
4393
4416
build_table_filename(path, sizeof(path), db, table_name, "", 0);
4395
mysql_ha_rm_tables(thd, table_list, false);
4418
mysql_ha_rm_tables(session, table_list, false);
4397
4420
/* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
4398
4421
if (alter_info->tablespace_op != NO_TABLESPACE_OP)
4399
4422
/* Conditionally writes to binlog. */
4400
return(mysql_discard_or_import_tablespace(thd,table_list,
4423
return(mysql_discard_or_import_tablespace(session,table_list,
4401
4424
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);
4426
oss << drizzle_data_home << "/" << db << "/" << table_name << reg_ext;
4428
(void) unpack_filename(new_name_buff, oss.str().c_str());
4406
4430
If this is just a rename of a view, short cut to the
4407
4431
following scenario: 1) lock LOCK_open 2) do a RENAME
4415
4439
into the main table list, like open_tables does).
4416
4440
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)))
4443
if (!(table= open_n_lock_single_table(session, table_list, TL_WRITE_ALLOW_READ)))
4422
4445
table->use_all_columns();
4424
4447
/* 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);
4450
strcpy(new_name_buff,new_name);
4451
strcpy(new_alias= new_alias_buff, new_name);
4429
4452
if (lower_case_table_names)
4431
4454
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
4456
my_casedn_str(files_charset_info, new_name_buff);
4457
new_alias= new_name; // Create lower case table name
4436
4459
my_casedn_str(files_charset_info, new_name);
4502
4525
if (create_info->row_type == ROW_TYPE_NOT_USED)
4503
4526
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))
4528
if (ha_check_storage_engine_flag(old_db_type, HTON_BIT_ALTER_NOT_SUPPORTED) ||
4529
ha_check_storage_engine_flag(new_db_type, HTON_BIT_ALTER_NOT_SUPPORTED))
4508
4531
my_error(ER_ILLEGAL_HA, MYF(0), table_name);
4512
thd_proc_info(thd, "setup");
4535
session->set_proc_info("setup");
4513
4536
if (!(alter_info->flags & ~(ALTER_RENAME | ALTER_KEYS_ONOFF)) &&
4514
4537
!table->s->tmp_table) // no need to touch frm
4527
4550
from concurrent DDL statements.
4529
4552
pthread_mutex_lock(&LOCK_open);
4530
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4553
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4531
4554
pthread_mutex_unlock(&LOCK_open);
4532
4555
error= table->file->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4533
4556
/* COND_refresh will be signaled in close_thread_tables() */
4536
4559
pthread_mutex_lock(&LOCK_open);
4537
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4560
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4538
4561
pthread_mutex_unlock(&LOCK_open);
4539
4562
error=table->file->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4540
4563
/* COND_refresh will be signaled in close_thread_tables() */
4638
4661
new_db_type= create_info->db_type;
4640
if (mysql_prepare_alter_table(thd, table, create_info, alter_info))
4663
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
4666
set_table_default_charset(session, create_info, db);
4668
if (session->variables.old_alter_table
4647
4669
|| (table->s->db_type() != create_info->db_type)
4650
4672
if (alter_info->build_method == HA_BUILD_ONLINE)
4652
my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
4674
my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
4655
4677
alter_info->build_method= HA_BUILD_OFFLINE;
4720
4742
case HA_ALTER_NOT_SUPPORTED:
4721
4743
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);
4745
my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
4746
close_temporary_table(session, altered_table, 1, 1);
4727
4749
need_copy_table= true;
4729
4751
case HA_ALTER_ERROR:
4731
close_temporary_table(thd, altered_table, 1, 1);
4753
close_temporary_table(session, altered_table, 1, 1);
4772
4794
if (altered_table)
4773
close_temporary_table(thd, altered_table, 1, 1);
4795
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);
4798
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, TMP_FILE_PREFIX,
4799
(unsigned long)current_pid, session->thread_id);
4778
4800
/* Safety fix for innodb */
4779
4801
if (lower_case_table_names)
4780
4802
my_casedn_str(files_charset_info, tmp_name);
4783
4805
/* 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,
4806
if ((error= create_temporary_table(session, table, new_db, tmp_name,
4807
create_info, alter_info,
4786
4808
!strcmp(db, new_db))))
4795
4817
memset(&tbl, 0, sizeof(tbl));
4796
4818
tbl.db= new_db;
4797
4819
tbl.table_name= tbl.alias= tmp_name;
4798
/* Table is in thd->temporary_tables */
4799
new_table= open_table(thd, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
4820
/* Table is in session->temporary_tables */
4821
new_table= open_table(session, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
4803
char path[FN_REFLEN];
4825
char tmp_path[FN_REFLEN];
4804
4826
/* table is a normal table: Create temporary table in same directory */
4805
build_table_filename(path, sizeof(path), new_db, tmp_name, "",
4827
build_table_filename(tmp_path, sizeof(tmp_path), new_db, tmp_name, "",
4807
4829
/* Open our intermediate table */
4808
new_table=open_temporary_table(thd, path, new_db, tmp_name, 0, OTM_OPEN);
4830
new_table=open_temporary_table(session, tmp_path, new_db, tmp_name, 0, OTM_OPEN);
4810
4832
if (!new_table)
4813
4835
/* 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");
4836
session->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields
4837
session->cuted_fields=0L;
4838
session->set_proc_info("copy to tmp table");
4817
4839
copied=deleted=0;
4819
4841
We do not copy data for MERGE tables. Only the children have data.
4835
4857
pthread_mutex_lock(&LOCK_open);
4836
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4858
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4837
4859
pthread_mutex_unlock(&LOCK_open);
4838
4860
alter_table_manage_keys(table, table->file->indexes_are_disabled(),
4839
4861
alter_info->keys_onoff);
4840
error= ha_autocommit_or_rollback(thd, 0);
4841
if (end_active_trans(thd))
4862
error= ha_autocommit_or_rollback(session, 0);
4863
if (end_active_trans(session))
4844
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
4866
/* We must not ignore bad input! */;
4867
session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
4846
4869
if (table->s->tmp_table != NO_TMP_TABLE)
4851
4874
/* Close lock if this is a transactional table */
4854
mysql_unlock_tables(thd, thd->lock);
4877
mysql_unlock_tables(session, session->lock);
4857
4880
/* Remove link to old table and rename the new one */
4858
close_temporary_table(thd, table, 1, 1);
4881
close_temporary_table(session, table, 1, 1);
4859
4882
/* 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))
4883
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
4885
goto end_temporary;
4898
4918
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);
4921
session->set_proc_info("rename result table");
4922
snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, TMP_FILE_PREFIX,
4923
(unsigned long)current_pid, session->thread_id);
4904
4924
if (lower_case_table_names)
4905
4925
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);
4927
wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
4928
close_data_files_and_morph_locks(session, db, table_name);
4911
4931
save_old_db_type= old_db_type;
4949
4969
quick_rm_table(old_db_type, db, old_name, FN_IS_TMP);
4952
if (thd->locked_tables && new_name == table_name && new_db == db)
4972
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;
4974
session->in_lock_tables= 1;
4975
error= reopen_tables(session, 1, 1);
4976
session->in_lock_tables= 0;
4958
4978
goto err_with_placeholders;
4960
4980
pthread_mutex_unlock(&LOCK_open);
4962
thd_proc_info(thd, "end");
4964
assert(!(mysql_bin_log.is_open() &&
4965
thd->current_stmt_binlog_row_based &&
4966
(create_info->options & HA_LEX_CREATE_TMP_TABLE)));
4967
write_bin_log(thd, true, thd->query, thd->query_length);
4969
if (ha_check_storage_engine_flag(old_db_type, HTON_FLUSH_AFTER_RENAME))
4982
session->set_proc_info("end");
4984
write_bin_log(session, true, session->query, session->query_length);
4986
if (ha_check_storage_engine_flag(old_db_type, HTON_BIT_FLUSH_AFTER_RENAME))
4972
4989
For the alter table to be properly flushed to the logs, we
4973
4990
have to open the new table. If not, we get a problem on server
4974
4991
shutdown. But we do not need to attach MERGE children.
4976
char path[FN_REFLEN];
4993
char table_path[FN_REFLEN];
4977
4994
Table *t_table;
4978
build_table_filename(path, sizeof(path), new_db, table_name, "", 0);
4979
t_table= open_temporary_table(thd, path, new_db, tmp_name, false, OTM_OPEN);
4995
build_table_filename(table_path, sizeof(table_path), new_db, table_name, "", 0);
4996
t_table= open_temporary_table(session, table_path, new_db, tmp_name, false, OTM_OPEN);
4982
4999
intern_close_table(t_table);
4986
sql_print_warning(_("Could not open table %s.%s after rename\n"),
5003
errmsg_printf(ERRMSG_LVL_WARN,
5004
_("Could not open table %s.%s after rename\n"),
4988
5006
ha_flush_logs(old_db_type);
4990
5008
table_list->table=0; // For query cache
4992
if (thd->locked_tables && (new_name != table_name || new_db != db))
5010
if (session->locked_tables && (new_name != table_name || new_db != db))
4995
5013
If are we under LOCK TABLES and did ALTER Table with RENAME we need
4998
5016
LOCK TABLES we can rely on close_thread_tables() doing this job.
5000
5018
pthread_mutex_lock(&LOCK_open);
5001
unlink_open_table(thd, table, false);
5002
unlink_open_table(thd, name_lock, false);
5019
unlink_open_table(session, table, false);
5020
unlink_open_table(session, name_lock, false);
5003
5021
pthread_mutex_unlock(&LOCK_open);
5007
snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
5008
(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;
5026
* Field::store() may have called my_error(). If this is
5027
* the case, we must not send an ok packet, since
5028
* Diagnostics_area::is_set() will fail an assert.
5030
if (! session->is_error())
5032
snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
5033
(ulong) (copied + deleted), (ulong) deleted,
5034
(ulong) session->cuted_fields);
5035
session->my_ok(copied + deleted, 0L, tmp_name);
5036
session->some_tables_deleted=0;
5041
/* my_error() was called. Return true (which means error...) */
5017
5048
/* close_temporary_table() frees the new_table pointer. */
5018
close_temporary_table(thd, new_table, 1, 1);
5049
close_temporary_table(session, new_table, 1, 1);
5021
5052
quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
5045
5076
/* 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,
5079
bool save_abort_on_warning= session->abort_on_warning;
5080
session->abort_on_warning= true;
5081
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
5051
5082
f_val, strlength(f_val), t_type,
5052
5083
alter_info->datetime_field->field_name);
5053
thd->abort_on_warning= save_abort_on_warning;
5084
session->abort_on_warning= save_abort_on_warning;
5057
5088
pthread_mutex_lock(&LOCK_open);
5058
unlink_open_table(thd, name_lock, false);
5089
unlink_open_table(session, name_lock, false);
5059
5090
pthread_mutex_unlock(&LOCK_open);
5103
5134
Turn off recovery logging since rollback of an alter table is to
5104
5135
delete the new table so there is no need to log the changes to it.
5106
5137
This needs to be done before external_lock
5108
error= ha_enable_transaction(thd, false);
5139
error= ha_enable_transaction(session, false);
5112
5143
if (!(copy= new Copy_field[to->s->fields]))
5113
5144
return(-1); /* purecov: inspected */
5115
if (to->file->ha_external_lock(thd, F_WRLCK))
5146
if (to->file->ha_external_lock(session, F_WRLCK))
5118
5149
/* We need external lock before we can disable/enable keys */
5119
5150
alter_table_manage_keys(to, from->file->indexes_are_disabled(), keys_onoff);
5121
5152
/* We can abort alter table for any table type */
5122
thd->abort_on_warning= !ignore;
5153
session->abort_on_warning= !ignore;
5124
5155
from->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
5125
5156
to->file->ha_start_bulk_insert(from->file->stats.records);
5127
save_sql_mode= thd->variables.sql_mode;
5158
save_sql_mode= session->variables.sql_mode;
5129
5160
List_iterator<Create_field> it(create);
5130
5161
Create_field *def;
5149
5180
if (to->s->primary_key != MAX_KEY && to->file->primary_key_is_clustered())
5151
5182
char warn_buff[DRIZZLE_ERRMSG_SIZE];
5152
snprintf(warn_buff, sizeof(warn_buff),
5183
snprintf(warn_buff, sizeof(warn_buff),
5153
5184
_("order_st BY ignored because there is a user-defined clustered "
5154
5185
"index in the table '%-.192s'"),
5155
5186
from->s->table_name.str);
5156
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
5187
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));
5192
from->sort.io_cache= new IO_CACHE;
5193
memset(from->sort.io_cache, 0, sizeof(IO_CACHE));
5163
5195
memset(&tables, 0, sizeof(tables));
5164
5196
tables.table= from;
5165
5197
tables.alias= tables.table_name= from->s->table_name.str;
5166
5198
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,
5201
if (session->lex->select_lex.setup_ref_array(session, order_num) ||
5202
setup_order(session, session->lex->select_lex.ref_pointer_array,
5171
5203
&tables, fields, all_fields, order) ||
5172
5204
!(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
5173
(from->sort.found_records= filesort(thd, from, sortorder, length,
5205
(from->sort.found_records= filesort(session, from, sortorder, length,
5174
5206
(SQL_SELECT *) 0, HA_POS_ERROR,
5175
5207
1, &examined_rows)) ==
5181
5213
/* Tell handler that we have values for all columns in the to table */
5182
5214
to->use_all_columns();
5183
init_read_record(&info, thd, from, (SQL_SELECT *) 0, 1,1);
5215
init_read_record(&info, session, from, (SQL_SELECT *) 0, 1,1);
5185
5217
to->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
5218
session->row_count= 0;
5187
5219
restore_record(to, s->default_values); // Create empty record
5188
5220
while (!(error=info.read_record(&info)))
5222
if (session->killed)
5192
thd->send_kill_message();
5224
session->send_kill_message();
5228
session->row_count++;
5197
5229
/* Return error if source table isn't empty. */
5198
5230
if (error_if_not_empty)
5265
5298
Ensure that the new table is saved properly to disk so that we
5266
5299
can do a rename
5268
if (ha_autocommit_or_rollback(thd, 0))
5301
if (ha_autocommit_or_rollback(session, 0))
5270
if (end_active_trans(thd))
5303
if (end_active_trans(session))
5274
thd->variables.sql_mode= save_sql_mode;
5275
thd->abort_on_warning= 0;
5307
session->variables.sql_mode= save_sql_mode;
5308
session->abort_on_warning= 0;
5276
5309
free_io_cache(from);
5277
5310
*copied= found_count;
5278
5311
*deleted=delete_count;
5279
5312
to->file->ha_release_auto_increment();
5280
if (to->file->ha_external_lock(thd,F_UNLCK))
5313
if (to->file->ha_external_lock(session,F_UNLCK))
5282
5315
return(error > 0 ? -1 : 0);
5311
5344
create_info.default_table_charset=default_charset_info;
5312
5345
/* Force alter table to recreate table */
5313
5346
alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
5314
return(mysql_alter_table(thd, NULL, NULL, &create_info,
5347
return(mysql_alter_table(session, NULL, NULL, &create_info,
5315
5348
table_list, &alter_info, 0,
5316
5349
(order_st *) 0, 0));
5320
bool mysql_checksum_table(THD *thd, TableList *tables,
5353
bool mysql_checksum_table(Session *session, TableList *tables,
5321
5354
HA_CHECK_OPT *check_opt)
5323
5356
TableList *table;
5324
5357
List<Item> field_list;
5326
Protocol *protocol= thd->protocol;
5359
Protocol *protocol= session->protocol;
5328
5361
field_list.push_back(item = new Item_empty_string("Table", NAME_LEN*2));
5329
5362
item->maybe_null= 1;
5340
5373
char table_name[NAME_LEN*2+2];
5343
strxmov(table_name, table->db ,".", table->table_name, NULL);
5376
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
5378
t= table->table= open_n_lock_single_table(session, table, TL_READ);
5379
session->clear_error(); // these errors shouldn't get client
5348
5381
protocol->prepare_for_resend();
5349
5382
protocol->store(table_name, system_charset_info);
5415
5448
t->file->ha_rnd_end();
5419
close_thread_tables(thd);
5451
session->clear_error();
5452
close_thread_tables(session);
5420
5453
table->table=0; // For query cache
5422
5455
if (protocol->write())
5430
close_thread_tables(thd); // Shouldn't be needed
5463
close_thread_tables(session); // Shouldn't be needed
5432
5465
table->table=0;
5436
static bool check_engine(THD *thd, const char *table_name,
5469
static bool check_engine(Session *session, const char *table_name,
5437
5470
HA_CREATE_INFO *create_info)
5439
5472
handlerton **new_engine= &create_info->db_type;
5440
5473
handlerton *req_engine= *new_engine;
5441
5474
bool no_substitution= 1;
5442
if (!(*new_engine= ha_checktype(thd, ha_legacy_type(req_engine),
5475
if (!(*new_engine= ha_checktype(session, ha_legacy_type(req_engine),
5443
5476
no_substitution, 1)))
5446
5479
if (req_engine && req_engine != *new_engine)
5448
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
5481
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
5449
5482
ER_WARN_USING_OTHER_HANDLER,
5450
5483
ER(ER_WARN_USING_OTHER_HANDLER),
5451
5484
ha_resolve_storage_engine_name(*new_engine),
5454
5487
if (create_info->options & HA_LEX_CREATE_TMP_TABLE &&
5455
ha_check_storage_engine_flag(*new_engine, HTON_TEMPORARY_NOT_SUPPORTED))
5488
ha_check_storage_engine_flag(*new_engine, HTON_BIT_TEMPORARY_NOT_SUPPORTED))
5457
5490
if (create_info->used_fields & HA_CREATE_USED_ENGINE)