ClickHouse/src/Interpreters/StorageID.h

84 lines
2.0 KiB
C++
Raw Normal View History

2019-12-05 11:42:13 +00:00
#pragma once
#include <Core/Types.h>
2019-12-27 19:30:22 +00:00
#include <Core/UUID.h>
2019-12-05 11:42:13 +00:00
#include <tuple>
2020-03-04 20:29:52 +00:00
#include <Parsers/IAST_fwd.h>
#include <Core/QualifiedTableName.h>
2020-03-13 10:30:55 +00:00
#include <Common/Exception.h>
2019-12-05 11:42:13 +00:00
namespace DB
{
2020-03-13 10:30:55 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
2019-12-10 19:48:16 +00:00
static constexpr char const * TABLE_WITH_UUID_NAME_PLACEHOLDER = "_";
2020-02-17 19:28:25 +00:00
class ASTQueryWithTableAndOutput;
2020-03-13 10:30:55 +00:00
class ASTIdentifier;
2020-02-17 19:28:25 +00:00
class Context;
2019-12-05 11:42:13 +00:00
struct StorageID
{
String database_name;
String table_name;
2020-02-17 19:28:25 +00:00
UUID uuid = UUIDHelpers::Nil;
2019-12-05 11:42:13 +00:00
2020-02-17 19:28:25 +00:00
StorageID(const String & database, const String & table, UUID uuid_ = UUIDHelpers::Nil)
2020-03-13 10:30:55 +00:00
: database_name(database), table_name(table), uuid(uuid_)
2019-12-05 11:42:13 +00:00
{
2019-12-30 18:20:43 +00:00
assertNotEmpty();
}
2020-03-13 10:30:55 +00:00
StorageID(const ASTQueryWithTableAndOutput & query);
StorageID(const ASTIdentifier & table_identifier_node);
StorageID(const ASTPtr & node);
2020-03-04 20:29:52 +00:00
2020-03-06 20:38:19 +00:00
String getDatabaseName() const;
2019-12-30 18:20:43 +00:00
2020-03-13 10:30:55 +00:00
String getTableName() const;
2019-12-05 11:42:13 +00:00
2020-03-06 20:38:19 +00:00
String getFullTableName() const;
2019-12-05 11:42:13 +00:00
2020-02-17 19:28:25 +00:00
String getNameForLogs() const;
2019-12-30 18:20:43 +00:00
2020-03-13 15:41:36 +00:00
operator bool () const
2020-02-12 18:14:12 +00:00
{
return !empty();
}
2019-12-30 18:20:43 +00:00
bool empty() const
{
return table_name.empty() && !hasUUID();
}
bool hasUUID() const
{
2020-03-13 10:30:55 +00:00
return uuid != UUIDHelpers::Nil;
2019-12-05 11:42:13 +00:00
}
2020-02-17 19:28:25 +00:00
bool operator<(const StorageID & rhs) const;
2019-12-10 19:48:16 +00:00
2020-03-13 10:30:55 +00:00
void assertNotEmpty() const
{
if (empty())
throw Exception("Both table name and UUID are empty", ErrorCodes::LOGICAL_ERROR);
if (table_name == TABLE_WITH_UUID_NAME_PLACEHOLDER && !hasUUID())
throw Exception("Table name was replaced with placeholder, but UUID is Nil", ErrorCodes::LOGICAL_ERROR);
if (table_name.empty() && !database_name.empty())
throw Exception("Table name is empty, but database name is not", ErrorCodes::LOGICAL_ERROR);
}
2019-12-27 19:30:22 +00:00
2019-12-30 18:20:43 +00:00
/// Avoid implicit construction of empty StorageID. However, it's needed for deferred initialization.
static StorageID createEmpty() { return {}; }
2020-03-11 19:10:55 +00:00
QualifiedTableName getQualifiedName() const { return {database_name, getTableName()}; }
2020-03-04 20:29:52 +00:00
2019-12-30 18:20:43 +00:00
private:
StorageID() = default;
2019-12-05 11:42:13 +00:00
};
}