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.opengauss.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.OpenGaussStatementParser.AnalyzeTableContext;
23  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColIdContext;
24  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ConfigurationParameterClauseContext;
25  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.EmptyStatementContext;
26  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ExplainContext;
27  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ExplainableStmtContext;
28  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.LoadContext;
29  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ResetParameterContext;
30  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SetContext;
31  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ShowContext;
32  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.VacuumContext;
33  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.VacuumRelationContext;
34  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.VacuumRelationListContext;
35  import org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.OpenGaussStatementVisitor;
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.opengauss.dal.OpenGaussAnalyzeTableStatement;
44  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dal.OpenGaussEmptyStatement;
45  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dal.OpenGaussExplainStatement;
46  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dal.OpenGaussLoadStatement;
47  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dal.OpenGaussResetParameterStatement;
48  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dal.OpenGaussSetStatement;
49  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dal.OpenGaussShowStatement;
50  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dal.OpenGaussVacuumStatement;
51  
52  import java.util.Collection;
53  import java.util.LinkedList;
54  
55  /**
56   * DAL statement visitor for openGauss.
57   */
58  public final class OpenGaussDALStatementVisitor extends OpenGaussStatementVisitor implements DALStatementVisitor {
59      
60      @Override
61      public ASTNode visitShow(final ShowContext ctx) {
62          if (null != ctx.varName()) {
63              return new OpenGaussShowStatement(ctx.varName().getText());
64          }
65          if (null != ctx.ZONE()) {
66              return new OpenGaussShowStatement("timezone");
67          }
68          if (null != ctx.ISOLATION()) {
69              return new OpenGaussShowStatement("transaction_isolation");
70          }
71          if (null != ctx.AUTHORIZATION()) {
72              return new OpenGaussShowStatement("session_authorization");
73          }
74          return new OpenGaussShowStatement("ALL");
75      }
76      
77      @Override
78      public ASTNode visitSet(final SetContext ctx) {
79          OpenGaussSetStatement result = new OpenGaussSetStatement();
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              result.getVariableAssigns().addAll(variableAssigns);
88          }
89          return result;
90      }
91      
92      @Override
93      public ASTNode visitConfigurationParameterClause(final ConfigurationParameterClauseContext ctx) {
94          VariableAssignSegment result = new VariableAssignSegment();
95          result.setStartIndex(ctx.start.getStartIndex());
96          result.setStopIndex(ctx.stop.getStopIndex());
97          result.setVariable(new VariableSegment(ctx.varName().start.getStartIndex(), ctx.varName().stop.getStopIndex(), ctx.varName().getText()));
98          if (null != ctx.varList()) {
99              result.setAssignValue(ctx.varList().getText());
100         }
101         if (null != ctx.DEFAULT()) {
102             result.setAssignValue(ctx.DEFAULT().getText());
103         }
104         return result;
105     }
106     
107     @Override
108     public ASTNode visitResetParameter(final ResetParameterContext ctx) {
109         return new OpenGaussResetParameterStatement(null != ctx.ALL() ? "ALL" : ctx.identifier().getText());
110     }
111     
112     @SuppressWarnings("unchecked")
113     @Override
114     public ASTNode visitAnalyzeTable(final AnalyzeTableContext ctx) {
115         OpenGaussAnalyzeTableStatement result = new OpenGaussAnalyzeTableStatement();
116         if (null != ctx.vacuumRelationList()) {
117             result.getTables().addAll(((CollectionValue<SimpleTableSegment>) visit(ctx.vacuumRelationList())).getValue());
118         }
119         return result;
120     }
121     
122     @Override
123     public ASTNode visitVacuumRelationList(final VacuumRelationListContext ctx) {
124         CollectionValue<SimpleTableSegment> result = new CollectionValue<>();
125         for (VacuumRelationContext each : ctx.vacuumRelation()) {
126             ColIdContext colId = each.qualifiedName().colId();
127             TableNameSegment tableName = new TableNameSegment(colId.start.getStartIndex(), colId.stop.getStopIndex(), new IdentifierValue(colId.getText()));
128             result.getValue().add(new SimpleTableSegment(tableName));
129         }
130         return result;
131     }
132     
133     @Override
134     public ASTNode visitLoad(final LoadContext ctx) {
135         return new OpenGaussLoadStatement();
136     }
137     
138     @Override
139     public ASTNode visitVacuum(final VacuumContext ctx) {
140         return new OpenGaussVacuumStatement();
141     }
142     
143     @Override
144     public ASTNode visitExplain(final ExplainContext ctx) {
145         OpenGaussExplainStatement result = new OpenGaussExplainStatement();
146         result.setStatement((SQLStatement) visit(ctx.explainableStmt()));
147         return result;
148     }
149     
150     @Override
151     public ASTNode visitExplainableStmt(final ExplainableStmtContext ctx) {
152         if (null != ctx.select()) {
153             return visit(ctx.select());
154         }
155         if (null != ctx.insert()) {
156             return visit(ctx.insert());
157         }
158         if (null != ctx.update()) {
159             return visit(ctx.update());
160         }
161         if (null != ctx.delete()) {
162             return visit(ctx.delete());
163         }
164         if (null != ctx.declare()) {
165             // TODO visit declare statement
166             return visit(ctx.declare());
167         }
168         if (null != ctx.executeStmt()) {
169             return visit(ctx.executeStmt());
170         }
171         if (null != ctx.createMaterializedView()) {
172             // TODO visit create materialized view statement
173             return visit(ctx.createMaterializedView());
174         }
175         // TODO visit refresh materialized view statement
176         return visit(ctx.refreshMatViewStmt());
177     }
178     
179     @Override
180     public ASTNode visitEmptyStatement(final EmptyStatementContext ctx) {
181         return new OpenGaussEmptyStatement();
182     }
183 }