1 | #pragma once |
---|---|
2 | |
3 | #include <Parsers/IAST.h> |
4 | #include <Parsers/ASTQueryWithOutput.h> |
5 | #include <Parsers/ASTQueryWithOnCluster.h> |
6 | #include <Common/quoteString.h> |
7 | |
8 | |
9 | namespace DB |
10 | { |
11 | |
12 | /** RENAME query |
13 | */ |
14 | class ASTRenameQuery : public ASTQueryWithOutput, public ASTQueryWithOnCluster |
15 | { |
16 | public: |
17 | struct Table |
18 | { |
19 | String database; |
20 | String table; |
21 | }; |
22 | |
23 | struct Element |
24 | { |
25 | Table from; |
26 | Table to; |
27 | }; |
28 | |
29 | using Elements = std::vector<Element>; |
30 | Elements elements; |
31 | |
32 | /** Get the text that identifies this element. */ |
33 | String getID(char) const override { return "Rename"; } |
34 | |
35 | ASTPtr clone() const override |
36 | { |
37 | auto res = std::make_shared<ASTRenameQuery>(*this); |
38 | cloneOutputOptions(*res); |
39 | return res; |
40 | } |
41 | |
42 | ASTPtr getRewrittenASTWithoutOnCluster(const std::string & new_database) const override |
43 | { |
44 | auto query_ptr = clone(); |
45 | auto & query = query_ptr->as<ASTRenameQuery &>(); |
46 | |
47 | query.cluster.clear(); |
48 | for (Element & elem : query.elements) |
49 | { |
50 | if (elem.from.database.empty()) |
51 | elem.from.database = new_database; |
52 | if (elem.to.database.empty()) |
53 | elem.to.database = new_database; |
54 | } |
55 | |
56 | return query_ptr; |
57 | } |
58 | |
59 | protected: |
60 | void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override |
61 | { |
62 | settings.ostr << (settings.hilite ? hilite_keyword : "") << "RENAME TABLE "<< (settings.hilite ? hilite_none : ""); |
63 | |
64 | for (auto it = elements.cbegin(); it != elements.cend(); ++it) |
65 | { |
66 | if (it != elements.cbegin()) |
67 | settings.ostr << ", "; |
68 | |
69 | settings.ostr << (!it->from.database.empty() ? backQuoteIfNeed(it->from.database) + ".": "") << backQuoteIfNeed(it->from.table) |
70 | << (settings.hilite ? hilite_keyword : "") << " TO "<< (settings.hilite ? hilite_none : "") |
71 | << (!it->to.database.empty() ? backQuoteIfNeed(it->to.database) + ".": "") << backQuoteIfNeed(it->to.table); |
72 | } |
73 | |
74 | formatOnCluster(settings); |
75 | } |
76 | }; |
77 | |
78 | } |
79 |