36
36
static TYPELIB deletable_extentions=
37
37
{array_elements(del_exts)-1,"del_exts", del_exts, NULL};
39
static long mysql_rm_known_files(THD *thd, MY_DIR *dirp,
39
static long mysql_rm_known_files(Session *session, MY_DIR *dirp,
40
40
const char *db, const char *path, uint32_t level,
41
41
TableList **dropped_tables);
43
43
static bool rm_dir_w_symlink(const char *org_path, bool send_error);
44
static void mysql_change_db_impl(THD *thd,
44
static void mysql_change_db_impl(Session *session,
45
45
LEX_STRING *new_db_name,
46
46
const CHARSET_INFO * const new_db_charset);
133
133
Helper function to write a query to binlog used by mysql_rm_db()
136
static inline void write_to_binlog(THD *thd, char *query, uint32_t q_len,
136
static inline void write_to_binlog(Session *session, char *query, uint32_t q_len,
137
137
char *db, uint32_t db_len)
139
Query_log_event qinfo(thd, query, q_len, 0, 0);
139
Query_log_event qinfo(session, query, q_len, 0, 0);
140
140
qinfo.error_code= 0;
142
142
qinfo.db_len= db_len;
334
334
1 Could not create file or write to it. Error sent through my_error()
337
static bool write_db_opt(THD *thd, const char *path, const char *name, HA_CREATE_INFO *create)
337
static bool write_db_opt(Session *session, const char *path, const char *name, HA_CREATE_INFO *create)
339
339
bool error= true;
340
340
drizzle::Schema db;
344
344
if (!create->default_table_charset)
345
create->default_table_charset= thd->variables.collation_server;
345
create->default_table_charset= session->variables.collation_server;
347
347
if (put_dbopt(path, create))
377
bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create)
377
bool load_db_opt(Session *session, const char *path, HA_CREATE_INFO *create)
380
380
drizzle::Schema db;
383
383
memset(create, 0, sizeof(*create));
384
create->default_table_charset= thd->variables.collation_server;
384
create->default_table_charset= session->variables.collation_server;
386
386
/* Check if options for this database are already in the hash */
387
387
if (!get_dbopt(path, create))
453
453
true Failed to retrieve options
456
bool load_db_opt_by_name(THD *thd, const char *db_name,
456
bool load_db_opt_by_name(Session *session, const char *db_name,
457
457
HA_CREATE_INFO *db_create_info)
459
459
char db_opt_path[FN_REFLEN];
465
465
(void) build_table_filename(db_opt_path, sizeof(db_opt_path),
466
466
db_name, "", MY_DB_OPT_FILE, 0);
468
return load_db_opt(thd, db_opt_path, db_create_info);
468
return load_db_opt(session, db_opt_path, db_create_info);
473
473
Return default database collation.
475
@param thd Thread context.
475
@param session Thread context.
476
476
@param db_name Database name.
478
478
@return CHARSET_INFO object. The operation always return valid character
479
479
set, even if the database does not exist.
482
const CHARSET_INFO *get_default_db_collation(THD *thd, const char *db_name)
482
const CHARSET_INFO *get_default_db_collation(Session *session, const char *db_name)
484
484
HA_CREATE_INFO db_info;
486
if (thd->db != NULL && strcmp(db_name, thd->db) == 0)
487
return thd->db_charset;
486
if (session->db != NULL && strcmp(db_name, session->db) == 0)
487
return session->db_charset;
489
load_db_opt_by_name(thd, db_name, &db_info);
489
load_db_opt_by_name(session, db_name, &db_info);
492
492
NOTE: even if load_db_opt_by_name() fails,
527
int mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create_info, bool silent)
527
int mysql_create_db(Session *session, char *db, HA_CREATE_INFO *create_info, bool silent)
529
529
char path[FN_REFLEN+16];
530
530
char tmp_query[FN_REFLEN+16];
553
553
has the global read lock and refuses the operation with
554
554
ER_CANT_UPDATE_WITH_READLOCK if applicable.
556
if (wait_if_global_read_lock(thd, 0, 1))
556
if (wait_if_global_read_lock(session, 0, 1))
576
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
576
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
577
577
ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS), db);
598
598
path[path_len-1]= FN_LIBCHAR;
599
599
strmake(path+path_len, MY_DB_OPT_FILE, sizeof(path)-path_len-1);
600
if (write_db_opt(thd, path, db, create_info))
600
if (write_db_opt(session, path, db, create_info))
603
603
Could not create options file.
633
query_length= thd->query_length;
632
query= session->query;
633
query_length= session->query_length;
636
636
if (mysql_bin_log.is_open())
638
Query_log_event qinfo(thd, query, query_length, 0,
638
Query_log_event qinfo(session, query, query_length, 0,
639
639
/* suppress_use */ true);
661
661
/* These DDL methods and logging protected with LOCK_mysql_create_db */
662
662
mysql_bin_log.write(&qinfo);
664
my_ok(session, result);
668
668
pthread_mutex_unlock(&LOCK_mysql_create_db);
669
start_waiting_global_read_lock(thd);
669
start_waiting_global_read_lock(session);
675
675
/* db-name is already validated when we come here */
677
bool mysql_alter_db(THD *thd, const char *db, HA_CREATE_INFO *create_info)
677
bool mysql_alter_db(Session *session, const char *db, HA_CREATE_INFO *create_info)
679
679
char path[FN_REFLEN+16];
692
692
has the global read lock and refuses the operation with
693
693
ER_CANT_UPDATE_WITH_READLOCK if applicable.
695
if ((error=wait_if_global_read_lock(thd,0,1)))
695
if ((error=wait_if_global_read_lock(session,0,1)))
698
698
pthread_mutex_lock(&LOCK_mysql_create_db);
703
703
"table name to file name" encoding.
705
705
build_table_filename(path, sizeof(path), db, "", MY_DB_OPT_FILE, 0);
706
if ((error=write_db_opt(thd, path, db, create_info)))
706
if ((error=write_db_opt(session, path, db, create_info)))
709
709
/* Change options if current database is being altered. */
711
if (thd->db && !strcmp(thd->db,db))
711
if (session->db && !strcmp(session->db,db))
713
thd->db_charset= create_info->default_table_charset ?
713
session->db_charset= create_info->default_table_charset ?
714
714
create_info->default_table_charset :
715
thd->variables.collation_server;
716
thd->variables.collation_database= thd->db_charset;
715
session->variables.collation_server;
716
session->variables.collation_database= session->db_charset;
719
719
if (mysql_bin_log.is_open())
721
Query_log_event qinfo(thd, thd->query, thd->query_length, 0,
721
Query_log_event qinfo(session, session->query, session->query_length, 0,
722
722
/* suppress_use */ true);
730
730
qinfo.db_len = strlen(db);
732
session->clear_error();
733
733
/* These DDL methods and logging protected with LOCK_mysql_create_db */
734
734
mysql_bin_log.write(&qinfo);
736
my_ok(session, result);
739
739
pthread_mutex_unlock(&LOCK_mysql_create_db);
740
start_waiting_global_read_lock(thd);
740
start_waiting_global_read_lock(session);
763
bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent)
763
bool mysql_rm_db(Session *session,char *db,bool if_exists, bool silent)
766
766
int error= false;
787
787
has the global read lock and refuses the operation with
788
788
ER_CANT_UPDATE_WITH_READLOCK if applicable.
790
if (wait_if_global_read_lock(thd, 0, 1))
790
if (wait_if_global_read_lock(session, 0, 1))
800
800
row-based replication. The flag will be reset at the end of the
803
thd->clear_current_stmt_binlog_row_based();
803
session->clear_current_stmt_binlog_row_based();
805
805
length= build_table_filename(path, sizeof(path), db, "", "", 0);
806
806
my_stpcpy(path+length, MY_DB_OPT_FILE); // Append db option file name
820
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
820
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
821
821
ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS), db);
831
if ((deleted= mysql_rm_known_files(thd, dirp, db, path, 0,
831
if ((deleted= mysql_rm_known_files(session, dirp, db, path, 0,
832
832
&dropped_tables)) >= 0)
834
834
ha_drop_database(path);
852
query_length= thd->query_length;
851
query =session->query;
852
query_length= session->query_length;
854
854
if (mysql_bin_log.is_open())
856
Query_log_event qinfo(thd, query, query_length, 0,
856
Query_log_event qinfo(session, query, query_length, 0,
857
857
/* suppress_use */ true);
859
859
Write should use the database being created as the "current
864
864
qinfo.db_len = strlen(db);
866
session->clear_error();
867
867
/* These DDL methods and logging protected with LOCK_mysql_create_db */
868
868
mysql_bin_log.write(&qinfo);
871
thd->server_status|= SERVER_STATUS_DB_DROPPED;
872
my_ok(thd, (uint32_t) deleted);
873
thd->server_status&= ~SERVER_STATUS_DB_DROPPED;
870
session->clear_error();
871
session->server_status|= SERVER_STATUS_DB_DROPPED;
872
my_ok(session, (uint32_t) deleted);
873
session->server_status&= ~SERVER_STATUS_DB_DROPPED;
875
875
else if (mysql_bin_log.is_open())
881
if (!(query= (char*) thd->alloc(MAX_DROP_TABLE_Q_LEN)))
881
if (!(query= (char*) session->alloc(MAX_DROP_TABLE_Q_LEN)))
882
882
goto exit; /* not much else we can do */
883
883
query_pos= query_data_start= my_stpcpy(query,"drop table ");
884
884
query_end= query + MAX_DROP_TABLE_Q_LEN;
893
893
if (query_pos + tbl_name_len + 1 >= query_end)
895
895
/* These DDL methods and logging protected with LOCK_mysql_create_db */
896
write_to_binlog(thd, query, query_pos -1 - query, db, db_len);
896
write_to_binlog(session, query, query_pos -1 - query, db, db_len);
897
897
query_pos= query_data_start;
906
906
if (query_pos != query_data_start)
908
908
/* These DDL methods and logging protected with LOCK_mysql_create_db */
909
write_to_binlog(thd, query, query_pos -1 - query, db, db_len);
909
write_to_binlog(session, query, query_pos -1 - query, db, db_len);
915
915
If this database was the client's selected database, we silently
916
916
change the client's selected database to nothing (to have an empty
917
SELECT DATABASE() in the future). For this we free() thd->db and set
917
SELECT DATABASE() in the future). For this we free() session->db and set
920
if (thd->db && !strcmp(thd->db, db))
921
mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
920
if (session->db && !strcmp(session->db, db))
921
mysql_change_db_impl(session, NULL, session->variables.collation_server);
922
922
pthread_mutex_unlock(&LOCK_mysql_create_db);
923
start_waiting_global_read_lock(thd);
923
start_waiting_global_read_lock(session);
929
929
Removes files with known extensions plus.
930
thd MUST be set when calling this function!
930
session MUST be set when calling this function!
933
static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db,
933
static long mysql_rm_known_files(Session *session, MY_DIR *dirp, const char *db,
934
934
const char *org_path, uint32_t level,
935
935
TableList **dropped_tables)
968
968
/* Drop the table nicely */
969
969
*extension= 0; // Remove extension
970
970
TableList *table_list=(TableList*)
971
thd->calloc(sizeof(*table_list) +
971
session->calloc(sizeof(*table_list) +
973
973
MYSQL50_TABLE_NAME_PREFIX_LENGTH +
974
974
strlen(file->name) + 1);
1000
(tot_list && mysql_rm_table_part2(thd, tot_list, 1, 0, 1, 1)))
999
if (session->killed ||
1000
(tot_list && mysql_rm_table_part2(session, tot_list, 1, 0, 1, 1)))
1003
1003
my_dirend(dirp);
1085
1085
@brief Internal implementation: switch current database to a valid one.
1087
@param thd Thread context.
1087
@param session Thread context.
1088
1088
@param new_db_name Name of the database to switch to. The function will
1089
1089
take ownership of the name (the caller must not free
1090
1090
the allocated memory). If the name is NULL, we're
1092
1092
@param new_db_charset Character set of the new database.
1095
static void mysql_change_db_impl(THD *thd,
1095
static void mysql_change_db_impl(Session *session,
1096
1096
LEX_STRING *new_db_name,
1097
1097
const CHARSET_INFO * const new_db_charset)
1099
/* 1. Change current database in THD. */
1099
/* 1. Change current database in Session. */
1101
1101
if (new_db_name == NULL)
1104
THD::set_db() does all the job -- it frees previous database name and
1104
Session::set_db() does all the job -- it frees previous database name and
1105
1105
sets the new one.
1108
thd->set_db(NULL, 0);
1108
session->set_db(NULL, 0);
1110
1110
else if (new_db_name == &INFORMATION_SCHEMA_NAME)
1113
Here we must use THD::set_db(), because we want to copy
1113
Here we must use Session::set_db(), because we want to copy
1114
1114
INFORMATION_SCHEMA_NAME constant.
1117
thd->set_db(INFORMATION_SCHEMA_NAME.str, INFORMATION_SCHEMA_NAME.length);
1117
session->set_db(INFORMATION_SCHEMA_NAME.str, INFORMATION_SCHEMA_NAME.length);
1122
Here we already have a copy of database name to be used in THD. So,
1123
we just call THD::reset_db(). Since THD::reset_db() does not releases
1122
Here we already have a copy of database name to be used in Session. So,
1123
we just call Session::reset_db(). Since Session::reset_db() does not releases
1124
1124
the previous database name, we should do it explicitly.
1130
thd->reset_db(new_db_name->str, new_db_name->length);
1130
session->reset_db(new_db_name->str, new_db_name->length);
1133
1133
/* 3. Update db-charset environment variables. */
1135
thd->db_charset= new_db_charset;
1136
thd->variables.collation_database= new_db_charset;
1135
session->db_charset= new_db_charset;
1136
session->variables.collation_database= new_db_charset;
1142
1142
Backup the current database name before switch.
1144
@param[in] thd thread handle
1144
@param[in] session thread handle
1145
1145
@param[in, out] saved_db_name IN: "str" points to a buffer where to store
1146
1146
the old database name, "length" contains the
1153
1153
"length" is set to 0.
1156
static void backup_current_db_name(THD *thd,
1156
static void backup_current_db_name(Session *session,
1157
1157
LEX_STRING *saved_db_name)
1161
1161
/* No current (default) database selected. */
1168
strmake(saved_db_name->str, thd->db, saved_db_name->length - 1);
1169
saved_db_name->length= thd->db_length;
1168
strmake(saved_db_name->str, session->db, saved_db_name->length - 1);
1169
saved_db_name->length= session->db_length;
1199
1199
@brief Change the current database and its attributes unconditionally.
1201
@param thd thread handle
1201
@param session thread handle
1202
1202
@param new_db_name database name
1203
1203
@param force_switch if force_switch is false, then the operation will fail if
1224
1224
@@collation_server, but the operation will fail;
1226
1226
- user privileges will not be checked
1227
(THD::db_access however is updated);
1227
(Session::db_access however is updated);
1229
1229
TODO: is this really the intention?
1230
1230
(see sp-security.test).
1237
1237
@details The function checks that the database name corresponds to a
1238
1238
valid and existent database, checks access rights and changes the current
1239
1239
database with database attributes (@@collation_database session variable,
1240
Session::db_access).
1242
1242
This function is not the only way to switch the database that is
1243
1243
currently employed. When the replication slave thread switches the
1244
database before executing a query, it calls thd->set_db directly.
1244
database before executing a query, it calls session->set_db directly.
1245
1245
However, if the query, in turn, uses a stored routine, the stored routine
1246
1246
will use this function, even if it's run on the slave.
1257
1257
@retval true Error
1260
bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch)
1260
bool mysql_change_db(Session *session, const LEX_STRING *new_db_name, bool force_switch)
1262
1262
LEX_STRING new_db_file_name;
1263
1263
const CHARSET_INFO *db_default_cl;
1277
1277
new_db_name->length == 0.
1280
mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
1280
mysql_change_db_impl(session, NULL, session->variables.collation_server);
1295
1295
/* Switch the current database to INFORMATION_SCHEMA. */
1297
mysql_change_db_impl(thd, &INFORMATION_SCHEMA_NAME, system_charset_info);
1297
mysql_change_db_impl(session, &INFORMATION_SCHEMA_NAME, system_charset_info);
1328
1328
free(new_db_file_name.str);
1330
1330
if (force_switch)
1331
mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
1331
mysql_change_db_impl(session, NULL, session->variables.collation_server);
1340
1340
/* Throw a warning and free new_db_file_name. */
1342
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1342
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1343
1343
ER_BAD_DB_ERROR, ER(ER_BAD_DB_ERROR),
1344
1344
new_db_file_name.str);
1348
1348
/* Change db to NULL. */
1350
mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
1350
mysql_change_db_impl(session, NULL, session->variables.collation_server);
1352
1352
/* The operation succeed. */
1370
NOTE: in mysql_change_db_impl() new_db_file_name is assigned to THD
1371
attributes and will be freed in THD::~THD().
1370
NOTE: in mysql_change_db_impl() new_db_file_name is assigned to Session
1371
attributes and will be freed in Session::~Session().
1374
db_default_cl= get_default_db_collation(thd, new_db_file_name.str);
1374
db_default_cl= get_default_db_collation(session, new_db_file_name.str);
1376
mysql_change_db_impl(thd, &new_db_file_name, db_default_cl);
1376
mysql_change_db_impl(session, &new_db_file_name, db_default_cl);
1383
1383
Change the current database and its attributes if needed.
1385
@param thd thread handle
1385
@param session thread handle
1386
1386
@param new_db_name database name
1387
1387
@param[in, out] saved_db_name IN: "str" points to a buffer where to store
1388
1388
the old database name, "length" contains the
1399
1399
the function suceeded)
1402
bool mysql_opt_change_db(THD *thd,
1402
bool mysql_opt_change_db(Session *session,
1403
1403
const LEX_STRING *new_db_name,
1404
1404
LEX_STRING *saved_db_name,
1405
1405
bool force_switch,
1406
1406
bool *cur_db_changed)
1408
*cur_db_changed= !cmp_db_names(thd->db, new_db_name->str);
1408
*cur_db_changed= !cmp_db_names(session->db, new_db_name->str);
1410
1410
if (!*cur_db_changed)
1413
backup_current_db_name(thd, saved_db_name);
1413
backup_current_db_name(session, saved_db_name);
1415
return mysql_change_db(thd, new_db_name, force_switch);
1415
return mysql_change_db(session, new_db_name, force_switch);