ClickHouse/src/Parsers/ASTQueryWithTableAndOutput.cpp

75 lines
1.7 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 (database)
{
2021-11-11 13:28:18 +00:00
std::erase(children, database);
database.reset();
2021-10-21 12:50:31 +00:00
}
2021-11-11 13:28:18 +00:00
if (!name.empty())
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 (table)
{
2021-11-11 13:28:18 +00:00
std::erase(children, table);
table.reset();
2021-10-21 12:50:31 +00:00
}
2021-11-11 13:28:18 +00:00
if (!name.empty())
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 : "");
2021-11-11 13:28:18 +00:00
settings.ostr << (database ? backQuoteIfNeed(getDatabase()) + "." : "") << backQuoteIfNeed(getTable());
}
}