SHOW CREATE for DatabaseMemory

This commit is contained in:
Alexander Tokmakov 2020-01-30 15:51:47 +03:00
parent 17de1b7519
commit 93dba03e81
6 changed files with 50 additions and 14 deletions

View File

@ -7,6 +7,11 @@
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_TABLE;
}
DatabaseMemory::DatabaseMemory(const String & name_)
: DatabaseWithOwnTablesBase(name_, "DatabaseMemory(" + name_ + ")")
, data_path("data/" + escapeForFileName(database_name) + "/")
@ -16,16 +21,20 @@ void DatabaseMemory::createTable(
const Context & /*context*/,
const String & table_name,
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::removeTable(
const Context & /*context*/,
const String & table_name)
{
detachTable(table_name);
std::lock_guard lock{mutex};
detachTableUnlocked(table_name);
create_queries.erase(table_name);
}
ASTPtr DatabaseMemory::getCreateDatabaseQuery(const Context & /*context*/) const
@ -37,4 +46,13 @@ ASTPtr DatabaseMemory::getCreateDatabaseQuery(const Context & /*context*/) const
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 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;
/// DatabaseMemory allows to create tables, which store data on disk.
@ -44,6 +45,8 @@ public:
private:
String data_path;
using NameToASTCreate = std::unordered_map<String, ASTPtr>;
NameToASTCreate create_queries;
};
}

View File

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

View File

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

View File

@ -5,3 +5,4 @@ CREATE DATABASE memory_01069 ENGINE = Memory()
4
3
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.file ORDER BY n;
SHOW CREATE TABLE memory_01069.mt; -- { serverError 60 }
SHOW CREATE TABLE memory_01069.file;
DROP DATABASE memory_01069;