ClickHouse/dbms/src/Parsers/ASTRenameQuery.h

79 lines
2.1 KiB
C++
Raw Normal View History

2012-06-18 06:19:13 +00:00
#pragma once
#include <Parsers/IAST.h>
#include <Parsers/ASTQueryWithOutput.h>
2017-04-21 12:39:28 +00:00
#include <Parsers/ASTQueryWithOnCluster.h>
#include <Common/quoteString.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
*/
class ASTRenameQuery : public ASTQueryWithOutput, 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
2017-05-27 17:27:16 +00:00
/** Get the text that identifies this element. */
String getID(char) const override { return "Rename"; }
2012-06-18 06:19:13 +00:00
ASTPtr clone() const override
{
auto res = std::make_shared<ASTRenameQuery>(*this);
cloneOutputOptions(*res);
return res;
}
ASTPtr getRewrittenASTWithoutOnCluster(const std::string & new_database) const override
2017-04-21 12:39:28 +00:00
{
auto query_ptr = clone();
auto & query = query_ptr->as<ASTRenameQuery &>();
2017-04-21 12:39:28 +00:00
query.cluster.clear();
for (Element & elem : query.elements)
2017-04-21 12:39:28 +00:00
{
if (elem.from.database.empty())
elem.from.database = new_database;
if (elem.to.database.empty())
elem.to.database = new_database;
}
return query_ptr;
}
protected:
2017-12-01 18:36:55 +00:00
void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) 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
};
}