2011-11-05 23:31:19 +00:00
|
|
|
|
#include <Poco/File.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Common/escapeForFileName.h>
|
|
|
|
|
#include <DB/Parsers/ASTDropQuery.h>
|
|
|
|
|
#include <DB/Interpreters/InterpreterDropQuery.h>
|
2014-03-20 10:59:45 +00:00
|
|
|
|
#include <DB/Storages/IStorage.h>
|
2016-03-19 01:18:49 +00:00
|
|
|
|
#include <DB/Databases/IDatabase.h>
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int TABLE_WAS_NOT_DROPPED;
|
|
|
|
|
extern const int DATABASE_NOT_EMPTY;
|
2016-03-19 01:18:49 +00:00
|
|
|
|
extern const int UNKNOWN_DATABASE;
|
2016-01-11 21:46:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
|
|
InterpreterDropQuery::InterpreterDropQuery(ASTPtr query_ptr_, Context & context_)
|
|
|
|
|
: query_ptr(query_ptr_), context(context_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
|
BlockIO InterpreterDropQuery::execute()
|
2011-11-05 23:31:19 +00:00
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
String path = context.getPath();
|
|
|
|
|
String current_database = context.getCurrentDatabase();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
ASTDropQuery & drop = typeid_cast<ASTDropQuery &>(*query_ptr);
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
bool drop_database = drop.table.empty() && !drop.database.empty();
|
|
|
|
|
|
|
|
|
|
if (drop_database && drop.detach)
|
|
|
|
|
{
|
|
|
|
|
context.detachDatabase(drop.database);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
String database_name = drop.database.empty() ? current_database : drop.database;
|
2011-11-05 23:31:19 +00:00
|
|
|
|
String database_name_escaped = escapeForFileName(database_name);
|
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
String data_path = path + "data/" + database_name_escaped + "/";
|
|
|
|
|
String metadata_path = path + "metadata/" + database_name_escaped + "/";
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
auto database = context.tryGetDatabase(database_name);
|
|
|
|
|
if (!database && !drop.if_exists)
|
|
|
|
|
throw Exception("Database " + database_name + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
|
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
StorageVector tables_to_drop;
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
if (!drop_database)
|
2011-11-05 23:31:19 +00:00
|
|
|
|
{
|
2014-03-20 10:59:45 +00:00
|
|
|
|
StoragePtr table;
|
|
|
|
|
|
|
|
|
|
if (drop.if_exists)
|
|
|
|
|
table = context.tryGetTable(database_name, drop.table);
|
|
|
|
|
else
|
|
|
|
|
table = context.getTable(database_name, drop.table);
|
|
|
|
|
|
|
|
|
|
if (table)
|
|
|
|
|
tables_to_drop.push_back(table);
|
2011-11-05 23:31:19 +00:00
|
|
|
|
else
|
2015-06-18 02:11:05 +00:00
|
|
|
|
return {};
|
2014-03-20 10:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
|
if (!database)
|
2011-11-05 23:31:19 +00:00
|
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
|
if (!drop.if_exists)
|
|
|
|
|
throw Exception("Database " + database_name + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
|
|
|
|
|
return {};
|
2011-11-05 23:31:19 +00:00
|
|
|
|
}
|
2016-03-19 01:18:49 +00:00
|
|
|
|
|
|
|
|
|
for (auto iterator = database->getIterator(); iterator->isValid(); iterator->next())
|
|
|
|
|
tables_to_drop.push_back(iterator->table());
|
2011-11-05 23:31:19 +00:00
|
|
|
|
}
|
2014-03-20 10:59:45 +00:00
|
|
|
|
|
|
|
|
|
for (StoragePtr table : tables_to_drop)
|
2011-11-05 23:31:19 +00:00
|
|
|
|
{
|
2014-03-20 10:59:45 +00:00
|
|
|
|
table->shutdown();
|
|
|
|
|
|
|
|
|
|
/// Если кто-то успел удалить эту таблицу, выбросит исключение.
|
2014-03-20 13:00:42 +00:00
|
|
|
|
auto table_lock = table->lockForAlter();
|
2014-03-20 10:59:45 +00:00
|
|
|
|
|
|
|
|
|
String current_table_name = table->getTableName();
|
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
if (drop.detach)
|
2011-11-05 23:31:19 +00:00
|
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
|
/// Удаляем таблицу из оперативки, метаданные и данные не трогаем.
|
|
|
|
|
database->detachTable(current_table_name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/// Удаляем метаданные и саму таблицу из оперативки.
|
|
|
|
|
database->removeTable(current_table_name);
|
2014-03-20 10:59:45 +00:00
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
/// Удаляем данные таблицы
|
|
|
|
|
table->drop(); /// TODO Не удалять метаданные, если таблицу не получилось удалить.
|
2014-03-20 10:59:45 +00:00
|
|
|
|
table->is_dropped = true;
|
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
String current_data_path = data_path + escapeForFileName(current_table_name);
|
2015-12-13 12:02:10 +00:00
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
if (Poco::File(current_data_path).exists())
|
|
|
|
|
Poco::File(current_data_path).remove(true);
|
2011-11-05 23:31:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-20 10:59:45 +00:00
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
if (drop_database)
|
2014-03-20 10:59:45 +00:00
|
|
|
|
{
|
|
|
|
|
/// Удаление базы данных. Таблицы в ней уже удалены.
|
|
|
|
|
|
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(context.getMutex());
|
|
|
|
|
|
|
|
|
|
/// Кто-то мог успеть удалить БД до нас.
|
|
|
|
|
context.assertDatabaseExists(database_name);
|
|
|
|
|
|
|
|
|
|
/// Кто-то мог успеть создать таблицу в удаляемой БД, пока мы удаляли таблицы без лока контекста.
|
2016-03-19 01:18:49 +00:00
|
|
|
|
if (!context.getDatabase(database_name)->empty())
|
2014-03-20 10:59:45 +00:00
|
|
|
|
throw Exception("New table appeared in database being dropped. Try dropping it again.", ErrorCodes::DATABASE_NOT_EMPTY);
|
|
|
|
|
|
|
|
|
|
/// Удаляем информацию о БД из оперативки
|
|
|
|
|
context.detachDatabase(database_name);
|
|
|
|
|
|
|
|
|
|
Poco::File(data_path).remove(false);
|
|
|
|
|
Poco::File(metadata_path).remove(false);
|
|
|
|
|
}
|
2015-06-18 02:11:05 +00:00
|
|
|
|
|
|
|
|
|
return {};
|
2014-03-20 10:59:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 12:02:10 +00:00
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
void InterpreterDropQuery::dropDetachedTable(String database_name, StoragePtr table, Context & context)
|
|
|
|
|
{
|
|
|
|
|
table->shutdown();
|
|
|
|
|
|
2014-03-20 13:00:42 +00:00
|
|
|
|
auto table_lock = table->lockForAlter();
|
2014-03-20 10:59:45 +00:00
|
|
|
|
|
|
|
|
|
String table_name = table->getTableName();
|
|
|
|
|
|
|
|
|
|
String path = context.getPath();
|
|
|
|
|
String database_name_escaped = escapeForFileName(database_name);
|
|
|
|
|
|
|
|
|
|
String data_path = path + "data/" + database_name_escaped + "/" + escapeForFileName(table_name);
|
|
|
|
|
String metadata_path = path + "metadata/" + database_name_escaped + "/" + escapeForFileName(table_name) + ".sql";
|
|
|
|
|
|
|
|
|
|
if (Poco::File(metadata_path).exists())
|
|
|
|
|
Poco::File(metadata_path).remove();
|
|
|
|
|
|
|
|
|
|
table->drop();
|
|
|
|
|
table->is_dropped = true;
|
|
|
|
|
|
|
|
|
|
if (Poco::File(data_path).exists())
|
|
|
|
|
Poco::File(data_path).remove(true);
|
2011-11-05 23:31:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|