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

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

namespace DB
{

namespace MySQLParser
{

ASTPtr ASTDeclareConstraint::clone() const
{
    auto res = std::make_shared<ASTDeclareConstraint>(*this);
    res->children.clear();

    if (check_expression)
    {
        res->check_expression = check_expression->clone();
        res->children.emplace_back(res->check_expression);
    }

    return res;
}

bool ParserDeclareConstraint::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
    bool enforced = true;
    ASTPtr constraint_symbol;
    ASTPtr index_check_expression;
    ParserExpression p_expression;

    if (ParserKeyword("CONSTRAINT").ignore(pos, expected))
    {
        if (!ParserKeyword("CHECK").checkWithoutMoving(pos, expected))
            ParserIdentifier().parse(pos, constraint_symbol, expected);
    }


    if (!ParserKeyword("CHECK").ignore(pos, expected))
        return false;

    if (!p_expression.parse(pos, index_check_expression, expected))
        return false;

    if (ParserKeyword("NOT").ignore(pos, expected))
    {
        if (!ParserKeyword("ENFORCED").ignore(pos, expected))
            return false;

        enforced = false;
    }
    else
    {
        enforced = true;
        ParserKeyword("ENFORCED").ignore(pos, expected);
    }

    auto declare_constraint = std::make_shared<ASTDeclareConstraint>();
    declare_constraint->enforced = enforced;
    declare_constraint->check_expression = index_check_expression;

    if (constraint_symbol)
        declare_constraint->constraint_name = constraint_symbol->as<ASTIdentifier>()->name();

    node = declare_constraint;
    return true;
}

}

}