You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/11/21 05:40:18 UTC

[shardingsphere] branch master updated: merge EncryptEqualCondition and EncryptLikeCondition to EncryptBinaryCondition (#22304)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new a950b46166d merge EncryptEqualCondition and EncryptLikeCondition to EncryptBinaryCondition (#22304)
a950b46166d is described below

commit a950b46166d88a0a2cd566b0410622de33773bf0
Author: gxxiong <xi...@foxmail.com>
AuthorDate: Mon Nov 21 13:40:11 2022 +0800

    merge EncryptEqualCondition and EncryptLikeCondition to EncryptBinaryCondition (#22304)
    
    Signed-off-by: gxxiong <xi...@foxmail.com>
    
    Signed-off-by: gxxiong <xi...@foxmail.com>
---
 .../rewrite/condition/EncryptConditionEngine.java  | 16 ++--
 ...lCondition.java => EncryptBinaryCondition.java} |  7 +-
 .../condition/impl/EncryptLikeCondition.java       | 89 ----------------------
 .../EncryptPredicateParameterRewriter.java         |  5 +-
 .../EncryptPredicateRightValueTokenGenerator.java  |  4 +-
 ...va => EncryptBinaryOperationConditionTest.java} |  6 +-
 6 files changed, 18 insertions(+), 109 deletions(-)

diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
index d504e71da8c..bfb97b6a5a5 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
@@ -19,9 +19,8 @@ package org.apache.shardingsphere.encrypt.rewrite.condition;
 
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.encrypt.exception.syntax.UnsupportedEncryptSQLException;
-import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptEqualCondition;
+import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptBinaryCondition;
 import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptInCondition;
-import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptLikeCondition;
 import org.apache.shardingsphere.encrypt.rule.EncryptColumn;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
@@ -172,22 +171,17 @@ public final class EncryptConditionEngine {
             return Optional.empty();
         }
         if (compareRightValue instanceof SimpleExpressionSegment) {
-            return Optional.of(createEncryptEqualCondition(tableName, expression, compareRightValue));
+            return Optional.of(createEncryptBinaryOperationCondition(tableName, expression, compareRightValue));
         }
         if (compareRightValue instanceof ListExpression) {
-            return Optional.of(createEncryptLikeCondition(tableName, expression, ((ListExpression) compareRightValue).getItems().get(0)));
+            return Optional.of(createEncryptBinaryOperationCondition(tableName, expression, ((ListExpression) compareRightValue).getItems().get(0)));
         }
         return Optional.empty();
     }
     
