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 2021/04/24 14:29:43 UTC

[shardingsphere] branch master updated: fix error of encrypt alter table when drop columns. (#10181)

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

zhangliang 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 d311e01  fix error of encrypt alter table when drop columns. (#10181)
d311e01 is described below

commit d311e01cef841aa1c2c2df473d005f28cd8a57f9
Author: huanghao495430759 <34...@users.noreply.github.com>
AuthorDate: Sat Apr 24 09:29:14 2021 -0500

    fix error of encrypt alter table when drop columns. (#10181)
---
 .../token/generator/impl/EncryptAlterTableTokenGenerator.java  |  8 ++++----
 .../src/test/resources/encrypt/alter_for_query_with_plain.xml  | 10 ++++++++++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java
index 4fd1a15..35428df 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptAlterTableTokenGenerator.java
@@ -130,7 +130,7 @@ public final class EncryptAlterTableTokenGenerator extends BaseEncryptSQLTokenGe
     private Collection<SQLToken> getDropColumnTokens(final String tableName, final String columnName, 
                                                      final ColumnSegment columnSegment, final DropColumnDefinitionSegment dropColumnDefinitionSegment) {
         Collection<SQLToken> result = new LinkedList<>();
-        result.add(new RemoveToken(dropColumnDefinitionSegment.getStartIndex() - 1, columnSegment.getStopIndex() + 1));
+        result.add(new RemoveToken(dropColumnDefinitionSegment.getStartIndex() - 1, columnSegment.getStopIndex()));
         result.add(getCipherColumn(tableName, columnName, columnSegment));
         getAssistedQueryColumn(tableName, columnName, columnSegment).ifPresent(result::add);
         getPlainColumn(tableName, columnName, columnSegment).ifPresent(result::add);
@@ -150,7 +150,7 @@ public final class EncryptAlterTableTokenGenerator extends BaseEncryptSQLTokenGe
     
     private EncryptAlterTableToken getCipherColumn(final String tableName, final String columnName, final ColumnSegment columnSegment) {
         String cipherColumn = getEncryptRule().getCipherColumn(tableName, columnName);
-        return new EncryptAlterTableToken(columnSegment.getStopIndex() + 1, columnSegment.getStartIndex() + columnName.length(), cipherColumn, "DROP COLUMN");
+        return new EncryptAlterTableToken(columnSegment.getStopIndex() + 1, columnSegment.getStopIndex(), cipherColumn, "DROP COLUMN");
     }
     
     private Optional<EncryptAlterTableToken> getAssistedQueryColumn(final String tableName, final String columnName, final ColumnDefinitionSegment columnDefinitionSegment) {
@@ -168,7 +168,7 @@ public final class EncryptAlterTableTokenGenerator extends BaseEncryptSQLTokenGe
     
     private Optional<EncryptAlterTableToken> getAssistedQueryColumn(final String tableName, final String columnName, final ColumnSegment columnSegment) {
         Optional<String> assistedQueryColumn = getEncryptRule().findAssistedQueryColumn(tableName, columnName);
-        return assistedQueryColumn.map(optional -> new EncryptAlterTableToken(columnSegment.getStopIndex() + 1, columnSegment.getStartIndex() + columnName.length(), optional, ", DROP COLUMN"));
+        return assistedQueryColumn.map(optional -> new EncryptAlterTableToken(columnSegment.getStopIndex() + 1, columnSegment.getStopIndex(), optional, ", DROP COLUMN"));
     }
     
     private Optional<EncryptAlterTableToken> getPlainColumn(final String tableName, final String columnName, final ColumnDefinitionSegment columnDefinitionSegment) {
@@ -186,6 +186,6 @@ public final class EncryptAlterTableTokenGenerator extends BaseEncryptSQLTokenGe
     
     private Optional<EncryptAlterTableToken> getPlainColumn(final String tableName, final String columnName, final ColumnSegment columnSegment) {
         Optional<String> plainColumn = getEncryptRule().findPlainColumn(tableName, columnName);
-        return plainColumn.map(optional -> new EncryptAlterTableToken(columnSegment.getStopIndex() + 1, columnSegment.getStartIndex() + columnName.length(), optional, ", DROP COLUMN"));
+        return plainColumn.map(optional -> new EncryptAlterTableToken(columnSegment.getStopIndex() + 1, columnSegment.getStopIndex(), optional, ", DROP COLUMN"));
     }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/alter_for_query_with_plain.xml b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/alter_for_query_with_plain.xml
index c656be7..fef1891 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/alter_for_query_with_plain.xml
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/alter_for_query_with_plain.xml
@@ -31,4 +31,14 @@
         <input sql="ALTER TABLE t_account_bak DROP COLUMN password" />
         <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password  , DROP COLUMN assisted_query_password  , DROP COLUMN plain_password " />
     </rewrite-assertion>
+    
+    <rewrite-assertion id="drop_columns_for_plain">
+        <input sql="ALTER TABLE t_account_bak DROP COLUMN password , DROP COLUMN amount" />
+        <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password  , DROP COLUMN assisted_query_password  , DROP COLUMN plain_password  , DROP COLUMN cipher_amount  , DROP COLUMN plain_amount " />
+    </rewrite-assertion>
+    
+    <rewrite-assertion id="drop_mix_columns_for_plain">
+        <input sql="ALTER TABLE t_account_bak DROP COLUMN password , DROP COLUMN id" />
+        <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password  , DROP COLUMN assisted_query_password  , DROP COLUMN plain_password  , DROP COLUMN id" />
+    </rewrite-assertion>
 </rewrite-assertions>