This is an automated email from the ASF dual-hosted git repository.
zhyee 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 f182787 Refactor SQLStatementContextConverter and
CreateDataSourcesStatementConvert (#6821)
f182787 is described below
commit f182787ce40448b900726df9041d1bfd23577d43
Author: Juan Pan(Trista) <[email protected]>
AuthorDate: Thu Aug 13 18:18:25 2020 +0800
Refactor SQLStatementContextConverter and CreateDataSourcesStatementConvert
(#6821)
* Refactor SQLStatementContextConverter and
CreateDataSourcesStatementContextConverter
* fix test
* fix tests
---
.../shardingsphere-shadow-rewrite/pom.xml | 5 ----
...reateShardingRuleStatementContextConverter.java | 30 +++++++++++-----------
.../jdbc/execute/RDLExecuteEngineTest.java | 4 +--
...CreateDataSourcesStatementContextConverter.java | 12 ++++-----
...teDataSourcesStatementContextConverterTest.java | 24 +++++++++--------
.../context/CreateDataSourcesStatementContext.java | 30 ++++++++--------------
.../parser/sql/visitor/ShardingSphereVisitor.java | 1 +
.../statement/rdl/DataSourceConnectionSegment.java | 2 --
8 files changed, 48 insertions(+), 60 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-rewrite/pom.xml
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-rewrite/pom.xml
index d62220c..8a29715 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-rewrite/pom.xml
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-rewrite/pom.xml
@@ -36,11 +36,6 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-sql-parser-binder</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-rewrite-engine</artifactId>
<version>${project.version}</version>
</dependency>
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
index 61c9ceb..09cf9fe 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
@@ -32,33 +32,33 @@ import
org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlStan
public final class CreateShardingRuleStatementContextConverter implements
SQLStatementContextConverter<CreateShardingRuleStatementContext,
YamlShardingRuleConfiguration> {
@Override
- public YamlShardingRuleConfiguration convert(final
CreateShardingRuleStatementContext sqlStatement) {
+ public YamlShardingRuleConfiguration convert(final
CreateShardingRuleStatementContext context) {
YamlShardingRuleConfiguration result = new
YamlShardingRuleConfiguration();
- addYamlShardingSphereAlgorithmConfiguration(sqlStatement, result);
- addYamlShardingAutoTableRuleConfiguration(sqlStatement, result);
+ addYamlShardingSphereAlgorithmConfiguration(context, result);
+ addYamlShardingAutoTableRuleConfiguration(context, result);
return result;
}
- private void addYamlShardingSphereAlgorithmConfiguration(final
CreateShardingRuleStatementContext sqlStatement, final
YamlShardingRuleConfiguration ruleConfiguration) {
+ private void addYamlShardingSphereAlgorithmConfiguration(final
CreateShardingRuleStatementContext context, final YamlShardingRuleConfiguration
ruleConfiguration) {
YamlShardingSphereAlgorithmConfiguration algorithmConfiguration = new
YamlShardingSphereAlgorithmConfiguration();
- algorithmConfiguration.setType(sqlStatement.getAlgorithmType());
- algorithmConfiguration.setProps(sqlStatement.getAlgorithmProperties());
-
ruleConfiguration.getShardingAlgorithms().put(getAlgorithmName(sqlStatement.getLogicTable(),
sqlStatement.getAlgorithmType()), algorithmConfiguration);
+ algorithmConfiguration.setType(context.getAlgorithmType());
+ algorithmConfiguration.setProps(context.getAlgorithmProperties());
+
ruleConfiguration.getShardingAlgorithms().put(getAlgorithmName(context.getLogicTable(),
context.getAlgorithmType()), algorithmConfiguration);
}
- private void addYamlShardingAutoTableRuleConfiguration(final
CreateShardingRuleStatementContext sqlStatement, final
YamlShardingRuleConfiguration ruleConfiguration) {
+ private void addYamlShardingAutoTableRuleConfiguration(final
CreateShardingRuleStatementContext context, final YamlShardingRuleConfiguration
ruleConfiguration) {
YamlShardingAutoTableRuleConfiguration tableRuleConfiguration = new
YamlShardingAutoTableRuleConfiguration();
- tableRuleConfiguration.setLogicTable(sqlStatement.getLogicTable());
-
tableRuleConfiguration.setActualDataSources(Joiner.on(",").join(sqlStatement.getDataSources()));
-
tableRuleConfiguration.setShardingStrategy(createYamlShardingStrategyConfiguration(sqlStatement));
- ruleConfiguration.getAutoTables().put(sqlStatement.getLogicTable(),
tableRuleConfiguration);
+ tableRuleConfiguration.setLogicTable(context.getLogicTable());
+
tableRuleConfiguration.setActualDataSources(Joiner.on(",").join(context.getDataSources()));
+
tableRuleConfiguration.setShardingStrategy(createYamlShardingStrategyConfiguration(context));
+ ruleConfiguration.getAutoTables().put(context.getLogicTable(),
tableRuleConfiguration);
}
- private YamlShardingStrategyConfiguration
createYamlShardingStrategyConfiguration(final
CreateShardingRuleStatementContext sqlStatement) {
+ private YamlShardingStrategyConfiguration
createYamlShardingStrategyConfiguration(final
CreateShardingRuleStatementContext context) {
YamlShardingStrategyConfiguration strategy = new
YamlShardingStrategyConfiguration();
YamlStandardShardingStrategyConfiguration standard = new
YamlStandardShardingStrategyConfiguration();
- standard.setShardingColumn(sqlStatement.getShardingColumn());
-
standard.setShardingAlgorithmName(getAlgorithmName(sqlStatement.getLogicTable(),
sqlStatement.getAlgorithmType()));
+ standard.setShardingColumn(context.getShardingColumn());
+
standard.setShardingAlgorithmName(getAlgorithmName(context.getLogicTable(),
context.getAlgorithmType()));
strategy.setStandard(standard);
return strategy;
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
index 0f3c715..36eada7 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.proxy.backend.communication.jdbc.execute;
import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.kernel.context.SchemaContext;
import org.apache.shardingsphere.kernel.context.StandardSchemaContexts;
@@ -58,8 +59,7 @@ public final class RDLExecuteEngineTest {
}
private void createDataSourcesContext() {
- dataSourcesContext = mock(CreateDataSourcesStatementContext.class);
- when(dataSourcesContext.getUrls()).thenReturn(Collections.emptyList());
+ dataSourcesContext = new CreateDataSourcesStatementContext(new
CreateDataSourcesStatement(Collections.emptyList()), new MySQLDatabaseType());
}
private void createRuleContext() {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverter.java
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverter.java
index db7540e..4bfd991 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverter.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverter.java
@@ -19,8 +19,8 @@ package org.apache.shardingsphere.proxy.convert;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
import
org.apache.shardingsphere.rdl.parser.binder.context.CreateDataSourcesStatementContext;
-import
org.apache.shardingsphere.rdl.parser.binder.context.CreateDataSourcesStatementContext.DataSourceConnectionUrl;
import
org.apache.shardingsphere.rdl.parser.binder.generator.SQLStatementContextConverter;
+import
org.apache.shardingsphere.rdl.parser.statement.rdl.DataSourceConnectionSegment;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -31,12 +31,12 @@ import java.util.Map;
public final class CreateDataSourcesStatementContextConverter implements
SQLStatementContextConverter<CreateDataSourcesStatementContext, Map<String,
YamlDataSourceParameter>> {
@Override
- public Map<String, YamlDataSourceParameter> convert(final
CreateDataSourcesStatementContext sqlStatement) {
- Map<String, YamlDataSourceParameter> result = new
LinkedHashMap<>(sqlStatement.getUrls().size(), 1);
- for (DataSourceConnectionUrl each : sqlStatement.getUrls()) {
+ public Map<String, YamlDataSourceParameter> convert(final
CreateDataSourcesStatementContext context) {
+ Map<String, YamlDataSourceParameter> result = new
LinkedHashMap<>(context.getSqlStatement().getConnectionInfos().size(), 1);
+ for (DataSourceConnectionSegment each :
context.getSqlStatement().getConnectionInfos()) {
YamlDataSourceParameter dataSource = new YamlDataSourceParameter();
- dataSource.setUrl(each.getUrl());
- dataSource.setUsername(each.getUserName());
+ dataSource.setUrl(context.getUrl(each));
+ dataSource.setUsername(each.getUser());
dataSource.setPassword(each.getPassword());
result.put(each.getName(), dataSource);
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverterTest.java
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverterTest.java
index e200958..524509e 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverterTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/convert/CreateDataSourcesStatementContextConverterTest.java
@@ -17,9 +17,11 @@
package org.apache.shardingsphere.proxy.convert;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
import
org.apache.shardingsphere.rdl.parser.binder.context.CreateDataSourcesStatementContext;
-import
org.apache.shardingsphere.rdl.parser.binder.context.CreateDataSourcesStatementContext.DataSourceConnectionUrl;
+import
org.apache.shardingsphere.rdl.parser.statement.rdl.CreateDataSourcesStatement;
+import
org.apache.shardingsphere.rdl.parser.statement.rdl.DataSourceConnectionSegment;
import org.junit.Before;
import org.junit.Test;
@@ -31,8 +33,6 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class CreateDataSourcesStatementContextConverterTest {
@@ -40,16 +40,20 @@ public final class
CreateDataSourcesStatementContextConverterTest {
@Before
public void setUp() {
- sqlStatement = mock(CreateDataSourcesStatementContext.class);
- when(sqlStatement.getUrls()).thenReturn(createDataSourceContexts());
+ sqlStatement = new CreateDataSourcesStatementContext(new
CreateDataSourcesStatement(createDataSourceConnectionSegments()), new
MySQLDatabaseType());
}
- private Collection<DataSourceConnectionUrl> createDataSourceContexts() {
- Collection<DataSourceConnectionUrl> result = new LinkedList<>();
+ private Collection<DataSourceConnectionSegment>
createDataSourceConnectionSegments() {
+ Collection<DataSourceConnectionSegment> result = new LinkedList<>();
for (int i = 0; i < 2; i++) {
- DataSourceConnectionUrl context =
- new DataSourceConnectionUrl("ds" + i,
"jdbc:mysql://127.0.0.1:3306/demo_ds_" + i +
"?serverTimezone=UTC&useSSL=false", "root" + i, "root" + i);
- result.add(context);
+ DataSourceConnectionSegment segment = new
DataSourceConnectionSegment();
+ segment.setName("ds" + i);
+ segment.setHostName("127.0.0.1");
+ segment.setPassword("3306");
+ segment.setDb("demo_ds_" + i);
+ segment.setUser("root" + i);
+ segment.setPassword("root" + i);
+ result.add(segment);
}
return result;
}
diff --git
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateDataSourcesStatementContext.java
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateDataSourcesStatementContext.java
index 49abdae..1081b4e 100644
---
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateDataSourcesStatementContext.java
+++
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateDataSourcesStatementContext.java
@@ -18,42 +18,32 @@
package org.apache.shardingsphere.rdl.parser.binder.context;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import
org.apache.shardingsphere.rdl.parser.binder.util.DataSourceConnectionUrlUtil;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.CreateDataSourcesStatement;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.DataSourceConnectionSegment;
import
org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
-import java.util.Collection;
-import java.util.LinkedList;
-
/**
* Create dataSource statement context.
*/
@Getter
public final class CreateDataSourcesStatementContext extends
CommonSQLStatementContext<CreateDataSourcesStatement> {
- private final Collection<DataSourceConnectionUrl> urls;
+ private final DatabaseType databaseType;
public CreateDataSourcesStatementContext(final CreateDataSourcesStatement
sqlStatement, final DatabaseType databaseType) {
super(sqlStatement);
- urls = new LinkedList<>();
- for (DataSourceConnectionSegment each :
sqlStatement.getConnectionInfos()) {
- urls.add(new DataSourceConnectionUrl(each.getName(),
DataSourceConnectionUrlUtil.getUrl(each, databaseType), each.getUser(),
each.getPassword()));
- }
+ this.databaseType = databaseType;
}
- @RequiredArgsConstructor
- @Getter
- public static final class DataSourceConnectionUrl {
-
- private final String name;
-
- private final String url;
-
- private final String userName;
-
- private final String password;
+ /**
+ * Get url.
+ *
+ * @param segment segment
+ * @return url
+ */
+ public String getUrl(final DataSourceConnectionSegment segment) {
+ return DataSourceConnectionUrlUtil.getUrl(segment, databaseType);
}
}
diff --git
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/src/main/java/org/apache/shardingsphere/rdl/parser/sql/visitor/ShardingSphereVisitor.java
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/src/main/java/org/apache/shardingsphere/rdl/parser/sql/visitor/ShardingSphereVisitor.java
index b7458a6..db3049f 100644
---
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/src/main/java/org/apache/shardingsphere/rdl/parser/sql/visitor/ShardingSphereVisitor.java
+++
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-sql/src/main/java/org/apache/shardingsphere/rdl/parser/sql/visitor/ShardingSphereVisitor.java
@@ -92,6 +92,7 @@ public final class ShardingSphereVisitor extends
ShardingSphereStatementBaseVisi
result.setLogicTable(ctx.tableName().getText());
result.setAlgorithmType(ctx.tableRuleDefinition().strategyType().getText());
result.setShardingColumn(ctx.tableRuleDefinition().strategyDefinition().columName().getText());
+ // TODO Future feature.
result.setDataSources(new LinkedList<>());
CollectionValue<String> props = (CollectionValue)
visit(ctx.tableRuleDefinition().strategyDefinition().strategyProps());
result.setProperties(props.getValue());
diff --git
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-statement/src/main/java/org/apache/shardingsphere/rdl/parser/statement/rdl/DataSourceConnectionSegment.java
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-statement/src/main/java/org/apache/shardingsphere/rdl/parser/statement/rdl/DataSourceConnectionSegment.java
index 7a28077..9b92b08 100644
---
a/shardingsphere-rdl-parser/shardingsphere-rdl-parser-statement/src/main/java/org/apache/shardingsphere/rdl/parser/statement/rdl/DataSourceConnectionSegment.java
+++
b/shardingsphere-rdl-parser/shardingsphere-rdl-parser-statement/src/main/java/org/apache/shardingsphere/rdl/parser/statement/rdl/DataSourceConnectionSegment.java
@@ -18,14 +18,12 @@
package org.apache.shardingsphere.rdl.parser.statement.rdl;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.api.ASTNode;
/**
* Data source connection segment.
*/
-@RequiredArgsConstructor
@Getter
@Setter
public final class DataSourceConnectionSegment implements ASTNode {