refactor Context 2

This commit is contained in:
Alexander Tokmakov 2019-12-05 16:44:22 +03:00
parent 0bc2fabbdd
commit 03b1a576ba
14 changed files with 23 additions and 61 deletions

View File

@ -119,7 +119,7 @@ ASTPtr DatabaseDictionary::getCreateTableQueryImpl(const Context & context,
return ast;
}
ASTPtr DatabaseDictionary::getCreateDatabaseQuery(const Context & /*context*/) const
ASTPtr DatabaseDictionary::getCreateDatabaseQuery() const
{
String query;
{

View File

@ -41,7 +41,7 @@ public:
bool empty(const Context & context) const override;
ASTPtr getCreateDatabaseQuery(const Context & context) const override;
ASTPtr getCreateDatabaseQuery() const override;
void shutdown() override;

View File

@ -1,18 +1,12 @@
#include <common/logger_useful.h>
#include <Databases/DatabaseMemory.h>
#include <Databases/DatabasesCommon.h>
#include <Parsers/ASTCreateQuery.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_GET_CREATE_TABLE_QUERY;
extern const int CANNOT_GET_CREATE_DICTIONARY_QUERY;
extern const int UNSUPPORTED_METHOD;
}
DatabaseMemory::DatabaseMemory(const String & name_)
: DatabaseWithOwnTablesBase(name_, "DatabaseMemory(" + name_ + ")")
{}
@ -33,11 +27,13 @@ void DatabaseMemory::removeTable(
detachTable(table_name);
}
ASTPtr DatabaseMemory::getCreateDatabaseQuery(
const Context &) const
ASTPtr DatabaseMemory::getCreateDatabaseQuery() const
{
//FIXME
throw Exception("There is no CREATE DATABASE query for DatabaseMemory", ErrorCodes::CANNOT_GET_CREATE_TABLE_QUERY);
auto create_query = std::make_shared<ASTCreateQuery>();
create_query->database = database_name;
create_query->set(create_query->storage, std::make_shared<ASTStorage>());
create_query->storage->set(create_query->storage->engine, makeASTFunction(getEngineName()));
return create_query;
}
}

View File

@ -31,7 +31,7 @@ public:
const Context & context,
const String & table_name) override;
ASTPtr getCreateDatabaseQuery(const Context & context) const override;
ASTPtr getCreateDatabaseQuery() const override;
};
}

View File

@ -124,7 +124,7 @@ time_t DatabaseMySQL::getObjectMetadataModificationTime(const String & table_nam
return time_t(local_tables_cache[table_name].modification_time);
}
ASTPtr DatabaseMySQL::getCreateDatabaseQuery(const Context &) const
ASTPtr DatabaseMySQL::getCreateDatabaseQuery() const
{
const auto & create_query = std::make_shared<ASTCreateQuery>();
create_query->database = database_name;

View File

@ -30,7 +30,7 @@ public:
DatabaseTablesIteratorPtr getTablesIterator(const Context & context, const FilterByNameFunction & filter_by_table_name = {}) override;
ASTPtr getCreateDatabaseQuery(const Context & context) const override;
ASTPtr getCreateDatabaseQuery() const override;
bool isTableExist(const Context & context, const String & name) const override;

View File

@ -296,7 +296,7 @@ ASTPtr DatabaseOnDisk::getCreateTableQueryImpl(const Context & context, const St
return ast;
}
ASTPtr DatabaseOnDisk::getCreateDatabaseQuery(const Context & /*context*/) const
ASTPtr DatabaseOnDisk::getCreateDatabaseQuery() const
{
ASTPtr ast;

View File

@ -48,7 +48,7 @@ public:
IDatabase & to_database,
const String & to_table_name) override;
ASTPtr getCreateDatabaseQuery(const Context & context) const override;
ASTPtr getCreateDatabaseQuery() const override;
void drop(const Context & context) override;

View File

@ -263,7 +263,7 @@ public:
}
/// Get the CREATE DATABASE query for current database.
virtual ASTPtr getCreateDatabaseQuery(const Context & context) const = 0;
virtual ASTPtr getCreateDatabaseQuery() const = 0;
/// Get name of database.
String getDatabaseName() const { return database_name; }

View File

