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.CheckpointContext;
24  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColIdContext;
25  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ConfigurationParameterClauseContext;
26  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.EmptyStatementContext;
27  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ExplainContext;
28  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ExplainableStmtContext;
29  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.LoadContext;
30  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ResetParameterContext;
31  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.SetContext;
32  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ShowContext;
33  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.VacuumContext;
34  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.VacuumRelationContext;
35  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.VacuumRelationListContext;
36  import org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.OpenGaussStatementVisitor;
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   * DAL statement visitor for openGauss.
60   */
61  public final class OpenGaussDALStatementVisitor extends OpenGaussStatementVisitor 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          return new SetStatement(variableAssigns);
91      }
92      
93      @Override
94      public ASTNode visitConfigurationParameterClause(final ConfigurationParameterClauseContext ctx) {
95          return new VariableAssignSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(),
96                  new VariableSegment(ctx.varName().start.getStartIndex(), ctx.varName().stop.getStopIndex(), ctx.varName().getText()), getAssignValue(ctx));
97      }
98      
99      private String getAssignValue(final ConfigurationParameterClauseContext ctx) {
100         if (null != ctx.varList()) {
101             return ctx.varList().getText();
102         }
103         if (null != ctx.DEFAULT()) {
104             return ctx.DEFAULT().getText();
105         }
106         return null;
107     }
108     
109     @Override
110     public ASTNode visitResetParameter(final ResetParameterContext ctx) {
111         return new PostgreSQLResetParameterStatement(null != ctx.ALL() ? "ALL" : ctx.identifier().getText());
112     }
113     
114     @SuppressWarnings("unchecked")
115     @Override
116     public ASTNode visitAnalyzeTable(final AnalyzeTableContext ctx) {
117         return new AnalyzeTableStatement(null == ctx.vacuumRelationList() ? Collections.emptyList() : ((CollectionValue<SimpleTableSegment>) visit(ctx.vacuumRelationList())).getValue());
118     }
119     
120     @Override
121     public ASTNode visitVacuumRelationList(final VacuumRelationListContext ctx) {
122         CollectionValue<SimpleTableSegment> result = new CollectionValue<>();
123         for (VacuumRelationContext each : ctx.vacuumRelation()) {
124             ColIdContext colId = each.qualifiedName().colId();
125             TableNameSegment tableName = new TableNameSegment(colId.start.getStartIndex(), colId.stop.getStopIndex(), new IdentifierValue(colId.getText()));
126             result.getValue().add(new SimpleTableSegment(tableName));
127         }
128         return result;
129     }
130     
131     @Override
132     public ASTNode visitLoad(final LoadContext ctx) {
133         return new PostgreSQLLoadStatement();
134     }
135     
136     @Override
137     public ASTNode visitVacuum(final VacuumContext ctx) {
138         return new PostgreSQLVacuumStatement();
139     }
140     
141     @Override
142     public ASTNode visitExplain(final ExplainContext ctx) {
143         return new ExplainStatement((SQLStatement) visit(ctx.explainableStmt()));
144     }
145     
146     @Override
147     public ASTNode visitExplainableStmt(final ExplainableStmtContext ctx) {
148         if (null != ctx.select()) {
149             return visit(ctx.select());
150         }
151         if (null != ctx.insert()) {
152             return visit(ctx.insert());
153         }
154         if (null != ctx.update()) {
155             return visit(ctx.update());
156         }
157         if (null != ctx.delete()) {
158             return visit(ctx.delete());
159         }
160         if (null != ctx.declare()) {
161             // TODO visit declare statement
162             return visit(ctx.declare());
163         }
164         if (null != ctx.executeStmt()) {
165             return visit(ctx.executeStmt());
166         }
167         if (null != ctx.createMaterializedView()) {
168             // TODO visit create materialized view statement
169             return visit(ctx.createMaterializedView());
170         }
171         // TODO visit refresh materialized view statement
172         return visit(ctx.refreshMatViewStmt());
173     }
174     
175     @Override
176     public ASTNode visitCheckpoint(final CheckpointContext ctx) {
177         return new PostgreSQLCheckpointStatement();
178     }
179     
180     @Override
181     public ASTNode visitEmptyStatement(final EmptyStatementContext ctx) {
182         return new EmptyStatement();
183     }
184 }