aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/Access/ParserShowAccessEntitiesQuery.cpp
blob: 15cb815f45748b8dd8654f3f0e63a60ed692a3d3 (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
#include <Parsers/Access/ParserShowAccessEntitiesQuery.h>
#include <Parsers/Access/ASTShowAccessEntitiesQuery.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/parseDatabaseAndTableName.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <base/range.h>


namespace DB
{
namespace
{
    bool parseEntityType(IParserBase::Pos & pos, Expected & expected, AccessEntityType & type)
    {
        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;
                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 ParserShowAccessEntitiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    if (!ParserKeyword{"SHOW"}.ignore(pos, expected))
        return false;

    AccessEntityType type;
    bool all = false;
    bool current_quota = false;
    bool current_roles = false;
    bool enabled_roles = false;

    if (parseEntityType(pos, expected, type))
    {
        all = true;
    }
    else if (ParserKeyword{"CURRENT ROLES"}.ignore(pos, expected))
    {
        type = AccessEntityType::ROLE;
        current_roles = true;
    }
    else if (ParserKeyword{"ENABLED ROLES"}.ignore(pos, expected))
    {
        type = AccessEntityType::ROLE;
        enabled_roles = true;
    }
    else if (ParserKeyword{"CURRENT QUOTA"}.ignore(pos, expected) || ParserKeyword{"QUOTA"}.ignore(pos, expected))
    {
        type = AccessEntityType::QUOTA;
        current_quota = true;
    }
    else
        return false;

    String short_name;
    std::optional<std::pair<String, String>> database_and_table_name;
    if (type == AccessEntityType::ROW_POLICY)
    {
        String database, table_name;
        bool any_database, any_table;
        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
            all = true;
    }

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

    query->type = type;
    query->all = all;
    query->current_quota = current_quota;
    query->current_roles = current_roles;
    query->enabled_roles = enabled_roles;
    query->short_name = std::move(short_name);
    query->database_and_table_name = std::move(database_and_table_name);

    return true;
}
}