@ -1084,27 +1084,6 @@ DatabasePtr Context::detachDatabase(const String & database_name)
}
ASTPtr Context::getCreateTableQuery(const String & database_name, const String & table_name) const
{
auto lock = getLock();
String db = resolveDatabase(database_name, current_database);
assertDatabaseExists(db);
return shared->databases[db]->getCreateTableQuery(*this, table_name);
}
ASTPtr Context::getCreateDictionaryQuery(const String & database_name, const String & dictionary_name) const
{
auto lock = getLock();
String db = resolveDatabase(database_name, current_database);
assertDatabaseExists(db);
return shared->databases[db]->getCreateDictionaryQuery(*this, dictionary_name);
}
ASTPtr Context::getCreateExternalTableQuery(const String & table_name) const
{
TableAndCreateASTs::const_iterator jt = external_tables.find(table_name);
@ -1114,16 +1093,6 @@ ASTPtr Context::getCreateExternalTableQuery(const String & table_name) const
return jt->second.second;
}
ASTPtr Context::getCreateDatabaseQuery(const String & database_name) const
{
auto lock = getLock();
String db = resolveDatabase(database_name, current_database);
assertDatabaseExists(db);
return shared->databases[db]->getCreateDatabaseQuery(*this);
}
Settings Context::getSettings() const
{
return settings;

View File

@ -148,6 +148,7 @@ private:
String default_format; /// Format, used when server formats data by itself and if query does not have FORMAT specification.
/// Thus, used in HTTP interface. If not specified - then some globally default format is used.
// TODO maybe replace with DatabaseMemory?
TableAndCreateASTs external_tables; /// Temporary tables.
Scalars scalars;
StoragePtr view_source; /// Temporary StorageValues used to generate alias columns for materialized views
@ -362,11 +363,7 @@ public:
std::optional<UInt16> getTCPPortSecure() const;
/// Get query for the CREATE table.
// TODO do we really need it here?
ASTPtr getCreateTableQuery(const String & database_name, const String & table_name) const;
ASTPtr getCreateExternalTableQuery(const String & table_name) const;
ASTPtr getCreateDatabaseQuery(const String & database_name) const;
ASTPtr getCreateDictionaryQuery(const String & database_name, const String & dictionary_name) const;
const DatabasePtr getDatabase(const String & database_name) const;
DatabasePtr getDatabase(const String & database_name);

View File

@ -525,7 +525,7 @@ void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const
String as_database_name = create.as_database.empty() ? context.getCurrentDatabase() : create.as_database;
String as_table_name = create.as_table;
ASTPtr as_create_ptr = context.getCreateTableQuery(as_database_name, as_table_name);
ASTPtr as_create_ptr = context.getDatabase(as_database_name)->getCreateTableQuery(context, as_table_name);
const auto & as_create = as_create_ptr->as<ASTCreateQuery &>();
if (as_create.is_view)
@ -569,7 +569,7 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create)
if (create.attach && !create.storage && !create.columns_list)
{
// Table SQL definition is available even if the table is detached
auto query = context.getCreateTableQuery(database_name, table_name);
auto query = context.getDatabase(database_name)->getCreateTableQuery(context, table_name);
create = query->as<ASTCreateQuery &>(); // Copy the saved create query, but use ATTACH instead of CREATE
create.attach = true;
}
@ -744,7 +744,7 @@ BlockIO InterpreterCreateQuery::createDictionary(ASTCreateQuery & create)
if (create.attach)
{
auto query = context.getCreateDictionaryQuery(database_name, dictionary_name);
auto query = context.getDatabase(database_name)->getCreateDictionaryQuery(context, dictionary_name);
create = query->as<ASTCreateQuery &>();
create.attach = true;
}

View File

@ -51,19 +51,19 @@ BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl()
if (show_query->temporary)
create_query = context.getCreateExternalTableQuery(show_query->table);
else
create_query = context.getCreateTableQuery(show_query->database, show_query->table);
create_query = context.getDatabase(show_query->database)->getCreateTableQuery(context, show_query->table);
}
else if (show_query = query_ptr->as<ASTShowCreateDatabaseQuery>(); show_query)
{
if (show_query->temporary)
throw Exception("Temporary databases are not possible.", ErrorCodes::SYNTAX_ERROR);
create_query = context.getCreateDatabaseQuery(show_query->database);
create_query = context.getDatabase(show_query->database)->getCreateDatabaseQuery();
}
else if (show_query = query_ptr->as<ASTShowCreateDictionaryQuery>(); show_query)
{
if (show_query->temporary)
throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
create_query = context.getCreateDictionaryQuery(show_query->database, show_query->table);
create_query = context.getDatabase(show_query->database)->getCreateDictionaryQuery(context, show_query->table);
}
if (!create_query && show_query->temporary)

View File

@ -274,7 +274,7 @@ StoragePtr InterpreterSystemQuery::tryRestartReplica(const String & database_nam
/// If table was already dropped by anyone, an exception will be thrown
auto table_lock = table->lockExclusively(context.getCurrentQueryId());
create_ast = system_context.getCreateTableQuery(database_name, table_name);
create_ast = database->getCreateTableQuery(system_context, table_name);
database->detachTable(table_name);
}