ClickHouse/src/Parsers/ASTRenameQuery.h

101 lines
3.0 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>
2020-11-09 16:05:40 +00:00
#include <IO/Operators.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
2020-03-31 20:38:05 +00:00
bool exchange{false}; /// For EXCHANGE TABLES
2020-07-07 12:11:58 +00:00
bool database{false}; /// For RENAME DATABASE
2020-07-16 21:41:26 +00:00
bool dictionary{false}; /// For RENAME DICTIONARY
2020-03-31 20:38:05 +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
{
2020-07-07 12:11:58 +00:00
if (database)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "RENAME DATABASE " << (settings.hilite ? hilite_none : "");
settings.ostr << backQuoteIfNeed(elements.at(0).from.database);
settings.ostr << (settings.hilite ? hilite_keyword : "") << " TO " << (settings.hilite ? hilite_none : "");
settings.ostr << backQuoteIfNeed(elements.at(0).to.database);
formatOnCluster(settings);
return;
}
2020-07-16 21:41:26 +00:00
settings.ostr << (settings.hilite ? hilite_keyword : "");
if (exchange)
settings.ostr << "EXCHANGE TABLES ";
else if (dictionary)
settings.ostr << "RENAME DICTIONARY ";
else
settings.ostr << "RENAME TABLE ";
settings.ostr << (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)
2020-03-31 20:38:05 +00:00
<< (settings.hilite ? hilite_keyword : "") << (exchange ? " AND " : " 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
};
}