ClickHouse/src/Parsers/ASTRenameQuery.h

116 lines
3.8 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;
bool if_exists{false}; /// If this directive is used, one will not get an error if the table/database/dictionary to be renamed/exchanged doesn't exist.
};
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
2021-07-01 13:21:38 +00:00
/// Special flag for CREATE OR REPLACE. Do not throw if the second table does not exist.
bool rename_if_cannot_exchange{false};
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;
}
2021-12-29 12:32:49 +00:00
virtual QueryKind getQueryKind() const override { return QueryKind::Rename; }
2021-08-04 14:09:23 +00:00
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 : "");
2021-11-08 07:14:45 +00:00
if (elements.at(0).if_exists)
2021-11-11 16:29:56 +00:00
settings.ostr << (settings.hilite ? hilite_keyword : "") << "IF EXISTS " << (settings.hilite ? hilite_none : "");
2021-11-08 07:14:45 +00:00
2020-07-07 12:11:58 +00:00
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 && dictionary)
settings.ostr << "EXCHANGE DICTIONARIES ";
else if (exchange)
2020-07-16 21:41:26 +00:00
settings.ostr << "EXCHANGE TABLES ";
else if (dictionary)
settings.ostr << "RENAME DICTIONARY ";
else
settings.ostr << "RENAME TABLE ";
2020-07-16 21:41:26 +00:00
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 << ", ";
2021-11-08 07:14:45 +00:00
if (it->if_exists)
2021-11-11 16:29:56 +00:00
settings.ostr << (settings.hilite ? hilite_keyword : "") << "IF EXISTS " << (settings.hilite ? hilite_none : "");
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
};
}