1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.encrypt.rewrite.condition.impl;
19
20 import lombok.EqualsAndHashCode;
21 import lombok.Getter;
22 import lombok.ToString;
23 import org.apache.shardingsphere.encrypt.rewrite.condition.EncryptCondition;
24 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
25 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
26 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
27
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33
34
35
36
37 @Getter
38 @EqualsAndHashCode
39 @ToString
40 public final class EncryptInCondition implements EncryptCondition {
41
42 private final String columnName;
43
44 private final String tableName;
45
46 private final int startIndex;
47
48 private final int stopIndex;
49
50 private final Map<Integer, Integer> positionIndexMap = new LinkedHashMap<>();
51
52 private final Map<Integer, Object> positionValueMap = new LinkedHashMap<>();
53
54 public EncryptInCondition(final String columnName, final String tableName, final int startIndex, final int stopIndex, final List<ExpressionSegment> expressionSegments) {
55 this.columnName = columnName;
56 this.tableName = tableName;
57 this.startIndex = startIndex;
58 this.stopIndex = stopIndex;
59 int count = 0;
60 for (ExpressionSegment each : expressionSegments) {
61 putPositionMap(count, each);
62 count++;
63 }
64 }
65
66 private void putPositionMap(final int position, final ExpressionSegment expressionSegment) {
67 if (expressionSegment instanceof ParameterMarkerExpressionSegment) {
68 positionIndexMap.put(position, ((ParameterMarkerExpressionSegment) expressionSegment).getParameterMarkerIndex());
69 } else if (expressionSegment instanceof LiteralExpressionSegment) {
70 positionValueMap.put(position, ((LiteralExpressionSegment) expressionSegment).getLiterals());
71 }
72 }
73
74 @Override
75 public List<Object> getValues(final List<Object> params) {
76 List<Object> result = new ArrayList<>(positionValueMap.values());
77 for (Entry<Integer, Integer> entry : positionIndexMap.entrySet()) {
78 Object param = params.get(entry.getValue());
79 if (entry.getKey() < result.size()) {
80 result.add(entry.getKey(), param);
81 } else {
82 result.add(param);
83 }
84 }
85 return result;
86 }
87 }