1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }