aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Databases/DDLRenamingVisitor.h
blob: 44146a8ee6be23ca3008ad9b9b8889c82268bb3a (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
#pragma once

#include <Core/Types.h>
#include <Core/QualifiedTableName.h>
#include <Interpreters/InDepthNodeVisitor.h>
#include <memory>
#include <unordered_map>


namespace DB
{
class IAST;
using ASTPtr = std::shared_ptr<IAST>;
class Context;
using ContextPtr = std::shared_ptr<const Context>;
class DDLRenamingMap;

/// Changes names of databases or tables in a create query according to a specified renaming map.
/// Does not validate AST, works a best-effort way.
void renameDatabaseAndTableNameInCreateQuery(ASTPtr ast, const DDLRenamingMap & renaming_map, const ContextPtr & global_context);

/// Renaming map keeps information about new names of databases or tables.
class DDLRenamingMap
{
public:
    void setNewTableName(const QualifiedTableName & old_table_name, const QualifiedTableName & new_table_name);
    void setNewDatabaseName(const String & old_database_name, const String & new_database_name);

    QualifiedTableName getNewTableName(const QualifiedTableName & old_table_name) const;
    const String & getNewDatabaseName(const String & old_database_name) const;

private:
    std::unordered_map<QualifiedTableName, QualifiedTableName> old_to_new_table_names;
    std::unordered_map<String, String> old_to_new_database_names;
};

/// Visits ASTCreateQuery and changes names of databases or tables.
class DDLRenamingVisitor
{
public:
    struct Data
    {
        ASTPtr create_query;
        const DDLRenamingMap & renaming_map;
        ContextPtr global_context;
    };

    using Visitor = InDepthNodeVisitor<DDLRenamingVisitor, false>;

    static bool needChildVisit(const ASTPtr &, const ASTPtr &);
    static void visit(ASTPtr ast, const Data & data);
};

}