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.infra.binder.context.statement.ddl;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.infra.binder.context.aware.CursorDefinition;
22  import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
23  import org.apache.shardingsphere.infra.binder.context.statement.CommonSQLStatementContext;
24  import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
25  import org.apache.shardingsphere.infra.binder.context.type.CursorAvailable;
26  import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
27  import org.apache.shardingsphere.infra.binder.context.type.WhereAvailable;
28  import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
29  import org.apache.shardingsphere.sql.parser.sql.common.extractor.TableExtractor;
30  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.cursor.CursorNameSegment;
31  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
32  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
33  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
34  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
35  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.ddl.OpenGaussCursorStatement;
36  
37  import java.util.Collection;
38  import java.util.LinkedList;
39  import java.util.List;
40  import java.util.Optional;
41  
42  /**
43   * Cursor statement context.
44   */
45  @Getter
46  public final class CursorStatementContext extends CommonSQLStatementContext implements CursorAvailable, TableAvailable, WhereAvailable, CursorDefinition {
47      
48      private final Collection<WhereSegment> whereSegments = new LinkedList<>();
49      
50      private final Collection<ColumnSegment> columnSegments = new LinkedList<>();
51      
52      private final Collection<BinaryOperationExpression> joinConditions = new LinkedList<>();
53      
54      private final TablesContext tablesContext;
55      
56      private final SelectStatementContext selectStatementContext;
57      
58      public CursorStatementContext(final ShardingSphereMetaData metaData, final List<Object> params,
59                                    final OpenGaussCursorStatement sqlStatement, final String defaultDatabaseName) {
60          super(sqlStatement);
61          tablesContext = new TablesContext(getSimpleTableSegments(), getDatabaseType());
62          selectStatementContext = new SelectStatementContext(metaData, params, sqlStatement.getSelect(), defaultDatabaseName);
63          whereSegments.addAll(selectStatementContext.getWhereSegments());
64          columnSegments.addAll(selectStatementContext.getColumnSegments());
65          joinConditions.addAll(selectStatementContext.getJoinConditions());
66      }
67      
68      private Collection<SimpleTableSegment> getSimpleTableSegments() {
69          TableExtractor tableExtractor = new TableExtractor();
70          tableExtractor.extractTablesFromSelect(getSqlStatement().getSelect());
71          return tableExtractor.getRewriteTables();
72      }
73      
74      @Override
75      public OpenGaussCursorStatement getSqlStatement() {
76          return (OpenGaussCursorStatement) super.getSqlStatement();
77      }
78      
79      @Override
80      public Collection<SimpleTableSegment> getAllTables() {
81          return tablesContext.getSimpleTableSegments();
82      }
83      
84      @Override
85      public Optional<CursorNameSegment> getCursorName() {
86          return Optional.of(getSqlStatement().getCursorName());
87      }
88      
89      @Override
90      public Collection<WhereSegment> getWhereSegments() {
91          return whereSegments;
92      }
93      
94      @Override
95      public Collection<ColumnSegment> getColumnSegments() {
96          return columnSegments;
97      }
98      
99      @Override
100     public Collection<BinaryOperationExpression> getJoinConditions() {
101         return joinConditions;
102     }
103 }