forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplainStatement.java
More file actions
183 lines (150 loc) · 4.51 KB
/
ExplainStatement.java
File metadata and controls
183 lines (150 loc) · 4.51 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
import net.sf.jsqlparser.schema.Table;
/**
* An {@code EXPLAIN} statement
*/
public class ExplainStatement implements Statement {
private String keyword;
private Statement statement;
private LinkedHashMap<OptionType, Option> options;
private Table table;
public ExplainStatement(String keyword) {
this.keyword = keyword;
}
public ExplainStatement() {
this("EXPLAIN");
}
public ExplainStatement(String keyword, Table table) {
this.keyword = keyword;
this.table = table;
}
public ExplainStatement(String keyword, Statement statement, List<Option> optionList) {
this.keyword = keyword;
setStatement(statement);
initializeOptions(optionList);
}
public ExplainStatement(Statement statement) {
this("EXPLAIN", statement, null);
}
public Table getTable() {
return table;
}
public ExplainStatement setTable(Table table) {
this.table = table;
if (table != null) {
this.statement = null;
}
return this;
}
public Statement getStatement() {
return statement;
}
public ExplainStatement setStatement(Statement statement) {
this.table = null;
this.statement = statement;
return this;
}
public LinkedHashMap<OptionType, Option> getOptions() {
return options == null ? null : new LinkedHashMap<>(options);
}
public void addOption(Option option) {
if (options == null) {
options = new LinkedHashMap<>();
}
options.put(option.getType(), option);
}
/**
* Returns the first option that matches this optionType
*
* @param optionType the option type to retrieve an Option for
* @return an option of that type, or null. In case of duplicate options, the first found option
* will be returned.
*/
public Option getOption(OptionType optionType) {
if (options == null) {
return null;
}
return options.get(optionType);
}
public String getKeyword() {
return keyword;
}
public ExplainStatement setKeyword(String keyword) {
this.keyword = keyword;
return this;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(keyword);
if (table != null) {
builder.append(" ").append(table);
} else {
if (options != null) {
builder.append(" ");
builder.append(options.values().stream().map(Option::formatOption)
.collect(Collectors.joining(" ")));
}
builder.append(" ");
if (statement != null) {
builder.append(statement);
}
}
return builder.toString();
}
@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
private void initializeOptions(List<Option> optionList) {
if (optionList != null && !optionList.isEmpty()) {
options = new LinkedHashMap<>();
for (Option o : optionList) {
options.put(o.getType(), o);
}
}
}
public enum OptionType {
ANALYZE, VERBOSE, COSTS, BUFFERS, FORMAT, PLAN, PLAN_FOR;
public static OptionType from(String type) {
return Enum.valueOf(OptionType.class, type.toUpperCase());
}
}
public static class Option implements Serializable {
private final OptionType type;
private String value;
public Option(OptionType type) {
this.type = type;
}
public OptionType getType() {
return type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String formatOption() {
return type.name().replace("_", " ") + (value != null
? " " + value
: "");
}
public Option withValue(String value) {
this.setValue(value);
return this;
}
}
}