You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/06/23 17:02:37 UTC

[shardingsphere] branch master updated: Refactor EncryptTable (#26512)

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

zhaojinchao 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 6333a1efdf7 Refactor EncryptTable (#26512)
6333a1efdf7 is described below

commit 6333a1efdf7046df60435d8f9c93ac1ef0c8adc6
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Jun 24 01:02:30 2023 +0800

    Refactor EncryptTable (#26512)
    
    * Refactor EncryptTable
    
    * Refactor EncryptOrderByItemTokenGenerator
    
    * Refactor EncryptAlterTableTokenGeneratorTest
    
    * Refactor EncryptAlterTableTokenGeneratorTest
---
 .../generator/EncryptOrderByItemTokenGenerator.java  | 13 +++++++------
 .../shardingsphere/encrypt/rule/EncryptTable.java    | 20 ++++++++++----------
 .../EncryptAlterTableTokenGeneratorTest.java         | 10 +---------
 .../encrypt/rule/EncryptTableTest.java               | 16 ++++++++--------
 4 files changed, 26 insertions(+), 33 deletions(-)

diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java
index 14795cc360b..4575a7eeb17 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java
@@ -21,6 +21,7 @@ import lombok.Setter;
 import org.apache.shardingsphere.encrypt.rewrite.aware.EncryptRuleAware;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.encrypt.rule.EncryptTable;
+import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn;
 import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
@@ -81,16 +82,16 @@ public final class EncryptOrderByItemTokenGenerator implements CollectionSQLToke
         for (ColumnSegment each : columnSegments) {
             String tableName = columnTableNames.getOrDefault(each.getExpression(), "");
             Optional<EncryptTable> encryptTable = encryptRule.findEncryptTable(tableName);
-            if (!encryptTable.isPresent() || !encryptTable.get().isEncryptColumn(each.getIdentifier().getValue())) {
+            String columnName = each.getIdentifier().getValue();
+            if (!encryptTable.isPresent() || !encryptTable.get().isEncryptColumn(columnName)) {
                 continue;
             }
             int startIndex = each.getOwner().isPresent() ? each.getOwner().get().getStopIndex() + 2 : each.getStartIndex();
             int stopIndex = each.getStopIndex();
-            SubstitutableColumnNameToken encryptColumnNameToken = encryptTable.get().getEncryptColumn(each.getIdentifier().getValue()).getAssistedQuery()
-                    .map(optional -> new SubstitutableColumnNameToken(startIndex, stopIndex,
-                            createColumnProjections(optional.getName(), each.getIdentifier().getQuoteCharacter())))
-                    .orElseGet(() -> new SubstitutableColumnNameToken(startIndex, stopIndex,
-                            createColumnProjections(encryptTable.get().getEncryptColumn(each.getIdentifier().getValue()).getCipher().getName(), each.getIdentifier().getQuoteCharacter())));
+            EncryptColumn encryptColumn = encryptTable.get().getEncryptColumn(columnName);
+            SubstitutableColumnNameToken encryptColumnNameToken = encryptColumn.getAssistedQuery()
+                    .map(optional -> new SubstitutableColumnNameToken(startIndex, stopIndex, createColumnProjections(optional.getName(), each.getIdentifier().getQuoteCharacter())))
+                    .orElseGet(() -> new SubstitutableColumnNameToken(startIndex, stopIndex, createColumnProjections(encryptColumn.getCipher().getName(), each.getIdentifier().getQuoteCharacter())));
             result.add(encryptColumnNameToken);
         }
         return result;
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java
index 5cb8471f7d3..a0bda5c2b7f 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java
@@ -97,6 +97,16 @@ public final class EncryptTable {
         return columns.keySet();
     }
     
+    /**
+     * Is cipher column or not.
+     *
+     * @param columnName column name
+     * @return cipher column or not
+     */
+    public boolean isCipherColumn(final String columnName) {
+        return columns.values().stream().anyMatch(each -> each.getCipher().getName().equalsIgnoreCase(columnName));
+    }
+    
     /**
      * Get logic column by cipher column.
      * 
@@ -113,16 +123,6 @@ public final class EncryptTable {
         throw new EncryptLogicColumnNotFoundException(cipherColumnName);
     }
     
-    /**
-     * Is cipher column or not.
-     *
-     * @param logicColumnName logic column name
-     * @return cipher column or not
-     */
-    public boolean isCipherColumn(final String logicColumnName) {
-        return columns.values().stream().anyMatch(each -> each.getCipher().getName().equalsIgnoreCase(logicColumnName));
-    }
-    
     /**
      * Get assisted query columns.
      *
diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGeneratorTest.java
index e9cb159b336..98beb5f2b1e 100644
--- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGeneratorTest.java
+++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGeneratorTest.java
@@ -43,7 +43,6 @@ import org.junit.jupiter.api.Test;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
-import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
@@ -69,18 +68,11 @@ class EncryptAlterTableTokenGeneratorTest {
     }
     
     private EncryptTable mockEncryptTable() {
-        EncryptTable result = mock(EncryptTable.class, RETURNS_DEEP_STUBS);
+        EncryptTable result = mock(EncryptTable.class);
         when(result.getTable()).thenReturn("t_encrypt");
         when(result.isEncryptColumn("certificate_number")).thenReturn(true);
-        when(result.getEncryptColumn("certificate_number").getCipher().getName()).thenReturn("cipher_certificate_number");
-        when(result.getEncryptColumn("certificate_number").getAssistedQuery())
-                .thenReturn(Optional.of(new AssistedQueryColumnItem("assisted_certificate_number", mock(AssistedEncryptAlgorithm.class))));
-        when(result.getEncryptColumn("certificate_number").getLikeQuery())
-                .thenReturn(Optional.of(new LikeQueryColumnItem("like_certificate_number", mock(LikeEncryptAlgorithm.class))));
-        when(result.getLogicColumns()).thenReturn(Collections.singleton("t_encrypt"));
         when(result.getEncryptColumn("certificate_number")).thenReturn(mockEncryptColumn());
         when(result.isEncryptColumn("certificate_number_new")).thenReturn(true);
-        when(result.getEncryptColumn("certificate_number_new").getCipher().getName()).thenReturn("cipher_certificate_number_new");
         when(result.getEncryptColumn("certificate_number_new")).thenReturn(mockNewEncryptColumn());
         return result;
     }
diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableTest.java
index 66ce1c232af..3cd443ce658 100644
--- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableTest.java
+++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableTest.java
@@ -64,23 +64,23 @@ class EncryptTableTest {
     }
     
     @Test
-    void assertGetLogicColumnByCipherColumn() {
-        assertNotNull(encryptTable.getLogicColumnByCipherColumn("cipherColumn"));
+    void assertIsCipherColumn() {
+        assertTrue(encryptTable.isCipherColumn("CipherColumn"));
     }
     
     @Test
-    void assertGetLogicColumnByCipherColumnWhenNotFind() {
-        assertThrows(EncryptLogicColumnNotFoundException.class, () -> encryptTable.getLogicColumnByCipherColumn("invalidColumn"));
+    void assertIsNotCipherColumn() {
+        assertFalse(encryptTable.isCipherColumn("logicColumn"));
     }
     
     @Test
-    void assertIsCipherColumn() {
-        assertTrue(encryptTable.isCipherColumn("CipherColumn"));
+    void assertGetLogicColumnByCipherColumn() {
+        assertNotNull(encryptTable.getLogicColumnByCipherColumn("cipherColumn"));
     }
     
     @Test
-    void assertIsNotCipherColumn() {
-        assertFalse(encryptTable.isCipherColumn("logicColumn"));
+    void assertGetLogicColumnByCipherColumnWhenNotFind() {
+        assertThrows(EncryptLogicColumnNotFoundException.class, () -> encryptTable.getLogicColumnByCipherColumn("invalidColumn"));
     }
     
     @Test