2012-06-18 06:19:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/IAST.h>
|
2017-08-03 17:00:41 +00:00
|
|
|
#include <Parsers/ASTQueryWithOutput.h>
|
2017-04-21 12:39:28 +00:00
|
|
|
#include <Parsers/ASTQueryWithOnCluster.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#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
|
|
|
*/
|
2017-08-03 17:00:41 +00:00
|
|
|
class ASTRenameQuery : public ASTQueryWithOutput, public ASTQueryWithOnCluster
|
2012-06-18 06:19:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Table
|
|
|
|
{
|
|
|
|
String database;
|
|
|
|
String table;
|
|
|
|
};
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Element
|
|
|
|
{
|
|
|
|
Table from;
|
|
|
|
Table to;
|
|
|
|
};
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +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
|
|
|
|
|
2017-05-27 17:27:16 +00:00
|
|
|
/** Get the text that identifies this element. */
|
2018-12-07 12:34:40 +00:00
|
|
|
String getID(char) const override { return "Rename"; }
|
2012-06-18 06:19:13 +00:00
|
|
|
|
2017-08-03 17:00:41 +00:00
|
|
|
ASTPtr clone() const override
|
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTRenameQuery>(*this);
|
|
|
|
cloneOutputOptions(*res);
|
|
|
|
return res;
|
|
|
|
}
|
2015-08-05 21:38:31 +00:00
|
|
|
|
2017-08-03 17:00:41 +00:00
|
|
|
ASTPtr getRewrittenASTWithoutOnCluster(const std::string & new_database) const override
|
2017-04-21 12:39:28 +00:00
|
|
|
{
|
|
|
|
auto query_ptr = clone();
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & query = query_ptr->as<ASTRenameQuery &>();
|
2017-04-21 12:39:28 +00:00
|
|
|
|
2019-03-15 16:14:13 +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;
|
|
|
|
}
|
|
|
|
|
2015-08-05 21:38:31 +00:00
|
|
|
protected:
|
2017-12-01 18:36:55 +00:00
|
|
|
void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-03-31 20:38:05 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "")
|
|
|
|
<< (exchange ? "EXCHANGE TABLES " : "RENAME TABLE ") << (settings.hilite ? hilite_none : "");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
for (auto it = elements.cbegin(); it != elements.cend(); ++it)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-04-21 12:39:28 +00:00
|
|
|
if (it != elements.cbegin())
|
2017-04-01 07:20:54 +00:00
|
|
|
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 : "")
|
2017-04-01 07:20:54 +00:00
|
|
|
<< (!it->to.database.empty() ? backQuoteIfNeed(it->to.database) + "." : "") << backQuoteIfNeed(it->to.table);
|
|
|
|
}
|
2017-04-21 12:39:28 +00:00
|
|
|
|
|
|
|
formatOnCluster(settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-06-18 06:19:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|