未验证 提交 2e4a458b 编写于 作者: L Liang Zhang 提交者: GitHub

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
上级 8130da3f
......@@ -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);
}
}
/*
* 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());
}
}
......@@ -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());
}
}
/*
* 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;
}
}
......@@ -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());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册