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

tuichenchuxin 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 6c75ff0a82a Pass databaseName to encrypt rule (#27897)
6c75ff0a82a is described below

commit 6c75ff0a82a9ff37e4d8489d5e9058cdbc63342a
Author: zhaojinchao <[email protected]>
AuthorDate: Fri Aug 4 09:53:51 2023 +0800

    Pass databaseName to encrypt rule (#27897)
    
    * Pass databaseName to encrypt rule
    
    * Fix ci
    
    * Fix checkstyle
    
    * Fix ci
    
    * Fix ci
    
    * Fix CI
---
 .../content/user-manual/error-code/sql-error-code.en.md    |  2 +-
 .../algorithm/MismatchedEncryptAlgorithmTypeException.java |  7 ++++---
 .../apache/shardingsphere/encrypt/rule/EncryptRule.java    | 14 +++++++++-----
 .../encrypt/rule/builder/CompatibleEncryptRuleBuilder.java |  2 +-
 .../encrypt/rule/builder/EncryptRuleBuilder.java           |  2 +-
 .../generator/fixture/EncryptGeneratorFixtureBuilder.java  |  2 +-
 .../shardingsphere/encrypt/rule/EncryptRuleTest.java       | 13 +++++++------
 .../util/YamlDatabaseConfigurationImportExecutor.java      |  2 +-
 8 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md 
b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 4a6f5e7df06..9e893b194a9 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -268,7 +268,7 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | 42000     | 20740       | Insert value of index \`%s\` can not support for 
encrypt.                                          |
 | 0A000     | 20741       | The SQL clause \`%s\` is unsupported in encrypt 
rule.                                              |
 | HY004     | 20780       | Encrypt algorithm \`%s\` initialization failed, 
reason is: %s.                                     |
-| HY004     | 20781       | \`%s\` column's encryptor name \`%s\` does not 
match encrypt algorithm type \`%s\`.                |
+| HY004     | 20781       | \`%s\` column's encryptor name \`%s\` does not 
match encrypt algorithm type \`%s\ in database \`%s\`.|
 | 44000     | 20703       | Cipher column of \`%s\` can not be null in 
database \`%s\`.                                        |
 | 44000     | 20704       | Can not find (STANDARD\|ASSIST_QUERY\|LIKE_QUERY) 
encryptor in table \`%s\` and column \`%s\`.     |
 | 44000     | 20705       | Assisted query column of \`%s\` can not be null in 
database \`%s\`.                                |
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/algorithm/MismatchedEncryptAlgorithmTypeException.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/algorithm/MismatchedEncryptAlgorithmTypeException.java
index e6987347187..a90b556fa89 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/algorithm/MismatchedEncryptAlgorithmTypeException.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/algorithm/MismatchedEncryptAlgorithmTypeException.java
@@ -25,9 +25,10 @@ import 
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpe
  */
 public final class MismatchedEncryptAlgorithmTypeException extends 
EncryptSQLException {
     
-    private static final long serialVersionUID = -3133058284863085899L;
+    private static final long serialVersionUID = 4258928279099223870L;
     
-    public MismatchedEncryptAlgorithmTypeException(final String columnType, 
final String encryptorName, final String encryptAlgorithmType) {
-        super(XOpenSQLState.GENERAL_ERROR, 81, "`%s` column's encryptor name 
`%s` does not match encrypt algorithm type `%s`.", columnType, encryptorName, 
encryptAlgorithmType);
+    public MismatchedEncryptAlgorithmTypeException(final String databaseName, 
final String columnType, final String encryptorName, final String 
encryptAlgorithmType) {
+        super(XOpenSQLState.GENERAL_ERROR, 81, "`%s` column's encryptor name 
`%s` does not match encrypt algorithm type `%s` in database `%s`.",
+                columnType, encryptorName, encryptAlgorithmType, databaseName);
     }
 }
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
index f385b98f0c0..d923ba65622 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
@@ -44,6 +44,8 @@ import java.util.Optional;
  */
 public final class EncryptRule implements DatabaseRule, TableContainedRule {
     
+    private final String databaseName;
+    
     @Getter
     private final RuleConfiguration configuration;
     
@@ -51,7 +53,8 @@ public final class EncryptRule implements DatabaseRule, 
TableContainedRule {
     
     private final TableNamesMapper tableNamesMapper = new TableNamesMapper();
     
-    public EncryptRule(final EncryptRuleConfiguration ruleConfig) {
+    public EncryptRule(final String databaseName, final 
EncryptRuleConfiguration ruleConfig) {
+        this.databaseName = databaseName;
         configuration = ruleConfig;
         @SuppressWarnings("rawtypes")
         Map<String, StandardEncryptAlgorithm> standardEncryptors = new 
LinkedHashMap<>();
@@ -76,7 +79,8 @@ public final class EncryptRule implements DatabaseRule, 
TableContainedRule {
      * @deprecated deprecated by compatible encrypt rule configuration
      */
     @Deprecated
-    public EncryptRule(final CompatibleEncryptRuleConfiguration ruleConfig) {
+    public EncryptRule(final String databaseName, final 
CompatibleEncryptRuleConfiguration ruleConfig) {
+        this.databaseName = databaseName;
         configuration = ruleConfig;
         @SuppressWarnings("rawtypes")
         Map<String, StandardEncryptAlgorithm> standardEncryptors = new 
LinkedHashMap<>();
@@ -112,19 +116,19 @@ public final class EncryptRule implements DatabaseRule, 
TableContainedRule {
     @SuppressWarnings("rawtypes")
     private void checkStandardEncryptorType(final 
EncryptColumnRuleConfiguration columnRuleConfig, final Map<String, 
StandardEncryptAlgorithm> standardEncryptors) {
         
ShardingSpherePreconditions.checkState(standardEncryptors.containsKey(columnRuleConfig.getCipher().getEncryptorName()),
-                () -> new MismatchedEncryptAlgorithmTypeException("Cipher", 
columnRuleConfig.getCipher().getEncryptorName(), 
StandardEncryptAlgorithm.class.getSimpleName()));
+                () -> new 
MismatchedEncryptAlgorithmTypeException(databaseName, "Cipher", 
columnRuleConfig.getCipher().getEncryptorName(), 
StandardEncryptAlgorithm.class.getSimpleName()));
     }
     
     @SuppressWarnings("rawtypes")
     private void checkAssistedQueryEncryptorType(final 
EncryptColumnRuleConfiguration columnRuleConfig, final Map<String, 
AssistedEncryptAlgorithm> assistedEncryptors) {
         columnRuleConfig.getAssistedQuery().ifPresent(optional -> 
ShardingSpherePreconditions.checkState(assistedEncryptors.containsKey(optional.getEncryptorName()),
-                () -> new MismatchedEncryptAlgorithmTypeException("Assisted 
query", optional.getEncryptorName(), 
AssistedEncryptAlgorithm.class.getSimpleName())));
+                () -> new 
MismatchedEncryptAlgorithmTypeException(databaseName, "Assisted query", 
optional.getEncryptorName(), AssistedEncryptAlgorithm.class.getSimpleName())));
     }
     
     @SuppressWarnings("rawtypes")
     private void checkLikeQueryEncryptorType(final 
EncryptColumnRuleConfiguration columnRuleConfig, final Map<String, 
LikeEncryptAlgorithm> likeEncryptors) {
         columnRuleConfig.getLikeQuery().ifPresent(optional -> 
ShardingSpherePreconditions.checkState(likeEncryptors.containsKey(optional.getEncryptorName()),
-                () -> new MismatchedEncryptAlgorithmTypeException("Like 
query", optional.getEncryptorName(), 
LikeEncryptAlgorithm.class.getSimpleName())));
+                () -> new 
MismatchedEncryptAlgorithmTypeException(databaseName, "Like query", 
optional.getEncryptorName(), LikeEncryptAlgorithm.class.getSimpleName())));
     }
     
     /**
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java
index 8a16c842652..57bfc21ccef 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/CompatibleEncryptRuleBuilder.java
@@ -39,7 +39,7 @@ public final class CompatibleEncryptRuleBuilder implements 
DatabaseRuleBuilder<C
     @Override
     public EncryptRule build(final CompatibleEncryptRuleConfiguration config, 
final String databaseName,
                              final Map<String, DataSource> dataSources, final 
Collection<ShardingSphereRule> builtRules, final InstanceContext 
instanceContext) {
-        return new EncryptRule(config);
+        return new EncryptRule(databaseName, config);
     }
     
     @Override
diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java
index b176e639950..34b8d7622bb 100644
--- 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java
@@ -36,7 +36,7 @@ public final class EncryptRuleBuilder implements 
DatabaseRuleBuilder<EncryptRule
     @Override
     public EncryptRule build(final EncryptRuleConfiguration config, final 
String databaseName,
                              final Map<String, DataSource> dataSources, final 
Collection<ShardingSphereRule> builtRules, final InstanceContext 
instanceContext) {
-        return new EncryptRule(config);
+        return new EncryptRule(databaseName, config);
     }
     
     @Override
diff --git 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
index 36cd909dde3..193181e5a78 100644
--- 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
+++ 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
@@ -86,7 +86,7 @@ public final class EncryptGeneratorFixtureBuilder {
                 new EncryptColumnRuleConfiguration("pwd", new 
EncryptColumnItemRuleConfiguration("pwd_cipher", "standard_encryptor"));
         pwdColumnConfig.setAssistedQuery(new 
EncryptColumnItemRuleConfiguration("pwd_assist", "assisted_encryptor"));
         pwdColumnConfig.setLikeQuery(new 
EncryptColumnItemRuleConfiguration("pwd_like", "like_encryptor"));
-        return new EncryptRule(
+        return new EncryptRule("foo_db",
                 new EncryptRuleConfiguration(Collections.singleton(new 
EncryptTableRuleConfiguration("t_user", 
Collections.singletonList(pwdColumnConfig))), encryptors));
     }
     
diff --git 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
index bc1b16c05e6..5355ec93332 100644
--- 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
+++ 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
@@ -48,27 +48,28 @@ class EncryptRuleTest {
     
     @Test
     void assertFindEncryptTable() {
-        assertTrue(new 
EncryptRule(createEncryptRuleConfiguration()).findEncryptTable("t_encrypt").isPresent());
+        assertTrue(new EncryptRule("foo_db", 
createEncryptRuleConfiguration()).findEncryptTable("t_encrypt").isPresent());
     }
     
     @Test
     void assertGetEncryptTable() {
-        assertThat(new 
EncryptRule(createEncryptRuleConfiguration()).getEncryptTable("t_encrypt").getTable(),
 is("t_encrypt"));
+        assertThat(new EncryptRule("foo_db", 
createEncryptRuleConfiguration()).getEncryptTable("t_encrypt").getTable(), 
is("t_encrypt"));
     }
     
     @Test
     void assertGetNotExistedEncryptTable() {
-        assertThrows(EncryptTableNotFoundException.class, () -> new 
EncryptRule(createEncryptRuleConfiguration()).getEncryptTable("not_existed_tbl"));
+        assertThrows(EncryptTableNotFoundException.class, () -> new 
EncryptRule("foo_db", 
createEncryptRuleConfiguration()).getEncryptTable("not_existed_tbl"));
     }
     
     @Test
     void assertGetTables() {
-        assertThat(new LinkedList<>(new 
EncryptRule(createEncryptRuleConfiguration()).getLogicTableMapper().getTableNames()),
 is(Collections.singletonList("t_encrypt")));
+        assertThat(new LinkedList<>(new EncryptRule("foo_db", 
createEncryptRuleConfiguration()).getLogicTableMapper().getTableNames()), 
is(Collections.singletonList("t_encrypt")));
     }
     
     @Test
     void assertGetTableWithLowercase() {
-        assertThat(new LinkedList<>(new 
EncryptRule(createEncryptRuleConfigurationWithUpperCaseLogicTable()).getLogicTableMapper().getTableNames()),
 is(Collections.singletonList("T_ENCRYPT")));
+        assertThat(new LinkedList<>(new EncryptRule("foo_db", 
createEncryptRuleConfigurationWithUpperCaseLogicTable()).getLogicTableMapper().getTableNames()),
+                is(Collections.singletonList("T_ENCRYPT")));
     }
     
     private EncryptRuleConfiguration createEncryptRuleConfiguration() {
@@ -125,7 +126,7 @@ class EncryptRuleTest {
         EncryptTableRuleConfiguration tableConfig = new 
EncryptTableRuleConfiguration("t_encrypt", Arrays.asList(pwdColumnConfig, 
creditCardColumnConfig));
         EncryptRuleConfiguration ruleConfig = new 
EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(new 
AlgorithmConfiguration("CORE.FIXTURE", new Properties()),
                 new AlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new 
Properties()), new AlgorithmConfiguration("CORE.QUERY_LIKE.FIXTURE", new 
Properties())));
-        assertThrows(MismatchedEncryptAlgorithmTypeException.class, () -> new 
EncryptRule(ruleConfig));
+        assertThrows(MismatchedEncryptAlgorithmTypeException.class, () -> new 
EncryptRule("foo_db", ruleConfig));
     }
     
     private static EncryptColumnRuleConfiguration 
createEncryptColumnRuleConfiguration(final String encryptorName, final String 
assistedQueryEncryptorName, final String likeEncryptorName) {
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
index f31bd1bfe87..34df57dfcd8 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
@@ -260,7 +260,7 @@ public final class YamlDatabaseConfigurationImportExecutor {
     private void addEncryptRuleConfiguration(final EncryptRuleConfiguration 
encryptRuleConfig, final Collection<RuleConfiguration> allRuleConfigs, final 
ShardingSphereDatabase database) {
         encryptRuleConfigImportChecker.check(database, encryptRuleConfig);
         allRuleConfigs.add(encryptRuleConfig);
-        database.getRuleMetaData().getRules().add(new 
EncryptRule(encryptRuleConfig));
+        database.getRuleMetaData().getRules().add(new 
EncryptRule(database.getName(), encryptRuleConfig));
     }
     
     private void addShadowRuleConfiguration(final ShadowRuleConfiguration 
shadowRuleConfig, final Collection<RuleConfiguration> allRuleConfigs, final 
ShardingSphereDatabase database) {

Reply via email to