未验证 提交 275956fc 编写于 作者: L Liang Zhang 提交者: GitHub

Merge pull request #2479 from tristaZero/dev

Store encryptRule from ShardingRule
......@@ -33,6 +33,10 @@ public final class EncryptRule implements BaseRule {
private final ShardingEncryptorEngine encryptorEngine;
public EncryptRule() {
encryptorEngine = new ShardingEncryptorEngine();
}
public EncryptRule(final EncryptRuleConfiguration encryptRuleConfiguration) {
encryptorEngine = new ShardingEncryptorEngine(encryptRuleConfiguration);
}
......
......@@ -30,7 +30,6 @@ import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.ShardingStrategyConfiguration;
import org.apache.shardingsphere.core.exception.ShardingConfigurationException;
import org.apache.shardingsphere.core.spi.algorithm.keygen.ShardingKeyGeneratorServiceLoader;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategy;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategyFactory;
import org.apache.shardingsphere.core.strategy.route.hint.HintShardingStrategy;
......@@ -71,7 +70,7 @@ public class ShardingRule implements BaseRule {
private final Collection<MasterSlaveRule> masterSlaveRules;
private final ShardingEncryptorEngine shardingEncryptorEngine;
private final EncryptRule encryptRule;
public ShardingRule(final ShardingRuleConfiguration shardingRuleConfig, final Collection<String> dataSourceNames) {
Preconditions.checkArgument(!dataSourceNames.isEmpty(), "Data sources cannot be empty.");
......@@ -84,7 +83,7 @@ public class ShardingRule implements BaseRule {
defaultTableShardingStrategy = createDefaultShardingStrategy(shardingRuleConfig.getDefaultTableShardingStrategyConfig());
defaultShardingKeyGenerator = createDefaultKeyGenerator(shardingRuleConfig.getDefaultKeyGeneratorConfig());
masterSlaveRules = createMasterSlaveRules(shardingRuleConfig.getMasterSlaveRuleConfigs());
shardingEncryptorEngine = createShardingEncryptorEngine(shardingRuleConfig.getEncryptRuleConfig());
encryptRule = createEncryptRule(shardingRuleConfig.getEncryptRuleConfig());
}
private Collection<TableRule> createTableRules(final ShardingRuleConfiguration shardingRuleConfig) {
......@@ -138,8 +137,8 @@ public class ShardingRule implements BaseRule {
return result;
}
private ShardingEncryptorEngine createShardingEncryptorEngine(final EncryptRuleConfiguration encryptRuleConfig) {
return null == encryptRuleConfig ? new ShardingEncryptorEngine() : new ShardingEncryptorEngine(shardingRuleConfig.getEncryptRuleConfig());
private EncryptRule createEncryptRule(final EncryptRuleConfiguration encryptRuleConfig) {
return null == encryptRuleConfig ? new EncryptRule() : new EncryptRule(shardingRuleConfig.getEncryptRuleConfig());
}
/**
......
......@@ -104,7 +104,7 @@ public abstract class BaseShardingEngine {
private Collection<RouteUnit> rewriteAndConvert(final List<Object> parameters, final SQLRouteResult sqlRouteResult) {
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, sqlRouteResult.getSqlStatement(), parameters);
ShardingSQLRewriter shardingSQLRewriter = new ShardingSQLRewriter(shardingRule, databaseType, sqlRouteResult);
EncryptSQLRewriter encryptSQLRewriter = new EncryptSQLRewriter(shardingRule.getShardingEncryptorEngine(), sqlRouteResult.getSqlStatement(), sqlRouteResult.getOptimizeResult());
EncryptSQLRewriter encryptSQLRewriter = new EncryptSQLRewriter(shardingRule.getEncryptRule().getEncryptorEngine(), sqlRouteResult.getSqlStatement(), sqlRouteResult.getOptimizeResult());
rewriteEngine.init(shardingSQLRewriter, encryptSQLRewriter);
Collection<RouteUnit> result = new LinkedHashSet<>();
for (RoutingUnit each : sqlRouteResult.getRoutingResult().getRoutingUnits()) {
......
......@@ -23,7 +23,9 @@ import org.apache.shardingsphere.core.metadata.ShardingMetaData;
import org.apache.shardingsphere.core.parse.cache.ParsingResultCache;
import org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -51,7 +53,11 @@ public final class PreparedQueryShardingEngineTest extends BaseShardingEngineTes
@Before
public void setUp() {
shardingEngine = new PreparedQueryShardingEngine(getSql(), mock(ShardingRule.class), getShardingProperties(), mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache());
ShardingRule shardingRule = mock(ShardingRule.class);
EncryptRule encryptRule = mock(EncryptRule.class);
when(encryptRule.getEncryptorEngine()).thenReturn(new ShardingEncryptorEngine());
when(shardingRule.getEncryptRule()).thenReturn(encryptRule);
shardingEngine = new PreparedQueryShardingEngine(getSql(), shardingRule, getShardingProperties(), mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache());
setRoutingEngine();
}
......
......@@ -23,7 +23,9 @@ import org.apache.shardingsphere.core.metadata.ShardingMetaData;
import org.apache.shardingsphere.core.parse.cache.ParsingResultCache;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.route.StatementRoutingEngine;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
......@@ -49,7 +51,11 @@ public final class SimpleQueryShardingEngineTest extends BaseShardingEngineTest
@Before
public void setUp() {
shardingEngine = new SimpleQueryShardingEngine(mock(ShardingRule.class), getShardingProperties(), mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache());
ShardingRule shardingRule = mock(ShardingRule.class);
EncryptRule encryptRule = mock(EncryptRule.class);
when(encryptRule.getEncryptorEngine()).thenReturn(new ShardingEncryptorEngine());
when(shardingRule.getEncryptRule()).thenReturn(encryptRule);
shardingEngine = new SimpleQueryShardingEngine(shardingRule, getShardingProperties(), mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache());
setRoutingEngine();
}
......
......@@ -19,6 +19,7 @@ package org.apache.shardingsphere.core.execute.sql.execute.result;
import com.google.common.base.Optional;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.rule.TableRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
......@@ -48,7 +49,7 @@ public class QueryResultMetaDataTest {
public void setUp() {
ResultSetMetaData resultSetMetaData = getResultMetaData();
ShardingRule shardingRule = getShardingRule();
queryResultMetaData = new QueryResultMetaData(resultSetMetaData, shardingRule, shardingRule.getShardingEncryptorEngine());
queryResultMetaData = new QueryResultMetaData(resultSetMetaData, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine());
}
@SuppressWarnings("unchecked")
......@@ -57,7 +58,9 @@ public class QueryResultMetaDataTest {
ShardingEncryptorEngine shardingEncryptorEngine = mock(ShardingEncryptorEngine.class);
when(shardingEncryptorEngine.getShardingEncryptor(anyString(), anyString())).thenReturn(Optional.of(shardingEncryptor));
ShardingRule result = mock(ShardingRule.class);
when(result.getShardingEncryptorEngine()).thenReturn(shardingEncryptorEngine);
EncryptRule encryptRule = mock(EncryptRule.class);
when(encryptRule.getEncryptorEngine()).thenReturn(shardingEncryptorEngine);
when(result.getEncryptRule()).thenReturn(encryptRule);
when(result.getLogicTableNames(anyString())).thenReturn(Collections.<String>emptyList());
when(result.findTableRuleByActualTable("table")).thenReturn(Optional.<TableRule>absent());
return result;
......
......@@ -114,7 +114,7 @@ public final class InsertOptimizeEngine implements OptimizeEngine {
result += 1;
}
if (isNeededToAppendQueryAssistedColumn()) {
result += shardingRule.getShardingEncryptorEngine().getAssistedQueryColumnCount(insertStatement.getTables().getSingleTableName());
result += shardingRule.getEncryptRule().getEncryptorEngine().getAssistedQueryColumnCount(insertStatement.getTables().getSingleTableName());
}
return result;
}
......@@ -154,14 +154,14 @@ public final class InsertOptimizeEngine implements OptimizeEngine {
}
private boolean isNeededToAppendQueryAssistedColumn() {
return shardingRule.getShardingEncryptorEngine().isHasShardingQueryAssistedEncryptor(insertStatement.getTables().getSingleTableName());
return shardingRule.getEncryptRule().getEncryptorEngine().isHasShardingQueryAssistedEncryptor(insertStatement.getTables().getSingleTableName());
}
private void fillWithQueryAssistedColumn(final InsertOptimizeResult insertOptimizeResult, final int insertOptimizeResultIndex) {
Collection<String> assistedColumnNames = new LinkedList<>();
for (String each : insertOptimizeResult.getColumnNames()) {
InsertOptimizeResultUnit unit = insertOptimizeResult.getUnits().get(insertOptimizeResultIndex);
Optional<String> assistedColumnName = shardingRule.getShardingEncryptorEngine().getAssistedQueryColumn(insertStatement.getTables().getSingleTableName(), each);
Optional<String> assistedColumnName = shardingRule.getEncryptRule().getEncryptorEngine().getAssistedQueryColumn(insertStatement.getTables().getSingleTableName(), each);
if (assistedColumnName.isPresent()) {
assistedColumnNames.add(assistedColumnName.get());
fillInsertOptimizeResultUnit(unit, (Comparable<?>) unit.getColumnValue(each));
......
......@@ -113,10 +113,10 @@ public final class InsertOptimizeEngineTest {
insertValuesStatementWithPlaceholder.getTables().add(new Table("t_order", null));
insertValuesStatementWithPlaceholder.setParametersIndex(4);
AndCondition andCondition1 = new AndCondition();
andCondition1.getConditions().add(new Condition(new Column("user_id", "t_order"), new ParameterMarkerExpressionSegment(0, 0, 0)));
andCondition1.getConditions().add(new Condition(new Column("user_id", "t_order"), null, new ParameterMarkerExpressionSegment(0, 0, 0)));
insertValuesStatementWithPlaceholder.getRouteCondition().getOrConditions().add(andCondition1);
AndCondition andCondition2 = new AndCondition();
andCondition2.getConditions().add(new Condition(new Column("user_id", "t_order"), new ParameterMarkerExpressionSegment(0, 0, 2)));
andCondition2.getConditions().add(new Condition(new Column("user_id", "t_order"), null, new ParameterMarkerExpressionSegment(0, 0, 2)));
insertValuesStatementWithPlaceholder.getRouteCondition().getOrConditions().add(andCondition2);
insertValuesStatementWithPlaceholder.getColumnNames().add("user_id");
insertValuesStatementWithPlaceholder.getColumnNames().add("status");
......@@ -130,10 +130,10 @@ public final class InsertOptimizeEngineTest {
insertValuesStatementWithPlaceholderWithEncrypt.getTables().add(new Table("t_encrypt", null));
insertValuesStatementWithPlaceholderWithEncrypt.setParametersIndex(4);
AndCondition andCondition1 = new AndCondition();
andCondition1.getConditions().add(new Condition(new Column("user_id", "t_encrypt"), new ParameterMarkerExpressionSegment(0, 0, 0)));
andCondition1.getConditions().add(new Condition(new Column("user_id", "t_encrypt"), null, new ParameterMarkerExpressionSegment(0, 0, 0)));
insertValuesStatementWithPlaceholderWithEncrypt.getRouteCondition().getOrConditions().add(andCondition1);
AndCondition andCondition2 = new AndCondition();
andCondition2.getConditions().add(new Condition(new Column("user_id", "t_encrypt"), new ParameterMarkerExpressionSegment(0, 0, 2)));
andCondition2.getConditions().add(new Condition(new Column("user_id", "t_encrypt"), null, new ParameterMarkerExpressionSegment(0, 0, 2)));
insertValuesStatementWithPlaceholderWithEncrypt.getRouteCondition().getOrConditions().add(andCondition2);
insertValuesStatementWithPlaceholderWithEncrypt.getColumnNames().add("user_id");
insertValuesStatementWithPlaceholderWithEncrypt.getColumnNames().add("status");
......@@ -147,7 +147,7 @@ public final class InsertOptimizeEngineTest {
insertValuesStatementWithoutPlaceholder.getTables().add(new Table("t_order", null));
insertValuesStatementWithoutPlaceholder.setParametersIndex(0);
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(new Condition(new Column("user_id", "t_order"), new LiteralExpressionSegment(0, 0, 12)));
andCondition.getConditions().add(new Condition(new Column("user_id", "t_order"), null, new LiteralExpressionSegment(0, 0, 12)));
insertValuesStatementWithoutPlaceholder.getRouteCondition().getOrConditions().add(andCondition);
}
......@@ -156,7 +156,7 @@ public final class InsertOptimizeEngineTest {
insertValuesStatementWithoutPlaceholderWithQueryEncrypt.getTables().add(new Table("t_encrypt_query", null));
insertValuesStatementWithoutPlaceholderWithQueryEncrypt.setParametersIndex(0);
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(new Condition(new Column("user_id", "t_encrypt_query"), new LiteralExpressionSegment(0, 0, 12)));
andCondition.getConditions().add(new Condition(new Column("user_id", "t_encrypt_query"), null, new LiteralExpressionSegment(0, 0, 12)));
insertValuesStatementWithoutPlaceholderWithQueryEncrypt.getRouteCondition().getOrConditions().add(andCondition);
}
......@@ -167,7 +167,7 @@ public final class InsertOptimizeEngineTest {
insertSetStatementWithPlaceholder.getColumnNames().add("user_id");
insertSetStatementWithPlaceholder.getColumnNames().add("status");
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(new Condition(new Column("user_id", "t_order"), new LiteralExpressionSegment(0, 0, 12)));
andCondition.getConditions().add(new Condition(new Column("user_id", "t_order"), null, new LiteralExpressionSegment(0, 0, 12)));
insertSetStatementWithPlaceholder.getRouteCondition().getOrConditions().add(andCondition);
}
......@@ -178,7 +178,7 @@ public final class InsertOptimizeEngineTest {
insertSetStatementWithPlaceholderWithQueryEncrypt.getColumnNames().add("user_id");
insertSetStatementWithPlaceholderWithQueryEncrypt.getColumnNames().add("status");
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(new Condition(new Column("user_id", "t_encrypt_query"), new LiteralExpressionSegment(0, 0, 12)));
andCondition.getConditions().add(new Condition(new Column("user_id", "t_encrypt_query"), null, new LiteralExpressionSegment(0, 0, 12)));
insertSetStatementWithPlaceholderWithQueryEncrypt.getRouteCondition().getOrConditions().add(andCondition);
}
......@@ -189,7 +189,7 @@ public final class InsertOptimizeEngineTest {
insertSetStatementWithoutPlaceholder.getColumnNames().add("user_id");
insertSetStatementWithoutPlaceholder.getColumnNames().add("status");
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(new Condition(new Column("user_id", "t_order"), new LiteralExpressionSegment(0, 0, 12)));
andCondition.getConditions().add(new Condition(new Column("user_id", "t_order"), null, new LiteralExpressionSegment(0, 0, 12)));
insertSetStatementWithoutPlaceholder.getRouteCondition().getOrConditions().add(andCondition);
}
......@@ -200,7 +200,7 @@ public final class InsertOptimizeEngineTest {
insertSetStatementWithoutPlaceholderWithEncrypt.getColumnNames().add("user_id");
insertSetStatementWithoutPlaceholderWithEncrypt.getColumnNames().add("status");
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(new Condition(new Column("user_id", "t_encrypt"), new LiteralExpressionSegment(0, 0, 12)));
andCondition.getConditions().add(new Condition(new Column("user_id", "t_encrypt"), null, new LiteralExpressionSegment(0, 0, 12)));
insertSetStatementWithoutPlaceholderWithEncrypt.getRouteCondition().getOrConditions().add(andCondition);
}
......
......@@ -46,8 +46,8 @@ public final class QueryOptimizeEngineTest {
@Test
public void assertOptimizeAlwaysFalseListConditions() {
Condition condition1 = new Condition(new Column("column", "tbl"), Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 3));
Condition condition1 = new Condition(new Column("column", "tbl"), null, Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 3));
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(condition1);
andCondition.getConditions().add(condition2);
......@@ -59,8 +59,8 @@ public final class QueryOptimizeEngineTest {
@Test
public void assertOptimizeAlwaysFalseRangeConditions() {
Condition condition1 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2));
Condition condition2 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 3), new LiteralExpressionSegment(0, 0, 4));
Condition condition1 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2));
Condition condition2 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 3), new LiteralExpressionSegment(0, 0, 4));
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(condition1);
andCondition.getConditions().add(condition2);
......@@ -72,8 +72,8 @@ public final class QueryOptimizeEngineTest {
@Test
public void assertOptimizeAlwaysFalseListConditionsAndRangeConditions() {
Condition condition1 = new Condition(new Column("column", "tbl"), Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 3), new LiteralExpressionSegment(0, 0, 4));
Condition condition1 = new Condition(new Column("column", "tbl"), null, Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 3), new LiteralExpressionSegment(0, 0, 4));
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(condition1);
andCondition.getConditions().add(condition2);
......@@ -86,8 +86,8 @@ public final class QueryOptimizeEngineTest {
@SuppressWarnings("unchecked")
@Test
public void assertOptimizeListConditions() {
Condition condition1 = new Condition(new Column("column", "tbl"), Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 1));
Condition condition1 = new Condition(new Column("column", "tbl"), null, Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 1));
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(condition1);
andCondition.getConditions().add(condition2);
......@@ -105,8 +105,8 @@ public final class QueryOptimizeEngineTest {
@SuppressWarnings("unchecked")
@Test
public void assertOptimizeRangeConditions() {
Condition condition1 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2));
Condition condition2 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 3));
Condition condition1 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2));
Condition condition2 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 3));
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(condition1);
andCondition.getConditions().add(condition2);
......@@ -124,8 +124,8 @@ public final class QueryOptimizeEngineTest {
@SuppressWarnings("unchecked")
@Test
public void assertOptimizeListConditionsAndRangeConditions() {
Condition condition1 = new Condition(new Column("column", "tbl"), Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2));
Condition condition1 = new Condition(new Column("column", "tbl"), null, Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)));
Condition condition2 = new Condition(new Column("column", "tbl"), null, new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2));
AndCondition andCondition = new AndCondition();
andCondition.getConditions().add(condition1);
andCondition.getConditions().add(condition2);
......
......@@ -41,6 +41,7 @@ import java.util.List;
* Predicate utils.
*
* @author zhangliang
* @author panjuan
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PredicateUtils {
......@@ -94,10 +95,12 @@ public final class PredicateUtils {
*
* @param compareRightValue right value of compare operator
* @param column column
* @param predicateSegment predicate segment
* @return condition
*/
public static Optional<Condition> createCompareCondition(final PredicateCompareRightValue compareRightValue, final Column column) {
return compareRightValue.getExpression() instanceof SimpleExpressionSegment ? Optional.of(new Condition(column, compareRightValue.getExpression())) : Optional.<Condition>absent();
public static Optional<Condition> createCompareCondition(final PredicateCompareRightValue compareRightValue, final Column column, final PredicateSegment predicateSegment) {
return compareRightValue.getExpression() instanceof SimpleExpressionSegment
? Optional.of(new Condition(column, predicateSegment, compareRightValue.getExpression())) : Optional.<Condition>absent();
}
/**
......@@ -105,9 +108,10 @@ public final class PredicateUtils {
*
* @param inRightValue right value of IN operator
* @param column column
* @param predicateSegment predicate segment
* @return condition
*/
public static Optional<Condition> createInCondition(final PredicateInRightValue inRightValue, final Column column) {
public static Optional<Condition> createInCondition(final PredicateInRightValue inRightValue, final Column column, final PredicateSegment predicateSegment) {
List<ExpressionSegment> expressionSegments = new LinkedList<>();
for (ExpressionSegment each : inRightValue.getSqlExpressions()) {
if (each instanceof SimpleExpressionSegment) {
......@@ -116,7 +120,7 @@ public final class PredicateUtils {
return Optional.absent();
}
}
return expressionSegments.isEmpty() ? Optional.<Condition>absent() : Optional.of(new Condition(column, expressionSegments));
return expressionSegments.isEmpty() ? Optional.<Condition>absent() : Optional.of(new Condition(column, predicateSegment, expressionSegments));
}
/**
......@@ -124,10 +128,11 @@ public final class PredicateUtils {
*
* @param betweenRightValue right value of BETWEEN operator
* @param column column
* @param predicateSegment predicate segment
* @return condition
*/
public static Optional<Condition> createBetweenCondition(final PredicateBetweenRightValue betweenRightValue, final Column column) {
public static Optional<Condition> createBetweenCondition(final PredicateBetweenRightValue betweenRightValue, final Column column, final PredicateSegment predicateSegment) {
return betweenRightValue.getBetweenExpression() instanceof SimpleExpressionSegment && betweenRightValue.getAndExpression() instanceof SimpleExpressionSegment
? Optional.of(new Condition(column, betweenRightValue.getBetweenExpression(), betweenRightValue.getAndExpression())) : Optional.<Condition>absent();
? Optional.of(new Condition(column, predicateSegment, betweenRightValue.getBetweenExpression(), betweenRightValue.getAndExpression())) : Optional.<Condition>absent();
}
}
......@@ -35,7 +35,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.Pred
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateCompareRightValue;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateInRightValue;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
......@@ -85,7 +84,6 @@ public final class EncryptOrPredicateFiller implements SQLSegmentFiller<OrPredic
andCondition = sqlStatement.getEncryptCondition().getOrConditions().get(0);
}
andCondition.getConditions().add(condition.get());
sqlStatement.getSQLTokens().add(new EncryptColumnToken(predicateSegment.getColumn().getStartIndex(), predicateSegment.getStopIndex(), column, true));
}
}
......@@ -95,10 +93,11 @@ public final class EncryptOrPredicateFiller implements SQLSegmentFiller<OrPredic
}
if (predicateSegment.getRightValue() instanceof PredicateCompareRightValue) {
PredicateCompareRightValue compareRightValue = (PredicateCompareRightValue) predicateSegment.getRightValue();
return isOperatorSupportedWithEncrypt(compareRightValue.getOperator()) ? PredicateUtils.createCompareCondition(compareRightValue, column) : Optional.<Condition>absent();
return isOperatorSupportedWithEncrypt(compareRightValue.getOperator())
? PredicateUtils.createCompareCondition(compareRightValue, column, predicateSegment) : Optional.<Condition>absent();
}
if (predicateSegment.getRightValue() instanceof PredicateInRightValue) {
return PredicateUtils.createInCondition((PredicateInRightValue) predicateSegment.getRightValue(), column);
return PredicateUtils.createInCondition((PredicateInRightValue) predicateSegment.getRightValue(), column, predicateSegment);
}
return Optional.absent();
}
......
......@@ -30,7 +30,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.Paramete
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertSetAddItemsToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertSetEncryptValueToken;
import org.apache.shardingsphere.core.rule.EncryptRule;
......@@ -116,8 +115,5 @@ public final class EncryptSetAssignmentsFiller implements SQLSegmentFiller<SetAs
private void fillEncryptCondition(final AssignmentSegment assignment, final String tableName, final UpdateStatement updateStatement) {
Column column = new Column(assignment.getColumn().getName(), tableName);
updateStatement.getAssignments().put(column, assignment.getValue());
if (encryptRule.getEncryptorEngine().getShardingEncryptor(column.getTableName(), column.getName()).isPresent()) {
updateStatement.getSQLTokens().add(new EncryptColumnToken(assignment.getColumn().getStartIndex(), assignment.getValue().getStopIndex(), column, false));
}
}
}
......@@ -45,6 +45,7 @@ import org.apache.shardingsphere.core.rule.ShardingRule;
*
* @author duhongjun
* @author zhangliang
* @author panjuan
*/
@Setter
public final class ShardingOrPredicateFiller implements SQLSegmentFiller<OrPredicateSegment>, ShardingRuleAwareFiller, ShardingTableMetaDataAwareFiller {
......@@ -78,7 +79,7 @@ public final class ShardingOrPredicateFiller implements SQLSegmentFiller<OrPredi
private EncryptOrPredicateFiller createEncryptOrPredicateFiller() {
EncryptOrPredicateFiller result = new EncryptOrPredicateFiller();
result.setEncryptorEngine(shardingRule.getShardingEncryptorEngine());
result.setEncryptorEngine(shardingRule.getEncryptRule().getEncryptorEngine());
result.setShardingTableMetaData(shardingTableMetaData);
return result;
}
......@@ -109,13 +110,14 @@ public final class ShardingOrPredicateFiller implements SQLSegmentFiller<OrPredi
private Optional<Condition> createCondition(final PredicateSegment predicateSegment, final Column column) {
if (predicateSegment.getRightValue() instanceof PredicateCompareRightValue) {
PredicateCompareRightValue compareRightValue = (PredicateCompareRightValue) predicateSegment.getRightValue();
return isOperatorSupportedWithSharding(compareRightValue.getOperator()) ? PredicateUtils.createCompareCondition(compareRightValue, column) : Optional.<Condition>absent();
return isOperatorSupportedWithSharding(compareRightValue.getOperator())
? PredicateUtils.createCompareCondition(compareRightValue, column, predicateSegment) : Optional.<Condition>absent();
}
if (predicateSegment.getRightValue() instanceof PredicateInRightValue) {
return PredicateUtils.createInCondition((PredicateInRightValue) predicateSegment.getRightValue(), column);
return PredicateUtils.createInCondition((PredicateInRightValue) predicateSegment.getRightValue(), column, predicateSegment);
}
if (predicateSegment.getRightValue() instanceof PredicateBetweenRightValue) {
return PredicateUtils.createBetweenCondition((PredicateBetweenRightValue) predicateSegment.getRightValue(), column);
return PredicateUtils.createBetweenCondition((PredicateBetweenRightValue) predicateSegment.getRightValue(), column, predicateSegment);
}
return Optional.absent();
}
......
......@@ -96,7 +96,7 @@ public final class ShardingInsertColumnsFiller implements SQLSegmentFiller<Inser
private void fillWithQueryAssistedColumn(final InsertStatement insertStatement, final InsertColumnsToken insertColumnsToken) {
for (String each : insertStatement.getColumnNames()) {
Optional<String> assistedColumnName = shardingRule.getShardingEncryptorEngine().getAssistedQueryColumn(insertStatement.getTables().getSingleTableName(), each);
Optional<String> assistedColumnName = shardingRule.getEncryptRule().getEncryptorEngine().getAssistedQueryColumn(insertStatement.getTables().getSingleTableName(), each);
if (assistedColumnName.isPresent()) {
insertColumnsToken.getColumns().remove(assistedColumnName.get());
insertColumnsToken.getColumns().add(assistedColumnName.get());
......
......@@ -28,6 +28,7 @@ import org.apache.shardingsphere.core.parse.sql.context.insertvalue.InsertValue;
import org.apache.shardingsphere.core.parse.sql.segment.dml.InsertValuesSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.SimpleExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertColumnsToken;
......@@ -57,7 +58,7 @@ public final class ShardingInsertValuesFiller implements SQLSegmentFiller<Insert
Iterator<String> columnNames = getColumnNames(sqlSegment, insertStatement);
for (ExpressionSegment each : sqlSegment.getValues()) {
if (each instanceof SimpleExpressionSegment) {
fillShardingCondition(andCondition, insertStatement.getTables().getSingleTableName(), columnNames.next(), (SimpleExpressionSegment) each);
fillShardingCondition(andCondition, insertStatement.getTables().getSingleTableName(), columnNames.next(), null, (SimpleExpressionSegment) each);
}
}
insertStatement.getRouteCondition().getOrConditions().add(andCondition);
......@@ -70,7 +71,7 @@ public final class ShardingInsertValuesFiller implements SQLSegmentFiller<Insert
private Iterator<String> getColumnNames(final InsertValuesSegment sqlSegment, final InsertStatement insertStatement) {
Collection<String> result = new ArrayList<>(insertStatement.getColumnNames());
result.removeAll(shardingRule.getShardingEncryptorEngine().getAssistedQueryColumns(insertStatement.getTables().getSingleTableName()));
result.removeAll(shardingRule.getEncryptRule().getEncryptorEngine().getAssistedQueryColumns(insertStatement.getTables().getSingleTableName()));
Optional<String> generateKeyColumnName = shardingRule.findGenerateKeyColumnName(insertStatement.getTables().getSingleTableName());
if (insertStatement.getColumnNames().size() != sqlSegment.getValues().size() && generateKeyColumnName.isPresent()) {
result.remove(generateKeyColumnName.get());
......@@ -78,9 +79,10 @@ public final class ShardingInsertValuesFiller implements SQLSegmentFiller<Insert
return result.iterator();
}
private void fillShardingCondition(final AndCondition andCondition, final String tableName, final String columnName, final SimpleExpressionSegment expressionSegment) {
private void fillShardingCondition(final AndCondition andCondition, final String tableName,
final String columnName, final PredicateSegment predicateSegment, final SimpleExpressionSegment expressionSegment) {
if (shardingRule.isShardingColumn(columnName, tableName)) {
andCondition.getConditions().add(new Condition(new Column(columnName, tableName), expressionSegment));
andCondition.getConditions().add(new Condition(new Column(columnName, tableName), predicateSegment, expressionSegment));
}
}
......@@ -98,7 +100,7 @@ public final class ShardingInsertValuesFiller implements SQLSegmentFiller<Insert
private void reviseInsertColumnNames(final InsertValuesSegment sqlSegment, final InsertStatement insertStatement) {
Collection<String> result = new ArrayList<>(insertStatement.getColumnNames());
result.removeAll(shardingRule.getShardingEncryptorEngine().getAssistedQueryColumns(insertStatement.getTables().getSingleTableName()));
result.removeAll(shardingRule.getEncryptRule().getEncryptorEngine().getAssistedQueryColumns(insertStatement.getTables().getSingleTableName()));
Optional<String> generateKeyColumnName = shardingRule.findGenerateKeyColumnName(insertStatement.getTables().getSingleTableName());
if (insertStatement.getColumnNames().size() != sqlSegment.getValues().size() && generateKeyColumnName.isPresent()) {
result.remove(generateKeyColumnName.get());
......
......@@ -33,10 +33,10 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssign
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.SimpleExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertSetAddItemsToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertSetEncryptValueToken;
import org.apache.shardingsphere.core.rule.ShardingRule;
......@@ -52,6 +52,7 @@ import java.util.List;
* Set assignments filler.
*
* @author zhangliang
* @author panjuan
*/
@Setter
public final class ShardingSetAssignmentsFiller implements SQLSegmentFiller<SetAssignmentsSegment>, ShardingRuleAwareFiller, ShardingTableMetaDataAwareFiller {
......@@ -82,7 +83,7 @@ public final class ShardingSetAssignmentsFiller implements SQLSegmentFiller<SetA
List<ExpressionSegment> columnValues = new LinkedList<>();
for (AssignmentSegment each : sqlSegment.getAssignments()) {
if (each.getValue() instanceof SimpleExpressionSegment) {
fillShardingCondition(andCondition, columnNames.next(), insertStatement.getTables().getSingleTableName(), (SimpleExpressionSegment) each.getValue());
fillShardingCondition(andCondition, columnNames.next(), insertStatement.getTables().getSingleTableName(), null, (SimpleExpressionSegment) each.getValue());
}
columnValues.add(each.getValue());
fillWithInsertSetEncryptValueToken(insertStatement, each, each.getValue());
......@@ -95,7 +96,8 @@ public final class ShardingSetAssignmentsFiller implements SQLSegmentFiller<SetA
}
private void fillWithInsertSetEncryptValueToken(final InsertStatement insertStatement, final AssignmentSegment segment, final ExpressionSegment expressionSegment) {
Optional<ShardingEncryptor> shardingEncryptor = shardingRule.getShardingEncryptorEngine().getShardingEncryptor(insertStatement.getTables().getSingleTableName(), segment.getColumn().getName());
Optional<ShardingEncryptor> shardingEncryptor =
shardingRule.getEncryptRule().getEncryptorEngine().getShardingEncryptor(insertStatement.getTables().getSingleTableName(), segment.getColumn().getName());
if (shardingEncryptor.isPresent() && !(expressionSegment instanceof ParameterMarkerExpressionSegment)) {
insertStatement.getSQLTokens().add(new InsertSetEncryptValueToken(segment.getValue().getStartIndex(), segment.getValue().getStopIndex(), segment.getColumn().getName()));
}
......@@ -122,7 +124,7 @@ public final class ShardingSetAssignmentsFiller implements SQLSegmentFiller<SetA
private Collection<String> getQueryAssistedColumn(final InsertStatement insertStatement) {
Collection<String> result = new LinkedList<>();
for (String each : insertStatement.getColumnNames()) {
Optional<String> assistedColumnName = shardingRule.getShardingEncryptorEngine().getAssistedQueryColumn(insertStatement.getTables().getSingleTableName(), each);
Optional<String> assistedColumnName = shardingRule.getEncryptRule().getEncryptorEngine().getAssistedQueryColumn(insertStatement.getTables().getSingleTableName(), each);
if (assistedColumnName.isPresent()) {
result.add(assistedColumnName.get());
}
......@@ -135,21 +137,20 @@ public final class ShardingSetAssignmentsFiller implements SQLSegmentFiller<SetA
if (shardingTableMetaData.containsTable(tableName) && shardingTableMetaData.get(tableName).getColumns().size() == insertStatement.getColumnNames().size()) {
return insertStatement.getColumnNames().size();
}
Integer assistedQueryColumnCount = shardingRule.getShardingEncryptorEngine().getAssistedQueryColumnCount(insertStatement.getTables().getSingleTableName());
Integer assistedQueryColumnCount = shardingRule.getEncryptRule().getEncryptorEngine().getAssistedQueryColumnCount(insertStatement.getTables().getSingleTableName());
return insertStatement.getColumnNames().size() - assistedQueryColumnCount;
}
private void fillShardingCondition(final AndCondition andCondition, final String columnName, final String tableName, final SimpleExpressionSegment simpleExpressionSegment) {
private void fillShardingCondition(final AndCondition andCondition,
final String columnName, final String tableName, final PredicateSegment predicateSegment, final SimpleExpressionSegment simpleExpressionSegment) {
if (shardingRule.isShardingColumn(columnName, tableName)) {
andCondition.getConditions().add(new Condition(new Column(columnName, tableName), simpleExpressionSegment));
andCondition.getConditions().add(new Condition(new Column(columnName, tableName), predicateSegment, simpleExpressionSegment));
}
}
private void fillUpdate(final SetAssignmentsSegment sqlSegment, final UpdateStatement updateStatement) {
String tableName = updateStatement.getTables().getSingleTableName();
for (AssignmentSegment each : sqlSegment.getAssignments()) {
Column column = new Column(each.getColumn().getName(), tableName);
updateStatement.getAssignments().put(column, each.getValue());
fillEncryptCondition(each, tableName, updateStatement);
}
}
......@@ -157,8 +158,5 @@ public final class ShardingSetAssignmentsFiller implements SQLSegmentFiller<SetA
private void fillEncryptCondition(final AssignmentSegment assignment, final String tableName, final UpdateStatement updateStatement) {
Column column = new Column(assignment.getColumn().getName(), tableName);
updateStatement.getAssignments().put(column, assignment.getValue());
if (shardingRule.getShardingEncryptorEngine().getShardingEncryptor(column.getTableName(), column.getName()).isPresent()) {
updateStatement.getSQLTokens().add(new EncryptColumnToken(assignment.getColumn().getStartIndex(), assignment.getValue().getStopIndex(), column, false));
}
}
}
......@@ -28,6 +28,7 @@ import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import java.util.LinkedHashMap;
import java.util.LinkedList;
......@@ -43,12 +44,14 @@ import java.util.Map.Entry;
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@EqualsAndHashCode
@EqualsAndHashCode(exclude = {"predicateSegment"})
@ToString
public class Condition {
private final Column column;
private final PredicateSegment predicateSegment;
private final ShardingOperator operator;
@Setter
......@@ -61,15 +64,17 @@ public class Condition {
protected Condition() {
column = null;
operator = null;
predicateSegment = null;
}
public Condition(final Column column, final ExpressionSegment expressionSegment) {
this(column, ShardingOperator.EQUAL);
public Condition(final Column column, final PredicateSegment predicateSegment, final ExpressionSegment expressionSegment) {
this(column, predicateSegment, ShardingOperator.EQUAL);
init(expressionSegment, 0);
}
public Condition(final Column column, final String compareOperator, final ExpressionSegment expressionSegment) {
public Condition(final Column column, final PredicateSegment predicateSegment, final String compareOperator, final ExpressionSegment expressionSegment) {
this.column = column;
this.predicateSegment = predicateSegment;
this.compareOperator = compareOperator;
if ("=".equals(compareOperator)) {
operator = ShardingOperator.EQUAL;
......@@ -79,14 +84,14 @@ public class Condition {
init(expressionSegment, 0);
}
public Condition(final Column column, final ExpressionSegment beginExpressionSegment, final ExpressionSegment endExpressionSegment) {
this(column, ShardingOperator.BETWEEN);
public Condition(final Column column, final PredicateSegment predicateSegment, final ExpressionSegment beginExpressionSegment, final ExpressionSegment endExpressionSegment) {
this(column, predicateSegment, ShardingOperator.BETWEEN);
init(beginExpressionSegment, 0);
init(endExpressionSegment, 1);
}
public Condition(final Column column, final List<ExpressionSegment> expressionSegments) {
this(column, ShardingOperator.IN);
public Condition(final Column column, final PredicateSegment predicateSegment, final List<ExpressionSegment> expressionSegments) {
this(column, predicateSegment, ShardingOperator.IN);
int count = 0;
for (ExpressionSegment each : expressionSegments) {
init(each, count);
......
......@@ -24,11 +24,11 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.core.constant.SQLType;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.parse.sql.context.condition.Condition;
import org.apache.shardingsphere.core.parse.sql.context.condition.ParseCondition;
import org.apache.shardingsphere.core.parse.sql.context.table.Tables;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
......@@ -95,26 +95,16 @@ public abstract class AbstractSQLStatement implements SQLStatement {
* @return encrypt condition
*/
public Optional<Condition> getEncryptCondition(final EncryptColumnToken encryptColumnToken) {
List<Condition> conditions = encryptCondition.findConditions(encryptColumnToken.getColumn());
if (conditions.isEmpty()) {
return Optional.absent();
}
if (1 == conditions.size()) {
return Optional.of(conditions.iterator().next());
for (Condition each : encryptCondition.findConditions(encryptColumnToken.getColumn())) {
if (isSameIndexes(each.getPredicateSegment(), encryptColumnToken)) {
return Optional.of(each);
}
}
return Optional.of(conditions.get(getEncryptConditionIndex(encryptColumnToken)));
return Optional.absent();
}
private int getEncryptConditionIndex(final EncryptColumnToken encryptColumnToken) {
int result = 0;
for (SQLToken each : sqlTokens) {
if (each.equals(encryptColumnToken)) {
return result;
} else if (each instanceof EncryptColumnToken) {
result++;
}
}
throw new ShardingException("Index Out Of Bounds For sqlTokens.");
private boolean isSameIndexes(final PredicateSegment predicateSegment, final EncryptColumnToken encryptColumnToken) {
return predicateSegment.getStartIndex() == encryptColumnToken.getStartIndex() && predicateSegment.getStopIndex() == encryptColumnToken.getStopIndex();
}
@Override
......
......@@ -17,11 +17,6 @@
package org.apache.shardingsphere.core.parse.filler.common.dml;
import static org.junit.Assert.assertEquals;
import java.util.Stack;
import java.util.Vector;
import com.google.common.base.Optional;
import org.apache.shardingsphere.core.parse.sql.context.condition.Column;
import org.apache.shardingsphere.core.parse.sql.context.condition.Condition;
......@@ -32,6 +27,11 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.Pred
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateInRightValue;
import org.junit.Test;
import java.util.Stack;
import java.util.Vector;
import static org.junit.Assert.assertEquals;
/**
* Unit tests for class {@link PredicateUtils}.
*
......@@ -44,17 +44,17 @@ public class PredicateUtilsTest {
Vector<ExpressionSegment> vector = new Vector<>();
vector.add(new SubquerySegment(-32, -32, "test"));
Column column = new Column(null, "");
assertEquals(Optional.<Condition>absent(), PredicateUtils.createInCondition(new PredicateInRightValue(vector), column));
assertEquals(Optional.<Condition>absent(), PredicateUtils.createInCondition(new PredicateInRightValue(vector), column, null));
}
@Test
public void testCreateInConditionTwo() {
Column column = new Column("!fE3s*#0- ece4_ :", "!fE3s*#0- ece4_ :");
Stack<ExpressionSegment> stack = new Stack<ExpressionSegment>();
stack.add(new ParameterMarkerExpressionSegment((-3652), (-3652), 0));
assertEquals("Optional.of(Condition(column=Column(name=!fE3s*#0- ece4_ :, tableName=!fE3s*#0- ece4_ :)," +
" operator=IN, compareOperator=null, positionValueMap={}, positionIndexMap={0=0}))",
PredicateUtils.createInCondition(new PredicateInRightValue(stack), column).toString()
stack.add(new ParameterMarkerExpressionSegment(-3652, -3652, 0));
assertEquals("Optional.of(Condition(column=Column(name=!fE3s*#0- ece4_ :, tableName=!fE3s*#0- ece4_ :), "
+ "predicateSegment=null, operator=IN, compareOperator=null, positionValueMap={}, positionIndexMap={0=0}))",
PredicateUtils.createInCondition(new PredicateInRightValue(stack), column, null).toString()
);
}
......@@ -63,14 +63,13 @@ public class PredicateUtilsTest {
ParameterMarkerExpressionSegment parameterMarkerExpressionSegment = new ParameterMarkerExpressionSegment(0, 0, 0);
PredicateCompareRightValue predicateCompareRightValue = new PredicateCompareRightValue("Z}4,%s6+6mvg{", parameterMarkerExpressionSegment);
Column column = new Column("Z}4,%s6+6mvg{ a", "Z}4,%s6+6mvg{");
assertEquals("Optional.of(Condition(column=Column(name=Z}4,%s6+6mvg" +
"{ a, tableName=Z}4,%s6+6mvg{), operator=EQUAL, compareOperator=null, " +
"positionValueMap={}, positionIndexMap={0=0}))",
PredicateUtils.createCompareCondition(predicateCompareRightValue, column).toString());
assertEquals("Optional.of(Condition(column=Column(name=Z}4,%s6+6mvg{ a, tableName=Z}4,%s6+6mvg{), predicateSegment=null, "
+ "operator=EQUAL, compareOperator=null, positionValueMap={}, positionIndexMap={0=0}))",
PredicateUtils.createCompareCondition(predicateCompareRightValue, column, null).toString());
}
@Test
public void testCreateCompareConditionWithNull() {
assertEquals(Optional.absent(), PredicateUtils.createCompareCondition(new PredicateCompareRightValue("$VALUES", (ExpressionSegment) null), null));
assertEquals(Optional.absent(), PredicateUtils.createCompareCondition(new PredicateCompareRightValue("$VALUES", null), null, null));
}
}
......@@ -32,14 +32,14 @@ public final class ConditionTest {
@Test
public void assertGetConditionValuesForEqual() {
List<Comparable<?>> actual = new Condition(new Column("test", "test"), new LiteralExpressionSegment(0, 0, 1)).getConditionValues(Collections.emptyList());
List<Comparable<?>> actual = new Condition(new Column("test", "test"), null, new LiteralExpressionSegment(0, 0, 1)).getConditionValues(Collections.emptyList());
assertThat(actual.size(), is(1));
assertThat((Integer) actual.get(0), is(1));
}
@Test
public void assertGetConditionValuesForIn() {
List<Comparable<?>> actual = new Condition(new Column("test", "test"), Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)))
List<Comparable<?>> actual = new Condition(new Column("test", "test"), null, Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)))
.getConditionValues(Collections.emptyList());
assertThat(actual.size(), is(2));
assertThat((Integer) actual.get(0), is(1));
......@@ -49,7 +49,7 @@ public final class ConditionTest {
@Test
public void assertGetConditionValuesForBetween() {
List<Comparable<?>> actual = new Condition(
new Column("test", "test"), new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)).getConditionValues(Collections.emptyList());
new Column("test", "test"), null, new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2)).getConditionValues(Collections.emptyList());
assertThat(actual.size(), is(2));
assertThat((Integer) actual.get(0), is(1));
assertThat((Integer) actual.get(1), is(2));
......
......@@ -84,7 +84,7 @@ public final class SQLStatementAssert {
expected = parserResultSetLoader.getParserResult(sqlCaseId);
tableAssert = new TableAssert(assertMessage);
conditionAssert = new ConditionAssert(assertMessage);
tokenAssert = new TokenAssert(sqlCaseType, assertMessage, databaseType);
tokenAssert = new TokenAssert(sqlCaseType, assertMessage);
indexAssert = new IndexAssert(sqlCaseType, assertMessage);
itemAssert = new ItemAssert(assertMessage);
groupByAssert = new GroupByAssert(assertMessage);
......
/*
* 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.core.parse.integrate.asserts.token;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.integrate.asserts.SQLStatementAssertMessage;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedEncryptColumnToken;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedTokens;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.test.sql.SQLCaseType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RequiredArgsConstructor
public final class EncryptColumnTokenAssert {
private final SQLCaseType sqlCaseType;
private final SQLStatementAssertMessage assertMessage;
void assertEncryptColumnsToken(final Collection<SQLToken> actual, final ExpectedTokens expected) {
List<EncryptColumnToken> encryptColumnTokens = getEncryptColumnTokens(actual);
assertThat(assertMessage.getFullAssertMessage("Encrypt column tokens size error: "), encryptColumnTokens.size(), is(expected.getEncryptColumnTokens().size()));
int count = 0;
for (ExpectedEncryptColumnToken each : expected.getEncryptColumnTokens()) {
assertEncryptColumnToken(encryptColumnTokens.get(count), each);
count++;
}
}
private void assertEncryptColumnToken(final EncryptColumnToken actual, final ExpectedEncryptColumnToken expected) {
if (SQLCaseType.Placeholder == sqlCaseType) {
assertThat(assertMessage.getFullAssertMessage("Encrypt column start index for placeholder assertion error: "), actual.getStartIndex(), is(expected.getStartIndexForPlaceholder()));
assertThat(assertMessage.getFullAssertMessage("Encrypt column stop index for placeholder assertion error: "), actual.getStopIndex(), is(expected.getStopIndexForPlaceholder()));
} else {
assertThat(assertMessage.getFullAssertMessage("Encrypt column start index for literal assertion error: "), actual.getStartIndex(), is(expected.getStartIndexForLiteral()));
assertThat(assertMessage.getFullAssertMessage("Encrypt column stop index for literal assertion error: "), actual.getStopIndex(), is(expected.getStopIndexForLiteral()));
}
assertNotNull(assertMessage.getFullAssertMessage("Encrypt column does not exist assertion error: "), expected.getColumn());
assertTrue(assertMessage.getFullAssertMessage("Missing encrypt column assertion error: "), actual.getColumn() != null);
assertThat(assertMessage.getFullAssertMessage("Encrypt column name assertion error: "), actual.getColumn().getName(), is(expected.getColumn().getName()));
assertThat(assertMessage.getFullAssertMessage("Encrypt column table name assertion error: "), actual.getColumn().getTableName(), is(expected.getColumn().getTableName()));
assertThat(assertMessage.getFullAssertMessage("Encrypt column isInWhere assertion error: "), actual.isInWhere(), is(expected.isInWhere()));
}
private List<EncryptColumnToken> getEncryptColumnTokens(final Collection<SQLToken> actual) {
List<EncryptColumnToken> result = new ArrayList<>(actual.size());
for (SQLToken each : actual) {
if (each instanceof EncryptColumnToken) {
result.add((EncryptColumnToken) each);
}
}
return result;
}
}
......@@ -17,7 +17,6 @@
package org.apache.shardingsphere.core.parse.integrate.asserts.token;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parse.integrate.asserts.SQLStatementAssertMessage;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedTokens;
import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
......@@ -38,16 +37,10 @@ public final class TokenAssert {
private final RowCountTokenAssert rowCountTokenAssert;
private final EncryptColumnTokenAssert encryptColumnTokenAssert;
private final DatabaseType databaseType;
public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessage assertMessage, final DatabaseType databaseType) {
public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessage assertMessage) {
insertValuesTokenAssert = new InsertValuesTokenAssert(assertMessage);
offsetTokenAssert = new OffsetTokenAssert(sqlCaseType, assertMessage);
rowCountTokenAssert = new RowCountTokenAssert(sqlCaseType, assertMessage);
encryptColumnTokenAssert = new EncryptColumnTokenAssert(sqlCaseType, assertMessage);
this.databaseType = databaseType;
}
/**
......@@ -61,8 +54,5 @@ public final class TokenAssert {
insertValuesTokenAssert.assertInsertValuesToken(actual, expected);
offsetTokenAssert.assertOffsetToken(actual, expected);
rowCountTokenAssert.assertRowCountToken(actual, expected);
if (DatabaseType.MySQL == databaseType) {
encryptColumnTokenAssert.assertEncryptColumnsToken(actual, expected);
}
}
}
......@@ -54,7 +54,4 @@ public final class ExpectedTokens {
@XmlElement(name = "insert-column-token")
private ExpectedInsertColumnToken insertColumnToken;
@XmlElement(name = "encrypt-column-token")
private List<ExpectedEncryptColumnToken> encryptColumnTokens = new LinkedList<>();
}
......@@ -26,11 +26,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="28" stop-index-for-placeholder="35" start-index-for-literal="28" stop-index-for-literal="39" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......@@ -43,14 +38,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="28" stop-index-for-placeholder="35" start-index-for-literal="28" stop-index-for-literal="39" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
<encrypt-column-token start-index-for-placeholder="41" stop-index-for-placeholder="49" start-index-for-literal="45" stop-index-for-literal="64" is-in-where="true">
<column table-name="t_encrypt" name="mobile"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......@@ -76,11 +63,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="28" stop-index-for-placeholder="39" start-index-for-literal="28" stop-index-for-literal="48" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="IN">
......
......@@ -21,11 +21,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="44" stop-index-for-placeholder="51" start-index-for-literal="44" stop-index-for-literal="55" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......@@ -38,14 +33,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="44" stop-index-for-placeholder="51" start-index-for-literal="44" stop-index-for-literal="55" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
<encrypt-column-token start-index-for-placeholder="56" stop-index-for-placeholder="63" start-index-for-literal="60" stop-index-for-literal="72" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......@@ -61,14 +48,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="44" stop-index-for-placeholder="51" start-index-for-literal="44" stop-index-for-literal="55" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
<encrypt-column-token start-index-for-placeholder="57" stop-index-for-placeholder="66" start-index-for-literal="61" stop-index-for-literal="81" is-in-where="true">
<column table-name="t_encrypt" name="mobile"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......@@ -84,11 +63,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="44" stop-index-for-placeholder="51" start-index-for-literal="44" stop-index-for-literal="55" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......
......@@ -26,21 +26,11 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="21" stop-index-for-placeholder="28" start-index-for-literal="21" stop-index-for-literal="32" is-in-where="false">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
</parser-result>
<parser-result sql-case-id="update_with_one_encrypt_column_in_where" parameters="'invalid', 'tom'">
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="38" stop-index-for-placeholder="45" start-index-for-literal="46" stop-index-for-literal="57" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......@@ -53,14 +43,6 @@
<tables>
<table name="t_encrypt" />
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="21" stop-index-for-placeholder="30" start-index-for-literal="21" stop-index-for-literal="41" is-in-where="false">
<column table-name="t_encrypt" name="mobile"/>
</encrypt-column-token>
<encrypt-column-token start-index-for-placeholder="50" stop-index-for-placeholder="57" start-index-for-literal="69" stop-index-for-literal="80" is-in-where="true">
<column table-name="t_encrypt" name="name"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="name" table-name="t_encrypt" operator="EQUAL">
......@@ -73,12 +55,6 @@
<tables>
<table name="t_encrypt"/>
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="21" stop-index-for-placeholder="30"
start-index-for-literal="21" stop-index-for-literal="41" is-in-where="false">
<column table-name="t_encrypt" name="mobile"/>
</encrypt-column-token>
</tokens>
</parser-result>
<parser-result sql-case-id="update_with_encrypt_column_in_function" parameters="'1333333333', 'invalid', 'tom'">
<tables>
......@@ -89,12 +65,6 @@
<tables>
<table name="t_encrypt"/>
</tables>
<tokens>
<encrypt-column-token start-index-for-placeholder="59" stop-index-for-placeholder="68"
start-index-for-literal="67" stop-index-for-literal="87" is-in-where="true">
<column table-name="t_encrypt" name="mobile"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="mobile" table-name="t_encrypt" operator="EQUAL">
......
......@@ -23,9 +23,6 @@
</tables>
<tokens>
<table-token start-index="12" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="59" stop-index-for-placeholder="66" start-index-for-literal="65" stop-index-for-literal="77" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -52,9 +49,6 @@
</tables>
<tokens>
<table-token start-index="12" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="26" stop-index-for-placeholder="33" start-index-for-literal="26" stop-index-for-literal="38" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
......@@ -71,9 +65,6 @@
</tables>
<tokens>
<table-token start-index="12" table-name="t_order" left-delimiter="`" right-delimiter="`" length="9" />
<encrypt-column-token start-index-for-placeholder="28" stop-index-for-placeholder="42" start-index-for-literal="28" stop-index-for-literal="42" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
......
......@@ -23,9 +23,6 @@
</tables>
<tokens>
<table-token start-index="7" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="19" stop-index-for-placeholder="28" start-index-for-literal="19" stop-index-for-literal="35" is-in-where="false">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -45,9 +42,6 @@
</tables>
<tokens>
<table-token start-index="7" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="24" stop-index-for-placeholder="35" start-index-for-literal="24" stop-index-for-literal="42" is-in-where="false">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -86,9 +80,6 @@
</tables>
<tokens>
<table-token start-index="7" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="21" stop-index-for-placeholder="41" start-index-for-literal="21" stop-index-for-literal="41" is-in-where="false">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
</parser-result>
......@@ -98,9 +89,6 @@
</tables>
<tokens>
<table-token start-index="27" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="39" stop-index-for-placeholder="48" start-index-for-literal="39" stop-index-for-literal="55" is-in-where="false">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -120,9 +108,6 @@
</tables>
<tokens>
<table-token start-index="7" table-name="t_order" left-delimiter="`" right-delimiter="`" length="9" />
<encrypt-column-token start-index-for-placeholder="21" stop-index-for-placeholder="32" start-index-for-literal="21" stop-index-for-literal="39" is-in-where="false">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -161,9 +146,6 @@
</tables>
<tokens>
<table-token start-index="7" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="19" stop-index-for-placeholder="35" start-index-for-literal="19" stop-index-for-literal="35" is-in-where="false">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -183,9 +165,6 @@
</tables>
<tokens>
<table-token start-index="7" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="19" stop-index-for-placeholder="35" start-index-for-literal="19" stop-index-for-literal="35" is-in-where="false">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......
......@@ -318,9 +318,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="31" table-name="t_order_item" length="12" />
<encrypt-column-token start-index-for-placeholder="208" stop-index-for-placeholder="219" start-index-for-literal="209" stop-index-for-literal="225" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -354,9 +351,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="31" table-name="t_order_item" length="12" />
<encrypt-column-token start-index-for-placeholder="199" stop-index-for-placeholder="210" start-index-for-literal="200" stop-index-for-literal="216" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -756,9 +750,6 @@
</tables>
<tokens>
<table-token start-index="14" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="28" stop-index-for-placeholder="38" start-index-for-literal="28" stop-index-for-literal="38" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
......@@ -775,9 +766,6 @@
</tables>
<tokens>
<table-token start-index="14" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="28" stop-index-for-placeholder="38" start-index-for-literal="28" stop-index-for-literal="38" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
......@@ -794,9 +782,6 @@
</tables>
<tokens>
<table-token start-index="83" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="97" stop-index-for-placeholder="106" start-index-for-literal="97" stop-index-for-literal="106" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
......@@ -813,9 +798,6 @@
</tables>
<tokens>
<table-token start-index="78" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="92" stop-index-for-placeholder="101" start-index-for-literal="92" stop-index-for-literal="101" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
......
......@@ -65,9 +65,6 @@
</tables>
<tokens>
<table-token start-index="14" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="44" stop-index-for-placeholder="53" start-index-for-literal="44" stop-index-for-literal="58" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
......@@ -84,9 +81,6 @@
</tables>
<tokens>
<table-token start-index="14" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="45" stop-index-for-placeholder="54" start-index-for-literal="45" stop-index-for-literal="59" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -118,9 +112,6 @@
</tables>
<tokens>
<table-token start-index="14" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="30" stop-index-for-placeholder="39" start-index-for-literal="30" stop-index-for-literal="44" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
......@@ -201,10 +192,7 @@
</tables>
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="31" table-name="t_order_item" length="12" />
<encrypt-column-token start-index-for-placeholder="210" stop-index-for-placeholder="221" start-index-for-literal="210" stop-index-for-literal="226" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
<table-token start-index="31" table-name="t_order_item" length="12" />F
</tokens>
<or-condition>
<and-condition>
......
......@@ -38,9 +38,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="27" table-name="t_order_item" length="12" />
<encrypt-column-token start-index-for-placeholder="76" stop-index-for-placeholder="92" start-index-for-literal="76" stop-index-for-literal="92" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<order-by-columns>
<order-by-column owner="o" name="order_id" order-direction="DESC" />
......@@ -75,9 +72,6 @@
<tokens>
<table-token start-index="16" table-name="t_order" length="7" />
<table-token start-index="27" table-name="t_order_item" length="12" />
<encrypt-column-token start-index-for-placeholder="76" stop-index-for-placeholder="92" start-index-for-literal="76" stop-index-for-literal="92" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<order-by-columns>
<order-by-column owner="i" name="creation_date" order-direction="DESC" />
......
......@@ -24,6 +24,7 @@ import org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder;
import org.apache.shardingsphere.core.rewrite.builder.SQLBuilder;
import org.apache.shardingsphere.core.rewrite.rewriter.BaseSQLRewriter;
import org.apache.shardingsphere.core.rewrite.rewriter.SQLRewriter;
import org.apache.shardingsphere.core.rewrite.token.EncryptTokenGenerateEngine;
import org.apache.shardingsphere.core.rewrite.token.MasterSlaveTokenGenerateEngine;
import org.apache.shardingsphere.core.rewrite.token.ShardingTokenGenerateEngine;
import org.apache.shardingsphere.core.route.SQLUnit;
......@@ -36,6 +37,7 @@ import org.apache.shardingsphere.core.rule.ShardingRule;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
......@@ -62,7 +64,7 @@ public final class SQLRewriteEngine {
public SQLRewriteEngine(final ShardingRule shardingRule, final SQLStatement sqlStatement, final List<Object> parameters) {
baseRule = shardingRule;
this.sqlStatement = sqlStatement;
sqlTokens = new ShardingTokenGenerateEngine().generateSQLTokens(sqlStatement, shardingRule);
sqlTokens = createSQLTokens(shardingRule, sqlStatement);
sqlBuilder = new SQLBuilder();
parameterBuilder = new ParameterBuilder(parameters);
baseSQLRewriter = new BaseSQLRewriter(sqlStatement, sqlTokens);
......@@ -71,7 +73,7 @@ public final class SQLRewriteEngine {
public SQLRewriteEngine(final EncryptRule encryptRule, final SQLStatement sqlStatement, final List<Object> parameters) {
baseRule = encryptRule;
this.sqlStatement = sqlStatement;
sqlTokens = sqlStatement.getSQLTokens();
sqlTokens = createSQLTokens(encryptRule, sqlStatement);
sqlBuilder = new SQLBuilder();
parameterBuilder = new ParameterBuilder(parameters);
baseSQLRewriter = new BaseSQLRewriter(sqlStatement, sqlTokens);
......@@ -80,12 +82,27 @@ public final class SQLRewriteEngine {
public SQLRewriteEngine(final SQLStatement sqlStatement) {
baseRule = null;
this.sqlStatement = sqlStatement;
sqlTokens = new MasterSlaveTokenGenerateEngine().generateSQLTokens(sqlStatement, null);
sqlTokens = createSQLTokens(null, sqlStatement);
sqlBuilder = new SQLBuilder();
parameterBuilder = new ParameterBuilder(Collections.emptyList());
baseSQLRewriter = new BaseSQLRewriter(sqlStatement, sqlTokens);
}
private List<SQLToken> createSQLTokens(final BaseRule baseRule, final SQLStatement sqlStatement) {
List<SQLToken> result = new LinkedList<>(sqlStatement.getSQLTokens());
if (baseRule instanceof ShardingRule) {
ShardingRule shardingRule = (ShardingRule) baseRule;
result.addAll(new ShardingTokenGenerateEngine().generateSQLTokens(sqlStatement, shardingRule));
result.addAll(new EncryptTokenGenerateEngine().generateSQLTokens(sqlStatement, shardingRule.getEncryptRule()));
} else if (baseRule instanceof EncryptRule) {
result.addAll(new EncryptTokenGenerateEngine().generateSQLTokens(sqlStatement, (EncryptRule) baseRule));
} else {
result.addAll(new MasterSlaveTokenGenerateEngine().generateSQLTokens(sqlStatement, null));
}
Collections.sort(result);
return result;
}
/**
* Initialize SQL rewrite engine.
*
......
......@@ -15,34 +15,30 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.integrate.jaxb.token;
package org.apache.shardingsphere.core.rewrite.token;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.core.rewrite.token.generator.EncryptColumnTokenGenerator;
import org.apache.shardingsphere.core.rewrite.token.generator.SQLTokenGenerator;
import org.apache.shardingsphere.core.rule.EncryptRule;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import java.util.Collection;
import java.util.LinkedList;
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public final class ExpectedEncryptColumnToken {
@XmlAttribute(name = "start-index-for-placeholder")
private int startIndexForPlaceholder;
@XmlAttribute(name = "stop-index-for-placeholder")
private int stopIndexForPlaceholder;
@XmlAttribute(name = "start-index-for-literal")
private int startIndexForLiteral;
/**
* SQL token generator for encrypt.
*
* @author panjuan
*/
public final class EncryptTokenGenerateEngine extends SQLTokenGenerateEngine<EncryptRule> {
@XmlAttribute(name = "stop-index-for-literal")
private int stopIndexForLiteral;
private static final Collection<SQLTokenGenerator> SQL_TOKEN_GENERATORS = new LinkedList<>();
private ExpectedColumn column;
static {
SQL_TOKEN_GENERATORS.add(new EncryptColumnTokenGenerator());
}
@XmlAttribute(name = "is-in-where")
private boolean isInWhere;
@Override
protected Collection<SQLTokenGenerator> getSQLTokenGenerators() {
return SQL_TOKEN_GENERATORS;
}
}
......@@ -26,7 +26,6 @@ import org.apache.shardingsphere.core.rewrite.token.generator.SQLTokenGenerator;
import org.apache.shardingsphere.core.rule.BaseRule;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
......@@ -48,7 +47,7 @@ public abstract class SQLTokenGenerateEngine<T extends BaseRule> {
*/
@SuppressWarnings("unchecked")
public final List<SQLToken> generateSQLTokens(final SQLStatement sqlStatement, final T rule) {
List<SQLToken> result = new LinkedList<>(sqlStatement.getSQLTokens());
List<SQLToken> result = new LinkedList<>();
for (SQLTokenGenerator each : getSQLTokenGenerators()) {
if (each instanceof OptionalSQLTokenGenerator) {
Optional<? extends SQLToken> sqlToken = ((OptionalSQLTokenGenerator) each).generateSQLToken(sqlStatement, rule);
......@@ -59,9 +58,7 @@ public abstract class SQLTokenGenerateEngine<T extends BaseRule> {
result.addAll(((CollectionSQLTokenGenerator) each).generateSQLTokens(sqlStatement, rule));
}
}
Collections.sort(result);
return result;
}
protected abstract Collection<SQLTokenGenerator> getSQLTokenGenerators();
}
/*
* 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.core.rewrite.token.generator;
import org.apache.shardingsphere.core.parse.sql.context.condition.Column;
import org.apache.shardingsphere.core.parse.sql.context.condition.Condition;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.rule.EncryptRule;
import java.util.Collection;
import java.util.LinkedList;
/**
* Encrypt column token generator.
*
* @author panjuan
*/
public final class EncryptColumnTokenGenerator implements CollectionSQLTokenGenerator<EncryptRule> {
@Override
public Collection<EncryptColumnToken> generateSQLTokens(final SQLStatement sqlStatement, final EncryptRule encryptRule) {
Collection<EncryptColumnToken> result = new LinkedList<>();
for (SQLSegment each : sqlStatement.getSqlSegments()) {
if (each instanceof SetAssignmentsSegment) {
result.addAll(createFromUpdateSetAssignment(sqlStatement, encryptRule, (SetAssignmentsSegment) each));
}
}
result.addAll(createFromWhereCondition(sqlStatement));
return result;
}
private Collection<EncryptColumnToken> createFromUpdateSetAssignment(final SQLStatement sqlStatement, final EncryptRule encryptRule, final SetAssignmentsSegment segment) {
Collection<EncryptColumnToken> result = new LinkedList<>();
if (sqlStatement instanceof InsertStatement) {
return result;
}
for (AssignmentSegment each : segment.getAssignments()) {
Column column = new Column(each.getColumn().getName(), sqlStatement.getTables().getSingleTableName());
if (encryptRule.getEncryptorEngine().getShardingEncryptor(column.getTableName(), column.getName()).isPresent()) {
result.add(new EncryptColumnToken(each.getColumn().getStartIndex(), each.getStopIndex(), column, false));
}
}
return result;
}
private Collection<EncryptColumnToken> createFromWhereCondition(final SQLStatement sqlStatement) {
Collection<EncryptColumnToken> result = new LinkedList<>();
if (sqlStatement.getEncryptCondition().getOrConditions().isEmpty()) {
return result;
}
for (Condition each : sqlStatement.getEncryptCondition().getOrConditions().get(0).getConditions()) {
result.add(new EncryptColumnToken(each.getPredicateSegment().getStartIndex(), each.getPredicateSegment().getStopIndex(), each.getColumn(), true));
}
return result;
}
}
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.rewrite.token.generator;
import org.apache.shardingsphere.core.parse.sql.segment.RemoveAvailable;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.RemoveToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.RemoveToken;
import org.apache.shardingsphere.core.rule.ShardingRule;
import java.util.Collection;
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parse.sql.token.impl;
package org.apache.shardingsphere.core.rewrite.token.pojo;
import lombok.Getter;
import lombok.ToString;
......
......@@ -33,22 +33,24 @@ import org.apache.shardingsphere.core.parse.sql.context.limit.LimitValue;
import org.apache.shardingsphere.core.parse.sql.context.table.Table;
import org.apache.shardingsphere.core.parse.sql.segment.common.TableSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.AggregationDistinctSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.DeleteStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertColumnsToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.InsertValuesToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.OffsetToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.RemoveToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.RemoveToken;
import org.apache.shardingsphere.core.parse.sql.token.impl.RowCountToken;
import org.apache.shardingsphere.core.rewrite.SQLRewriteEngine;
import org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder;
......@@ -551,7 +553,7 @@ public final class ShardingSQLRewriterTest {
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, routeResult.getSqlStatement(), parameters);
rewriteEngine.init(
new ShardingSQLRewriter(shardingRule, DatabaseType.MySQL, routeResult),
new EncryptSQLRewriter(shardingRule.getShardingEncryptorEngine(), routeResult.getSqlStatement(), routeResult.getOptimizeResult()));
new EncryptSQLRewriter(shardingRule.getEncryptRule().getEncryptorEngine(), routeResult.getSqlStatement(), routeResult.getOptimizeResult()));
RoutingUnit routingUnit = new RoutingUnit("db0");
routingUnit.getTableUnits().add(new TableUnit("table_x", "table_x"));
assertThat(rewriteEngine.generateSQL(routingUnit).getSql(), is("SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?"));
......@@ -771,9 +773,9 @@ public final class ShardingSQLRewriterTest {
parameters.add("x");
Column column = new Column("id", "table_z");
selectStatement.addSQLToken(new TableToken(15, 21, "table_z", QuoteCharacter.NONE));
selectStatement.addSQLToken(new EncryptColumnToken(29, 32, column, true));
selectStatement.getEncryptCondition().getOrConditions().add(new AndCondition());
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new ParameterMarkerExpressionSegment(0, 0, 0)));
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column,
new PredicateSegment(29, 32, null, null), new ParameterMarkerExpressionSegment(0, 0, 0)));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(selectStatement.getLimit());
routeResult.setRoutingResult(new RoutingResult());
......@@ -785,14 +787,13 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertSelectInWithShardingEncryptor() {
Column column = new Column("id", "table_z");
selectStatement.addSQLToken(new TableToken(15, 21, "table_z", QuoteCharacter.NONE));
selectStatement.addSQLToken(new EncryptColumnToken(29, 39, column, true));
selectStatement.getEncryptCondition().getOrConditions().add(new AndCondition());
List<ExpressionSegment> expressionSegments = new LinkedList<>();
expressionSegments.add(new LiteralExpressionSegment(0, 0, 3));
expressionSegments.add(new LiteralExpressionSegment(0, 0, 5));
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, expressionSegments));
Column column = new Column("id", "table_z");
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new PredicateSegment(29, 39, null, null), expressionSegments));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(selectStatement.getLimit());
routeResult.setRoutingResult(new RoutingResult());
......@@ -807,17 +808,15 @@ public final class ShardingSQLRewriterTest {
List<Object> parameters = new ArrayList<>(2);
parameters.add(1);
parameters.add(2);
Column column = new Column("id", "table_z");
selectStatement.addSQLToken(new TableToken(15, 21, "table_z", QuoteCharacter.NONE));
selectStatement.addSQLToken(new EncryptColumnToken(29, 40, column, true));
selectStatement.addSQLToken(new EncryptColumnToken(45, 50, column, true));
selectStatement.getEncryptCondition().getOrConditions().add(new AndCondition());
selectStatement.getEncryptCondition().getOrConditions().add(new AndCondition());
List<ExpressionSegment> expressionSegments = new LinkedList<>();
expressionSegments.add(new ParameterMarkerExpressionSegment(0, 0, 0));
expressionSegments.add(new ParameterMarkerExpressionSegment(0, 0, 1));
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, expressionSegments));
selectStatement.getEncryptCondition().getOrConditions().get(1).getConditions().add(new Condition(column, new LiteralExpressionSegment(0, 0, 3)));
Column column = new Column("id", "table_z");
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new PredicateSegment(29, 40, null, null), expressionSegments));
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new PredicateSegment(45, 50, null, null), new LiteralExpressionSegment(0, 0, 3)));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(selectStatement.getLimit());
routeResult.setRoutingResult(new RoutingResult());
......@@ -835,9 +834,9 @@ public final class ShardingSQLRewriterTest {
parameters.add("k");
Column column = new Column("id", "table_k");
selectStatement.addSQLToken(new TableToken(15, 21, "table_k", QuoteCharacter.NONE));
selectStatement.addSQLToken(new EncryptColumnToken(29, 32, column, true));
selectStatement.getEncryptCondition().getOrConditions().add(new AndCondition());
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new ParameterMarkerExpressionSegment(0, 0, 0)));
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column,
new PredicateSegment(29, 32, null, null), new ParameterMarkerExpressionSegment(0, 0, 0)));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(selectStatement.getLimit());
routeResult.setRoutingResult(new RoutingResult());
......@@ -849,14 +848,13 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertSelectInWithQueryAssistedShardingEncryptor() {
Column column = new Column("id", "table_k");
selectStatement.addSQLToken(new TableToken(15, 21, "table_k", QuoteCharacter.NONE));
selectStatement.addSQLToken(new EncryptColumnToken(29, 39, column, true));
selectStatement.getEncryptCondition().getOrConditions().add(new AndCondition());
List<ExpressionSegment> expressionSegments = new LinkedList<>();
expressionSegments.add(new LiteralExpressionSegment(0, 0, 3));
expressionSegments.add(new LiteralExpressionSegment(0, 0, 5));
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, expressionSegments));
Column column = new Column("id", "table_k");
selectStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new PredicateSegment(29, 39, null, null), expressionSegments));
routeResult = new SQLRouteResult(selectStatement);
routeResult.setLimit(selectStatement.getLimit());
routeResult.setRoutingResult(new RoutingResult());
......@@ -868,13 +866,14 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertUpdateWithShardingEncryptor() {
Column column = new Column("id", "table_z");
updateStatement.getTables().add(new Table("table_z", ""));
updateStatement.addSQLToken(new TableToken(7, 13, "table_z", QuoteCharacter.NONE));
updateStatement.addSQLToken(new EncryptColumnToken(19, 24, column, false));
SetAssignmentsSegment setAssignmentsSegment = new SetAssignmentsSegment(15, 24, Collections.singleton(new AssignmentSegment(19, 24, new ColumnSegment(19, 20, "id"), null)));
updateStatement.getSqlSegments().add(setAssignmentsSegment);
Column column = new Column("id", "table_z");
updateStatement.getAssignments().put(column, new LiteralExpressionSegment(0, 0, 1));
updateStatement.addSQLToken(new EncryptColumnToken(32, 37, column, true));
updateStatement.getEncryptCondition().getOrConditions().add(new AndCondition());
updateStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new LiteralExpressionSegment(0, 0, 2)));
updateStatement.getEncryptCondition().getOrConditions().get(0).getConditions().add(new Condition(column, new PredicateSegment(32, 37, null, null), new LiteralExpressionSegment(0, 0, 2)));
routeResult = new SQLRouteResult(updateStatement);
routeResult.setRoutingResult(new RoutingResult());
updateStatement.setLogicSQL("UPDATE table_z SET id = 1 WHERE id = 2");
......@@ -885,9 +884,6 @@ public final class ShardingSQLRewriterTest {
@Test
public void assertSelectInWithAggregationDistinct() {
selectStatement.addSQLToken(new TableToken(49, 55, "table_z", QuoteCharacter.NONE));
// SelectItemPrefixToken selectItemPrefixToken = new SelectItemPrefixToken(7);
// selectItemPrefixToken.setToAppendDistinct(true);
// selectStatement.addSQLToken(selectItemPrefixToken);
selectStatement.getSqlSegments().add(new SelectItemsSegment(7, 44, false));
AggregationDistinctSelectItemSegment selectItemSegment1 = new AggregationDistinctSelectItemSegment(7, 24, "DISTINCT id", AggregationType.COUNT, 12, "id");
selectItemSegment1.setAlias("a");
......@@ -907,7 +903,7 @@ public final class ShardingSQLRewriterTest {
SQLRewriteEngine result = new SQLRewriteEngine(shardingRule, routeResult.getSqlStatement(), parameters);
result.init(
new ShardingSQLRewriter(shardingRule, databaseType, routeResult),
new EncryptSQLRewriter(shardingRule.getShardingEncryptorEngine(), routeResult.getSqlStatement(), routeResult.getOptimizeResult()));
new EncryptSQLRewriter(shardingRule.getEncryptRule().getEncryptorEngine(), routeResult.getSqlStatement(), routeResult.getOptimizeResult()));
return result;
}
}
......@@ -115,8 +115,8 @@ public final class PreparedStatementExecutor extends AbstractStatementExecutor {
ResultSet resultSet = preparedStatement.executeQuery();
ShardingRule shardingRule = getConnection().getShardingContext().getShardingRule();
getResultSets().add(resultSet);
return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine())
: new MemoryQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine());
return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine())
: new MemoryQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine());
}
/**
......
......@@ -103,8 +103,8 @@ public final class StatementExecutor extends AbstractStatementExecutor {
ResultSet resultSet = statement.executeQuery(routeUnit.getSqlUnit().getSql());
ShardingRule shardingRule = getConnection().getShardingContext().getShardingRule();
getResultSets().add(resultSet);
return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine())
: new MemoryQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine());
return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine())
: new MemoryQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine());
}
/**
......
......@@ -20,7 +20,6 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.adapter;
import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor.ForceExecuteCallback;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.executor.ForceExecuteTemplate;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset.ShardingResultSetMetaData;
......@@ -69,10 +68,6 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat
: ((ShardingStatement) statement).getConnection().getShardingContext().getShardingRule();
}
protected final ShardingEncryptorEngine getShardingEncryptorEngine() {
return getShardingRule().getShardingEncryptorEngine();
}
@Override
public final int findColumn(final String columnLabel) throws SQLException {
return resultSets.get(0).findColumn(columnLabel);
......
......@@ -133,7 +133,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta
ResultSet resultSet = each.getResultSet();
resultSets.add(resultSet);
ShardingRule shardingRule = connection.getShardingContext().getShardingRule();
queryResults.add(new StreamQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine()));
queryResults.add(new StreamQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine()));
}
if (routeResult.getSqlStatement() instanceof SelectStatement || routeResult.getSqlStatement() instanceof DALStatement) {
MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getDatabaseType(),
......
......@@ -114,7 +114,7 @@ public final class ShardingStatement extends AbstractStatementAdapter {
ResultSet resultSet = each.getResultSet();
resultSets.add(resultSet);
ShardingRule shardingRule = connection.getShardingContext().getShardingRule();
queryResults.add(new StreamQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine()));
queryResults.add(new StreamQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine()));
}
if (routeResult.getSqlStatement() instanceof SelectStatement || routeResult.getSqlStatement() instanceof DALStatement) {
MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getDatabaseType(),
......
......@@ -25,6 +25,7 @@ import org.apache.shardingsphere.core.constant.properties.ShardingProperties;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.core.execute.ShardingExecuteEngine;
import org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.rule.TableRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
......@@ -87,7 +88,9 @@ public abstract class AbstractBaseExecutorTest {
when(shardingEncryptor.decrypt(anyString())).thenReturn("decryptValue");
ShardingEncryptorEngine shardingEncryptorEngine = mock(ShardingEncryptorEngine.class);
when(shardingEncryptorEngine.getShardingEncryptor(anyString(), anyString())).thenReturn(Optional.of(shardingEncryptor));
when(shardingRule.getShardingEncryptorEngine()).thenReturn(shardingEncryptorEngine);
EncryptRule encryptRule = mock(EncryptRule.class);
when(encryptRule.getEncryptorEngine()).thenReturn(shardingEncryptorEngine);
when(shardingRule.getEncryptRule()).thenReturn(encryptRule);
when(shardingRule.findTableRuleByActualTable("table_x")).thenReturn(Optional.<TableRule>absent());
return shardingRule;
}
......
......@@ -20,6 +20,7 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
import com.google.common.base.Optional;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.merge.MergedResult;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.ShardingContext;
......@@ -85,8 +86,10 @@ public final class ShardingResultSetTest {
ShardingRule shardingRule = mock(ShardingRule.class);
when(shardingRule.getLogicTableNames(anyString())).thenReturn(Collections.singletonList("test"));
ShardingEncryptorEngine shardingEncryptorEngine = mock(ShardingEncryptorEngine.class);
EncryptRule encryptRule = mock(EncryptRule.class);
when(encryptRule.getEncryptorEngine()).thenReturn(shardingEncryptorEngine);
when(shardingEncryptorEngine.getShardingEncryptor(anyString(), anyString())).thenReturn(Optional.<ShardingEncryptor>absent());
when(shardingRule.getShardingEncryptorEngine()).thenReturn(shardingEncryptorEngine);
when(shardingRule.getEncryptRule()).thenReturn(encryptRule);
when(shardingContext.getShardingRule()).thenReturn(shardingRule);
when(shardingConnection.getShardingContext()).thenReturn(shardingContext);
ShardingStatement statement = mock(ShardingStatement.class);
......
......@@ -100,8 +100,8 @@ public final class ProxySQLExecuteCallback extends SQLExecuteCallback<ExecuteRes
LogicSchema logicSchema = backendConnection.getLogicSchema();
if (logicSchema instanceof ShardingSchema) {
ShardingRule shardingRule = logicSchema.getShardingRule();
return connectionMode == ConnectionMode.MEMORY_STRICTLY ? new StreamQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine())
: new MemoryQueryResult(resultSet, shardingRule, shardingRule.getShardingEncryptorEngine());
return connectionMode == ConnectionMode.MEMORY_STRICTLY ? new StreamQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine())
: new MemoryQueryResult(resultSet, shardingRule, shardingRule.getEncryptRule().getEncryptorEngine());
}
if (logicSchema instanceof EncryptSchema) {
EncryptSchema encryptSchema = (EncryptSchema) logicSchema;
......
......@@ -65,7 +65,7 @@ public class SpringBootShardingTest {
ShardingProperties shardingProperties = shardingContext.getShardingProperties();
assertTrue((Boolean) shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW));
assertThat((Integer) shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE), is(100));
ShardingEncryptorEngine shardingEncryptorEngine = shardingContext.getShardingRule().getShardingEncryptorEngine();
ShardingEncryptorEngine shardingEncryptorEngine = shardingContext.getShardingRule().getEncryptRule().getEncryptorEngine();
assertThat(shardingEncryptorEngine.getEncryptTableNames().iterator().next(), is("t_order"));
assertThat(shardingEncryptorEngine.getAssistedQueryColumnCount("t_order"), is(0));
assertThat(shardingEncryptorEngine.getShardingEncryptor("t_order", "pwd2").get(), instanceOf(TestShardingEncryptor.class));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册