ClickHouse/dbms/src/Parsers/ASTRenameQuery.h

75 lines
2.0 KiB
C++
Raw Normal View History

2012-06-18 06:19:13 +00:00
#pragma once
#include <Parsers/IAST.h>
2017-04-21 12:39:28 +00:00
#include <Parsers/ASTQueryWithOnCluster.h>
2012-06-18 06:19:13 +00:00
namespace DB
{
2017-05-27 17:27:16 +00:00
/** RENAME query
2012-06-18 06:19:13 +00:00
*/
2017-04-21 12:39:28 +00:00
class ASTRenameQuery : public IAST, public ASTQueryWithOnCluster
2012-06-18 06:19:13 +00:00
{
public:
struct Table
{
String database;
String table;
};
2012-06-18 06:19:13 +00:00
struct Element
{
Table from;
Table to;
};
2012-06-18 06:19:13 +00:00
using Elements = std::vector<Element>;
Elements elements;
2012-06-18 06:19:13 +00:00
ASTRenameQuery() = default;
ASTRenameQuery(const StringRange range_) : IAST(range_) {}
2017-05-27 17:27:16 +00:00
/** Get the text that identifies this element. */
String getID() const override { return "Rename"; };
2012-06-18 06:19:13 +00:00
ASTPtr clone() const override { return std::make_shared<ASTRenameQuery>(*this); }
2017-04-21 12:39:28 +00:00
ASTPtr getRewrittenASTWithoutOnCluster(const std::string & new_database = {}) const override
{
auto query_ptr = clone();
ASTRenameQuery & query = static_cast<ASTRenameQuery &>(*query_ptr);
query.cluster.clear();
for (Element & elem : query.elements)
{
if (elem.from.database.empty())
elem.from.database = new_database;
if (elem.to.database.empty())
elem.to.database = new_database;
}
return query_ptr;
}
protected:
void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "RENAME TABLE " << (settings.hilite ? hilite_none : "");
2017-04-21 12:39:28 +00:00
for (auto it = elements.cbegin(); it != elements.cend(); ++it)
{
2017-04-21 12:39:28 +00:00
if (it != elements.cbegin())
settings.ostr << ", ";
settings.ostr << (!it->from.database.empty() ? backQuoteIfNeed(it->from.database) + "." : "") << backQuoteIfNeed(it->from.table)
<< (settings.hilite ? hilite_keyword : "") << " TO " << (settings.hilite ? hilite_none : "")
<< (!it->to.database.empty() ? backQuoteIfNeed(it->to.database) + "." : "") << backQuoteIfNeed(it->to.table);
}
2017-04-21 12:39:28 +00:00
formatOnCluster(settings);
}
2012-06-18 06:19:13 +00:00
};
}