ClickHouse/src/Parsers/ParserDropQuery.cpp

131 lines
3.5 KiB
C++
Raw Normal View History

#include <Parsers/ASTDropQuery.h>
2011-11-05 23:31:19 +00:00
#include <Parsers/CommonParsers.h>
#include <Parsers/ParserDropQuery.h>
2011-11-05 23:31:19 +00:00
namespace DB
{
2020-03-18 00:57:00 +00:00
namespace
{
2018-04-21 00:35:20 +00:00
2020-12-07 13:55:24 +00:00
bool parseDropQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected, const ASTDropQuery::Kind kind)
2018-04-21 00:35:20 +00:00
{
ParserKeyword s_temporary("TEMPORARY");
ParserKeyword s_table("TABLE");
2019-10-08 11:10:29 +00:00
ParserKeyword s_dictionary("DICTIONARY");
2020-03-23 22:28:30 +00:00
ParserKeyword s_view("VIEW");
ParserKeyword s_database("DATABASE");
ParserToken s_dot(TokenType::Dot);
ParserKeyword s_if_exists("IF EXISTS");
ParserIdentifier name_p(true);
2020-11-30 17:52:32 +00:00
ParserKeyword s_permanently("PERMANENTLY");
2020-03-20 00:07:52 +00:00
ParserKeyword s_no_delay("NO DELAY");
2020-09-29 14:12:40 +00:00
ParserKeyword s_sync("SYNC");
ASTPtr database;
ASTPtr table;
2017-04-21 12:39:28 +00:00
String cluster_str;
bool if_exists = false;
bool temporary = false;
2019-10-08 11:10:29 +00:00
bool is_dictionary = false;
2020-03-23 22:28:30 +00:00
bool is_view = false;
2022-06-23 07:59:13 +00:00
bool sync = false;
2020-11-30 17:52:32 +00:00
bool permanently = false;
if (s_database.ignore(pos, expected))
{
if (s_if_exists.ignore(pos, expected))
if_exists = true;
if (!name_p.parse(pos, database, expected))
return false;
}
else
{
2020-03-23 22:28:30 +00:00
if (s_view.ignore(pos, expected))
is_view = true;
else if (s_dictionary.ignore(pos, expected))
is_dictionary = true;
else if (s_temporary.ignore(pos, expected))
temporary = true;
2020-12-07 13:55:24 +00:00
/// for TRUNCATE queries TABLE keyword is assumed as default and can be skipped
2020-12-07 15:19:05 +00:00
if (!is_view && !is_dictionary && (!s_table.ignore(pos, expected) && kind != ASTDropQuery::Kind::Truncate))
2019-10-08 11:10:29 +00:00
{
2020-03-23 22:28:30 +00:00
return false;
2019-10-08 11:10:29 +00:00
}
if (s_if_exists.ignore(pos, expected))
if_exists = true;
if (!name_p.parse(pos, table, expected))
return false;
if (s_dot.ignore(pos, expected))
{
database = table;
if (!name_p.parse(pos, table, expected))
return false;
}
2020-12-07 13:55:24 +00:00
}
2020-12-07 13:55:24 +00:00
/// common for tables / dictionaries / databases
if (ParserKeyword{"ON"}.ignore(pos, expected))
{
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
return false;
}
2020-03-20 00:07:52 +00:00
2020-12-07 13:55:24 +00:00
if (kind == ASTDropQuery::Kind::Detach && s_permanently.ignore(pos, expected))
permanently = true;
2020-11-30 17:52:32 +00:00
2020-12-07 13:55:24 +00:00
/// actually for TRUNCATE NO DELAY / SYNC means nothing
if (s_no_delay.ignore(pos, expected) || s_sync.ignore(pos, expected))
2022-06-23 07:59:13 +00:00
sync = true;
2018-02-26 03:37:08 +00:00
auto query = std::make_shared<ASTDropQuery>();
node = query;
2020-12-07 13:55:24 +00:00
query->kind = kind;
query->if_exists = if_exists;
query->temporary = temporary;
2019-10-08 11:10:29 +00:00
query->is_dictionary = is_dictionary;
2020-03-23 22:28:30 +00:00
query->is_view = is_view;
2022-06-23 07:59:13 +00:00
query->sync = sync;
2020-11-30 17:52:32 +00:00
query->permanently = permanently;
query->database = database;
query->table = table;
2021-09-06 22:24:47 +00:00
if (database)
query->children.push_back(database);
if (table)
query->children.push_back(table);
2017-04-21 12:39:28 +00:00
query->cluster = cluster_str;
return true;
2011-11-05 23:31:19 +00:00
}
2020-03-18 00:57:00 +00:00
}
bool ParserDropQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserKeyword s_drop("DROP");
ParserKeyword s_detach("DETACH");
ParserKeyword s_truncate("TRUNCATE");
if (s_drop.ignore(pos, expected))
2020-12-07 13:55:24 +00:00
return parseDropQuery(pos, node, expected, ASTDropQuery::Kind::Drop);
2020-03-18 00:57:00 +00:00
else if (s_detach.ignore(pos, expected))
2020-12-07 13:55:24 +00:00
return parseDropQuery(pos, node, expected, ASTDropQuery::Kind::Detach);
2020-03-18 00:57:00 +00:00
else if (s_truncate.ignore(pos, expected))
2020-12-07 13:55:24 +00:00
return parseDropQuery(pos, node, expected, ASTDropQuery::Kind::Truncate);
2020-03-18 00:57:00 +00:00
else
return false;
}
2011-11-05 23:31:19 +00:00
}