From 8795c365a68ecdc027ebf439534d9a517901d346 Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Fri, 3 Nov 2023 10:00:17 +0800 Subject: [PATCH] Add props config in SQLTranslatorRuleConfiguration --- .../SQLTranslatorRuleConfiguration.java | 6 ++++- .../sqltranslator/rule/SQLTranslatorRule.java | 2 +- .../YamlSQLTranslatorRuleConfiguration.java | 4 ++++ ...SQLTranslatorRuleConfigurationSwapper.java | 3 ++- ...SQLTranslatorRuleConfigurationSwapper.java | 3 ++- .../rule/SQLTranslatorRuleTest.java | 24 ++++++++++++------- .../ShowSQLTranslatorRuleExecutorTest.java | 2 +- 7 files changed, 30 insertions(+), 14 deletions(-) diff --git a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/api/config/SQLTranslatorRuleConfiguration.java b/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/api/config/SQLTranslatorRuleConfiguration.java index bc2c2253a2612..a2222e1749035 100644 --- a/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/api/config/SQLTranslatorRuleConfiguration.java +++ b/kernel/sql-translator/api/src/main/java/org/apache/shardingsphere/sqltranslator/api/config/SQLTranslatorRuleConfiguration.java @@ -21,6 +21,8 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration; +import java.util.Properties; + /** * SQL translator rule configuration. */ @@ -30,9 +32,11 @@ public final class SQLTranslatorRuleConfiguration implements GlobalRuleConfigura private final String type; + private final Properties props; + private final boolean useOriginalSQLWhenTranslatingFailed; public SQLTranslatorRuleConfiguration() { - this(null, true); + this(null, new Properties(), true); } } diff --git a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java index a852d79c10ff3..e38376142e5bb 100644 --- a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java +++ b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java @@ -42,7 +42,7 @@ public final class SQLTranslatorRule implements GlobalRule { public SQLTranslatorRule(final SQLTranslatorRuleConfiguration ruleConfig) { configuration = ruleConfig; - translator = TypedSPILoader.getService(SQLTranslator.class, ruleConfig.getType()); + translator = TypedSPILoader.getService(SQLTranslator.class, ruleConfig.getType(), ruleConfig.getProps()); useOriginalSQLWhenTranslatingFailed = ruleConfig.isUseOriginalSQLWhenTranslatingFailed(); } diff --git a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/config/YamlSQLTranslatorRuleConfiguration.java b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/config/YamlSQLTranslatorRuleConfiguration.java index f9c4388e50a3f..5887855fc8c72 100644 --- a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/config/YamlSQLTranslatorRuleConfiguration.java +++ b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/config/YamlSQLTranslatorRuleConfiguration.java @@ -22,6 +22,8 @@ import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfiguration; import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration; +import java.util.Properties; + /** * SQL translator configuration for YAML. */ @@ -31,6 +33,8 @@ public final class YamlSQLTranslatorRuleConfiguration implements YamlGlobalRuleC private String type; + private Properties props; + private boolean useOriginalSQLWhenTranslatingFailed = true; @Override diff --git a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java index 1ca7e45835461..9b9a268ff9b34 100644 --- a/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java +++ b/kernel/sql-translator/core/src/main/java/org/apache/shardingsphere/sqltranslator/yaml/swapper/NewYamlSQLTranslatorRuleConfigurationSwapper.java @@ -43,6 +43,7 @@ public Collection swapToDataNodes(final SQLTranslatorRuleConfigura private YamlSQLTranslatorRuleConfiguration swapToYamlConfiguration(final SQLTranslatorRuleConfiguration data) { YamlSQLTranslatorRuleConfiguration result = new YamlSQLTranslatorRuleConfiguration(); result.setType(data.getType()); + result.setProps(data.getProps()); result.setUseOriginalSQLWhenTranslatingFailed(data.isUseOriginalSQLWhenTranslatingFailed()); return result; } @@ -60,7 +61,7 @@ public Optional swapToObject(final Collection new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("ALWAYS_FAILED", false)).translate("", queryContext, storageType, - mock(ShardingSphereDatabase.class), mock(RuleMetaData.class))); + assertThrows(UnsupportedTranslatedDatabaseException.class, + () -> new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("ALWAYS_FAILED", new Properties(), false)).translate("", queryContext, storageType, + mock(ShardingSphereDatabase.class), mock(RuleMetaData.class))); } @Test void assertGetConfiguration() { - SQLTranslatorRuleConfiguration expected = new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false); + SQLTranslatorRuleConfiguration expected = new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", new Properties(), false); assertThat(new SQLTranslatorRule(expected).getConfiguration(), is(expected)); } } diff --git a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/ShowSQLTranslatorRuleExecutorTest.java b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/ShowSQLTranslatorRuleExecutorTest.java index 5282a5ab9d5da..fc28b45cd29f6 100644 --- a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/ShowSQLTranslatorRuleExecutorTest.java +++ b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/ShowSQLTranslatorRuleExecutorTest.java @@ -70,6 +70,6 @@ private ShardingSphereMetaData mockMetaData() { } private SQLTranslatorRuleConfiguration createSQLTranslatorRuleConfiguration() { - return new SQLTranslatorRuleConfiguration("NATIVE", true); + return new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true); } }