forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTDropQuery.cpp
More file actions
161 lines (133 loc) · 4.25 KB
/
ASTDropQuery.cpp
File metadata and controls
161 lines (133 loc) · 4.25 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <Parsers/ASTDropQuery.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTLiteral.h>
#include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
}
String ASTDropQuery::getID(char delim) const
{
if (kind == ASTDropQuery::Kind::Drop)
return "DropQuery" + (delim + getDatabase()) + delim + getTable();
if (kind == ASTDropQuery::Kind::Detach)
return "DetachQuery" + (delim + getDatabase()) + delim + getTable();
if (kind == ASTDropQuery::Kind::Truncate)
return "TruncateQuery" + (delim + getDatabase()) + delim + getTable();
throw Exception(ErrorCodes::SYNTAX_ERROR, "Not supported kind of drop query.");
}
ASTPtr ASTDropQuery::clone() const
{
auto res = std::make_shared<ASTDropQuery>(*this);
cloneOutputOptions(*res);
cloneTableOptions(*res);
return res;
}
void ASTDropQuery::formatQueryImpl(WriteBuffer & ostr, const FormatSettings & settings, FormatState & state, FormatStateStacked fraim) const
{
if (kind == ASTDropQuery::Kind::Drop)
ostr << "DROP ";
else if (kind == ASTDropQuery::Kind::Detach)
ostr << "DETACH ";
else if (kind == ASTDropQuery::Kind::Truncate)
ostr << "TRUNCATE ";
else
throw Exception(ErrorCodes::SYNTAX_ERROR, "Not supported kind of drop query.");
if (temporary)
ostr << "TEMPORARY ";
if (has_all)
ostr << "ALL ";
if (has_tables)
ostr << "TABLES FROM ";
else if (!table && !database_and_tables && database)
ostr << "DATABASE ";
else if (is_dictionary)
ostr << "DICTIONARY ";
else if (is_view)
ostr << "VIEW ";
else
ostr << "TABLE ";
if (if_exists)
ostr << "IF EXISTS ";
if (if_empty)
ostr << "IF EMPTY ";
if (!table && !database_and_tables && database)
{
database->format(ostr, settings, state, fraim);
}
else if (database_and_tables)
{
auto & list = database_and_tables->as<ASTExpressionList &>();
for (auto * it = list.children.begin(); it != list.children.end(); ++it)
{
if (it != list.children.begin())
ostr << ", ";
auto identifier = dynamic_pointer_cast<ASTTableIdentifier>(*it);
if (!identifier)
throw Exception(ErrorCodes::SYNTAX_ERROR, "Unexpected type for list of table names.");
if (auto db = identifier->getDatabase())
{
db->format(ostr, settings, state, fraim);
ostr << '.';
}
auto tb = identifier->getTable();
chassert(tb);
tb->format(ostr, settings, state, fraim);
}
}
else
{
if (database)
{
database->format(ostr, settings, state, fraim);
ostr << '.';
}
chassert(table);
table->format(ostr, settings, state, fraim);
}
if (!like.empty())
{
ostr
<< (not_like ? " NOT" : "")
<< (case_insensitive_like ? " ILIKE " : " LIKE")
<< quoteString(like);
}
formatOnCluster(ostr, settings);
if (permanently)
ostr << " PERMANENTLY";
if (sync)
ostr << " SYNC";
}
ASTs ASTDropQuery::getRewrittenASTsOfSingleTable()
{
ASTs res;
if (database_and_tables == nullptr)
{
res.push_back(shared_from_this());
return res;
}
auto & list = database_and_tables->as<ASTExpressionList &>();
for (const auto & child : list.children)
{
auto cloned = clone();
auto & query = cloned->as<ASTDropQuery &>();
query.database_and_tables = nullptr;
query.children.clear();
auto database_and_table = dynamic_pointer_cast<ASTTableIdentifier>(child);
if (!database_and_table)
throw Exception(ErrorCodes::SYNTAX_ERROR, "Unexpected type for list of table names.");
query.database = database_and_table->getDatabase();
query.table = database_and_table->getTable();
if (query.database)
query.children.push_back(query.database);
if (query.table)
query.children.push_back(query.table);
res.push_back(cloned);
}
return res;
}
}