ClickHouse/dbms/src/Parsers/ASTIdentifier.cpp

148 lines
3.6 KiB
C++
Raw Normal View History

#include <Parsers/ASTIdentifier.h>
#include <Common/typeid_cast.h>
#include <IO/WriteBufferFromOStream.h>
#include <IO/WriteHelpers.h>
namespace DB
{
2017-12-01 18:36:55 +00:00
void ASTIdentifier::formatImplWithoutAlias(const FormatSettings & settings, FormatState &, FormatStateStacked) const
{
2018-08-26 02:19:18 +00:00
auto format_element = [&](const String & elem_name)
{
settings.ostr << (settings.hilite ? hilite_identifier : "");
settings.writeIdentifier(elem_name);
settings.ostr << (settings.hilite ? hilite_none : "");
};
2017-04-02 17:37:49 +00:00
/// A simple or compound identifier?
2019-01-17 17:01:48 +00:00
if (name_parts.size() > 1)
{
2019-01-17 17:01:48 +00:00
for (size_t i = 0, size = name_parts.size(); i < size; ++i)
{
if (i != 0)
settings.ostr << '.';
2019-01-17 17:01:48 +00:00
format_element(name_parts[i]);
}
}
else
{
format_element(name);
}
}
void ASTIdentifier::appendColumnNameImpl(WriteBuffer & ostr) const
{
writeString(name, ostr);
}
2019-01-15 12:28:17 +00:00
ASTPtr createTableIdentifier(const String & database_name, const String & table_name)
{
if (database_name.empty())
return ASTIdentifier::createSpecial(table_name);
2019-01-17 17:01:48 +00:00
ASTPtr database_and_table = ASTIdentifier::createSpecial(database_name + "." + table_name, {database_name, table_name});
2019-01-15 12:28:17 +00:00
return database_and_table;
}
bool isIdentifier(const IAST * const ast)
{
if (ast)
return typeid_cast<const ASTIdentifier *>(ast);
return false;
}
std::optional<String> getIdentifierName(const IAST * const ast)
{
if (ast)
if (auto node = typeid_cast<const ASTIdentifier *>(ast))
return node->name;
return {};
}
bool getIdentifierName(const ASTPtr & ast, String & name)
{
if (ast)
if (auto node = typeid_cast<const ASTIdentifier *>(ast.get()))
{
name = node->name;
return true;
}
return false;
}
std::optional<String> getColumnIdentifierName(const ASTIdentifier & node)
{
2019-01-15 12:28:17 +00:00
if (!node.special)
return node.name;
return {};
}
std::optional<String> getColumnIdentifierName(const ASTPtr & ast)
{
if (ast)
if (auto id = typeid_cast<const ASTIdentifier *>(ast.get()))
2019-01-15 12:28:17 +00:00
if (!id->special)
return id->name;
return {};
}
std::optional<String> getTableIdentifierName(const ASTIdentifier & node)
{
2019-01-15 12:28:17 +00:00
if (node.special)
return node.name;
return {};
}
std::optional<String> getTableIdentifierName(const ASTPtr & ast)
{
if (ast)
if (auto id = typeid_cast<const ASTIdentifier *>(ast.get()))
2019-01-15 12:28:17 +00:00
if (id->special)
return id->name;
return {};
}
void setIdentifierSpecial(ASTPtr & ast)
{
if (ast)
if (ASTIdentifier * id = typeid_cast<ASTIdentifier *>(ast.get()))
id->setSpecial();
}
2019-01-16 17:26:14 +00:00
void addIdentifierQualifier(ASTIdentifier & identifier, const String & database, const String & table, const String & alias)
{
if (!alias.empty())
{
2019-01-17 17:01:48 +00:00
identifier.name_parts.emplace_back(alias);
2019-01-16 17:26:14 +00:00
}
else
{
if (!database.empty())
2019-01-17 17:01:48 +00:00
identifier.name_parts.emplace_back(database);
identifier.name_parts.emplace_back(table);
2019-01-16 17:26:14 +00:00
}
}
bool doesIdentifierBelongTo(const ASTIdentifier & identifier, const String & database, const String & table)
{
2019-01-17 17:01:48 +00:00
size_t num_components = identifier.name_parts.size();
2019-01-16 17:26:14 +00:00
if (num_components >= 3)
2019-01-17 17:01:48 +00:00
return identifier.name_parts[0] == database &&
identifier.name_parts[1] == table;
2019-01-16 17:26:14 +00:00
return false;
}
bool doesIdentifierBelongTo(const ASTIdentifier & identifier, const String & table)
{
2019-01-17 17:01:48 +00:00
size_t num_components = identifier.name_parts.size();
2019-01-16 17:26:14 +00:00
if (num_components >= 2)
2019-01-17 17:01:48 +00:00
return identifier.name_parts[0] == table;
2019-01-16 17:26:14 +00:00
return false;
}
}