From a2d67665d5901ee889839a87843bbd6d0b1cef23 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 30 Jan 2004 07:32:35 +0100 Subject: [PATCH] SHOW CREATE DATABASE moved to sql_show.cc where it belongs db name is printed with append_identifier for a proper quoting --- sql/mysql_priv.h | 2 ++ sql/sql_db.cc | 91 +----------------------------------------------- sql/sql_show.cc | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 90 deletions(-) diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 8963fea29f6..35740ff8bf1 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -763,6 +763,8 @@ uint check_word(TYPELIB *lib, const char *val, const char *end, void free_items(Item *item); void cleanup_items(Item *item); +#define MY_DB_OPT_FILE "db.opt" +bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create); /* External variables diff --git a/sql/sql_db.cc b/sql/sql_db.cc index 697859eb264..b9da6ffd3f4 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -25,8 +25,6 @@ #include #endif -#define MY_DB_OPT_FILE "db.opt" - const char *del_exts[]= {".frm", ".BAK", ".TMD",".opt", NullS}; static TYPELIB deletable_extentions= {array_elements(del_exts)-1,"del_exts", del_exts}; @@ -92,7 +90,7 @@ static bool write_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create) */ -static bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create) +bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create) { File file; char buf[256]; @@ -668,90 +666,3 @@ bool mysql_change_db(THD *thd, const char *name) DBUG_RETURN(0); } - -int mysqld_show_create_db(THD *thd, char *dbname, - HA_CREATE_INFO *create_info) -{ - int length; - char path[FN_REFLEN], *to; - uint db_access; - bool found_libchar; - HA_CREATE_INFO create; - uint create_options = create_info ? create_info->options : 0; - Protocol *protocol=thd->protocol; - DBUG_ENTER("mysql_show_create_db"); - - if (check_db_name(dbname)) - { - net_printf(thd,ER_WRONG_DB_NAME, dbname); - DBUG_RETURN(1); - } - -#ifndef NO_EMBEDDED_ACCESS_CHECKS - if (test_all_bits(thd->master_access,DB_ACLS)) - db_access=DB_ACLS; - else - db_access= (acl_get(thd->host,thd->ip, thd->priv_user,dbname,0) | - thd->master_access); - if (!(db_access & DB_ACLS) && (!grant_option || check_grant_db(thd,dbname))) - { - net_printf(thd,ER_DBACCESS_DENIED_ERROR, - thd->priv_user, - thd->host_or_ip, - dbname); - mysql_log.write(thd,COM_INIT_DB,ER(ER_DBACCESS_DENIED_ERROR), - thd->priv_user, - thd->host_or_ip, - dbname); - DBUG_RETURN(1); - } -#endif - - (void) sprintf(path,"%s/%s",mysql_data_home, dbname); - length=unpack_dirname(path,path); // Convert if not unix - found_libchar= 0; - if (length && path[length-1] == FN_LIBCHAR) - { - found_libchar= 1; - path[length-1]=0; // remove ending '\' - } - if (access(path,F_OK)) - { - net_printf(thd,ER_BAD_DB_ERROR,dbname); - DBUG_RETURN(1); - } - if (found_libchar) - path[length-1]= FN_LIBCHAR; - strmov(path+length, MY_DB_OPT_FILE); - load_db_opt(thd, path, &create); - - List field_list; - field_list.push_back(new Item_empty_string("Database",NAME_LEN)); - field_list.push_back(new Item_empty_string("Create Database",1024)); - - if (protocol->send_fields(&field_list,1)) - DBUG_RETURN(1); - - protocol->prepare_for_resend(); - protocol->store(dbname, strlen(dbname), system_charset_info); - to= strxmov(path, "CREATE DATABASE ", NullS); - if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS) - to= strxmov(to,"/*!32312 IF NOT EXISTS*/ ", NullS); - to=strxmov(to,"`",dbname,"`", NullS); - - if (create.default_table_charset) - { - int cl= (create.default_table_charset->state & MY_CS_PRIMARY) ? 0 : 1; - to= strxmov(to," /*!40100" - " DEFAULT CHARACTER SET ",create.default_table_charset->csname, - cl ? " COLLATE " : "", - cl ? create.default_table_charset->name : "", - " */",NullS); - } - protocol->store(path, (uint) (to-path), system_charset_info); - - if (protocol->write()) - DBUG_RETURN(1); - send_eof(thd); - DBUG_RETURN(0); -} diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 0100390afa0..3b937f97089 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -813,6 +813,94 @@ mysqld_show_create(THD *thd, TABLE_LIST *table_list) DBUG_RETURN(0); } +int mysqld_show_create_db(THD *thd, char *dbname, + HA_CREATE_INFO *create_info) +{ + int length; + char path[FN_REFLEN]; + char buff[2048]; + String buffer(buff, sizeof(buff), system_charset_info); + uint db_access; + bool found_libchar; + HA_CREATE_INFO create; + uint create_options = create_info ? create_info->options : 0; + Protocol *protocol=thd->protocol; + DBUG_ENTER("mysql_show_create_db"); + + if (check_db_name(dbname)) + { + net_printf(thd,ER_WRONG_DB_NAME, dbname); + DBUG_RETURN(1); + } + +#ifndef NO_EMBEDDED_ACCESS_CHECKS + if (test_all_bits(thd->master_access,DB_ACLS)) + db_access=DB_ACLS; + else + db_access= (acl_get(thd->host,thd->ip, thd->priv_user,dbname,0) | + thd->master_access); + if (!(db_access & DB_ACLS) && (!grant_option || check_grant_db(thd,dbname))) + { + net_printf(thd,ER_DBACCESS_DENIED_ERROR, + thd->priv_user, thd->host_or_ip, dbname); + mysql_log.write(thd,COM_INIT_DB,ER(ER_DBACCESS_DENIED_ERROR), + thd->priv_user, thd->host_or_ip, dbname); + DBUG_RETURN(1); + } +#endif + + (void) sprintf(path,"%s/%s",mysql_data_home, dbname); + length=unpack_dirname(path,path); // Convert if not unix + found_libchar= 0; + if (length && path[length-1] == FN_LIBCHAR) + { + found_libchar= 1; + path[length-1]=0; // remove ending '\' + } + if (access(path,F_OK)) + { + net_printf(thd,ER_BAD_DB_ERROR,dbname); + DBUG_RETURN(1); + } + if (found_libchar) + path[length-1]= FN_LIBCHAR; + strmov(path+length, MY_DB_OPT_FILE); + load_db_opt(thd, path, &create); + + List field_list; + field_list.push_back(new Item_empty_string("Database",NAME_LEN)); + field_list.push_back(new Item_empty_string("Create Database",1024)); + + if (protocol->send_fields(&field_list,1)) + DBUG_RETURN(1); + + protocol->prepare_for_resend(); + protocol->store(dbname, strlen(dbname), system_charset_info); + buffer.length(0); + buffer.append("CREATE DATABASE ", 16); + if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS) + buffer.append("/*!32312 IF NOT EXISTS*/ ", 25); + append_identifier(thd, &buffer, dbname, strlen(dbname)); + + if (create.default_table_charset) + { + buffer.append(" /*!40100", 9); + buffer.append(" DEFAULT CHARACTER SET ", 23); + buffer.append(create.default_table_charset->csname); + if (!(create.default_table_charset->state & MY_CS_PRIMARY)) + { + buffer.append(" COLLATE ", 9); + buffer.append(create.default_table_charset->name); + } + buffer.append(" */", 3); + } + protocol->store(buffer.ptr(), buffer.length(), buffer.charset()); + + if (protocol->write()) + DBUG_RETURN(1); + send_eof(thd); + DBUG_RETURN(0); +} int mysqld_show_logs(THD *thd)