ParserRolesOrUsersSet.cpp 3.9 KB
Newer Older
1
#include <Parsers/ParserRolesOrUsersSet.h>
V
Vitaly Baranov 已提交
2
#include <Parsers/CommonParsers.h>
3 4
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ASTLiteral.h>
5
#include <Parsers/ASTRolesOrUsersSet.h>
6
#include <Parsers/parseUserName.h>
7
#include <Parsers/ExpressionListParsers.h>
V
Vitaly Baranov 已提交
8 9 10 11 12
#include <boost/range/algorithm/find.hpp>


namespace DB
{
13
namespace
V
Vitaly Baranov 已提交
14
{
15
    bool parseNameOrID(IParserBase::Pos & pos, Expected & expected, bool id_mode, String & res)
16 17 18
    {
        return IParserBase::wrapParseImpl(pos, [&]
        {
19
            if (!id_mode)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
                return parseRoleName(pos, expected, res);

            if (!ParserKeyword{"ID"}.ignore(pos, expected))
                return false;
            if (!ParserToken(TokenType::OpeningRoundBracket).ignore(pos, expected))
                return false;
            ASTPtr ast;
            if (!ParserStringLiteral{}.parse(pos, ast, expected))
                return false;
            String id = ast->as<ASTLiteral &>().value.safeGet<String>();
            if (!ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
                return false;

            res = std::move(id);
            return true;
        });
    }

    bool parseBeforeExcept(
        IParserBase::Pos & pos,
        Expected & expected,
        bool id_mode,
42
        bool allow_all,
43
        bool allow_current_user,
44
        bool & all,
45
        Strings & names,
46
        bool & current_user)
V
Vitaly Baranov 已提交
47
    {
48 49
        bool res_all = false;
        Strings res_names;
50 51
        bool res_current_user = false;
        Strings res_with_roles_names;
52 53

        auto parse_element = [&]
V
Vitaly Baranov 已提交
54
        {
55 56 57 58
            if (ParserKeyword{"NONE"}.ignore(pos, expected))
                return true;

            if (allow_all && ParserKeyword{"ALL"}.ignore(pos, expected))
V
Vitaly Baranov 已提交
59
            {
60 61
                res_all = true;
                return true;
V
Vitaly Baranov 已提交
62 63
            }

64 65 66 67 68 69 70
            if (allow_current_user && parseCurrentUserTag(pos, expected))
            {
                res_current_user = true;
                return true;
            }

            String name;
71
            if (parseNameOrID(pos, expected, id_mode, name))
72 73 74 75 76 77 78 79 80 81 82 83 84
            {
                res_names.emplace_back(std::move(name));
                return true;
            }

            return false;
        };

        if (!ParserList::parseUtil(pos, expected, parse_element, false))
            return false;

        names = std::move(res_names);
        current_user = res_current_user;
85
        all = res_all;
86
        return true;
V
Vitaly Baranov 已提交
87 88
    }

89 90 91 92
    bool parseExceptAndAfterExcept(
        IParserBase::Pos & pos,
        Expected & expected,
        bool id_mode,
93
        bool allow_current_user,
94 95
        Strings & except_names,
        bool & except_current_user)
V
Vitaly Baranov 已提交
96
    {
97
        return IParserBase::wrapParseImpl(pos, [&] {
98 99 100
            if (!ParserKeyword{"EXCEPT"}.ignore(pos, expected))
                return false;

101
            bool unused;
102
            return parseBeforeExcept(pos, expected, id_mode, false, allow_current_user, unused, except_names, except_current_user);
103
        });
V
Vitaly Baranov 已提交
104
    }
105 106 107
}


108
bool ParserRolesOrUsersSet::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
109
{
110
    bool all = false;
111 112 113 114 115
    Strings names;
    bool current_user = false;
    Strings except_names;
    bool except_current_user = false;

116
    if (!parseBeforeExcept(pos, expected, id_mode, allow_all, allow_current_user, all, names, current_user))
117 118
        return false;

119
    parseExceptAndAfterExcept(pos, expected, id_mode, allow_current_user, except_names, except_current_user);
120 121 122

    if (all)
        names.clear();
V
Vitaly Baranov 已提交
123

124
    auto result = std::make_shared<ASTRolesOrUsersSet>();
125
    result->names = std::move(names);
V
Vitaly Baranov 已提交
126
    result->current_user = current_user;
127 128
    result->all = all;
    result->except_names = std::move(except_names);
V
Vitaly Baranov 已提交
129
    result->except_current_user = except_current_user;
130 131
    result->allow_users = allow_users;
    result->allow_roles = allow_roles;
132
    result->id_mode = id_mode;
V
Vitaly Baranov 已提交
133 134 135 136 137
    node = result;
    return true;
}

}