56
56
static long mysql_rm_known_files(Session *session,
57
SchemaIdentifier &schema_identifier,
58
58
plugin::TableNameList &dropped_tables);
59
static void mysql_change_db_impl(Session *session, LEX_STRING *new_db_name);
59
static void mysql_change_db_impl(Session *session);
60
static void mysql_change_db_impl(Session *session, SchemaIdentifier &schema_identifier);
107
108
// @todo push this lock down into the engine
108
109
pthread_mutex_lock(&LOCK_create_db);
110
// Check to see if it exists already.
111
if (plugin::StorageEngine::doesSchemaExist(schema_message.name()))
111
// Check to see if it exists already.
112
SchemaIdentifier schema_identifier(schema_message.name());
113
if (plugin::StorageEngine::doesSchemaExist(schema_identifier))
113
115
if (not is_if_not_exists)
165
167
pthread_mutex_lock(&LOCK_create_db);
167
if (not plugin::StorageEngine::doesSchemaExist(schema_message.name()))
169
SchemaIdentifier schema_idenifier(schema_message.name());
170
if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier))
169
172
my_error(ER_SCHEMA_DOES_NOT_EXIST, MYF(0), schema_message.name().c_str());
234
237
// Lets delete the temporary tables first outside of locks.
235
238
set<string> set_of_names;
236
session->doGetTableNames(schema_name, set_of_names);
239
session->doGetTableNames(schema_identifier, set_of_names);
238
241
for (set<string>::iterator iter= set_of_names.begin(); iter != set_of_names.end(); iter++)
240
TableIdentifier identifier(schema_name, *iter, message::Table::TEMPORARY);
243
TableIdentifier identifier(schema_identifier, *iter, message::Table::TEMPORARY);
241
244
Table *table= session->find_temporary_table(identifier);
242
245
session->close_temporary_table(table);
248
251
/* See if the schema exists */
249
if (not plugin::StorageEngine::doesSchemaExist(schema_name))
252
if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
253
256
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
254
257
ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS),
255
schema_name.c_str());
258
schema_identifier.getSQLPath().c_str());
260
my_error(ER_DB_DROP_EXISTS, MYF(0), schema_name.c_str());
263
my_error(ER_DB_DROP_EXISTS, MYF(0), schema_identifier.getSQLPath().c_str());
266
269
pthread_mutex_lock(&LOCK_open); /* After deleting database, remove all cache entries related to schema */
267
remove_db_from_cache(schema_name);
270
remove_db_from_cache(schema_identifier);
268
271
pthread_mutex_unlock(&LOCK_open);
272
deleted= mysql_rm_known_files(session, schema_name, dropped_tables);
275
deleted= mysql_rm_known_files(session, schema_identifier, dropped_tables);
273
276
if (deleted >= 0)
280
283
assert(! session->query.empty());
282
285
TransactionServices &transaction_services= TransactionServices::singleton();
283
transaction_services.dropSchema(session, schema_name);
286
transaction_services.dropSchema(session, schema_identifier.getSchemaName());
284
287
session->clear_error();
285
288
session->server_status|= SERVER_STATUS_DB_DROPPED;
286
289
session->my_ok((uint32_t) deleted);
291
294
char *query, *query_pos, *query_end, *query_data_start;
294
296
if (!(query= (char*) session->alloc(MAX_DROP_TABLE_Q_LEN)))
295
297
goto exit; /* not much else we can do */
296
298
query_pos= query_data_start= strcpy(query,"drop table ")+11;
297
299
query_end= query + MAX_DROP_TABLE_Q_LEN;
298
db_len= schema_name.length();
300
301
TransactionServices &transaction_services= TransactionServices::singleton();
301
302
for (plugin::TableNameList::iterator it= dropped_tables.begin();
333
334
SELECT DATABASE() in the future). For this we free() session->db and set
336
if (not session->db.empty() && session->db.compare(schema_name) == 0)
337
mysql_change_db_impl(session, NULL);
337
if (schema_identifier.compare(session->db))
338
mysql_change_db_impl(session);
338
339
pthread_mutex_unlock(&LOCK_create_db);
339
340
start_waiting_global_read_lock(session);
496
498
tot_list_next= &tot_list;
498
plugin::StorageEngine::getTableNames(*session, db, dropped_tables);
500
plugin::StorageEngine::getTableNames(*session, schema_identifier, dropped_tables);
500
502
for (plugin::TableNameList::iterator it= dropped_tables.begin();
501
503
it != dropped_tables.end();
504
size_t db_len= db.size();
506
size_t db_len= schema_identifier.getSchemaName().size();
506
508
/* Drop the table nicely */
507
509
TableList *table_list=(TableList*)
515
517
table_list->db= (char*) (table_list+1);
516
table_list->table_name= strcpy(table_list->db, db.c_str()) + db_len + 1;
518
table_list->table_name= strcpy(table_list->db, schema_identifier.getSchemaName().c_str()) + db_len + 1;
517
519
filename_to_tablename((*it).c_str(), table_list->table_name,
518
520
(*it).size() + 1);
519
521
table_list->alias= table_list->table_name; // If lower_case_table_names=2
537
if (not plugin::StorageEngine::dropSchema(db))
540
if (not plugin::StorageEngine::dropSchema(schema_identifier))
539
my_error(ER_DROP_SCHEMA, MYF(0), db.c_str());
542
my_error(ER_DROP_SCHEMA, MYF(0), schema_identifier.getSQLPath().c_str());
605
608
@retval true Error
608
bool mysql_change_db(Session *session, const std::string &new_db_name)
611
bool mysql_change_db(Session *session, SchemaIdentifier &schema_identifier)
611
assert(not new_db_name.empty());
613
if (not plugin::Authorization::isAuthorized(session->getSecurityContext(),
614
if (not plugin::Authorization::isAuthorized(session->getSecurityContext(), schema_identifier))
616
616
/* Error message is set in isAuthorized */
622
Now we need to make a copy because check_db_name requires a
623
non-constant argument. Actually, it takes database file name.
625
TODO: fix check_db_name().
628
LEX_STRING new_db_file_name;
629
new_db_file_name.length= new_db_name.length();
630
new_db_file_name.str= (char *)malloc(new_db_name.length() + 1);
631
if (new_db_file_name.str == NULL)
632
return true; /* the error is set */
633
memcpy(new_db_file_name.str, new_db_name.c_str(), new_db_name.length());
634
new_db_file_name.str[new_db_name.length()]= 0;
638
NOTE: if check_db_name() fails, we should throw an error in any case,
639
even if we are called from sp_head::execute().
641
It's next to impossible however to get this error when we are called
642
from sp_head::execute(). But let's switch the current database to NULL
643
in this case to be sure.
646
if (check_db_name(&new_db_file_name))
620
if (not check_db_name(schema_identifier))
648
my_error(ER_WRONG_DB_NAME, MYF(0), new_db_file_name.str);
649
free(new_db_file_name.str);
622
my_error(ER_WRONG_DB_NAME, MYF(0), schema_identifier.getSQLPath().c_str());
654
if (not plugin::StorageEngine::doesSchemaExist(new_db_file_name.str))
627
if (not plugin::StorageEngine::doesSchemaExist(schema_identifier))
656
629
/* Report an error and free new_db_file_name. */
658
my_error(ER_BAD_DB_ERROR, MYF(0), new_db_file_name.str);
659
free(new_db_file_name.str);
631
my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
661
633
/* The operation failed. */
666
mysql_change_db_impl(session, &new_db_file_name);
667
free(new_db_file_name.str);
638
mysql_change_db_impl(session, schema_identifier);
701
674
the previous database name, we should do it explicitly.
704
session->set_db(new_db_name->str, new_db_name->length);
677
session->set_db(schema_identifier.getLower());
681
static void mysql_change_db_impl(Session *session)
683
session->set_db(string());
708
686
} /* namespace drizzled */