2011-11-05 23:31:19 +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>
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-05-05 16:14:06 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
}
|
2011-11-05 23:31:19 +00:00
|
|
|
|
2017-05-27 17:27:16 +00:00
|
|
|
/** DROP query
|
2011-11-05 23:31:19 +00:00
|
|
|
*/
|
2017-08-03 17:00:41 +00:00
|
|
|
class ASTDropQuery : public ASTQueryWithOutput, public ASTQueryWithOnCluster
|
2011-11-05 23:31:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-04-21 00:35:20 +00:00
|
|
|
enum Kind
|
|
|
|
{
|
|
|
|
Drop,
|
|
|
|
Detach,
|
|
|
|
Truncate,
|
|
|
|
};
|
|
|
|
|
|
|
|
Kind kind;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool if_exists{false};
|
2018-01-27 16:17:05 +00:00
|
|
|
bool temporary{false};
|
2017-04-01 07:20:54 +00:00
|
|
|
String database;
|
|
|
|
String table;
|
2011-11-05 23:31:19 +00:00
|
|
|
|
2017-05-27 17:27:16 +00:00
|
|
|
/** Get the text that identifies this element. */
|
2018-04-21 00:35:20 +00:00
|
|
|
String getID() const override
|
|
|
|
{
|
|
|
|
if (kind == ASTDropQuery::Kind::Drop)
|
|
|
|
return "DropQuery_" + database + "_" + table;
|
|
|
|
else if (kind == ASTDropQuery::Kind::Detach)
|
|
|
|
return "DetachQuery_" + database + "_" + table;
|
|
|
|
else if (kind == ASTDropQuery::Kind::Truncate)
|
|
|
|
return "TruncateQuery_" + database + "_" + table;
|
|
|
|
else
|
|
|
|
throw Exception("Not supported kind of drop query.", ErrorCodes::SYNTAX_ERROR);
|
2018-06-09 14:24:50 +00:00
|
|
|
}
|
2011-12-12 06:15:34 +00:00
|
|
|
|
2017-08-03 17:00:41 +00:00
|
|
|
ASTPtr clone() const override
|
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTDropQuery>(*this);
|
|
|
|
cloneOutputOptions(*res);
|
|
|
|
return res;
|
|
|
|
}
|
2015-08-05 21:38:31 +00:00
|
|
|
|
2017-04-13 16:12:56 +00:00
|
|
|
ASTPtr getRewrittenASTWithoutOnCluster(const std::string & new_database) const override
|
|
|
|
{
|
|
|
|
auto query_ptr = clone();
|
2017-08-03 17:00:41 +00:00
|
|
|
auto & query = static_cast<ASTDropQuery &>(*query_ptr);
|
2017-04-13 16:12:56 +00:00
|
|
|
|
|
|
|
query.cluster.clear();
|
|
|
|
if (query.database.empty())
|
|
|
|
query.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
|
|
|
{
|
|
|
|
if (table.empty() && !database.empty())
|
|
|
|
{
|
2018-04-21 00:35:20 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "");
|
|
|
|
if (kind == ASTDropQuery::Kind::Drop)
|
|
|
|
settings.ostr << "DROP DATABASE ";
|
|
|
|
else if (kind == ASTDropQuery::Kind::Detach)
|
|
|
|
settings.ostr << "DETACH DATABASE ";
|
|
|
|
else if (kind == ASTDropQuery::Kind::Truncate)
|
|
|
|
settings.ostr << "TRUNCATE DATABASE ";
|
|
|
|
else
|
|
|
|
throw Exception("Not supported kind of drop query.", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
settings.ostr << (if_exists ? "IF EXISTS " : "") << (settings.hilite ? hilite_none : "") << backQuoteIfNeed(database);
|
2017-05-30 11:49:17 +00:00
|
|
|
formatOnCluster(settings);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-21 00:35:20 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "");
|
|
|
|
if (kind == ASTDropQuery::Kind::Drop)
|
|
|
|
settings.ostr << "DROP TABLE ";
|
|
|
|
else if (kind == ASTDropQuery::Kind::Detach)
|
|
|
|
settings.ostr << "DETACH TABLE ";
|
|
|
|
else if (kind == ASTDropQuery::Kind::Truncate)
|
|
|
|
settings.ostr << "TRUNCATE TABLE ";
|
|
|
|
else
|
|
|
|
throw Exception("Not supported kind of drop query.", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
settings.ostr << (if_exists ? "IF EXISTS " : "") << (settings.hilite ? hilite_none : "")
|
|
|
|
<< (!database.empty() ? backQuoteIfNeed(database) + "." : "") << backQuoteIfNeed(table);
|
2017-05-30 11:49:17 +00:00
|
|
|
formatOnCluster(settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-05 23:31:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|