2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2019-01-14 18:15:04 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromOStream.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2015-11-08 01:29:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-01 18:36:55 +00:00
|
|
|
void ASTIdentifier::formatImplWithoutAlias(const FormatSettings & settings, FormatState &, FormatStateStacked) const
|
2015-11-08 01:29:37 +00:00
|
|
|
{
|
2018-08-26 02:19:18 +00:00
|
|
|
auto format_element = [&](const String & elem_name)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_identifier : "");
|
2018-09-04 14:39:08 +00:00
|
|
|
settings.writeIdentifier(elem_name);
|
2017-04-01 07:20:54 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "");
|
|
|
|
};
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// A simple or compound identifier?
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (children.size() > 1)
|
|
|
|
{
|
|
|
|
for (size_t i = 0, size = children.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
settings.ostr << '.';
|
|
|
|
|
|
|
|
format_element(static_cast<const ASTIdentifier &>(*children[i].get()).name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format_element(name);
|
|
|
|
}
|
2015-11-08 01:29:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 16:34:11 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
ASTPtr database = ASTIdentifier::createSpecial(database_name);
|
|
|
|
ASTPtr table = ASTIdentifier::createSpecial(table_name);
|
|
|
|
|
|
|
|
ASTPtr database_and_table = ASTIdentifier::createSpecial(database_name + "." + table_name);
|
|
|
|
database_and_table->children = {database, table};
|
|
|
|
return database_and_table;
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:15:04 +00:00
|
|
|
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)
|
2019-01-14 18:15:04 +00:00
|
|
|
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)
|
2019-01-14 18:15:04 +00:00
|
|
|
return id->name;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<String> getTableIdentifierName(const ASTIdentifier & node)
|
|
|
|
{
|
2019-01-15 12:28:17 +00:00
|
|
|
if (node.special)
|
2019-01-14 18:15:04 +00:00
|
|
|
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)
|
2019-01-14 18:15:04 +00:00
|
|
|
return id->name;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void setIdentifierSpecial(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
if (ast)
|
|
|
|
if (ASTIdentifier * id = typeid_cast<ASTIdentifier *>(ast.get()))
|
|
|
|
id->setSpecial();
|
|
|
|
}
|
|
|
|
|
2015-11-08 01:29:37 +00:00
|
|
|
}
|