Skip to content

Commit

Permalink
Add props config in SQLTranslatorRuleConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
strongduanmu committed Nov 3, 2023
1 parent 0de4f9b commit 8795c36
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;

import java.util.Properties;

/**
* SQL translator rule configuration.
*/
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -31,6 +33,8 @@ public final class YamlSQLTranslatorRuleConfiguration implements YamlGlobalRuleC

private String type;

private Properties props;

private boolean useOriginalSQLWhenTranslatingFailed = true;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public Collection<YamlDataNode> 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;
}
Expand All @@ -60,7 +61,7 @@ public Optional<SQLTranslatorRuleConfiguration> swapToObject(final Collection<Ya
}

private SQLTranslatorRuleConfiguration swapToObject(final YamlSQLTranslatorRuleConfiguration yamlConfig) {
return new SQLTranslatorRuleConfiguration(yamlConfig.getType(), yamlConfig.isUseOriginalSQLWhenTranslatingFailed());
return new SQLTranslatorRuleConfiguration(yamlConfig.getType(), yamlConfig.getProps(), yamlConfig.isUseOriginalSQLWhenTranslatingFailed());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public final class YamlSQLTranslatorRuleConfigurationSwapper implements YamlRule
public YamlSQLTranslatorRuleConfiguration swapToYamlConfiguration(final SQLTranslatorRuleConfiguration data) {
YamlSQLTranslatorRuleConfiguration result = new YamlSQLTranslatorRuleConfiguration();
result.setType(data.getType());
result.setProps(data.getProps());
result.setUseOriginalSQLWhenTranslatingFailed(data.isUseOriginalSQLWhenTranslatingFailed());
return result;
}

@Override
public SQLTranslatorRuleConfiguration swapToObject(final YamlSQLTranslatorRuleConfiguration yamlConfig) {
return new SQLTranslatorRuleConfiguration(yamlConfig.getType(), yamlConfig.isUseOriginalSQLWhenTranslatingFailed());
return new SQLTranslatorRuleConfiguration(yamlConfig.getType(), yamlConfig.getProps(), yamlConfig.isUseOriginalSQLWhenTranslatingFailed());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.Test;

import java.util.Locale;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -43,7 +44,8 @@ void assertTranslateWhenProtocolSameAsStorage() {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
QueryContext queryContext = mock(QueryContext.class, RETURNS_DEEP_STUBS);
when(queryContext.getSqlStatementContext().getDatabaseType()).thenReturn(databaseType);
String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false)).translate(expected, queryContext, databaseType, mock(ShardingSphereDatabase.class),
String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", new Properties(), false)).translate(expected, queryContext, databaseType,
mock(ShardingSphereDatabase.class),
mock(RuleMetaData.class));
assertThat(actual, is(expected));
}
Expand All @@ -54,8 +56,9 @@ void assertTranslateWhenNoStorage() {
DatabaseType sqlParserType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
QueryContext queryContext = mock(QueryContext.class, RETURNS_DEEP_STUBS);
when(queryContext.getSqlStatementContext().getDatabaseType()).thenReturn(sqlParserType);
String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false)).translate(expected, queryContext, null, mock(ShardingSphereDatabase.class),
mock(RuleMetaData.class));
String actual =
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", new Properties(), false)).translate(expected, queryContext, null, mock(ShardingSphereDatabase.class),
mock(RuleMetaData.class));
assertThat(actual, is(expected));
}

Expand All @@ -66,7 +69,8 @@ void assertTranslateWithProtocolDifferentWithStorage() {
QueryContext queryContext = mock(QueryContext.class, RETURNS_DEEP_STUBS);
when(queryContext.getSqlStatementContext().getDatabaseType()).thenReturn(sqlParserType);
DatabaseType storageType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", false)).translate(input, queryContext, storageType, mock(ShardingSphereDatabase.class),
String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("CONVERT_TO_UPPER_CASE", new Properties(), false)).translate(input, queryContext, storageType,
mock(ShardingSphereDatabase.class),
mock(RuleMetaData.class));
assertThat(actual, is(input.toUpperCase(Locale.ROOT)));
}
Expand All @@ -78,8 +82,9 @@ void assertUseOriginalSQLWhenTranslatingFailed() {
QueryContext queryContext = mock(QueryContext.class, RETURNS_DEEP_STUBS);
when(queryContext.getSqlStatementContext().getDatabaseType()).thenReturn(sqlParserType);
DatabaseType storageType = TypedSPILoader.getService(DatabaseType.class, "MySQL");
String actual = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("ALWAYS_FAILED", true)).translate(expected, queryContext, storageType, mock(ShardingSphereDatabase.class),
mock(RuleMetaData.class));
String actual =
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration("ALWAYS_FAILED", new Properties(), true)).translate(expected, queryContext, storageType, mock(ShardingSphereDatabase.class),
mock(RuleMetaData.class));
assertThat(actual, is(expected));
}

Expand All @@ -89,13 +94,14 @@ void assertNotUseOriginalSQLWhenTranslatingFailed() {
DatabaseType sqlParserType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
when(queryContext.getSqlStatementContext().getDatabaseType()).thenReturn(sqlParserType);
DatabaseType storageType = TypedSPILoader.getService(DatabaseType.class, "MySQL");
assertThrows(UnsupportedTranslatedDatabaseException.class, () -> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ private ShardingSphereMetaData mockMetaData() {
}

private SQLTranslatorRuleConfiguration createSQLTranslatorRuleConfiguration() {
return new SQLTranslatorRuleConfiguration("NATIVE", true);
return new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true);
}
}

0 comments on commit 8795c36

Please sign in to comment.