ClickHouse/src/Interpreters/StorageID.cpp

86 lines
2.4 KiB
C++
Raw Normal View History

2020-03-13 10:30:55 +00:00
#include <Interpreters/StorageID.h>
2020-02-17 19:28:25 +00:00
#include <Parsers/ASTQueryWithTableAndOutput.h>
2020-03-13 10:30:55 +00:00
#include <Parsers/ASTIdentifier.h>
2020-02-17 19:28:25 +00:00
#include <Common/quoteString.h>
#include <IO/WriteHelpers.h>
2020-03-04 20:29:52 +00:00
#include <Interpreters/DatabaseAndTableWithAlias.h>
2020-02-17 19:28:25 +00:00
namespace DB
{
2020-03-11 19:10:55 +00:00
namespace ErrorCodes
{
2020-03-13 15:41:36 +00:00
extern const int LOGICAL_ERROR;
2020-03-11 19:10:55 +00:00
extern const int UNKNOWN_DATABASE;
}
2020-03-13 10:30:55 +00:00
StorageID::StorageID(const ASTQueryWithTableAndOutput & query)
2020-02-17 19:28:25 +00:00
{
2020-03-13 10:30:55 +00:00
database_name = query.database;
2020-02-17 19:28:25 +00:00
table_name = query.table;
uuid = query.uuid;
assertNotEmpty();
}
2020-03-13 10:30:55 +00:00
StorageID::StorageID(const ASTIdentifier & table_identifier_node)
{
DatabaseAndTableWithAlias database_table(table_identifier_node);
database_name = database_table.database;
table_name = database_table.table;
uuid = database_table.uuid;
assertNotEmpty();
}
StorageID::StorageID(const ASTPtr & node)
{
2020-03-13 19:46:16 +00:00
if (auto identifier = dynamic_cast<const ASTIdentifier *>(node.get()))
2020-03-13 10:30:55 +00:00
*this = StorageID(*identifier);
2020-03-13 19:46:16 +00:00
else if (auto simple_query = dynamic_cast<const ASTQueryWithTableAndOutput *>(node.get()))
2020-03-13 10:30:55 +00:00
*this = StorageID(*simple_query);
else
throw Exception("Unexpected AST", ErrorCodes::LOGICAL_ERROR);
}
String StorageID::getTableName() const
{
assertNotEmpty();
return table_name;
}
2020-03-06 20:38:19 +00:00
String StorageID::getDatabaseName() const
{
assertNotEmpty();
if (database_name.empty())
throw Exception("Database name is empty", ErrorCodes::UNKNOWN_DATABASE);
return database_name;
}
2020-02-17 19:28:25 +00:00
String StorageID::getNameForLogs() const
{
assertNotEmpty();
return (database_name.empty() ? "" : backQuoteIfNeed(database_name) + ".") + backQuoteIfNeed(table_name)
+ (hasUUID() ? " (UUID " + toString(uuid) + ")" : "");
}
bool StorageID::operator<(const StorageID & rhs) const
{
assertNotEmpty();
/// It's needed for ViewDependencies
if (!hasUUID() && !rhs.hasUUID())
/// If both IDs don't have UUID, compare them like pair of strings
return std::tie(database_name, table_name) < std::tie(rhs.database_name, rhs.table_name);
else if (hasUUID() && rhs.hasUUID())
/// If both IDs have UUID, compare UUIDs and ignore database and table name
return uuid < rhs.uuid;
else
/// All IDs without UUID are less, then all IDs with UUID
return !hasUUID();
}
2020-03-06 20:38:19 +00:00
String StorageID::getFullTableName() const
{
return backQuoteIfNeed(getDatabaseName()) + "." + backQuoteIfNeed(table_name);
}
2020-02-17 19:28:25 +00:00
}