aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/MySQL/InterpretersMySQLDDLQuery.h
blob: 27f467a12aefec36f5ef67fe90e226a49a2e5d35 (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
#pragma once

#include <Interpreters/IInterpreter.h>
#include <Interpreters/executeQuery.h>
#include <Parsers/ASTRenameQuery.h>
#include <Parsers/IAST_fwd.h>
#include <Parsers/MySQL/ASTAlterQuery.h>
#include <Parsers/MySQL/ASTCreateQuery.h>
#include <Parsers/MySQL/ASTDropQuery.h>
#include <Parsers/queryToString.h>
#include <Parsers/ASTExpressionList.h>

namespace DB
{

class NamesAndTypesList;

namespace MySQLInterpreter
{
    struct InterpreterDropImpl
    {
        using TQuery = MySQLParser::ASTDropQuery;

        static void validate(const TQuery & query, ContextPtr context);

        static ASTs getRewrittenQueries(
            const TQuery & drop_query, ContextPtr context, const String & mapped_to_database, const String & mysql_database);
    };

    struct InterpreterAlterImpl
    {
        using TQuery = MySQLParser::ASTAlterQuery;

        static void validate(const TQuery & query, ContextPtr context);

        static ASTs getRewrittenQueries(
            const TQuery & alter_query, ContextPtr context, const String & mapped_to_database, const String & mysql_database);
    };

    struct InterpreterRenameImpl
    {
        using TQuery = ASTRenameQuery;

        static void validate(const TQuery & query, ContextPtr context);

        static ASTs getRewrittenQueries(
            const TQuery & rename_query, ContextPtr context, const String & mapped_to_database, const String & mysql_database);
    };

    struct InterpreterCreateImpl
    {
        using TQuery = MySQLParser::ASTCreateQuery;

        static void validate(const TQuery & query, ContextPtr context);

        static ASTs getRewrittenQueries(
            const TQuery & create_query, ContextPtr context, const String & mapped_to_database, const String & mysql_database);
    };

template <typename InterpreterImpl>
class InterpreterMySQLDDLQuery : public IInterpreter, WithMutableContext
{
public:
    InterpreterMySQLDDLQuery(
        const ASTPtr & query_ptr_, ContextMutablePtr context_, const String & mapped_to_database_, const String & mysql_database_)
        : WithMutableContext(context_), query_ptr(query_ptr_), mapped_to_database(mapped_to_database_), mysql_database(mysql_database_)
    {
    }

    BlockIO execute() override
    {
        const typename InterpreterImpl::TQuery & query = query_ptr->as<typename InterpreterImpl::TQuery &>();

        InterpreterImpl::validate(query, getContext());
        ASTs rewritten_queries = InterpreterImpl::getRewrittenQueries(query, getContext(), mapped_to_database, mysql_database);

        for (const auto & rewritten_query : rewritten_queries)
            executeQuery("/* Rewritten MySQL DDL Query */ " + queryToString(rewritten_query), getContext(), true);

        return BlockIO{};
    }

private:
    ASTPtr query_ptr;
    const String mapped_to_database;
    const String mysql_database;
};

using InterpreterMySQLDropQuery = InterpreterMySQLDDLQuery<InterpreterDropImpl>;
using InterpreterMySQLAlterQuery = InterpreterMySQLDDLQuery<InterpreterAlterImpl>;
using InterpreterMySQLRenameQuery = InterpreterMySQLDDLQuery<InterpreterRenameImpl>;
using InterpreterMySQLCreateQuery = InterpreterMySQLDDLQuery<InterpreterCreateImpl>;

NamesAndTypesList getColumnsList(const ASTExpressionList * columns_definition);

}

}