aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/Access/InterpreterShowAccessEntitiesQuery.cpp
blob: e7b9237b680f86773aba908096c02e123f7f8fd6 (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
#include <Interpreters/Access/InterpreterShowAccessEntitiesQuery.h>
#include <Parsers/Access/ASTShowAccessEntitiesQuery.h>
#include <Parsers/formatAST.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/quoteString.h>
#include <Interpreters/Context.h>
#include <Interpreters/executeQuery.h>


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


InterpreterShowAccessEntitiesQuery::InterpreterShowAccessEntitiesQuery(const ASTPtr & query_ptr_, ContextMutablePtr context_)
    : WithMutableContext(context_), query_ptr(query_ptr_)
{
}


BlockIO InterpreterShowAccessEntitiesQuery::execute()
{
    return executeQuery(getRewrittenQuery(), getContext(), true);
}


String InterpreterShowAccessEntitiesQuery::getRewrittenQuery() const
{
    auto & query = query_ptr->as<ASTShowAccessEntitiesQuery &>();
    query.replaceEmptyDatabase(getContext()->getCurrentDatabase());

    String origin;
    String expr = "*";
    String filter;
    String order;

    switch (query.type)
    {
        case AccessEntityType::ROW_POLICY:
        {
            origin = "row_policies";
            expr = "name";

            if (!query.short_name.empty())
                filter = "short_name = " + quoteString(query.short_name);

            if (query.database_and_table_name)
            {
                const String & database = query.database_and_table_name->first;
                const String & table_name = query.database_and_table_name->second;
                if (!database.empty())
                    filter += String{filter.empty() ? "" : " AND "} + "database = " + quoteString(database);
                if (!table_name.empty())
                    filter += String{filter.empty() ? "" : " AND "} + "table = " + quoteString(table_name);
                if (!database.empty() && !table_name.empty())
                    expr = "short_name";
            }
            break;
        }

        case AccessEntityType::QUOTA:
        {
            if (query.current_quota)
            {
                origin = "quota_usage";
                order = "duration";
            }
            else
            {
                origin = "quotas";
                expr = "name";
            }
            break;
        }

        case AccessEntityType::SETTINGS_PROFILE:
        {
            origin = "settings_profiles";
            expr = "name";
            break;
        }

        case AccessEntityType::USER:
        {
            origin = "users";
            expr = "name";
            break;
        }

        case AccessEntityType::ROLE:
        {
            if (query.current_roles)
            {
                origin = "current_roles";
                order = "role_name";
            }
            else if (query.enabled_roles)
            {
                origin = "enabled_roles";
                order = "role_name";
            }
            else
            {
                origin = "roles";
                expr = "name";
            }
            break;
        }

        case AccessEntityType::MAX:
            break;
    }

    if (origin.empty())
        throw Exception(ErrorCodes::NOT_IMPLEMENTED, "{}: type is not supported by SHOW query", toString(query.type));

    if (order.empty() && expr != "*")
        order = expr;

    return "SELECT " + expr + " from system." + origin +
            (filter.empty() ? "" : " WHERE " + filter) +
            (order.empty() ? "" : " ORDER BY " + order);
}

}