ClickHouse/src/Parsers/ASTQueryWithTableAndOutput.cpp

83 lines
1.9 KiB
C++
Raw Normal View History

2021-10-12 18:10:07 +00:00
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTQueryWithTableAndOutput.h>
#include <Common/quoteString.h>
2020-11-09 16:05:40 +00:00
#include <IO/Operators.h>
namespace DB
{
2021-10-12 18:10:07 +00:00
String ASTQueryWithTableAndOutput::getDatabase() const
{
String name;
tryGetIdentifierNameInto(database, name);
return name;
}
String ASTQueryWithTableAndOutput::getTable() const
{
String name;
tryGetIdentifierNameInto(table, name);
return name;
}
void ASTQueryWithTableAndOutput::setDatabase(const String & name)
{
2021-10-21 12:50:31 +00:00
if (name.empty() && !database)
return;
assert(!name.empty());
if (database)
{
if (auto * database_ptr = database->as<ASTIdentifier>())
database_ptr->setShortName(name);
}
2021-10-12 18:10:07 +00:00
else
2021-10-21 12:50:31 +00:00
{
2021-10-12 18:10:07 +00:00
database = std::make_shared<ASTIdentifier>(name);
2021-10-21 12:50:31 +00:00
children.push_back(database);
}
2021-10-12 18:10:07 +00:00
}
void ASTQueryWithTableAndOutput::setTable(const String & name)
{
2021-10-21 12:50:31 +00:00
if (name.empty() && !table)
return;
assert(!name.empty());
if (table)
{
if (auto * table_ptr = table->as<ASTIdentifier>())
table_ptr->setShortName(name);
}
2021-10-12 18:10:07 +00:00
else
2021-10-21 12:50:31 +00:00
{
2021-10-12 18:10:07 +00:00
table = std::make_shared<ASTIdentifier>(name);
2021-10-21 12:50:31 +00:00
children.push_back(table);
}
2021-10-12 18:10:07 +00:00
}
void ASTQueryWithTableAndOutput::cloneTableOptions(ASTQueryWithTableAndOutput & cloned) const
{
if (database)
{
cloned.database = database->clone();
cloned.children.push_back(cloned.database);
}
if (table)
{
cloned.table = table->clone();
cloned.children.push_back(cloned.table);
}
}
void ASTQueryWithTableAndOutput::formatHelper(const FormatSettings & settings, const char * name) const
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << name << " " << (settings.hilite ? hilite_none : "");
settings.ostr << (!getDatabase().empty() ? backQuoteIfNeed(getDatabase()) + "." : "") << backQuoteIfNeed(getTable());
}
}