SHOW CREATE for DatabaseMemory

This commit is contained in:
Alexander Tokmakov 2020-01-30 15:51:47 +03:00
parent 89a31e4253
commit 7f3b5efebe
6 changed files with 53 additions and 16 deletions

View File

@ -9,6 +9,11 @@
namespace DB namespace DB
{ {
namespace ErrorCodes
{
extern const int UNKNOWN_TABLE;
}
DatabaseMemory::DatabaseMemory(const String & name_) DatabaseMemory::DatabaseMemory(const String & name_)
: DatabaseWithOwnTablesBase(name_, "DatabaseMemory(" + name_ + ")") : DatabaseWithOwnTablesBase(name_, "DatabaseMemory(" + name_ + ")")
, data_path("data/" + escapeForFileName(database_name) + "/") , data_path("data/" + escapeForFileName(database_name) + "/")
@ -18,16 +23,19 @@ void DatabaseMemory::createTable(
const Context & /*context*/, const Context & /*context*/,
const String & table_name, const String & table_name,
const StoragePtr & table, const StoragePtr & table,
const ASTPtr & /*query*/) const ASTPtr & query)
{ {
attachTable(table_name, table); std::lock_guard lock{mutex};
attachTableUnlocked(table_name, table);
create_queries.emplace(table_name, query);
} }
void DatabaseMemory::dropTable( void DatabaseMemory::dropTable(
const Context & /*context*/, const Context & /*context*/,
const String & table_name) const String & table_name)
{ {
auto table = detachTable(table_name); std::lock_guard lock{mutex};
auto table = detachTableUnlocked(table_name);
try try
{ {
table->drop(); table->drop();
@ -37,9 +45,11 @@ void DatabaseMemory::dropTable(
} }
catch (...) catch (...)
{ {
attachTable(table_name, table); attachTableUnlocked(table_name, table);
throw;
} }
table->is_dropped = true; table->is_dropped = true;
create_queries.erase(table_name);
} }
ASTPtr DatabaseMemory::getCreateDatabaseQuery(const Context & /*context*/) const ASTPtr DatabaseMemory::getCreateDatabaseQuery(const Context & /*context*/) const
@ -51,4 +61,13 @@ ASTPtr DatabaseMemory::getCreateDatabaseQuery(const Context & /*context*/) const
return create_query; return create_query;
} }
ASTPtr DatabaseMemory::getCreateTableQueryImpl(const Context &, const String & table_name, bool throw_on_error) const
{
std::lock_guard lock{mutex};
auto it = create_queries.find(table_name);
if (it == create_queries.end() && throw_on_error)
throw Exception("No table " + table_name + " in database " + database_name, ErrorCodes::UNKNOWN_TABLE);
return it->second;
}
} }

View File

@ -33,6 +33,7 @@ public:
const Context & context, const Context & context,
const String & table_name) override; const String & table_name) override;
ASTPtr getCreateTableQueryImpl(const Context & /*context*/, const String & name, bool throw_on_error) const override;
ASTPtr getCreateDatabaseQuery(const Context & /*context*/) const override; ASTPtr getCreateDatabaseQuery(const Context & /*context*/) const override;
/// DatabaseMemory allows to create tables, which store data on disk. /// DatabaseMemory allows to create tables, which store data on disk.
@ -44,6 +45,8 @@ public:
private: private:
String data_path; String data_path;
using NameToASTCreate = std::unordered_map<String, ASTPtr>;
NameToASTCreate create_queries;
}; };
} }

View File

@ -67,9 +67,13 @@ bool DatabaseWithOwnTablesBase::empty(const Context & /*context*/) const
StoragePtr DatabaseWithOwnTablesBase::detachTable(const String & table_name) StoragePtr DatabaseWithOwnTablesBase::detachTable(const String & table_name)
{ {
StoragePtr res;
{
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
return detachTableUnlocked(table_name);
}
StoragePtr DatabaseWithOwnTablesBase::detachTableUnlocked(const String & table_name)
{
StoragePtr res;
if (dictionaries.count(table_name)) if (dictionaries.count(table_name))
throw Exception("Cannot detach dictionary " + database_name + "." + table_name + " as table, use DETACH DICTIONARY query.", ErrorCodes::UNKNOWN_TABLE); throw Exception("Cannot detach dictionary " + database_name + "." + table_name + " as table, use DETACH DICTIONARY query.", ErrorCodes::UNKNOWN_TABLE);
@ -78,14 +82,18 @@ StoragePtr DatabaseWithOwnTablesBase::detachTable(const String & table_name)
throw Exception("Table " + backQuote(database_name) + "." + backQuote(table_name) + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE); throw Exception("Table " + backQuote(database_name) + "." + backQuote(table_name) + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
res = it->second; res = it->second;
tables.erase(it); tables.erase(it);
}
return res; return res;
} }
void DatabaseWithOwnTablesBase::attachTable(const String & table_name, const StoragePtr & table, const String &) void DatabaseWithOwnTablesBase::attachTable(const String & table_name, const StoragePtr & table, const String & relative_table_path)
{ {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
attachTableUnlocked(table_name, table, relative_table_path);
}
void DatabaseWithOwnTablesBase::attachTableUnlocked(const String & table_name, const StoragePtr & table, const String &)
{
if (!tables.emplace(table_name, table).second) if (!tables.emplace(table_name, table).second)
throw Exception("Table " + database_name + "." + table_name + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS); throw Exception("Table " + database_name + "." + table_name + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS);
} }

View File

@ -47,6 +47,9 @@ protected:
Poco::Logger * log; Poco::Logger * log;
DatabaseWithOwnTablesBase(const String & name_, const String & logger); DatabaseWithOwnTablesBase(const String & name_, const String & logger);
void attachTableUnlocked(const String & table_name, const StoragePtr & table, const String & relative_table_path = {});
StoragePtr detachTableUnlocked(const String & table_name);
}; };
} }

View File

@ -5,3 +5,4 @@ CREATE DATABASE memory_01069 ENGINE = Memory()
4 4
3 3
4 4
CREATE TABLE memory_01069.file (`n` UInt8) ENGINE = File(CSV)

View File

@ -15,4 +15,7 @@ DROP TABLE memory_01069.mt;
SELECT * FROM memory_01069.mt ORDER BY n; -- { serverError 60 } SELECT * FROM memory_01069.mt ORDER BY n; -- { serverError 60 }
SELECT * FROM memory_01069.file ORDER BY n; SELECT * FROM memory_01069.file ORDER BY n;
SHOW CREATE TABLE memory_01069.mt; -- { serverError 60 }
SHOW CREATE TABLE memory_01069.file;
DROP DATABASE memory_01069; DROP DATABASE memory_01069;