aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/Access/ParserShowCreateAccessEntityQuery.cpp
blob: 17caa6366f15aa6f7b7b3f3f051461801b14c1ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <Parsers/Access/ParserShowCreateAccessEntityQuery.h>
#include <Parsers/Access/ASTShowCreateAccessEntityQuery.h>
#include <Parsers/Access/ASTRowPolicyName.h>
#include <Parsers/Access/ParserRowPolicyName.h>
#include <Parsers/Access/parseUserName.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Parsers/parseDatabaseAndTableName.h>
#include <base/range.h>
#include <cassert>


namespace DB
{
namespace ErrorCodes
{
    extern const int NOT_IMPLEMENTED;
}


namespace
{
    bool parseEntityType(IParserBase::Pos & pos, Expected & expected, AccessEntityType & type, bool & plural)
    {
        for (auto i : collections::range(AccessEntityType::MAX))
        {
            const auto & type_info = AccessEntityTypeInfo::get(i);
            if (ParserKeyword{type_info.name}.ignore(pos, expected)
                || (!type_info.alias.empty() && ParserKeyword{type_info.alias}.ignore(pos, expected)))
            {
                type = i;
                plural = false;
                return true;
            }
        }

        for (auto i : collections::range(AccessEntityType::MAX))
        {
            const auto & type_info = AccessEntityTypeInfo::get(i);
            if (ParserKeyword{type_info.plural_name}.ignore(pos, expected)
                || (!type_info.plural_alias.empty() && ParserKeyword{type_info.plural_alias}.ignore(pos, expected)))
            {
                type = i;
                plural = true;
                return true;
            }
        }

        return false;
    }

    bool parseOnDBAndTableName(IParserBase::Pos & pos, Expected & expected, String & database, bool & any_database, String & table, bool & any_table)
    {
        return IParserBase::wrapParseImpl(pos, [&]
        {
            return ParserKeyword{"ON"}.ignore(pos, expected)
                && parseDatabaseAndTableNameOrAsterisks(pos, expected, database, any_database, table, any_table);
        });
    }
}


bool ParserShowCreateAccessEntityQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    if (!ParserKeyword{"SHOW CREATE"}.ignore(pos, expected))
        return false;

    AccessEntityType type;
    bool plural;
    if (!parseEntityType(pos, expected, type, plural))
        return false;

    Strings names;
    std::shared_ptr<ASTRowPolicyNames> row_policy_names;
    bool all = false;
    bool current_quota = false;
    bool current_user = false;
    String short_name;
    std::optional<std::pair<String, String>> database_and_table_name;

    switch (type)
    {
        case AccessEntityType::USER:
        {
            if (parseCurrentUserTag(pos, expected))
                current_user = true;
            else if (parseUserNames(pos, expected, names))
            {
            }
            else if (plural)
                all = true;
            else
                current_user = true;
            break;
        }
        case AccessEntityType::ROLE:
        {
            if (parseRoleNames(pos, expected, names))
            {
            }
            else if (plural)
                all = true;
            else
                return false;
            break;
        }
        case AccessEntityType::ROW_POLICY:
        {
            ASTPtr ast;
            String database, table_name;
            bool any_database, any_table;
            if (ParserRowPolicyNames{}.parse(pos, ast, expected))
                row_policy_names = typeid_cast<std::shared_ptr<ASTRowPolicyNames>>(ast);
            else if (parseOnDBAndTableName(pos, expected, database, any_database, table_name, any_table))
            {
                if (any_database)
                    all = true;
                else
                    database_and_table_name.emplace(database, table_name);
            }
            else if (parseIdentifierOrStringLiteral(pos, expected, short_name))
            {
            }
            else if (plural)
                all = true;
            else
                return false;
            break;
        }
        case AccessEntityType::SETTINGS_PROFILE:
        {
            if (parseIdentifiersOrStringLiterals(pos, expected, names))
            {
            }
            else if (plural)
                all = true;
            else
                return false;
            break;
        }
        case AccessEntityType::QUOTA:
        {
            if (parseIdentifiersOrStringLiterals(pos, expected, names))
            {
            }
            else if (plural)
                all = true;
            else
                current_quota = true;
            break;
        }
        case AccessEntityType::MAX:
            throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Type {} is not implemented in SHOW CREATE query", toString(type));
    }

    auto query = std::make_shared<ASTShowCreateAccessEntityQuery>();
    node = query;

    query->type = type;
    query->names = std::move(names);
    query->current_quota = current_quota;
    query->current_user = current_user;
    query->row_policy_names = std::move(row_policy_names);
    query->all = all;
    query->short_name = std::move(short_name);
    query->database_and_table_name = std::move(database_and_table_name);

    return true;
}
}