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);
|
2013-02-05 13:03:35 +00:00
|
|
|
|
DatabaseDropperPtr database_dropper = context.getDatabaseDropper(database_name);
|
2013-02-05 09:52:13 +00:00
|
|
|
|
table->path_to_remove_on_drop = data_path;
|
2013-02-05 13:03:35 +00:00
|
|
|
|
/// Присвоим database_to_drop на случай, если БД попробуют удалить до завершения удаления этой таблицы.
|
|
|
|
|
table->database_to_drop = database_dropper;
|
2011-11-05 23:31:19 +00:00
|
|
|
|
table->drop();
|
|
|
|
|
|
2013-06-17 07:01:31 +00:00
|
|
|
|
/// Для таблиц типа ChunkRef, файла с метаданными не существует.
|
|
|
|
|
if (Poco::File(metadata_path).exists())
|
|
|
|
|
Poco::File(metadata_path).remove();
|
2011-11-05 23:31:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Удаляем информацию о таблице из оперативки
|
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)
|
|
|
|
|
{
|
2013-02-05 09:52:13 +00:00
|
|
|
|
/// Тот, кто удалит директорию с БД, когда все ее таблицы будут удалены.
|
2013-02-05 13:03:35 +00:00
|
|
|
|
DatabaseDropperPtr database_dropper = context.getDatabaseDropper(database_name);
|
|
|
|
|
database_dropper->drop_on_destroy = true;
|
2013-02-05 09:52:13 +00:00
|
|
|
|
|
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)
|
2013-02-05 09:52:13 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|