提交 981b8442 编写于 作者: T terrymanu

refactor

上级 487df317
......@@ -18,8 +18,8 @@
package com.dangdang.ddframe.rdb.sharding.api.strategy.common;
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType;
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
......@@ -46,21 +46,16 @@ public class ShardingStrategy {
/**
* 根据分片值计算数据源名称集合.
*
*
* @param sqlStatementType SQL语句的类型
* @param availableTargetNames 所有的可用数据源名称集合
* @param shardingValues 分库片值集合
* @return 分库后指向的数据源名称集合
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public Collection<String> doSharding(final SQLStatementType sqlStatementType, final Collection<String> availableTargetNames,
final Collection<ShardingValue<?>> shardingValues) {
public Collection<String> doSharding(final SQLStatementType sqlStatementType, final Collection<String> availableTargetNames, final Collection<ShardingValue<?>> shardingValues) {
if (shardingValues.isEmpty()) {
if (SQLStatementType.INSERT.equals(sqlStatementType) && availableTargetNames.size() > 1) {
throw new ShardingJdbcException("INSERT statement must contains sharding value");
} else {
return availableTargetNames;
}
Preconditions.checkState(!isInsertMultiple(sqlStatementType, availableTargetNames), "INSERT statement should contain sharding value.");
return availableTargetNames;
}
if (shardingAlgorithm instanceof SingleKeyShardingAlgorithm) {
SingleKeyShardingAlgorithm<?> singleKeyShardingAlgorithm = (SingleKeyShardingAlgorithm<?>) shardingAlgorithm;
......@@ -81,4 +76,8 @@ public class ShardingStrategy {
}
throw new UnsupportedOperationException(shardingAlgorithm.getClass().getName());
}
private boolean isInsertMultiple(final SQLStatementType sqlStatementType, final Collection<String> availableTargetNames) {
return SQLStatementType.INSERT.equals(sqlStatementType) && availableTargetNames.size() > 1;
}
}
......@@ -23,5 +23,6 @@ package com.dangdang.ddframe.rdb.sharding.parser.result.router;
* @author gaohongtao
*/
public enum SQLStatementType {
INSERT, DELETE, UPDATE, SELECT
}
......@@ -19,6 +19,7 @@ package com.dangdang.ddframe.rdb;
import com.dangdang.ddframe.rdb.integrate.AllIntegrateTests;
import com.dangdang.ddframe.rdb.sharding.api.AllApiTest;
import com.dangdang.ddframe.rdb.sharding.constants.AllConstantsTest;
import com.dangdang.ddframe.rdb.sharding.hint.AllHintTest;
import com.dangdang.ddframe.rdb.sharding.jdbc.AllJDBCTest;
import com.dangdang.ddframe.rdb.sharding.merger.AllMergerTest;
......@@ -32,6 +33,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
AllConstantsTest.class,
AllApiTest.class,
AllParserTest.class,
AllRouterTest.class,
......
......@@ -29,13 +29,11 @@ import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingS
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategyTest;
import com.dangdang.ddframe.rdb.sharding.constants.DatabaseTypeTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
DatabaseTypeTest.class,
ShardingPropertiesTest.class,
ShardingPropertiesConstantTest.class,
ShardingDataSourceTest.class,
......
package com.dangdang.ddframe.rdb.sharding.api.rule.fixture;
import java.sql.Connection;
import java.sql.SQLException;
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractDataSourceAdapter;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import java.sql.Connection;
import java.sql.SQLException;
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = false)
@ToString
public final class TestDataSource extends AbstractDataSourceAdapter {
private final String name;
......
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.constants;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses(DatabaseTypeTest.class)
public class AllConstantsTest {
}
......@@ -17,16 +17,15 @@
package com.dangdang.ddframe.rdb.sharding.router;
import java.util.Arrays;
import java.util.Collections;
import com.dangdang.ddframe.rdb.sharding.exception.SQLParserException;
import com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException;
import com.google.common.collect.Lists;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays;
import java.util.Collections;
public final class DMLTest extends AbstractDynamicRouteSqlTest {
@Rule
......@@ -42,8 +41,8 @@ public final class DMLTest extends AbstractDynamicRouteSqlTest {
@Test
public void assertInsertError() {
expectedException.expect(ShardingJdbcException.class);
expectedException.expectMessage("INSERT statement must contains sharding value");
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("INSERT statement should contain sharding value.");
assertSingleTarget("insert into `order` value (1,'test')", null, null);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册