View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.ColIdContext;
24  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ConfigurationParameterClauseContext;
25  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.EmptyStatementContext;
26  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ExplainContext;
27  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ExplainableStmtContext;
28  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.LoadContext;
29  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ResetParameterContext;
30  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.SetContext;
31  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ShowContext;
32  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.VacuumContext;
33  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.VacuumRelationContext;
34  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.VacuumRelationListContext;
35  import org.apache.shardingsphere.sql.parser.postgresql.visitor.statement.PostgreSQLStatementVisitor;
36  import org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableAssignSegment;
37  import org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableSegment;
38  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
39  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
40  import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
41  import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
42  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
43  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLAnalyzeTableStatement;
44  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLEmptyStatement;
45  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLExplainStatement;
46  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLLoadStatement;
47  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLResetParameterStatement;
48  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLSetStatement;
49  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLShowStatement;
50  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLVacuumStatement;
51  
52  import java.util.Collection;
53  import java.util.LinkedList;
54  
55  /**
56   * DAL statement visitor for PostgreSQL.
57   */
58  public final class PostgreSQLDALStatementVisitor extends PostgreSQLStatementVisitor implements DALStatementVisitor {
59      
60      @Override
61      public ASTNode visitShow(final ShowContext ctx) {
62          if (null != ctx.varName()) {
63              return new PostgreSQLShowStatement(ctx.varName().getText());
64          }
65          if (null != ctx.ZONE()) {
66              return new PostgreSQLShowStatement("timezone");
67          }
68          if (null != ctx.ISOLATION()) {
69              return new PostgreSQLShowStatement("transaction_isolation");
70          }
71          if (null != ctx.AUTHORIZATION()) {
72              return new PostgreSQLShowStatement("session_authorization");
73          }
74          return new PostgreSQLShowStatement("ALL");
75      }
76      
77      @Override
78      public ASTNode visitSet(final SetContext ctx) {
79          PostgreSQLSetStatement result = new PostgreSQLSetStatement();
80          Collection<VariableAssignSegment> variableAssigns = new LinkedList<>();
81          if (null != ctx.configurationParameterClause()) {
82              VariableAssignSegment variableAssignSegment = (VariableAssignSegment) visit(ctx.configurationParameterClause());
83              if (null != ctx.runtimeScope()) {
84                  variableAssignSegment.getVariable().setScope(ctx.runtimeScope().getText());
85              }
86              variableAssigns.add(variableAssignSegment);
87          }
88          if (null != ctx.encoding()) {
89              VariableAssignSegment variableAssignSegment = new VariableAssignSegment();
90              variableAssignSegment.setVariable(new VariableSegment(ctx.NAMES().getSymbol().getStartIndex(), ctx.NAMES().getSymbol().getStopIndex(), "client_encoding"));
91              String value = ctx.encoding().getText();
92              variableAssignSegment.setAssignValue(value);
93              variableAssigns.add(variableAssignSegment);
94          }
95          result.getVariableAssigns().addAll(variableAssigns);
96          return result;
97      }
98      
99      @Override
100     public ASTNode visitConfigurationParameterClause(final ConfigurationParameterClauseContext ctx) {
101         VariableAssignSegment result = new VariableAssignSegment();
102         result.setStartIndex(ctx.start.getStartIndex());
103         result.setStopIndex(ctx.stop.getStopIndex());
104         result.setVariable(new VariableSegment(ctx.varName().start.getStartIndex(), ctx.varName().stop.getStopIndex(), ctx.varName().getText()));
105         if (null != ctx.varList()) {
106             result.setAssignValue(ctx.varList().getText());
107         }
108         if (null != ctx.DEFAULT()) {
109             result.setAssignValue(ctx.DEFAULT().getText());
110         }
111         return result;
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         PostgreSQLAnalyzeTableStatement result = new PostgreSQLAnalyzeTableStatement();
123         if (null != ctx.vacuumRelationList()) {
124             result.getTables().addAll(((CollectionValue<SimpleTableSegment>) visit(ctx.vacuumRelationList())).getValue());
125         }
126         return result;
127     }
128     
129     @Override
130     public ASTNode visitVacuumRelationList(final VacuumRelationListContext ctx) {
131         CollectionValue<SimpleTableSegment> result = new CollectionValue<>();
132         for (VacuumRelationContext each : ctx.vacuumRelation()) {
133             ColIdContext colId = each.qualifiedName().colId();
134             TableNameSegment tableName = new TableNameSegment(colId.start.getStartIndex(), colId.stop.getStopIndex(), new IdentifierValue(colId.getText()));
135             result.getValue().add(new SimpleTableSegment(tableName));
136         }
137         return result;
138     }
139     
140     @Override
141     public ASTNode visitLoad(final LoadContext ctx) {
142         return new PostgreSQLLoadStatement();
143     }
144     
145     @Override
146     public ASTNode visitVacuum(final VacuumContext ctx) {
147         return new PostgreSQLVacuumStatement();
148     }
149     
150     @Override
151     public ASTNode visitExplain(final ExplainContext ctx) {
152         PostgreSQLExplainStatement result = new PostgreSQLExplainStatement();
153         result.setStatement((SQLStatement) visit(ctx.explainableStmt()));
154         return result;
155     }
156     
157     @Override
158     public ASTNode visitExplainableStmt(final ExplainableStmtContext ctx) {
159         if (null != ctx.select()) {
160             return visit(ctx.select());
161         }
162         if (null != ctx.insert()) {
163             return visit(ctx.insert());
164         }
165         if (null != ctx.update()) {
166             return visit(ctx.update());
167         }
168         if (null != ctx.delete()) {
169             return visit(ctx.delete());
170         }
171         if (null != ctx.declare()) {
172             // TODO visit declare statement
173             return visit(ctx.declare());
174         }
175         if (null != ctx.executeStmt()) {
176             return visit(ctx.executeStmt());
177         }
178         if (null != ctx.createMaterializedView()) {
179             return visit(ctx.createMaterializedView());
180         }
181         // TODO visit refresh materialized view statement
182         return visit(ctx.refreshMatViewStmt());
183     }
184     
185     @Override
186     public ASTNode visitEmptyStatement(final EmptyStatementContext ctx) {
187         return new PostgreSQLEmptyStatement();
188     }
189 }