You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by do...@apache.org on 2020/10/31 12:15:05 UTC
[shardingsphere] branch master updated: Move
DataSourceConnectionUrlUtil's logic into
CreateDataSourcesStatementContextConverter (#7994)
This is an automated email from the ASF dual-hosted git repository.
dongzonglei 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 2e4a458 Move DataSourceConnectionUrlUtil's logic into CreateDataSourcesStatementContextConverter (#7994)
2e4a458 is described below
commit 2e4a458bf948fc565cea31c8591b2f4569d1a120
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Oct 31 20:14:53 2020 +0800
Move DataSourceConnectionUrlUtil's logic into CreateDataSourcesStatementContextConverter (#7994)
* private ShardingAlgorithmPropertiesUtil.TYPE_PROPERTIES_MAP
* Move DataSourceConnectionUrlUtil's logic into CreateDataSourcesStatementContextConverter
* Refactor ShardingAlgorithmPropertiesUtil
* Remove CreateDataSourcesStatementContext.getUrl
* For code style
---
.../rdl/CreateDataSourcesStatementContext.java | 14 +---
.../rdl/util/DataSourceConnectionUrlUtil.java | 46 -----------
.../rdl/util/ShardingAlgorithmPropertiesUtil.java | 33 ++++----
.../rdl/util/DataSourceConnectionUrlUtilTest.java | 89 ----------------------
...CreateDataSourcesStatementContextConverter.java | 13 +++-
5 files changed, 28 insertions(+), 167 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/CreateDataSourcesStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/CreateDataSourcesStatementContext.java
index e01359a..4a89722 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/CreateDataSourcesStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/CreateDataSourcesStatementContext.java
@@ -18,11 +18,9 @@
package org.apache.shardingsphere.infra.binder.statement.rdl;
import lombok.Getter;
-import org.apache.shardingsphere.infra.binder.statement.rdl.util.DataSourceConnectionUrlUtil;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.distsql.parser.statement.rdl.CreateDataSourcesStatement;
-import org.apache.shardingsphere.distsql.parser.statement.rdl.DataSourceConnectionSegment;
import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
/**
* Create dataSource statement context.
@@ -36,14 +34,4 @@ public final class CreateDataSourcesStatementContext extends CommonSQLStatementC
super(sqlStatement);
this.databaseType = databaseType;
}
-
- /**
- * Get URL.
- *
- * @param segment segment
- * @return URL
- */
- public String getUrl(final DataSourceConnectionSegment segment) {
- return DataSourceConnectionUrlUtil.getUrl(segment, databaseType);
- }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/DataSourceConnectionUrlUtil.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/DataSourceConnectionUrlUtil.java
deleted file mode 100644
index 02407ef..0000000
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/DataSourceConnectionUrlUtil.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.binder.statement.rdl.util;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-
-import org.apache.shardingsphere.distsql.parser.statement.rdl.DataSourceConnectionSegment;
-
-/**
- * Data source connection url util.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class DataSourceConnectionUrlUtil {
-
- /**
- * Get URL.
- *
- * @param connectionSegment connection segment
- * @param databaseType database type
- * @return URL
- */
- public static String getUrl(final DataSourceConnectionSegment connectionSegment, final DatabaseType databaseType) {
- return getUrl(connectionSegment, databaseType.getJdbcUrlPrefixes().iterator().next());
- }
-
- private static String getUrl(final DataSourceConnectionSegment connectionSegment, final String jdbcUrlPrefix) {
- return String.format("%s//%s:%s/%s", jdbcUrlPrefix, connectionSegment.getHostName(), connectionSegment.getPort(), connectionSegment.getDb());
- }
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/ShardingAlgorithmPropertiesUtil.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/ShardingAlgorithmPropertiesUtil.java
index 9afc41d..878f56a 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/ShardingAlgorithmPropertiesUtil.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/ShardingAlgorithmPropertiesUtil.java
@@ -35,34 +35,37 @@ import java.util.Properties;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingAlgorithmPropertiesUtil {
- public static final Map<String, Collection<String>> TYPE_AND_PROPERTIES;
+ private static final Map<String, Collection<String>> TYPE_PROPERTIES_MAP;
static {
- TYPE_AND_PROPERTIES = new LinkedHashMap<>();
- TYPE_AND_PROPERTIES.put("MOD", Collections.singleton("sharding-count"));
- TYPE_AND_PROPERTIES.put("HASH_MOD", Collections.singleton("sharding-count"));
- TYPE_AND_PROPERTIES.put("VOLUME_RANGE", Arrays.asList("range-lower", "range-upper", "sharding-volume"));
- TYPE_AND_PROPERTIES.put("BOUNDARY_RANGE", Collections.singleton("sharding-ranges"));
+ TYPE_PROPERTIES_MAP = new LinkedHashMap<>(4, 1);
+ TYPE_PROPERTIES_MAP.put("MOD", Collections.singleton("sharding-count"));
+ TYPE_PROPERTIES_MAP.put("HASH_MOD", Collections.singleton("sharding-count"));
+ TYPE_PROPERTIES_MAP.put("VOLUME_RANGE", Arrays.asList("range-lower", "range-upper", "sharding-volume"));
+ TYPE_PROPERTIES_MAP.put("BOUNDARY_RANGE", Collections.singleton("sharding-ranges"));
}
/**
* Get properties.
*
- * @param shardingAlgorithmType sharding algorithm type
- * @param properties properties
+ * @param algorithmType sharding algorithm type
+ * @param algorithmProperties sharding algorithm properties
* @return properties
*/
- public static Properties getProperties(final String shardingAlgorithmType, final Collection<String> properties) {
- String algorithmType = shardingAlgorithmType.toUpperCase();
- Preconditions.checkArgument(TYPE_AND_PROPERTIES.containsKey(algorithmType), "Bad sharding algorithm type: %s.", algorithmType);
- Preconditions.checkArgument(TYPE_AND_PROPERTIES.get(algorithmType).size() == properties.size(),
- "%s needs %d properties, but %s properties are given.", algorithmType, TYPE_AND_PROPERTIES.get(algorithmType).size(), properties.size());
+ public static Properties getProperties(final String algorithmType, final Collection<String> algorithmProperties) {
+ validate(algorithmType, algorithmProperties);
Properties result = new Properties();
- Iterator<String> keys = TYPE_AND_PROPERTIES.get(algorithmType).iterator();
- Iterator<String> values = properties.iterator();
+ Iterator<String> keys = TYPE_PROPERTIES_MAP.get(algorithmType).iterator();
+ Iterator<String> values = algorithmProperties.iterator();
while (keys.hasNext()) {
result.setProperty(keys.next(), values.next());
}
return result;
}
+
+ private static void validate(final String algorithmType, final Collection<String> algorithmProperties) {
+ Preconditions.checkArgument(TYPE_PROPERTIES_MAP.containsKey(algorithmType), "Bad sharding algorithm type: %s.", algorithmType);
+ Preconditions.checkArgument(TYPE_PROPERTIES_MAP.get(algorithmType).size() == algorithmProperties.size(),
+ "%s needs %d properties, but %s properties are given.", algorithmType, TYPE_PROPERTIES_MAP.get(algorithmType).size(), algorithmProperties.size());
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/DataSourceConnectionUrlUtilTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/DataSourceConnectionUrlUtilTest.java
deleted file mode 100644
index bdee5d3..0000000
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/rdl/util/DataSourceConnectionUrlUtilTest.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.binder.statement.rdl.util;
-
-import org.apache.shardingsphere.distsql.parser.statement.rdl.DataSourceConnectionSegment;
-import org.apache.shardingsphere.infra.database.type.dialect.MariaDBDatabaseType;
-import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.infra.database.type.dialect.OracleDatabaseType;
-import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
-import org.apache.shardingsphere.infra.database.type.dialect.SQLServerDatabaseType;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class DataSourceConnectionUrlUtilTest {
-
- @Test
- public void assertMySQLGetUrl() {
- DataSourceConnectionSegment segment = buildDataSourceConnectionSegment();
- MySQLDatabaseType databaseType = new MySQLDatabaseType();
- String actual = DataSourceConnectionUrlUtil.getUrl(segment, databaseType);
- String expected = "jdbc:mysql://127.0.0.1:3306/test";
- assertThat(actual, is(expected));
- }
-
- @Test
- public void assertPostgreSQLGetUrl() {
- DataSourceConnectionSegment segment = buildDataSourceConnectionSegment();
- PostgreSQLDatabaseType databaseType = new PostgreSQLDatabaseType();
- String actual = DataSourceConnectionUrlUtil.getUrl(segment, databaseType);
- String expected = "jdbc:postgresql://127.0.0.1:3306/test";
- assertThat(actual, is(expected));
- }
-
- @Test
- public void assertMariaDBGetUrl() {
- DataSourceConnectionSegment segment = buildDataSourceConnectionSegment();
- MariaDBDatabaseType databaseType = new MariaDBDatabaseType();
- String actual = DataSourceConnectionUrlUtil.getUrl(segment, databaseType);
- String expected = "jdbc:mariadb://127.0.0.1:3306/test";
- assertThat(actual, is(expected));
- }
-
- @Test
- public void assertOracleGetUrl() {
- DataSourceConnectionSegment segment = buildDataSourceConnectionSegment();
- OracleDatabaseType databaseType = new OracleDatabaseType();
- String actual = DataSourceConnectionUrlUtil.getUrl(segment, databaseType);
- String expected = "jdbc:oracle://127.0.0.1:3306/test";
- assertThat(actual, is(expected));
- }
-
- @Test
- public void assertSQLServerGetUrl() {
- DataSourceConnectionSegment segment = buildDataSourceConnectionSegment();
- SQLServerDatabaseType databaseType = new SQLServerDatabaseType();
- String actual = DataSourceConnectionUrlUtil.getUrl(segment, databaseType);
- String expected = "jdbc:microsoft:sqlserver://127.0.0.1:3306/test";
- assertThat(actual, is(expected));
- }
-
- private DataSourceConnectionSegment buildDataSourceConnectionSegment() {
- DataSourceConnectionSegment result = new DataSourceConnectionSegment();
- result.setHostName("127.0.0.1");
- result.setDb("test");
- result.setUser("root");
- result.setPort("3306");
- return result;
- }
-}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java
index d0f2c6d..c85da06 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/converter/CreateDataSourcesStatementContextConverter.java
@@ -17,11 +17,12 @@
package org.apache.shardingsphere.proxy.converter;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.DataSourceConnectionSegment;
+import org.apache.shardingsphere.infra.binder.converter.SQLStatementContextConverter;
+import org.apache.shardingsphere.infra.binder.statement.rdl.CreateDataSourcesStatementContext;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
-import org.apache.shardingsphere.infra.binder.statement.rdl.CreateDataSourcesStatementContext;
-import org.apache.shardingsphere.infra.binder.converter.SQLStatementContextConverter;
-import org.apache.shardingsphere.distsql.parser.statement.rdl.DataSourceConnectionSegment;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -37,7 +38,7 @@ public final class CreateDataSourcesStatementContextConverter implements SQLStat
for (DataSourceConnectionSegment each : sqlStatementContext.getSqlStatement().getConnectionInfos()) {
DataSourceParameter parameter = new DataSourceParameter();
YamlDataSourceParameter dataSource = new YamlDataSourceParameter();
- dataSource.setUrl(sqlStatementContext.getUrl(each));
+ dataSource.setUrl(getURL(sqlStatementContext.getDatabaseType(), each));
dataSource.setUsername(each.getUser());
dataSource.setPassword(each.getPassword());
dataSource.setMinPoolSize(parameter.getMinPoolSize());
@@ -49,4 +50,8 @@ public final class CreateDataSourcesStatementContextConverter implements SQLStat
}
return result;
}
+
+ private String getURL(final DatabaseType databaseType, final DataSourceConnectionSegment connectionSegment) {
+ return String.format("%s//%s:%s/%s", databaseType.getJdbcUrlPrefixes().iterator().next(), connectionSegment.getHostName(), connectionSegment.getPort(), connectionSegment.getDb());
+ }
}