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 ce8fa97  fix alter encrypt column. (#14171)
ce8fa97 is described below

commit ce8fa974a065ad179f170cc36142f8fcd87d1ac0
Author: tuichenchuxin <[email protected]>
AuthorDate: Tue Dec 21 09:22:50 2021 +0800

    fix alter encrypt column. (#14171)
---
 .../impl/EncryptAlterTableTokenGenerator.java      | 26 ++++++++++++++++++++++
 .../rewrite/token/pojo/EncryptAlterTableToken.java |  4 ++++
 .../encrypt/case/alter_for_query_with_cipher.xml   | 10 +++++++++
 3 files changed, 40 insertions(+)

diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java
index 2f45d28..e09572e 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java
@@ -30,6 +30,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.Column
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.AddColumnDefinitionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.DropColumnDefinitionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.ModifyColumnDefinitionSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.position.ColumnPositionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 
 import java.util.List;
@@ -107,6 +108,7 @@ public final class EncryptAlterTableTokenGenerator extends 
BaseEncryptSQLTokenGe
                 result.addAll(getAddColumnTokens(tableName, columnName, 
addColumnDefinitionSegment, each));
             }
         }
+        getAddColumnPositionToken(tableName, 
addColumnDefinitionSegment).ifPresent(result::add);
         return result;
     }
     
@@ -120,6 +122,20 @@ public final class EncryptAlterTableTokenGenerator extends 
BaseEncryptSQLTokenGe
         return result;
     }
     
+    private Optional<SQLToken> getAddColumnPositionToken(final String 
tableName, final AddColumnDefinitionSegment addColumnDefinitionSegment) {
+        Optional<EncryptAlgorithm> encryptor = 
addColumnDefinitionSegment.getColumnPosition().filter(positionSegment -> null 
!= positionSegment.getColumnName())
+                .flatMap(positionSegment -> 
getEncryptRule().findEncryptor(tableName, 
positionSegment.getColumnName().getIdentifier().getValue()));
+        if (encryptor.isPresent()) {
+            return 
Optional.of(getPositionColumnToken(addColumnDefinitionSegment.getColumnPosition().get(),
 tableName));
+        }
+        return Optional.empty();
+    }
+    
+    private EncryptAlterTableToken getPositionColumnToken(final 
ColumnPositionSegment positionSegment, final String tableName) {
+        return new 
EncryptAlterTableToken(positionSegment.getColumnName().getStartIndex(), 
positionSegment.getStopIndex(), getEncryptRule()
+                .getCipherColumn(tableName, 
positionSegment.getColumnName().getIdentifier().getValue()), null);
+    }
+    
     private Collection<SQLToken> getModifyColumnTokens(final String tableName, 
final Collection<ModifyColumnDefinitionSegment> columnDefinitionSegments) {
         Collection<SQLToken> result = new LinkedList<>();
         for (ModifyColumnDefinitionSegment each : columnDefinitionSegments) {
@@ -131,6 +147,7 @@ public final class EncryptAlterTableTokenGenerator extends 
BaseEncryptSQLTokenGe
             if (encryptor.isPresent() || encryptorPrevious.isPresent()) {
                 result.addAll(getModifyColumnTokens(tableName, columnName, 
each, segment));
             }
+            getModifyColumnPositionToken(tableName, 
each).ifPresent(result::add);
         }
         return result;
     }
@@ -145,6 +162,15 @@ public final class EncryptAlterTableTokenGenerator extends 
BaseEncryptSQLTokenGe
         return result;
     }
     
+    private Optional<SQLToken> getModifyColumnPositionToken(final String 
tableName, final ModifyColumnDefinitionSegment modifyColumnDefinitionSegment) {
+        Optional<EncryptAlgorithm> encryptor = 
modifyColumnDefinitionSegment.getColumnPosition().filter(positionSegment -> 
null != positionSegment.getColumnName())
+                .flatMap(positionSegment -> 
getEncryptRule().findEncryptor(tableName, 
positionSegment.getColumnName().getIdentifier().getValue()));
+        if (encryptor.isPresent()) {
+            return 
Optional.of(getPositionColumnToken(modifyColumnDefinitionSegment.getColumnPosition().get(),
 tableName));
+        }
+        return Optional.empty();
+    }
+    
     private Collection<SQLToken> getDropColumnTokens(final String tableName, 
final Collection<DropColumnDefinitionSegment> columnDefinitionSegments) {
         Collection<SQLToken> result = new LinkedList<>();
         for (DropColumnDefinitionSegment each : columnDefinitionSegments) {
diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/pojo/EncryptAlterTableToken.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/pojo/EncryptAlterTableToken.java
index b3bb0ba..e665dfe 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/pojo/EncryptAlterTableToken.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/pojo/EncryptAlterTableToken.java
@@ -43,6 +43,10 @@ public final class EncryptAlterTableToken extends SQLToken 
implements Substituta
     
     @Override
     public String toString() {
+        if (null == operationType) {
+            return columnName;
+        }
+        // TODO refactor alter table token
         return String.format(" %s %s ", operationType, columnName);
     }
 }
diff --git 
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/alter_for_query_with_cipher.xml
 
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/alter_for_query_with_cipher.xml
index 9a89ddc..950247c 100644
--- 
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/alter_for_query_with_cipher.xml
+++ 
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/alter_for_query_with_cipher.xml
@@ -26,4 +26,14 @@
         <input sql="ALTER TABLE t_account CHANGE COLUMN amount amountA text"/>
         <output sql="ALTER TABLE t_account CHANGE COLUMN cipher_amount 
amountA_cipher text"/>
     </rewrite-assertion>
+
+    <rewrite-assertion id="add_column_with_position_cipher" db-types="MySQL">
+        <input sql="ALTER TABLE t_account ADD COLUMN status text AFTER 
amount"/>
+        <output sql="ALTER TABLE t_account ADD COLUMN status text AFTER 
cipher_amount"/>
+    </rewrite-assertion>
+
+    <rewrite-assertion id="modify_column_with_position_cipher" 
db-types="MySQL">
+        <input sql="ALTER TABLE t_account MODIFY COLUMN status text AFTER 
amount"/>
+        <output sql="ALTER TABLE t_account MODIFY COLUMN status text AFTER 
cipher_amount"/>
+    </rewrite-assertion>
 </rewrite-assertions>

Reply via email to