1#include <Parsers/ASTIdentifier.h>
2#include <Parsers/ASTDropQuery.h>
3
4#include <Parsers/CommonParsers.h>
5#include <Parsers/ParserDropQuery.h>
6
7
8namespace DB
9{
10
11namespace ErrorCodes
12{
13 extern const int SYNTAX_ERROR;
14 extern const int LOGICAL_ERROR;
15}
16
17bool ParserDropQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
18{
19 ParserKeyword s_drop("DROP");
20 ParserKeyword s_detach("DETACH");
21 ParserKeyword s_truncate("TRUNCATE");
22
23 if (s_drop.ignore(pos, expected))
24 return parseDropQuery(pos, node, expected);
25 else if (s_detach.ignore(pos, expected))
26 return parseDetachQuery(pos, node, expected);
27 else if (s_truncate.ignore(pos, expected))
28 return parseTruncateQuery(pos, node, expected);
29 else
30 return false;
31}
32
33bool ParserDropQuery::parseDetachQuery(Pos & pos, ASTPtr & node, Expected & expected)
34{
35 if (parseDropQuery(pos, node, expected))
36 {
37 auto * drop_query = node->as<ASTDropQuery>();
38 drop_query->kind = ASTDropQuery::Kind::Detach;
39 return true;
40 }
41 return false;
42}
43
44bool ParserDropQuery::parseTruncateQuery(Pos & pos, ASTPtr & node, Expected & expected)
45{
46 if (parseDropQuery(pos, node, expected))
47 {
48 auto * drop_query = node->as<ASTDropQuery>();
49 drop_query->kind = ASTDropQuery::Kind::Truncate;
50 return true;
51 }
52 return false;
53}
54
55bool ParserDropQuery::parseDropQuery(Pos & pos, ASTPtr & node, Expected & expected)
56{
57 ParserKeyword s_temporary("TEMPORARY");
58 ParserKeyword s_table("TABLE");
59 ParserKeyword s_dictionary("DICTIONARY");
60 ParserKeyword s_database("DATABASE");
61 ParserToken s_dot(TokenType::Dot);
62 ParserKeyword s_if_exists("IF EXISTS");
63 ParserIdentifier name_p;
64
65 ASTPtr database;
66 ASTPtr table;
67 String cluster_str;
68 bool if_exists = false;
69 bool temporary = false;
70 bool is_dictionary = false;
71
72 if (s_database.ignore(pos, expected))
73 {
74 if (s_if_exists.ignore(pos, expected))
75 if_exists = true;
76
77 if (!name_p.parse(pos, database, expected))
78 return false;
79
80 if (ParserKeyword{"ON"}.ignore(pos, expected))
81 {
82 if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
83 return false;
84 }
85 }
86 else
87 {
88 if (s_temporary.ignore(pos, expected))
89 temporary = true;
90
91 if (!s_table.ignore(pos, expected))
92 {
93 if (!s_dictionary.ignore(pos, expected))
94 return false;
95 is_dictionary = true;
96 }
97
98 if (s_if_exists.ignore(pos, expected))
99 if_exists = true;
100
101 if (!name_p.parse(pos, table, expected))
102 return false;
103
104 if (s_dot.ignore(pos, expected))
105 {
106 database = table;
107 if (!name_p.parse(pos, table, expected))
108 return false;
109 }
110
111 if (ParserKeyword{"ON"}.ignore(pos, expected))
112 {
113 if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
114 return false;
115 }
116 }
117
118 auto query = std::make_shared<ASTDropQuery>();
119 node = query;
120
121 query->kind = ASTDropQuery::Kind::Drop;
122 query->if_exists = if_exists;
123 query->temporary = temporary;
124 query->is_dictionary = is_dictionary;
125
126 tryGetIdentifierNameInto(database, query->database);
127 tryGetIdentifierNameInto(table, query->table);
128
129 query->cluster = cluster_str;
130
131 return true;
132}
133
134}
135