ClickHouse/dbms/src/Interpreters/InterpreterDropQuery.cpp

94 lines
2.7 KiB
C++
Raw Normal View History

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>
namespace DB
{
InterpreterDropQuery::InterpreterDropQuery(ASTPtr query_ptr_, Context & context_)
: query_ptr(query_ptr_), context(context_)
{
}
void InterpreterDropQuery::execute()
{
2012-08-02 17:33:31 +00:00
Poco::ScopedLock<Poco::Mutex> lock(context.getMutex());
String path = context.getPath();
String current_database = context.getCurrentDatabase();
2011-11-05 23:31:19 +00:00
ASTDropQuery & drop = dynamic_cast<ASTDropQuery &>(*query_ptr);
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);
String table_name = drop.table;
String table_name_escaped = escapeForFileName(table_name);
2012-08-02 17:33:31 +00:00
String data_path = path + "data/" + database_name_escaped + "/" + table_name_escaped;
String metadata_path = path + "metadata/" + database_name_escaped + "/" + (!table_name.empty() ? table_name_escaped + ".sql" : "");
2011-11-05 23:31:19 +00:00
2012-08-02 17:33:31 +00:00
if (!drop.if_exists)
context.assertDatabaseExists(database_name);
2011-11-05 23:31:19 +00:00
if (!drop.table.empty())
{
/// Удаление таблицы
2012-08-02 17:33:31 +00:00
if (!context.isTableExist(database_name, table_name))
2011-11-05 23:31:19 +00:00
{
if (!drop.if_exists)
throw Exception("Table " + database_name + "." + table_name + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
}
else
{
/// Удаляем данные таблицы
if (!drop.detach)
{
2012-08-02 17:33:31 +00:00
StoragePtr table = context.getTable(database_name, table_name);
table->path_to_remove_on_drop = data_path;
2011-11-05 23:31:19 +00:00
table->drop();
Poco::File(metadata_path).remove();
}
/// Удаляем информацию о таблице из оперативки
2012-08-02 17:33:31 +00:00
context.detachTable(database_name, table_name);
2011-11-05 23:31:19 +00:00
}
}
else
{
2012-08-02 17:33:31 +00:00
if (context.isDatabaseExist(database_name))
2011-11-05 23:31:19 +00:00
{
/// Удаление базы данных
if (!drop.detach)
{
/// Тот, кто удалит директорию с БД, когда все ее таблицы будут удалены.
boost::shared_ptr<IStorage::DatabaseDropper> database_dropper(new IStorage::DatabaseDropper(data_path));
2011-11-05 23:31:19 +00:00
/// Удаление всех таблиц
2012-08-02 17:33:31 +00:00
for (Tables::iterator it = context.getDatabases()[database_name].begin(); it != context.getDatabases()[database_name].end(); ++it)
{
StoragePtr table = it->second;
table->path_to_remove_on_drop = data_path + escapeForFileName(it->first);
table->database_to_drop = database_dropper;
table->drop();
}
2011-11-05 23:31:19 +00:00
Poco::File(metadata_path).remove(true);
}
/// Удаляем информацию о БД из оперативки
2012-08-02 17:33:31 +00:00
context.detachDatabase(database_name);
2011-11-05 23:31:19 +00:00
}
}
}
}