The world's most popular open source database
#include "mysql_priv.h"#include <mysys_err.h>#include "sp.h"#include "events.h"#include <my_dir.h>#include <m_ctype.h>Include dependency graph for sql_db.cc:

Go to the source code of this file.
Classes | |
| struct | my_dblock_st |
| struct | my_dbopt_st |
Defines | |
| #define | MAX_DROP_TABLE_Q_LEN 1024 |
Typedefs | |
| typedef my_dblock_st | my_dblock_t |
| typedef my_dbopt_st | my_dbopt_t |
Functions | |
| static long | mysql_rm_known_files (THD *thd, MY_DIR *dirp, const char *db, const char *path, uint level, TABLE_LIST **dropped_tables) |
| static long | mysql_rm_arc_files (THD *thd, MY_DIR *dirp, const char *org_path) |
| static my_bool | rm_dir_w_symlink (const char *org_path, my_bool send_error) |
| static byte * | lock_db_get_key (my_dblock_t *ptr, uint *length, my_bool not_used __attribute__((unused))) |
| static void | lock_db_free_element (void *ptr) |
| static my_bool | lock_db_insert (const char *dbname, uint length) |
| void | lock_db_delete (const char *name, uint length) |
| static byte * | dboptions_get_key (my_dbopt_t *opt, uint *length, my_bool not_used __attribute__((unused))) |
| static void | write_to_binlog (THD *thd, char *query, uint q_len, char *db, uint db_len) |
| static void | free_dbopt (void *dbopt) |
| bool | my_database_names_init (void) |
| void | my_database_names_free (void) |
| void | my_dbopt_cleanup (void) |
| static my_bool | get_dbopt (const char *dbname, HA_CREATE_INFO *create) |
| static my_bool | put_dbopt (const char *dbname, HA_CREATE_INFO *create) |
| void | del_dbopt (const char *path) |
| static bool | write_db_opt (THD *thd, const char *path, HA_CREATE_INFO *create) |
| bool | load_db_opt (THD *thd, const char *path, HA_CREATE_INFO *create) |
| bool | load_db_opt_by_name (THD *thd, const char *db_name, HA_CREATE_INFO *db_create_info) |
| bool | mysql_create_db (THD *thd, char *db, HA_CREATE_INFO *create_info, bool silent) |
| bool | mysql_alter_db (THD *thd, const char *db, HA_CREATE_INFO *create_info) |
| bool | mysql_rm_db (THD *thd, char *db, bool if_exists, bool silent) |
| bool | mysql_change_db (THD *thd, const char *name, bool no_access_check) |
| static int | lock_databases (THD *thd, const char *db1, uint length1, const char *db2, uint length2) |
| bool | mysql_rename_db (THD *thd, LEX_STRING *old_db, LEX_STRING *new_db) |
| bool | check_db_dir_existence (const char *db_name) |
Variables | |
| const char * | del_exts [] = {".frm", ".BAK", ".TMD",".opt", NullS} |
| static TYPELIB | deletable_extentions |
| HASH | lock_db_cache |
| pthread_mutex_t | LOCK_lock_db |
| int | creating_database = 0 |
| static HASH | dboptions |
| static my_bool | dboptions_init = 0 |
| static rw_lock_t | LOCK_dboptions |
| #define MAX_DROP_TABLE_Q_LEN 1024 |
| typedef struct my_dblock_st my_dblock_t |
| typedef struct my_dbopt_st my_dbopt_t |
| bool check_db_dir_existence | ( | const char * | db_name | ) |
Definition at line 1728 of file sql_db.cc.
References build_table_filename(), F_OK, FN_LIBCHAR, FN_REFLEN, and my_access.
Referenced by mysql_change_db(), and mysqld_show_create_db().
01729 { 01730 char db_dir_path[FN_REFLEN]; 01731 uint db_dir_path_len; 01732 01733 db_dir_path_len= build_table_filename(db_dir_path, sizeof(db_dir_path), 01734 db_name, "", "", 0); 01735 01736 if (db_dir_path_len && db_dir_path[db_dir_path_len - 1] == FN_LIBCHAR) 01737 db_dir_path[db_dir_path_len - 1]= 0; 01738 01739 /* Check access. */ 01740 01741 return my_access(db_dir_path, F_OK); 01742 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static byte* dboptions_get_key | ( | my_dbopt_t * | opt, | |
| uint * | length, | |||
| my_bool not_used | __attribute__((unused)) | |||
| ) | [static] |
Definition at line 161 of file sql_db.cc.
References opt().
Referenced by my_database_names_init(), and my_dbopt_cleanup().
Here is the call graph for this function:

Here is the caller graph for this function:

| void del_dbopt | ( | const char * | path | ) |
Definition at line 351 of file sql_db.cc.
References dboptions, hash_delete(), hash_search(), LOCK_dboptions, opt(), rw_unlock, rw_wrlock, and strlen().
Referenced by mysql_rm_db().
00352 { 00353 my_dbopt_t *opt; 00354 rw_wrlock(&LOCK_dboptions); 00355 if ((opt= (my_dbopt_t *)hash_search(&dboptions, (const byte*) path, 00356 strlen(path)))) 00357 hash_delete(&dboptions, (byte*) opt); 00358 rw_unlock(&LOCK_dboptions); 00359 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void free_dbopt | ( | void * | dbopt | ) | [static] |
Definition at line 188 of file sql_db.cc.
Referenced by my_database_names_init(), and my_dbopt_cleanup().
Here is the caller graph for this function:

| static my_bool get_dbopt | ( | const char * | dbname, | |
| HA_CREATE_INFO * | create | |||
| ) | [static] |
Definition at line 274 of file sql_db.cc.
References dboptions, st_ha_create_information::default_table_charset, error, hash_search(), LOCK_dboptions, opt(), rw_rdlock, rw_unlock, and strlen().
Referenced by load_db_opt().
00275 { 00276 my_dbopt_t *opt; 00277 uint length; 00278 my_bool error= 1; 00279 00280 length= (uint) strlen(dbname); 00281 00282 rw_rdlock(&LOCK_dboptions); 00283 if ((opt= (my_dbopt_t*) hash_search(&dboptions, (byte*) dbname, length))) 00284 { 00285 create->default_table_charset= opt->charset; 00286 error= 0; 00287 } 00288 rw_unlock(&LOCK_dboptions); 00289 return error; 00290 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool load_db_opt | ( | THD * | thd, | |
| const char * | path, | |||
| HA_CREATE_INFO * | create | |||
| ) |
Definition at line 418 of file sql_db.cc.
References buf, bzero, DBUG_ENTER, DBUG_RETURN, default_charset_info, end_io_cache(), ER, ER_UNKNOWN_CHARACTER_SET, ER_UNKNOWN_COLLATION, error, get_charset_by_csname(), get_charset_by_name(), get_dbopt(), init_io_cache(), IO_SIZE, my_b_gets(), my_charset_latin1, my_close(), MY_CS_PRIMARY, my_isgraph, my_open(), MYF, O_SHARE, pos(), put_dbopt(), READ_CACHE, sql_print_error(), and strchr().
Referenced by load_db_opt_by_name(), and mysql_rename_db().
00419 { 00420 File file; 00421 char buf[256]; 00422 DBUG_ENTER("load_db_opt"); 00423 bool error=1; 00424 uint nbytes; 00425 00426 bzero((char*) create,sizeof(*create)); 00427 create->default_table_charset= thd->variables.collation_server; 00428 00429 /* Check if options for this database are already in the hash */ 00430 if (!get_dbopt(path, create)) 00431 DBUG_RETURN(0); 00432 00433 /* Otherwise, load options from the .opt file */ 00434 if ((file=my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0) 00435 goto err1; 00436 00437 IO_CACHE cache; 00438 if (init_io_cache(&cache, file, IO_SIZE, READ_CACHE, 0, 0, MYF(0))) 00439 goto err2; 00440 00441 while ((int) (nbytes= my_b_gets(&cache, (char*) buf, sizeof(buf))) > 0) 00442 { 00443 char *pos= buf+nbytes-1; 00444 /* Remove end space and control characters */ 00445 while (pos > buf && !my_isgraph(&my_charset_latin1, pos[-1])) 00446 pos--; 00447 *pos=0; 00448 if ((pos= strchr(buf, '='))) 00449 { 00450 if (!strncmp(buf,"default-character-set", (pos-buf))) 00451 { 00452 /* 00453 Try character set name, and if it fails 00454 try collation name, probably it's an old 00455 4.1.0 db.opt file, which didn't have 00456 separate default-character-set and 00457 default-collation commands. 00458 */ 00459 if (!(create->default_table_charset= 00460 get_charset_by_csname(pos+1, MY_CS_PRIMARY, MYF(0))) && 00461 !(create->default_table_charset= 00462 get_charset_by_name(pos+1, MYF(0)))) 00463 { 00464 sql_print_error("Error while loading database options: '%s':",path); 00465 sql_print_error(ER(ER_UNKNOWN_CHARACTER_SET),pos+1); 00466 create->default_table_charset= default_charset_info; 00467 } 00468 } 00469 else if (!strncmp(buf,"default-collation", (pos-buf))) 00470 { 00471 if (!(create->default_table_charset= get_charset_by_name(pos+1, 00472 MYF(0)))) 00473 { 00474 sql_print_error("Error while loading database options: '%s':",path); 00475 sql_print_error(ER(ER_UNKNOWN_COLLATION),pos+1); 00476 create->default_table_charset= default_charset_info; 00477 } 00478 } 00479 } 00480 } 00481 /* 00482 Put the loaded value into the hash. 00483 Note that another thread could've added the same 00484 entry to the hash after we called get_dbopt(), 00485 but it's not an error, as put_dbopt() takes this 00486 possibility into account. 00487 */ 00488 error= put_dbopt(path, create); 00489 00490 end_io_cache(&cache); 00491 err2: 00492 my_close(file,MYF(0)); 00493 err1: 00494 DBUG_RETURN(error); 00495 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool load_db_opt_by_name | ( | THD * | thd, | |
| const char * | db_name, | |||
| HA_CREATE_INFO * | db_create_info | |||
| ) |
Definition at line 528 of file sql_db.cc.
References build_table_filename(), FN_REFLEN, and load_db_opt().
Referenced by sp_head::fill_field_definition(), fill_schema_shemata(), mysql_change_db(), mysqld_show_create_db(), and set_table_default_charset().
00530 { 00531 char db_opt_path[FN_REFLEN]; 00532 00533 /* 00534 Pass an empty file name, and the database options file name as extension 00535 to avoid table name to file name encoding. 00536 */ 00537 (void) build_table_filename(db_opt_path, sizeof(db_opt_path), 00538 db_name, "", MY_DB_OPT_FILE, 0); 00539 00540 return load_db_opt(thd, db_opt_path, db_create_info); 00541 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static int lock_databases | ( | THD * | thd, | |
| const char * | db1, | |||
| uint | length1, | |||
| const char * | db2, | |||
| uint | length2 | |||
| ) | [static] |
Definition at line 1407 of file sql_db.cc.
References COND_refresh, creating_database, creating_table, hash_search(), lock_db_cache, lock_db_delete(), lock_db_insert(), LOCK_lock_db, pthread_mutex_lock, pthread_mutex_unlock, and wait_for_condition().
Referenced by mysql_rename_db().
01409 { 01410 pthread_mutex_lock(&LOCK_lock_db); 01411 while (!thd->killed && 01412 (hash_search(&lock_db_cache,(byte*) db1, length1) || 01413 hash_search(&lock_db_cache,(byte*) db2, length2))) 01414 { 01415 wait_for_condition(thd, &LOCK_lock_db, &COND_refresh); 01416 pthread_mutex_lock(&LOCK_lock_db); 01417 } 01418 01419 if (thd->killed) 01420 { 01421 pthread_mutex_unlock(&LOCK_lock_db); 01422 return 1; 01423 } 01424 01425 lock_db_insert(db1, length1); 01426 lock_db_insert(db2, length2); 01427 creating_database++; 01428 01429 /* 01430 Wait if a concurent thread is creating a table at the same time. 01431 The assumption here is that it will not take too long until 01432 there is a point in time when a table is not created. 01433 */ 01434 01435 while (!thd->killed && creating_table) 01436 { 01437 wait_for_condition(thd, &LOCK_lock_db, &COND_refresh); 01438 pthread_mutex_lock(&LOCK_lock_db); 01439 } 01440 01441 if (thd->killed) 01442 { 01443 lock_db_delete(db1, length1); 01444 lock_db_delete(db2, length2); 01445 creating_database--; 01446 pthread_mutex_unlock(&LOCK_lock_db); 01447 pthread_cond_signal(&COND_refresh); 01448 return(1); 01449 } 01450 01451 /* 01452 We can unlock now as the hash will protect against anyone creating a table 01453 in the databases we are using 01454 */ 01455 pthread_mutex_unlock(&LOCK_lock_db); 01456 return 0; 01457 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void lock_db_delete | ( | const char * | name, | |
| uint | length | |||
| ) |
Definition at line 133 of file sql_db.cc.
References hash_delete(), hash_search(), lock_db_cache, LOCK_lock_db, opt(), and safe_mutex_assert_owner.
Referenced by lock_databases(), and mysql_rename_db().
00134 { 00135 my_dblock_t *opt; 00136 safe_mutex_assert_owner(&LOCK_lock_db); 00137 if ((opt= (my_dblock_t *)hash_search(&lock_db_cache, 00138 (const byte*) name, length))) 00139 hash_delete(&lock_db_cache, (byte*) opt); 00140 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void lock_db_free_element | ( | void * | ptr | ) | [static] |
| static byte* lock_db_get_key | ( | my_dblock_t * | ptr, | |
| uint * | length, | |||
| my_bool not_used | __attribute__((unused)) | |||
| ) | [static] |
Definition at line 62 of file sql_db.cc.
References my_dblock_st::name, and my_dblock_st::name_length.
Referenced by my_database_names_init().
00064 { 00065 *length= ptr->name_length; 00066 return (byte*) ptr->name; 00067 }
Here is the caller graph for this function:

Definition at line 92 of file sql_db.cc.
References DBUG_ENTER, DBUG_RETURN, error, hash_search(), lock_db_cache, LOCK_lock_db, my_free, my_hash_insert(), my_multi_malloc(), MY_WME, MY_ZEROFILL, MYF, NullS, opt(), safe_mutex_assert_owner, and strmov().
Referenced by lock_databases().
00093 { 00094 my_dblock_t *opt; 00095 my_bool error= 0; 00096 DBUG_ENTER("lock_db_insert"); 00097 00098 safe_mutex_assert_owner(&LOCK_lock_db); 00099 00100 if (!(opt= (my_dblock_t*) hash_search(&lock_db_cache, 00101 (byte*) dbname, length))) 00102 { 00103 /* Db is not in the hash, insert it */ 00104 char *tmp_name; 00105 if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL), 00106 &opt, (uint) sizeof(*opt), &tmp_name, length+1, 00107 NullS)) 00108 { 00109 error= 1; 00110 goto end; 00111 } 00112 00113 opt->name= tmp_name; 00114 strmov(opt->name, dbname); 00115 opt->name_length= length; 00116 00117 if ((error= my_hash_insert(&lock_db_cache, (byte*) opt))) 00118 { 00119 my_free((gptr) opt, MYF(0)); 00120 goto end; 00121 } 00122 } 00123 00124 end: 00125 DBUG_RETURN(error); 00126 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void my_database_names_free | ( | void | ) |
Definition at line 234 of file sql_db.cc.
References dboptions, hash_free(), lock_db_cache, LOCK_dboptions, and rwlock_destroy.
Referenced by clean_up().
00235 { 00236 if (dboptions_init) 00237 { 00238 dboptions_init= 0; 00239 hash_free(&dboptions); 00240 (void) rwlock_destroy(&LOCK_dboptions); 00241 hash_free(&lock_db_cache); 00242 } 00243 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool my_database_names_init | ( | void | ) |
Definition at line 208 of file sql_db.cc.
References dboptions, dboptions_get_key(), error, free_dbopt(), hash_init, lock_db_cache, lock_db_free_element(), lock_db_get_key(), LOCK_dboptions, lower_case_table_names, my_charset_bin, my_rwlock_init, NULL, and system_charset_info.
Referenced by init_common_variables().
00209 { 00210 bool error= 0; 00211 (void) my_rwlock_init(&LOCK_dboptions, NULL); 00212 if (!dboptions_init) 00213 { 00214 dboptions_init= 1; 00215 error= hash_init(&dboptions, lower_case_table_names ? 00216 &my_charset_bin : system_charset_info, 00217 32, 0, 0, (hash_get_key) dboptions_get_key, 00218 free_dbopt,0) || 00219 hash_init(&lock_db_cache, lower_case_table_names ? 00220 &my_charset_bin : system_charset_info, 00221 32, 0, 0, (hash_get_key) lock_db_get_key, 00222 lock_db_free_element,0); 00223 00224 } 00225 return error; 00226 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void my_dbopt_cleanup | ( | void | ) |
Definition at line 250 of file sql_db.cc.
References dboptions, dboptions_get_key(), free_dbopt(), hash_free(), hash_init, LOCK_dboptions, lower_case_table_names, my_charset_bin, rw_unlock, rw_wrlock, and system_charset_info.
Referenced by reload_acl_and_cache().
00251 { 00252 rw_wrlock(&LOCK_dboptions); 00253 hash_free(&dboptions); 00254 hash_init(&dboptions, lower_case_table_names ? 00255 &my_charset_bin : system_charset_info, 00256 32, 0, 0, (hash_get_key) dboptions_get_key, 00257 free_dbopt,0); 00258 rw_unlock(&LOCK_dboptions); 00259 }
Here is the call graph for this function:

Here is the caller graph for this function:

| bool mysql_alter_db | ( | THD * | thd, | |
| const char * | db, | |||
| HA_CREATE_INFO * | create_info | |||
| ) |
Definition at line 723 of file sql_db.cc.
References build_table_filename(), DBUG_ENTER, DBUG_RETURN, st_ha_create_information::default_table_charset, error, exit, FN_REFLEN, ha_binlog_log_query, MYSQL_LOG::is_open(), LOCK_mysql_create_db, LOGCOM_ALTER_DB, mysql_bin_log, path, pthread_mutex_lock, pthread_mutex_unlock, Query_log_event::query, send_ok(), start_waiting_global_read_lock(), strcmp(), strlen(), TRUE, VOID, wait_if_global_read_lock(), MYSQL_BIN_LOG::write(), and write_db_opt().
Referenced by mysql_execute_command().
00724 { 00725 char path[FN_REFLEN+16]; 00726 long result=1; 00727 int error= 0; 00728 DBUG_ENTER("mysql_alter_db"); 00729 00730 /* 00731 Do not alter database if another thread is holding read lock. 00732 Wait for global read lock before acquiring LOCK_mysql_create_db. 00733 After wait_if_global_read_lock() we have protection against another 00734 global read lock. If we would acquire LOCK_mysql_create_db first, 00735 another thread could step in and get the global read lock before we 00736 reach wait_if_global_read_lock(). If this thread tries the same as we 00737 (admin a db), it would then go and wait on LOCK_mysql_create_db... 00738 Furthermore wait_if_global_read_lock() checks if the current thread 00739 has the global read lock and refuses the operation with 00740 ER_CANT_UPDATE_WITH_READLOCK if applicable. 00741 */ 00742 if ((error=wait_if_global_read_lock(thd,0,1))) 00743 goto exit2; 00744 00745 VOID(pthread_mutex_lock(&LOCK_mysql_create_db)); 00746 00747 /* 00748 Recreate db options file: /dbpath/.db.opt 00749 We pass MY_DB_OPT_FILE as "extension" to avoid 00750 "table name to file name" encoding. 00751 */ 00752 build_table_filename(path, sizeof(path), db, "", MY_DB_OPT_FILE, 0); 00753 if ((error=write_db_opt(thd, path, create_info))) 00754 goto exit; 00755 00756 /* 00757 Change options if current database is being altered 00758 TODO: Delete this code 00759 */ 00760 if (thd->db && !strcmp(thd->db,db)) 00761 { 00762 thd->db_charset= create_info->default_table_charset ? 00763 create_info->default_table_charset : 00764 thd->variables.collation_server; 00765 thd->variables.collation_database= thd->db_charset; 00766 } 00767 00768 ha_binlog_log_query(thd, 0,

