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.FunctionSegment;
26 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
27 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
28
29 import java.util.ArrayList;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34
35
36
37
38 @Getter
39 @EqualsAndHashCode
40 @ToString
41 public final class EncryptBinaryCondition implements EncryptCondition {
42
43 private final String columnName;
44
45 private final String tableName;
46
47 private final String operator;
48
49 private final int startIndex;
50
51 private final int stopIndex;
52
53 private final ExpressionSegment expressionSegment;
54
55 private final Map<Integer, Integer> positionIndexMap = new LinkedHashMap<>();
56
57 private final Map<Integer, Object> positionValueMap = new LinkedHashMap<>();
58
59 public EncryptBinaryCondition(final String columnName, final String tableName, final String operator, final int startIndex, final int stopIndex, final ExpressionSegment expressionSegment) {
60 this.columnName = columnName;
61 this.tableName = tableName;
62 this.operator = operator;
63 this.startIndex = startIndex;
64 this.stopIndex = stopIndex;
65 this.expressionSegment = expressionSegment;
66 putPositionMap(0, expressionSegment);
67 }
68
69 private void putPositionMap(final int index, final ExpressionSegment expressionSegment) {
70 if (expressionSegment instanceof ParameterMarkerExpressionSegment) {
71 positionIndexMap.put(index, ((ParameterMarkerExpressionSegment) expressionSegment).getParameterMarkerIndex());
72 } else if (expressionSegment instanceof LiteralExpressionSegment) {
73 positionValueMap.put(index, ((LiteralExpressionSegment) expressionSegment).getLiterals());
74 } else if (expressionSegment instanceof FunctionSegment && "CONCAT".equalsIgnoreCase(((FunctionSegment) expressionSegment).getFunctionName())) {
75 int parameterIndex = index;
76 for (ExpressionSegment each : ((FunctionSegment) expressionSegment).getParameters()) {
77 putPositionMap(parameterIndex++, each);
78 }
79 }
80 }
81
82 @Override
83 public List<Object> getValues(final List<Object> params) {
84 List<Object> result = new ArrayList<>(positionValueMap.values());
85 for (Entry<Integer, Integer> entry : positionIndexMap.entrySet()) {
86 Object param = params.get(entry.getValue());
87 if (entry.getKey() < result.size()) {
88 result.add(entry.getKey(), param);
89 } else {
90 result.add(param);
91 }
92 }
93 return result;
94 }
95 }