aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/MySQL/ASTDropQuery.cpp
blob: 890451e3e55a9d600d44d1fb06d505490e0349fb (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
#include <Parsers/MySQL/ASTDropQuery.h>

#include <Parsers/ASTIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/parseDatabaseAndTableName.h>
#include <Parsers/ExpressionListParsers.h>

namespace DB
{

namespace MySQLParser
{

ASTPtr ASTDropQuery::clone() const
{
    auto res = std::make_shared<ASTDropQuery>(*this);
    res->children.clear();
    res->is_truncate = is_truncate;
    res->if_exists = if_exists;
    return res;
}

bool ParserDropQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
    ParserKeyword s_drop("DROP");
    ParserKeyword s_truncate("TRUNCATE");
    ParserKeyword s_table("TABLE");
    ParserKeyword s_database("DATABASE");
    ParserKeyword s_if_exists("IF EXISTS");
    ParserKeyword s_view("VIEW");
    ParserKeyword on("ON");
    ParserIdentifier name_p(false);

    ParserKeyword s_event("EVENT");
    ParserKeyword s_function("FUNCTION");
    ParserKeyword s_index("INDEX");
    ParserKeyword s_server("SERVER");
    ParserKeyword s_trigger("TRIGGER");

    auto query = std::make_shared<ASTDropQuery>();
    node = query;
    ASTDropQuery::QualifiedNames names;
    bool if_exists = false;
    bool is_truncate = false;

    if (s_truncate.ignore(pos, expected))
    {
        s_table.ignore(pos, expected);
        is_truncate = true;
        query->kind = ASTDropQuery::Kind::Table;
        ASTDropQuery::QualifiedName name;
        if (parseDatabaseAndTableName(pos, expected, name.schema, name.shortName))
            names.push_back(name);
        else
            return false;
    }
    else if (s_drop.ignore(pos, expected))
    {
        if (s_database.ignore(pos, expected))
        {
            query->kind = ASTDropQuery::Kind::Database;
            if (s_if_exists.ignore(pos, expected))
                if_exists = true;
            ASTPtr database;
            if (!name_p.parse(pos, database, expected))
                return false;
        }
        else
        {
            if (s_view.ignore(pos, expected))
                query->kind = ASTDropQuery::Kind::View;
            else if (s_table.ignore(pos, expected))
                query->kind = ASTDropQuery::Kind::Table;
            else if (s_index.ignore(pos, expected))
            {
                ASTPtr index;
                query->kind = ASTDropQuery::Kind::Index;
                if (!(name_p.parse(pos, index, expected) && on.ignore(pos, expected)))
                    return false;
            }
            else if (s_event.ignore(pos, expected) || s_function.ignore(pos, expected) || s_server.ignore(pos, expected)
                || s_trigger.ignore(pos, expected))
            {
                query->kind = ASTDropQuery::Kind::Other;
            }
            else
                return false;

            if (s_if_exists.ignore(pos, expected))
                if_exists = true;
            //parse name
            auto parse_element = [&]
            {
                ASTDropQuery::QualifiedName element;
                if (parseDatabaseAndTableName(pos, expected, element.schema, element.shortName))
                {
                    names.emplace_back(std::move(element));
                    return true;
                }
                return false;
            };

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

    query->if_exists = if_exists;
    query->names = names;
    query->is_truncate = is_truncate;

    return true;
}

}

}