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.dml;
19
20 import lombok.Getter;
21 import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
22 import org.apache.shardingsphere.infra.binder.context.statement.CommonSQLStatementContext;
23 import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
24 import org.apache.shardingsphere.infra.binder.context.type.WhereAvailable;
25 import org.apache.shardingsphere.sql.parser.sql.common.extractor.TableExtractor;
26 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
27 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
28 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
29 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
30 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
31 import org.apache.shardingsphere.sql.parser.sql.common.util.ColumnExtractor;
32 import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUtils;
33
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.LinkedList;
37 import java.util.Map;
38
39
40
41
42 @Getter
43 public final class DeleteStatementContext extends CommonSQLStatementContext implements TableAvailable, WhereAvailable {
44
45 private final TablesContext tablesContext;
46
47 private final Collection<WhereSegment> whereSegments = new LinkedList<>();
48
49 private final Collection<ColumnSegment> columnSegments = new LinkedList<>();
50
51 private final Collection<BinaryOperationExpression> joinConditions = new LinkedList<>();
52
53 public DeleteStatementContext(final DeleteStatement sqlStatement) {
54 super(sqlStatement);
55 tablesContext = new TablesContext(getAllSimpleTableSegments(), getDatabaseType());
56 getSqlStatement().getWhere().ifPresent(whereSegments::add);
57 ColumnExtractor.extractColumnSegments(columnSegments, whereSegments);
58 ExpressionExtractUtils.extractJoinConditions(joinConditions, whereSegments);
59 }
60
61 private Collection<SimpleTableSegment> getAllSimpleTableSegments() {
62 TableExtractor tableExtractor = new TableExtractor();
63 tableExtractor.extractTablesFromDelete(getSqlStatement());
64 return filterAliasDeleteTable(tableExtractor.getRewriteTables());
65 }
66
67 private Collection<SimpleTableSegment> filterAliasDeleteTable(final Collection<SimpleTableSegment> tableSegments) {
68 Collection<SimpleTableSegment> result = new LinkedList<>();
69 Map<String, SimpleTableSegment> aliasAndTableSegmentMap = getAliasAndTableSegmentMap(tableSegments);
70 for (SimpleTableSegment each : tableSegments) {
71 SimpleTableSegment aliasDeleteTable = aliasAndTableSegmentMap.get(each.getTableName().getIdentifier().getValue());
72 if (null == aliasDeleteTable || aliasDeleteTable.equals(each)) {
73 result.add(each);
74 }
75 }
76 return result;
77 }
78
79 private Map<String, SimpleTableSegment> getAliasAndTableSegmentMap(final Collection<SimpleTableSegment> tableSegments) {
80 Map<String, SimpleTableSegment> result = new HashMap<>(tableSegments.size(), 1F);
81 for (SimpleTableSegment each : tableSegments) {
82 each.getAliasName().ifPresent(optional -> result.putIfAbsent(optional, each));
83 }
84 return result;
85 }
86
87 @Override
88 public DeleteStatement getSqlStatement() {
89 return (DeleteStatement) super.getSqlStatement();
90 }
91
92 @Override
93 public Collection<SimpleTableSegment> getAllTables() {
94 return tablesContext.getSimpleTableSegments();
95 }
96
97 @Override
98 public Collection<WhereSegment> getWhereSegments() {
99 return whereSegments;
100 }
101
102 @Override
103 public Collection<ColumnSegment> getColumnSegments() {
104 return columnSegments;
105 }
106
107 @Override
108 public Collection<BinaryOperationExpression> getJoinConditions() {
109 return joinConditions;
110 }
111 }