aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Databases/MySQL/tryParseTableIDFromDDL.cpp
blob: a01eb31145001b7a82e7109bdeb65526219edd0e (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
#include <Databases/MySQL/tryParseTableIDFromDDL.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>

namespace DB
{

StorageID tryParseTableIDFromDDL(const String & query, const String & default_database_name)
{
    bool is_ddl = false;
    Tokens tokens(query.data(), query.data() + query.size());
    IParser::Pos pos(tokens, 0);
    Expected expected;
    if (ParserKeyword("CREATE TEMPORARY TABLE").ignore(pos, expected) || ParserKeyword("CREATE TABLE").ignore(pos, expected))
    {
        ParserKeyword("IF NOT EXISTS").ignore(pos, expected);
        is_ddl = true;
    }
    else if (ParserKeyword("ALTER TABLE").ignore(pos, expected) || ParserKeyword("RENAME TABLE").ignore(pos, expected))
    {
        is_ddl = true;
    }
    else if (ParserKeyword("DROP TABLE").ignore(pos, expected) || ParserKeyword("DROP TEMPORARY TABLE").ignore(pos, expected))
    {
        ParserKeyword("IF EXISTS").ignore(pos, expected);
        is_ddl = true;
    }
    else if (ParserKeyword("TRUNCATE").ignore(pos, expected))
    {
        ParserKeyword("TABLE").ignore(pos, expected);
        is_ddl = true;
    }

    ASTPtr table;
    if (!is_ddl || !ParserCompoundIdentifier(true).parse(pos, table, expected))
        return StorageID::createEmpty();
    auto table_id = table->as<ASTTableIdentifier>()->getTableId();
    if (table_id.database_name.empty())
        table_id.database_name = default_database_name;
    return table_id;
}

}