-    private EncryptLikeCondition createEncryptLikeCondition(final String tableName, final BinaryOperationExpression expression, final ExpressionSegment compareRightValue) {
+    private EncryptBinaryCondition createEncryptBinaryOperationCondition(final String tableName, final BinaryOperationExpression expression, final ExpressionSegment compareRightValue) {
         String columnName = ((ColumnSegment) expression.getLeft()).getIdentifier().getValue();
-        return new EncryptLikeCondition(columnName, tableName, compareRightValue.getStartIndex(), expression.getStopIndex(), compareRightValue);
-    }
-    
-    private EncryptEqualCondition createEncryptEqualCondition(final String tableName, final BinaryOperationExpression expression, final ExpressionSegment compareRightValue) {
-        String columnName = ((ColumnSegment) expression.getLeft()).getIdentifier().getValue();
-        return new EncryptEqualCondition(columnName, tableName, compareRightValue.getStartIndex(), expression.getStopIndex(), compareRightValue);
+        return new EncryptBinaryCondition(columnName, tableName, expression.getOperator(), compareRightValue.getStartIndex(), expression.getStopIndex(), compareRightValue);
     }
     
     private static Optional<EncryptCondition> createInEncryptCondition(final String tableName, final InExpression inExpression, final ExpressionSegment inRightValue) {
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptEqualCondition.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptBinaryCondition.java
similarity index 90%
rename from features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptEqualCondition.java
rename to features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptBinaryCondition.java
index 11c1a535aa7..6f535243a1e 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptEqualCondition.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptBinaryCondition.java
@@ -37,12 +37,14 @@ import java.util.Map.Entry;
 @Getter
 @EqualsAndHashCode
 @ToString
-public final class EncryptEqualCondition implements EncryptCondition {
+public final class EncryptBinaryCondition implements EncryptCondition {
     
     private final String columnName;
     
     private final String tableName;
     
+    private final String operator;
+    
     private final int startIndex;
     
     private final int stopIndex;
@@ -51,9 +53,10 @@ public final class EncryptEqualCondition implements EncryptCondition {
     
     private final Map<Integer, Object> positionValueMap = new LinkedHashMap<>();
     
-    public EncryptEqualCondition(final String columnName, final String tableName, final int startIndex, final int stopIndex, final ExpressionSegment expressionSegment) {
+    public EncryptBinaryCondition(final String columnName, final String tableName, final String operator, final int startIndex, final int stopIndex, final ExpressionSegment expressionSegment) {
         this.columnName = columnName;
         this.tableName = tableName;
+        this.operator = operator;
         this.startIndex = startIndex;
         this.stopIndex = stopIndex;
         putPositionMap(expressionSegment);
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptLikeCondition.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptLikeCondition.java
deleted file mode 100644
index 93d3bbd0272..00000000000
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/impl/EncryptLikeCondition.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.encrypt.rewrite.condition.impl;
-
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.ToString;
-import org.apache.shardingsphere.encrypt.rewrite.condition.EncryptCondition;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
-
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-/**
- * Encrypt condition for like.
- */
-@Getter
-@EqualsAndHashCode
-@ToString
-public final class EncryptLikeCondition implements EncryptCondition {
-    
-    private final String columnName;
-    
-    private final String tableName;
-    
-    private final int startIndex;
-    
-    private final int stopIndex;
-    
-    private final Map<Integer, Integer> positionIndexMap = new LinkedHashMap<>();
-    
-    private final Map<Integer, Object> positionValueMap = new LinkedHashMap<>();
-    
-    public EncryptLikeCondition(final String columnName, final String tableName, final int startIndex, final int stopIndex, final ExpressionSegment expressionSegment) {
-        this.columnName = columnName;
-        this.tableName = tableName;
-        this.startIndex = startIndex;
-        this.stopIndex = stopIndex;
-        putPositionMap(expressionSegment);
-    }
-    
-    private void putPositionMap(final ExpressionSegment expressionSegment) {
-        if (expressionSegment instanceof ParameterMarkerExpressionSegment) {
-            positionIndexMap.put(0, ((ParameterMarkerExpressionSegment) expressionSegment).getParameterMarkerIndex());
-        } else if (expressionSegment instanceof LiteralExpressionSegment) {
-            positionValueMap.put(0, ((LiteralExpressionSegment) expressionSegment).getLiterals());
-        }
-    }
-    
-    /**
-     * Get values.
-     *
-     * @param params SQL parameters
-     * @return values
-     */
-    @Override
-    public List<Object> getValues(final List<Object> params) {
-        List<Object> result = new ArrayList<>(positionValueMap.values());
-        for (Entry<Integer, Integer> entry : positionIndexMap.entrySet()) {
-            Object param = params.get(entry.getValue());
-            if (entry.getKey() < result.size()) {
-                result.add(entry.getKey(), param);
-            } else {
-                result.add(param);
-            }
-        }
-        return result;
-    }
-}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptPredicateParameterRewriter.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptPredicateParameterRewriter.java
index 8aba5028b9b..485f7d58bb7 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptPredicateParameterRewriter.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptPredicateParameterRewriter.java
@@ -21,7 +21,7 @@ import lombok.Setter;
 import org.apache.shardingsphere.encrypt.rewrite.aware.DatabaseNameAware;
 import org.apache.shardingsphere.encrypt.rewrite.aware.EncryptConditionsAware;
 import org.apache.shardingsphere.encrypt.rewrite.condition.EncryptCondition;
-import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptLikeCondition;
+import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptBinaryCondition;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.encrypt.rule.aware.EncryptRuleAware;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
@@ -67,7 +67,8 @@ public final class EncryptPredicateParameterRewriter implements ParameterRewrite
     private List<Object> getEncryptedValues(final String schemaName, final EncryptCondition encryptCondition, final List<Object> originalValues) {
         String tableName = encryptCondition.getTableName();
         String columnName = encryptCondition.getColumnName();
-        if (encryptCondition instanceof EncryptLikeCondition && encryptRule.findLikeQueryColumn(tableName, columnName).isPresent()) {
+        if (encryptCondition instanceof EncryptBinaryCondition && "LIKE".equals(((EncryptBinaryCondition) encryptCondition).getOperator())
+                && encryptRule.findLikeQueryColumn(tableName, columnName).isPresent()) {
             return encryptRule.getEncryptLikeQueryValues(databaseName, schemaName, tableName, columnName, originalValues);
         }
         
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java
index c1eed1d6354..43a92ebef80 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java
@@ -22,8 +22,8 @@ import org.apache.shardingsphere.encrypt.exception.syntax.UnsupportedEncryptSQLE
 import org.apache.shardingsphere.encrypt.rewrite.aware.DatabaseNameAware;
 import org.apache.shardingsphere.encrypt.rewrite.aware.EncryptConditionsAware;
 import org.apache.shardingsphere.encrypt.rewrite.condition.EncryptCondition;
+import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptBinaryCondition;
 import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptInCondition;
-import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptLikeCondition;
 import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptPredicateEqualRightValueToken;
 import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptPredicateInRightValueToken;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
@@ -96,7 +96,7 @@ public final class EncryptPredicateRightValueTokenGenerator
     }
     
     private List<Object> getEncryptedValues(final String schemaName, final EncryptCondition encryptCondition, final List<Object> originalValues) {
-        if (encryptCondition instanceof EncryptLikeCondition) {
+        if (encryptCondition instanceof EncryptBinaryCondition && "LIKE".equals(((EncryptBinaryCondition) encryptCondition).getOperator())) {
             Optional<String> likeQueryColumn = encryptRule.findLikeQueryColumn(encryptCondition.getTableName(), encryptCondition.getColumnName());
             if (!likeQueryColumn.isPresent()) {
                 throw new UnsupportedEncryptSQLException("LIKE");
diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptEqualConditionTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptBinaryOperationConditionTest.java
similarity index 90%
rename from features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptEqualConditionTest.java
rename to features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptBinaryOperationConditionTest.java
index 281d0f96cc7..bf5450de69b 100644
--- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptEqualConditionTest.java
+++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptBinaryOperationConditionTest.java
@@ -17,7 +17,7 @@
 
 package org.apache.shardingsphere.encrypt.rewrite.impl;
 
-import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptEqualCondition;
+import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptBinaryCondition;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
 import org.junit.Test;
 
@@ -27,11 +27,11 @@ import java.util.List;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 
-public final class EncryptEqualConditionTest {
+public final class EncryptBinaryOperationConditionTest {
     
     @Test
     public void assertGetConditionValues() {
-        List<Object> actual = new EncryptEqualCondition("col", null,
+        List<Object> actual = new EncryptBinaryCondition("col", null, null,
                 0, 0, new LiteralExpressionSegment(0, 0, 1)).getValues(Collections.emptyList());
         assertThat(actual.size(), is(1));
         assertThat(actual.get(0), is(1));