1#include <Parsers/ParserDropAccessEntityQuery.h>
2#include <Parsers/ASTDropAccessEntityQuery.h>
3#include <Parsers/CommonParsers.h>
4#include <Parsers/parseIdentifierOrStringLiteral.h>
5#include <Parsers/parseDatabaseAndTableName.h>
6#include <Access/Quota.h>
7
8
9namespace DB
10{
11namespace
12{
13 bool parseNames(IParserBase::Pos & pos, Expected & expected, Strings & names)
14 {
15 do
16 {
17 String name;
18 if (!parseIdentifierOrStringLiteral(pos, expected, name))
19 return false;
20
21 names.push_back(std::move(name));
22 }
23 while (ParserToken{TokenType::Comma}.ignore(pos, expected));
24 return true;
25 }
26}
27
28
29bool ParserDropAccessEntityQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
30{
31 if (!ParserKeyword{"DROP"}.ignore(pos, expected))
32 return false;
33
34 using Kind = ASTDropAccessEntityQuery::Kind;
35 Kind kind;
36 if (ParserKeyword{"QUOTA"}.ignore(pos, expected))
37 kind = Kind::QUOTA;
38 else if (ParserKeyword{"POLICY"}.ignore(pos, expected) || ParserKeyword{"ROW POLICY"}.ignore(pos, expected))
39 kind = Kind::ROW_POLICY;
40 else
41 return false;
42
43 bool if_exists = false;
44 if (ParserKeyword{"IF EXISTS"}.ignore(pos, expected))
45 if_exists = true;
46
47 Strings names;
48 std::vector<RowPolicy::FullNameParts> row_policies_names;
49
50 if (kind == Kind::ROW_POLICY)
51 {
52 do
53 {
54 Strings policy_names;
55 if (!parseNames(pos, expected, policy_names))
56 return false;
57 String database, table_name;
58 if (!ParserKeyword{"ON"}.ignore(pos, expected) || !parseDatabaseAndTableName(pos, expected, database, table_name))
59 return false;
60 for (const String & policy_name : policy_names)
61 row_policies_names.push_back({database, table_name, policy_name});
62 }
63 while (ParserToken{TokenType::Comma}.ignore(pos, expected));
64 }
65 else
66 {
67 if (!parseNames(pos, expected, names))
68 return false;
69 }
70
71 auto query = std::make_shared<ASTDropAccessEntityQuery>(kind);
72 node = query;
73
74 query->if_exists = if_exists;
75 query->names = std::move(names);
76 query->row_policies_names = std::move(row_policies_names);
77
78 return true;
79}
80}
81