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.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.statement.core.segment.dml.column.ColumnSegment;
25  import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
26  import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
27  import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment;
28  import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
29  
30  import java.util.LinkedHashMap;
31  import java.util.Map;
32  
33  /**
34   * Encrypt condition for equal.
35   */
36  @Getter
37  @EqualsAndHashCode
38  @ToString
39  public final class EncryptBinaryCondition implements EncryptCondition {
40      
41      private final ColumnSegment columnSegment;
42      
43      private final String tableName;
44      
45      private final String operator;
46      
47      private final int startIndex;
48      
49      private final int stopIndex;
50      
51      private final ExpressionSegment expressionSegment;
52      
53      private final Map<Integer, Integer> positionIndexMap = new LinkedHashMap<>();
54      
55      private final Map<Integer, Object> positionValueMap = new LinkedHashMap<>();
56      
57      public EncryptBinaryCondition(final ColumnSegment columnSegment, final String tableName, final String operator, final int startIndex, final int stopIndex,
58                                    final ExpressionSegment expressionSegment) {
59          this.columnSegment = columnSegment;
60          this.tableName = tableName;
61          this.operator = operator;
62          this.startIndex = startIndex;
63          this.stopIndex = stopIndex;
64          this.expressionSegment = expressionSegment;
65          putPositionMap(0, expressionSegment);
66      }
67      
68      private int putPositionMap(final int index, final ExpressionSegment expressionSegment) {
69          int parameterIndex = index;
70          if (expressionSegment instanceof ParameterMarkerExpressionSegment) {
71              positionIndexMap.put(parameterIndex, ((ParameterMarkerExpressionSegment) expressionSegment).getParameterMarkerIndex());
72              return parameterIndex + 1;
73          } else if (expressionSegment instanceof LiteralExpressionSegment) {
74              positionValueMap.put(parameterIndex, ((LiteralExpressionSegment) expressionSegment).getLiterals());
75              return parameterIndex + 1;
76          } else if (expressionSegment instanceof FunctionSegment && "CONCAT".equalsIgnoreCase(((FunctionSegment) expressionSegment).getFunctionName())) {
77              for (ExpressionSegment each : ((FunctionSegment) expressionSegment).getParameters()) {
78                  parameterIndex = putPositionMap(parameterIndex, each);
79              }
80          }
81          return parameterIndex;
82      }
83  }