2017-01-21 04:24:28 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Databases/DatabaseMemory.h>
|
|
|
|
#include <Databases/DatabasesCommon.h>
|
2020-04-13 14:09:56 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2019-12-05 13:44:22 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
2020-01-22 11:30:11 +00:00
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Poco/File.h>
|
2020-05-18 13:51:01 +00:00
|
|
|
#include <filesystem>
|
2016-10-25 13:49:07 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2016-10-25 13:49:07 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-01-30 12:51:47 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNKNOWN_TABLE;
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:10:45 +00:00
|
|
|
DatabaseMemory::DatabaseMemory(const String & name_, const Context & context)
|
|
|
|
: DatabaseWithOwnTablesBase(name_, "DatabaseMemory(" + name_ + ")", context)
|
2020-01-20 20:08:47 +00:00
|
|
|
, data_path("data/" + escapeForFileName(database_name) + "/")
|
2018-03-23 20:46:43 +00:00
|
|
|
{}
|
|
|
|
|
2017-01-23 18:05:07 +00:00
|
|
|
void DatabaseMemory::createTable(
|
2017-12-01 20:21:35 +00:00
|
|
|
const Context & /*context*/,
|
2017-09-11 12:39:01 +00:00
|
|
|
const String & table_name,
|
|
|
|
const StoragePtr & table,
|
2020-01-30 12:51:47 +00:00
|
|
|
const ASTPtr & query)
|
2016-10-25 13:49:07 +00:00
|
|
|
{
|
2020-04-22 20:43:10 +00:00
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
attachTableUnlocked(table_name, table, lock);
|
2020-01-30 12:51:47 +00:00
|
|
|
create_queries.emplace(table_name, query);
|
2016-10-25 13:49:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 11:30:11 +00:00
|
|
|
void DatabaseMemory::dropTable(
|
2017-12-01 20:21:35 +00:00
|
|
|
const Context & /*context*/,
|
2020-03-20 00:07:52 +00:00
|
|
|
const String & table_name,
|
|
|
|
bool /*no_delay*/)
|
2016-10-25 13:49:07 +00:00
|
|
|
{
|
2020-04-22 20:43:10 +00:00
|
|
|
std::unique_lock lock{mutex};
|
|
|
|
auto table = detachTableUnlocked(table_name, lock);
|
2020-01-22 11:30:11 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
table->drop();
|
|
|
|
Poco::File table_data_dir{getTableDataPath(table_name)};
|
|
|
|
if (table_data_dir.exists())
|
|
|
|
table_data_dir.remove(true);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2020-04-22 20:43:10 +00:00
|
|
|
attachTableUnlocked(table_name, table, lock);
|
2020-01-30 12:51:47 +00:00
|
|
|
throw;
|
2020-01-22 11:30:11 +00:00
|
|
|
}
|
|
|
|
table->is_dropped = true;
|
2020-01-30 12:51:47 +00:00
|
|
|
create_queries.erase(table_name);
|
2016-10-25 13:49:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:51:48 +00:00
|
|
|
ASTPtr DatabaseMemory::getCreateDatabaseQuery() const
|
2018-03-13 13:28:32 +00:00
|
|
|
{
|
2019-12-05 13:44:22 +00:00
|
|
|
auto create_query = std::make_shared<ASTCreateQuery>();
|
2020-07-07 12:11:58 +00:00
|
|
|
create_query->database = getDatabaseName();
|
2019-12-05 13:44:22 +00:00
|
|
|
create_query->set(create_query->storage, std::make_shared<ASTStorage>());
|
|
|
|
create_query->storage->set(create_query->storage->engine, makeASTFunction(getEngineName()));
|
|
|
|
return create_query;
|
2018-03-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:10:45 +00:00
|
|
|
ASTPtr DatabaseMemory::getCreateTableQueryImpl(const String & table_name, const Context &, bool throw_on_error) const
|
2020-01-30 12:51:47 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
auto it = create_queries.find(table_name);
|
2020-06-10 12:53:12 +00:00
|
|
|
if (it == create_queries.end() || !it->second)
|
2020-03-13 20:00:54 +00:00
|
|
|
{
|
|
|
|
if (throw_on_error)
|
|
|
|
throw Exception("There is no metadata of table " + table_name + " in database " + database_name, ErrorCodes::UNKNOWN_TABLE);
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
2020-01-30 12:51:47 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2020-04-13 14:09:56 +00:00
|
|
|
UUID DatabaseMemory::tryGetTableUUID(const String & table_name) const
|
|
|
|
{
|
2020-05-28 20:10:45 +00:00
|
|
|
if (auto table = tryGetTable(table_name, global_context))
|
2020-04-13 14:09:56 +00:00
|
|
|
return table->getStorageID().uuid;
|
|
|
|
return UUIDHelpers::Nil;
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:51:01 +00:00
|
|
|
void DatabaseMemory::drop(const Context & context)
|
|
|
|
{
|
|
|
|
/// Remove data on explicit DROP DATABASE
|
|
|
|
std::filesystem::remove_all(context.getPath() + data_path);
|
|
|
|
}
|
|
|
|
|
2016-10-25 13:49:07 +00:00
|
|
|
}
|