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.token.pojo;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
22  import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.Substitutable;
23  
24  import java.util.Collection;
25  import java.util.Map;
26  import java.util.Objects;
27  
28  /**
29   * Predicate in right value token for encrypt.
30   */
31  public final class EncryptPredicateFunctionRightValueToken extends SQLToken implements Substitutable {
32      
33      @Getter
34      private final int stopIndex;
35      
36      private final String functionName;
37      
38      private final Map<Integer, Object> indexValues;
39      
40      private final Collection<Integer> paramMarkerIndexes;
41      
42      public EncryptPredicateFunctionRightValueToken(final int startIndex, final int stopIndex, final String functionName,
43                                                     final Map<Integer, Object> indexValues, final Collection<Integer> paramMarkerIndexes) {
44          super(startIndex);
45          this.stopIndex = stopIndex;
46          this.functionName = functionName;
47          this.indexValues = indexValues;
48          this.paramMarkerIndexes = paramMarkerIndexes;
49      }
50      
51      @Override
52      public String toString() {
53          StringBuilder result = new StringBuilder();
54          result.append(functionName).append(" (");
55          for (int i = 0; i < indexValues.size() + paramMarkerIndexes.size(); i++) {
56              if (paramMarkerIndexes.contains(i)) {
57                  result.append('?');
58              } else {
59                  if (indexValues.get(i) instanceof String) {
60                      result.append('\'').append(indexValues.get(i)).append('\'');
61                  } else {
62                      result.append(indexValues.get(i));
63                  }
64              }
65              result.append(", ");
66          }
67          result.delete(result.length() - 2, result.length()).append(')');
68          return result.toString();
69      }
70      
71      @Override
72      public boolean equals(final Object obj) {
73          return obj instanceof EncryptPredicateFunctionRightValueToken && ((EncryptPredicateFunctionRightValueToken) obj).getStartIndex() == getStartIndex()
74                  && ((EncryptPredicateFunctionRightValueToken) obj).getStopIndex() == stopIndex && ((EncryptPredicateFunctionRightValueToken) obj).indexValues.equals(indexValues)
75                  && ((EncryptPredicateFunctionRightValueToken) obj).paramMarkerIndexes.equals(paramMarkerIndexes);
76      }
77      
78      @Override
79      public int hashCode() {
80          return Objects.hash(getStartIndex(), stopIndex, indexValues, paramMarkerIndexes);
81      }
82  }