1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.sql.parser.postgresql.visitor.statement.type;
19
20 import org.apache.shardingsphere.sql.parser.api.ASTNode;
21 import org.apache.shardingsphere.sql.parser.api.visitor.statement.type.DALStatementVisitor;
22 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.AnalyzeTableContext;
23 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.CheckpointContext;
24 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ColIdContext;
25 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ConfigurationParameterClauseContext;
26 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.EmptyStatementContext;
27 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ExplainContext;
28 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ExplainableStmtContext;
29 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.LoadContext;
30 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ResetParameterContext;
31 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.SetContext;
32 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ShowContext;
33 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.VacuumContext;
34 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.VacuumRelationContext;
35 import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.VacuumRelationListContext;
36 import org.apache.shardingsphere.sql.parser.postgresql.visitor.statement.PostgreSQLStatementVisitor;
37 import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.VariableAssignSegment;
38 import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.VariableSegment;
39 import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
40 import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
41 import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
42 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.AnalyzeTableStatement;
43 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.EmptyStatement;
44 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ExplainStatement;
45 import org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLLoadStatement;
46 import org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLResetParameterStatement;
47 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.SetStatement;
48 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ShowStatement;
49 import org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLVacuumStatement;
50 import org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLCheckpointStatement;
51 import org.apache.shardingsphere.sql.parser.statement.core.value.collection.CollectionValue;
52 import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
53
54 import java.util.Collections;
55 import java.util.LinkedList;
56 import java.util.List;
57
58
59
60
61 public final class PostgreSQLDALStatementVisitor extends PostgreSQLStatementVisitor implements DALStatementVisitor {
62
63 @Override
64 public ASTNode visitShow(final ShowContext ctx) {
65 if (null != ctx.varName()) {
66 return new ShowStatement(ctx.varName().getText());
67 }
68 if (null != ctx.ZONE()) {
69 return new ShowStatement("timezone");
70 }
71 if (null != ctx.ISOLATION()) {
72 return new ShowStatement("transaction_isolation");
73 }
74 if (null != ctx.AUTHORIZATION()) {
75 return new ShowStatement("session_authorization");
76 }
77 return new ShowStatement("ALL");
78 }
79
80 @Override
81 public ASTNode visitSet(final SetContext ctx) {
82 List<VariableAssignSegment> variableAssigns = new LinkedList<>();
83 if (null != ctx.configurationParameterClause()) {
84 VariableAssignSegment variableAssignSegment = (VariableAssignSegment) visit(ctx.configurationParameterClause());
85 if (null != ctx.runtimeScope()) {
86 variableAssignSegment.getVariable().setScope(ctx.runtimeScope().getText());
87 }
88 variableAssigns.add(variableAssignSegment);
89 }
90 if (null != ctx.encoding()) {
91 VariableSegment variable = new VariableSegment(ctx.NAMES().getSymbol().getStartIndex(), ctx.NAMES().getSymbol().getStopIndex(), "client_encoding");
92 VariableAssignSegment variableAssign = new VariableAssignSegment(ctx.encoding().start.getStartIndex(), ctx.encoding().stop.getStopIndex(), variable, ctx.encoding().getText());
93 variableAssigns.add(variableAssign);
94 }
95 return new SetStatement(variableAssigns);
96 }
97
98 @Override
99 public ASTNode visitConfigurationParameterClause(final ConfigurationParameterClauseContext ctx) {
100 return new VariableAssignSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(),
101 new VariableSegment(ctx.varName().start.getStartIndex(), ctx.varName().stop.getStopIndex(), ctx.varName().getText()), getAssignValue(ctx));
102 }
103
104 private String getAssignValue(final ConfigurationParameterClauseContext ctx) {
105 if (null != ctx.varList()) {
106 return ctx.varList().getText();
107 }
108 if (null != ctx.DEFAULT()) {
109 return ctx.DEFAULT().getText();
110 }
111 return null;
112 }
113
114 @Override
115 public ASTNode visitResetParameter(final ResetParameterContext ctx) {
116 return new PostgreSQLResetParameterStatement(null != ctx.ALL() ? "ALL" : ctx.identifier().getText());
117 }
118
119 @SuppressWarnings("unchecked")
120 @Override
121 public ASTNode visitAnalyzeTable(final AnalyzeTableContext ctx) {
122 return new AnalyzeTableStatement(null == ctx.vacuumRelationList() ? Collections.emptyList() : ((CollectionValue<SimpleTableSegment>) visit(ctx.vacuumRelationList())).getValue());
123 }
124
125 @Override
126 public ASTNode visitVacuumRelationList(final VacuumRelationListContext ctx) {
127 CollectionValue<SimpleTableSegment> result = new CollectionValue<>();
128 for (VacuumRelationContext each : ctx.vacuumRelation()) {
129 ColIdContext colId = each.qualifiedName().colId();
130 TableNameSegment tableName = new TableNameSegment(colId.start.getStartIndex(), colId.stop.getStopIndex(), new IdentifierValue(colId.getText()));
131 result.getValue().add(new SimpleTableSegment(tableName));
132 }
133 return result;
134 }
135
136 @Override
137 public ASTNode visitLoad(final LoadContext ctx) {
138 return new PostgreSQLLoadStatement();
139 }
140
141 @Override
142 public ASTNode visitVacuum(final VacuumContext ctx) {
143 return new PostgreSQLVacuumStatement();
144 }
145
146 @Override
147 public ASTNode visitExplain(final ExplainContext ctx) {
148 return new ExplainStatement((SQLStatement) visit(ctx.explainableStmt()));
149 }
150
151 @Override
152 public ASTNode visitExplainableStmt(final ExplainableStmtContext ctx) {
153 if (null != ctx.select()) {
154 return visit(ctx.select());
155 }
156 if (null != ctx.insert()) {
157 return visit(ctx.insert());
158 }
159 if (null != ctx.update()) {
160 return visit(ctx.update());
161 }
162 if (null != ctx.delete()) {
163 return visit(ctx.delete());
164 }
165 if (null != ctx.declare()) {
166
167 return visit(ctx.declare());
168 }
169 if (null != ctx.executeStmt()) {
170 return visit(ctx.executeStmt());
171 }
172 if (null != ctx.createMaterializedView()) {
173 return visit(ctx.createMaterializedView());
174 }
175
176 return visit(ctx.refreshMatViewStmt());
177 }
178
179 @Override
180 public ASTNode visitCheckpoint(final CheckpointContext ctx) {
181 return new PostgreSQLCheckpointStatement();
182 }
183
184 @Override
185 public ASTNode visitEmptyStatement(final EmptyStatementContext ctx) {
186 return new EmptyStatement();
187 }
188 }