未验证 提交 1714a26f 编写于 作者: L Liang Zhang 提交者: GitHub

Fixes #3335 (#3336)

* for code style

* add select_with_table_as_qualified_name for sharding

* only modify projection's column name, do not modify owner of encrypt

* only modify predicate's column name, do not modify owner of encrypt

* remove EncryptCondition.columnOwner

* split Encrypt Predicate token to EncryptPredicateColumnToken and EncryptPredicateRightValueToken

* rename EncryptProjectionToken to EncryptColumnNameToken

* refactor EncryptPredicateColumnTokenGenerator
上级 490c7829
......@@ -46,8 +46,6 @@ public final class EncryptCondition {
private final String tableName;
private final String columnOwner;
private final int startIndex;
private final int stopIndex;
......@@ -58,20 +56,18 @@ public final class EncryptCondition {
private final Map<Integer, Object> positionValueMap = new LinkedHashMap<>();
public EncryptCondition(final String columnName, final String tableName, final String columnOwner, final int startIndex, final int stopIndex, final ExpressionSegment expressionSegment) {
public EncryptCondition(final String columnName, final String tableName, final int startIndex, final int stopIndex, final ExpressionSegment expressionSegment) {
this.columnName = columnName;
this.tableName = tableName;
this.columnOwner = columnOwner;
this.startIndex = startIndex;
this.stopIndex = stopIndex;
operator = ShardingOperator.EQUAL;
putPositionMap(0, expressionSegment);
}
public EncryptCondition(final String columnName, final String tableName, final String columnOwner, final int startIndex, final int stopIndex, final List<ExpressionSegment> expressionSegments) {
public EncryptCondition(final String columnName, final String tableName, final int startIndex, final int stopIndex, final List<ExpressionSegment> expressionSegments) {
this.columnName = columnName;
this.tableName = tableName;
this.columnOwner = columnOwner;
this.startIndex = startIndex;
this.stopIndex = stopIndex;
operator = ShardingOperator.IN;
......
......@@ -30,7 +30,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.WhereSegme
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateBetweenRightValue;
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.segment.generic.TableSegment;
import org.apache.shardingsphere.core.parse.sql.statement.generic.WhereSegmentAvailable;
import org.apache.shardingsphere.core.preprocessor.segment.table.TablesContext;
import org.apache.shardingsphere.core.preprocessor.statement.SQLStatementContext;
......@@ -116,9 +115,8 @@ public final class EncryptConditionEngine {
private static Optional<EncryptCondition> createCompareEncryptCondition(final String tableName, final PredicateSegment predicateSegment, final PredicateCompareRightValue compareRightValue) {
return compareRightValue.getExpression() instanceof SimpleExpressionSegment
? Optional.of(
new EncryptCondition(predicateSegment.getColumn().getName(), tableName, getTableAlias(predicateSegment.getColumn().getOwner().orNull()),
predicateSegment.getStartIndex(), predicateSegment.getStopIndex(), compareRightValue.getExpression()))
? Optional.of(new EncryptCondition(predicateSegment.getColumn().getName(), tableName, compareRightValue.getExpression().getStartIndex(),
predicateSegment.getStopIndex(), compareRightValue.getExpression()))
: Optional.<EncryptCondition>absent();
}
......@@ -130,18 +128,11 @@ public final class EncryptConditionEngine {
}
}
return expressionSegments.isEmpty() ? Optional.<EncryptCondition>absent()
: Optional.of(new EncryptCondition(predicateSegment.getColumn().getName(), tableName, getTableAlias(predicateSegment.getColumn().getOwner().orNull()),
predicateSegment.getStartIndex(), predicateSegment.getStopIndex(), expressionSegments));
: Optional.of(new EncryptCondition(
predicateSegment.getColumn().getName(), tableName, inRightValue.getSqlExpressions().iterator().next().getStartIndex(), predicateSegment.getStopIndex(), expressionSegments));
}
private boolean isSupportedOperator(final String operator) {
return "=".equals(operator) || "<>".equals(operator) || "!=".equals(operator);
}
private static String getTableAlias(final TableSegment owner) {
if (null == owner) {
return null;
}
return owner.getTableName();
}
}
......@@ -24,7 +24,8 @@ import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.im
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.EncryptAssignmentTokenGenerator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.EncryptForInsertColumnsTokenGenerator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.EncryptInsertValuesTokenGenerator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.EncryptPredicateTokenGenerator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.EncryptPredicateColumnTokenGenerator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.EncryptPredicateRightValueTokenGenerator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.EncryptProjectionTokenGenerator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl.InsertCipherNameTokenGenerator;
import org.apache.shardingsphere.core.rewrite.sql.token.generator.SQLTokenGenerator;
......@@ -66,7 +67,8 @@ public final class EncryptTokenGenerateBuilder implements SQLTokenGeneratorBuild
result.add(new EncryptForInsertColumnsTokenGenerator());
result.add(new EncryptProjectionTokenGenerator());
result.add(new EncryptAssignmentTokenGenerator());
result.add(new EncryptPredicateTokenGenerator());
result.add(new EncryptPredicateColumnTokenGenerator());
result.add(new EncryptPredicateRightValueTokenGenerator());
result.add(new InsertCipherNameTokenGenerator());
result.add(new AssistQueryAndPlainInsertColumnsTokenGenerator());
result.add(new EncryptInsertValuesTokenGenerator());
......
/*
* 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.feature.encrypt.token.generator.impl;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.Setter;
import org.apache.shardingsphere.core.metadata.table.TableMetas;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.core.parse.sql.statement.generic.WhereSegmentAvailable;
import org.apache.shardingsphere.core.preprocessor.statement.SQLStatementContext;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.EncryptRuleAware;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.QueryWithCipherColumnAware;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.pojo.EncryptColumnNameToken;
import org.apache.shardingsphere.core.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.core.rewrite.sql.token.generator.aware.TableMetasAware;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.strategy.encrypt.EncryptTable;
import java.util.Collection;
import java.util.LinkedList;
/**
* Predicate column token generator for encrypt.
*
* @author panjuan
* @author zhangliang
*/
@Setter
public final class EncryptPredicateColumnTokenGenerator implements CollectionSQLTokenGenerator, TableMetasAware, EncryptRuleAware, QueryWithCipherColumnAware {
private TableMetas tableMetas;
private EncryptRule encryptRule;
private boolean queryWithCipherColumn;
@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
return sqlStatementContext.getSqlStatement() instanceof WhereSegmentAvailable && ((WhereSegmentAvailable) sqlStatementContext.getSqlStatement()).getWhere().isPresent();
}
@Override
public Collection<EncryptColumnNameToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
Preconditions.checkState(((WhereSegmentAvailable) sqlStatementContext.getSqlStatement()).getWhere().isPresent());
Collection<EncryptColumnNameToken> result = new LinkedList<>();
for (AndPredicate each : ((WhereSegmentAvailable) sqlStatementContext.getSqlStatement()).getWhere().get().getAndPredicates()) {
result.addAll(sss(sqlStatementContext, each));
}
return result;
}
private Collection<EncryptColumnNameToken> sss(final SQLStatementContext sqlStatementContext, final AndPredicate andPredicate) {
Collection<EncryptColumnNameToken> result = new LinkedList<>();
for (PredicateSegment each : andPredicate.getPredicates()) {
Optional<EncryptTable> encryptTable = findEncryptTable(sqlStatementContext, each);
if (!encryptTable.isPresent() || !encryptTable.get().findShardingEncryptor(each.getColumn().getName()).isPresent()) {
continue;
}
int startIndex = each.getColumn().getOwner().isPresent() ? each.getColumn().getOwner().get().getStopIndex() + 2 : each.getColumn().getStartIndex();
int stopIndex = each.getColumn().getStopIndex();
if (!queryWithCipherColumn) {
Optional<String> plainColumn = encryptTable.get().findPlainColumn(each.getColumn().getName());
if (plainColumn.isPresent()) {
result.add(new EncryptColumnNameToken(startIndex, stopIndex, plainColumn.get()));
continue;
}
}
Optional<String> assistedQueryColumn = encryptTable.get().findAssistedQueryColumn(each.getColumn().getName());
EncryptColumnNameToken encryptColumnNameToken = assistedQueryColumn.isPresent() ? new EncryptColumnNameToken(startIndex, stopIndex, assistedQueryColumn.get())
: new EncryptColumnNameToken(startIndex, stopIndex, encryptTable.get().getCipherColumn(each.getColumn().getName()));
result.add(encryptColumnNameToken);
}
return result;
}
private Optional<EncryptTable> findEncryptTable(final SQLStatementContext sqlStatementContext, final PredicateSegment segment) {
Optional<String> tableName = sqlStatementContext.getTablesContext().findTableName(segment.getColumn(), tableMetas);
return tableName.isPresent() ? encryptRule.findEncryptTable(tableName.get()) : Optional.<EncryptTable>absent();
}
}
......@@ -18,15 +18,15 @@
package org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.impl;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.Setter;
import org.apache.shardingsphere.core.metadata.table.TableMetas;
import org.apache.shardingsphere.core.parse.sql.statement.generic.WhereSegmentAvailable;
import org.apache.shardingsphere.core.preprocessor.statement.SQLStatementContext;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.EncryptCondition;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.EncryptConditionEngine;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.EncryptRuleAware;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.QueryWithCipherColumnAware;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.pojo.EncryptPredicateToken;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.pojo.EncryptPredicateRightValueToken;
import org.apache.shardingsphere.core.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.core.rewrite.sql.token.generator.aware.ParametersAware;
import org.apache.shardingsphere.core.rewrite.sql.token.generator.aware.TableMetasAware;
......@@ -40,13 +40,13 @@ import java.util.List;
import java.util.Map;
/**
* Predicate token generator for encrypt.
* Predicate right value token generator for encrypt.
*
* @author panjuan
* @author zhangliang
*/
@Setter
public final class EncryptPredicateTokenGenerator implements CollectionSQLTokenGenerator, TableMetasAware, EncryptRuleAware, ParametersAware, QueryWithCipherColumnAware {
public final class EncryptPredicateRightValueTokenGenerator implements CollectionSQLTokenGenerator, TableMetasAware, EncryptRuleAware, ParametersAware, QueryWithCipherColumnAware {
private TableMetas tableMetas;
......@@ -58,33 +58,31 @@ public final class EncryptPredicateTokenGenerator implements CollectionSQLTokenG
@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
return true;
return sqlStatementContext.getSqlStatement() instanceof WhereSegmentAvailable && ((WhereSegmentAvailable) sqlStatementContext.getSqlStatement()).getWhere().isPresent();
}
@Override
public Collection<EncryptPredicateToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
public Collection<EncryptPredicateRightValueToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
List<EncryptCondition> encryptConditions = new EncryptConditionEngine(encryptRule, tableMetas).createEncryptConditions(sqlStatementContext);
return encryptConditions.isEmpty() ? Collections.<EncryptPredicateToken>emptyList() : generateSQLTokens(encryptConditions);
return encryptConditions.isEmpty() ? Collections.<EncryptPredicateRightValueToken>emptyList() : generateSQLTokens(encryptConditions);
}
private Collection<EncryptPredicateToken> generateSQLTokens(final List<EncryptCondition> encryptConditions) {
Collection<EncryptPredicateToken> result = new LinkedList<>();
private Collection<EncryptPredicateRightValueToken> generateSQLTokens(final List<EncryptCondition> encryptConditions) {
Collection<EncryptPredicateRightValueToken> result = new LinkedList<>();
for (EncryptCondition each : encryptConditions) {
result.add(generateSQLToken(each));
}
return result;
}
private EncryptPredicateToken generateSQLToken(final EncryptCondition encryptCondition) {
private EncryptPredicateRightValueToken generateSQLToken(final EncryptCondition encryptCondition) {
List<Object> originalValues = encryptCondition.getValues(parameters);
return queryWithCipherColumn ? generateSQLTokenForQueryWithCipherColumn(encryptCondition, originalValues) : generateSQLTokenForQueryWithoutCipherColumn(encryptCondition, originalValues);
}
private EncryptPredicateToken generateSQLTokenForQueryWithCipherColumn(final EncryptCondition encryptCondition, final List<Object> originalValues) {
String encryptedColumnName = encryptRule.findAssistedQueryColumn(encryptCondition.getTableName(), encryptCondition.getColumnName())
.or(encryptRule.getCipherColumn(encryptCondition.getTableName(), encryptCondition.getColumnName()));
private EncryptPredicateRightValueToken generateSQLTokenForQueryWithCipherColumn(final EncryptCondition encryptCondition, final List<Object> originalValues) {
List<Object> encryptedValues = getEncryptedValues(encryptCondition, originalValues);
return new EncryptPredicateToken(encryptCondition.getStartIndex(), encryptCondition.getStopIndex(), encryptCondition.getColumnOwner(), encryptedColumnName,
return new EncryptPredicateRightValueToken(encryptCondition.getStartIndex(), encryptCondition.getStopIndex(),
getPositionValues(encryptCondition.getPositionValueMap().keySet(), encryptedValues), encryptCondition.getPositionIndexMap().keySet(), encryptCondition.getOperator());
}
......@@ -95,10 +93,8 @@ public final class EncryptPredicateTokenGenerator implements CollectionSQLTokenG
: encryptRule.getEncryptValues(encryptCondition.getTableName(), encryptCondition.getColumnName(), originalValues);
}
private EncryptPredicateToken generateSQLTokenForQueryWithoutCipherColumn(final EncryptCondition encryptCondition, final List<Object> originalValues) {
Optional<String> plainColumn = encryptRule.findPlainColumn(encryptCondition.getTableName(), encryptCondition.getColumnName());
Preconditions.checkState(plainColumn.isPresent(), "Plain column should be required.");
return new EncryptPredicateToken(encryptCondition.getStartIndex(), encryptCondition.getStopIndex(), encryptCondition.getColumnOwner(), plainColumn.get(),
private EncryptPredicateRightValueToken generateSQLTokenForQueryWithoutCipherColumn(final EncryptCondition encryptCondition, final List<Object> originalValues) {
return new EncryptPredicateRightValueToken(encryptCondition.getStartIndex(), encryptCondition.getStopIndex(),
getPositionValues(encryptCondition.getPositionValueMap().keySet(), originalValues), encryptCondition.getPositionIndexMap().keySet(), encryptCondition.getOperator());
}
......
......@@ -27,7 +27,7 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemsSegm
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.EncryptRuleAware;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.generator.QueryWithCipherColumnAware;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.pojo.EncryptProjectionToken;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.pojo.EncryptColumnNameToken;
import org.apache.shardingsphere.core.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.core.strategy.encrypt.EncryptTable;
......@@ -58,8 +58,8 @@ public final class EncryptProjectionTokenGenerator implements CollectionSQLToken
}
@Override
public Collection<EncryptProjectionToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
Collection<EncryptProjectionToken> result = new LinkedList<>();
public Collection<EncryptColumnNameToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
Collection<EncryptColumnNameToken> result = new LinkedList<>();
Optional<SelectItemsSegment> selectItemsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(SelectItemsSegment.class);
Preconditions.checkState(selectItemsSegment.isPresent());
String tableName = sqlStatementContext.getTablesContext().getSingleTableName();
......@@ -79,10 +79,10 @@ public final class EncryptProjectionTokenGenerator implements CollectionSQLToken
return selectItemSegment instanceof ColumnSelectItemSegment && encryptTable.getLogicColumns().contains(((ColumnSelectItemSegment) selectItemSegment).getName());
}
private EncryptProjectionToken generateSQLToken(final ColumnSelectItemSegment segment, final String tableName) {
private EncryptColumnNameToken generateSQLToken(final ColumnSelectItemSegment segment, final String tableName) {
Optional<String> plainColumn = encryptRule.findPlainColumn(tableName, segment.getName());
String columnName = plainColumn.isPresent() && !queryWithCipherColumn ? plainColumn.get() : encryptRule.getCipherColumn(tableName, segment.getName());
return segment.getOwner().isPresent() ? new EncryptProjectionToken(segment.getStartIndex(), segment.getStopIndex(), columnName, segment.getOwner().get().getTableName())
: new EncryptProjectionToken(segment.getStartIndex(), segment.getStopIndex(), columnName);
return segment.getOwner().isPresent() ? new EncryptColumnNameToken(segment.getOwner().get().getStopIndex() + 2, segment.getStopIndex(), columnName)
: new EncryptColumnNameToken(segment.getStartIndex(), segment.getStopIndex(), columnName);
}
}
......@@ -22,32 +22,25 @@ import org.apache.shardingsphere.core.rewrite.sql.token.pojo.SQLToken;
import org.apache.shardingsphere.core.rewrite.sql.token.pojo.Substitutable;
/**
* Encrypt projection token.
* Column name token of encrypt.
*
* @author panjuan
*/
public final class EncryptProjectionToken extends SQLToken implements Substitutable {
public final class EncryptColumnNameToken extends SQLToken implements Substitutable {
@Getter
private final int stopIndex;
private final String columnName;
private final String owner;
public EncryptProjectionToken(final int startIndex, final int stopIndex, final String columnName, final String owner) {
public EncryptColumnNameToken(final int startIndex, final int stopIndex, final String columnName) {
super(startIndex);
this.stopIndex = stopIndex;
this.columnName = columnName;
this.owner = owner;
}
public EncryptProjectionToken(final int startIndex, final int stopIndex, final String columnName) {
this(startIndex, stopIndex, columnName, null);
}
@Override
public String toString() {
return null == owner ? columnName : String.format("%s.%s", owner, columnName);
return columnName;
}
}
......@@ -27,32 +27,25 @@ import java.util.Collection;
import java.util.Map;
/**
* Predicate token for encrypt.
* Predicate right value token for encrypt.
*
* @author panjuan
*/
public final class EncryptPredicateToken extends SQLToken implements Substitutable {
public final class EncryptPredicateRightValueToken extends SQLToken implements Substitutable {
@Getter
private final int stopIndex;
private final String columnOwner;
private final String columnName;
private final Map<Integer, Object> indexValues;
private final Collection<Integer> parameterMarkerIndexes;
private final ShardingOperator operator;
public EncryptPredicateToken(final int startIndex, final int stopIndex,
final String columnOwner, final String columnName, final Map<Integer, Object> indexValues,
final Collection<Integer> parameterMarkerIndexes, final ShardingOperator operator) {
public EncryptPredicateRightValueToken(final int startIndex, final int stopIndex,
final Map<Integer, Object> indexValues, final Collection<Integer> parameterMarkerIndexes, final ShardingOperator operator) {
super(startIndex);
this.stopIndex = stopIndex;
this.columnOwner = columnOwner;
this.columnName = columnName;
this.indexValues = indexValues;
this.parameterMarkerIndexes = parameterMarkerIndexes;
this.operator = operator;
......@@ -72,15 +65,14 @@ public final class EncryptPredicateToken extends SQLToken implements Substitutab
private String toStringForEqual() {
if (parameterMarkerIndexes.isEmpty()) {
return indexValues.get(0) instanceof String ? String.format("%s = '%s'", getColumnNameWithOwner(), indexValues.get(0))
: String.format("%s = %s", getColumnNameWithOwner(), indexValues.get(0));
return indexValues.get(0) instanceof String ? String.format("'%s'", indexValues.get(0)) : indexValues.get(0).toString();
}
return String.format("%s = ?", getColumnNameWithOwner());
return "?";
}
private String toStringForIn() {
StringBuilder result = new StringBuilder();
result.append(getColumnNameWithOwner()).append(" ").append(operator.name()).append(" (");
result.append("(");
for (int i = 0; i < indexValues.size() + parameterMarkerIndexes.size(); i++) {
if (parameterMarkerIndexes.contains(i)) {
result.append("?");
......@@ -96,8 +88,4 @@ public final class EncryptPredicateToken extends SQLToken implements Substitutab
result.delete(result.length() - 2, result.length()).append(")");
return result.toString();
}
private String getColumnNameWithOwner() {
return null == columnOwner ? columnName : String.format("%s.%s", columnOwner, columnName);
}
}
......@@ -32,7 +32,7 @@ public final class EncryptConditionTest {
@Test
public void assertGetConditionValuesForEqual() {
List<Object> actual = new EncryptCondition("col", "tbl", null, 0, 0, new LiteralExpressionSegment(0, 0, 1)).getValues(Collections.emptyList());
List<Object> actual = new EncryptCondition("col", null, 0, 0, new LiteralExpressionSegment(0, 0, 1)).getValues(Collections.emptyList());
assertThat(actual.size(), is(1));
assertThat((Integer) actual.get(0), is(1));
}
......@@ -40,7 +40,7 @@ public final class EncryptConditionTest {
@Test
public void assertGetConditionValuesForIn() {
List<Object> actual = new EncryptCondition(
"col", "tbl", null, 0, 0, Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2))).getValues(Collections.emptyList());
"col", null, 0, 0, Arrays.<ExpressionSegment>asList(new LiteralExpressionSegment(0, 0, 1), new LiteralExpressionSegment(0, 0, 2))).getValues(Collections.emptyList());
assertThat(actual.size(), is(2));
assertThat((Integer) actual.get(0), is(1));
assertThat((Integer) actual.get(1), is(2));
......
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.core.rewrite.sql.impl;
import org.apache.shardingsphere.core.constant.ShardingOperator;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.pojo.EncryptPredicateToken;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.token.pojo.EncryptPredicateRightValueToken;
import org.junit.Test;
import java.util.Collections;
......@@ -34,28 +34,14 @@ public final class EncryptPredicateTokenTest {
public void assertToStringWithoutPlaceholderWithoutTableOwnerWithEqual() {
Map<Integer, Object> indexValues = new LinkedHashMap<>();
indexValues.put(0, "a");
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, null, "column_x", indexValues, Collections.<Integer>emptyList(), ShardingOperator.EQUAL);
assertThat(actual.toString(), is("column_x = 'a'"));
}
@Test
public void assertToStringWithoutPlaceholderWithTableOwnerWithEqual() {
Map<Integer, Object> indexValues = new LinkedHashMap<>();
indexValues.put(0, "a");
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, "table_x", "column_x", indexValues, Collections.<Integer>emptyList(), ShardingOperator.EQUAL);
assertThat(actual.toString(), is("table_x.column_x = 'a'"));
EncryptPredicateRightValueToken actual = new EncryptPredicateRightValueToken(0, 0, indexValues, Collections.<Integer>emptyList(), ShardingOperator.EQUAL);
assertThat(actual.toString(), is("'a'"));
}
@Test
public void assertToStringWithPlaceholderWithoutTableOwnerWithEqual() {
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, null, "column_x", Collections.<Integer, Object>emptyMap(), Collections.singletonList(0), ShardingOperator.EQUAL);
assertThat(actual.toString(), is("column_x = ?"));
}
@Test
public void assertToStringWithPlaceholderWithTableOwnerWithEqual() {
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, "table_x", "column_x", Collections.<Integer, Object>emptyMap(), Collections.singletonList(0), ShardingOperator.EQUAL);
assertThat(actual.toString(), is("table_x.column_x = ?"));
EncryptPredicateRightValueToken actual = new EncryptPredicateRightValueToken(0, 0, Collections.<Integer, Object>emptyMap(), Collections.singletonList(0), ShardingOperator.EQUAL);
assertThat(actual.toString(), is("?"));
}
@Test
......@@ -63,28 +49,13 @@ public final class EncryptPredicateTokenTest {
Map<Integer, Object> indexValues = new LinkedHashMap<>();
indexValues.put(0, "a");
indexValues.put(1, "b");
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, null, "column_x", indexValues, Collections.<Integer>emptyList(), ShardingOperator.IN);
assertThat(actual.toString(), is("column_x IN ('a', 'b')"));
}
@Test
public void assertToStringWithoutPlaceholderWithTableOwnerWithIn() {
Map<Integer, Object> indexValues = new LinkedHashMap<>();
indexValues.put(0, "a");
indexValues.put(1, "b");
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, "table_x", "column_x", indexValues, Collections.<Integer>emptyList(), ShardingOperator.IN);
assertThat(actual.toString(), is("table_x.column_x IN ('a', 'b')"));
EncryptPredicateRightValueToken actual = new EncryptPredicateRightValueToken(0, 0, indexValues, Collections.<Integer>emptyList(), ShardingOperator.IN);
assertThat(actual.toString(), is("('a', 'b')"));
}
@Test
public void assertToStringWithPlaceholderWithoutTableOwnerWithIn() {
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, null, "column_x", Collections.<Integer, Object>emptyMap(), Collections.singletonList(0), ShardingOperator.IN);
assertThat(actual.toString(), is("column_x IN (?)"));
}
@Test
public void assertToStringWithPlaceholderWithTableOwnerWithIn() {
EncryptPredicateToken actual = new EncryptPredicateToken(0, 0, "table_x", "column_x", Collections.<Integer, Object>emptyMap(), Collections.singletonList(0), ShardingOperator.IN);
assertThat(actual.toString(), is("table_x.column_x IN (?)"));
EncryptPredicateRightValueToken actual = new EncryptPredicateRightValueToken(0, 0, Collections.<Integer, Object>emptyMap(), Collections.singletonList(0), ShardingOperator.IN);
assertThat(actual.toString(), is("(?)"));
}
}
......@@ -21,7 +21,7 @@
<input sql="UPDATE t_account SET account_id = ?, password = ?, amount = ?, status = ? WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK, 1, aaa, 1000, OK" />
<output sql="UPDATE t_account SET account_id = ?, cipher_password = ?, assisted_query_password = ?, cipher_amount = ?, status = ? WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, encrypt_aaa, assisted_query_aaa, encrypt_1000, OK, 1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="update_for_literals">
<input sql="UPDATE t_account SET account_id = 1, password = 'aaa', amount = 1000, status = 'OK' WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="UPDATE t_account SET account_id = 1, cipher_password = 'encrypt_aaa', assisted_query_password = 'assisted_query_aaa', cipher_amount = 'encrypt_1000', status = 'OK' WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
......@@ -31,7 +31,7 @@
<input sql="UPDATE t_account_bak SET account_id = ?, password = ?, amount = ?, status = ? WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK, 1, aaa, 1000, OK" />
<output sql="UPDATE t_account_bak SET account_id = ?, cipher_password = ?, assisted_query_password = ?, plain_password = ?, cipher_amount = ?, plain_amount = ?, status = ? WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, encrypt_aaa, assisted_query_aaa, aaa, encrypt_1000, 1000, OK, 1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="update_plain_for_literals">
<input sql="UPDATE t_account_bak SET account_id = 1, password = 'aaa', amount = 1000, status = 'OK' WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="UPDATE t_account_bak SET account_id = 1, cipher_password = 'encrypt_aaa', assisted_query_password = 'assisted_query_aaa', plain_password = 'aaa', cipher_amount = 'encrypt_1000', plain_amount = 1000, status = 'OK' WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
......
......@@ -21,7 +21,7 @@
<input sql="UPDATE t_account_bak SET account_id = ?, password = ?, amount = ?, status = ? WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK, 1, aaa, 1000, OK" />
<output sql="UPDATE t_account_bak SET account_id = ?, cipher_password = ?, assisted_query_password = ?, plain_password = ?, cipher_amount = ?, plain_amount = ?, status = ? WHERE account_id = ? AND plain_password = ? AND plain_amount = ? AND status = ?" parameters="1, encrypt_aaa, assisted_query_aaa, aaa, encrypt_1000, 1000, OK, 1, aaa, 1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="update_plain_for_literals">
<input sql="UPDATE t_account_bak SET account_id = 1, password = 'aaa', amount = 1000, status = 'OK' WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="UPDATE t_account_bak SET account_id = 1, cipher_password = 'encrypt_aaa', assisted_query_password = 'assisted_query_aaa', plain_password = 'aaa', cipher_amount = 'encrypt_1000', plain_amount = 1000, status = 'OK' WHERE account_id = 1 AND plain_password = 'aaa' AND plain_amount = 1000 AND status = 'OK'" />
......
......@@ -101,7 +101,7 @@
<!--<input sql="INSERT INTO t_account_bak VALUES (?, ?, ?), ('bbb', 2000, 'OK'), (?, ?, ?), ('ddd', 4000, 'OK')" parameters="aaa, 1000, OK, ccc, 3000, OK" />-->
<!--<output sql="INSERT INTO t_account_bak_1(cipher_password, cipher_amount, status, account_id, assisted_query_password, plain_password, plain_amount) VALUES (?, ?, ?, ?, ?, ?, ?), ('encrypt_bbb', 'encrypt_2000', 'OK', ?, 'assisted_query_bbb', 'bbb', 2000), (?, ?, ?, ?, ?, ?, ?), ('encrypt_ddd', 'encrypt_4000', 'OK', ?, 'assisted_query_ddd', 'ddd', 4000)" parameters="encrypt_aaa, encrypt_1000, OK, 1, assisted_query_aaa, aaa, 1000, 1, encrypt_ccc, encrypt_3000, OK, 1, assisted_query_ccc, ccc, 3000, 1" />-->
<!--</rewrite-assertion>-->
<rewrite-assertion id="insert_values_without_columns_with_plain_without_id_for_literals">
<input sql="INSERT INTO t_account_bak VALUES ('aaa', 1000, 'OK'), ('bbb', 2000, 'OK'), ('ccc', 3000, 'OK'), ('ddd', 4000, 'OK')" />
<output sql="INSERT INTO t_account_bak_1(cipher_password, cipher_amount, status, account_id, assisted_query_password, plain_password, plain_amount) VALUES ('encrypt_aaa', 'encrypt_1000', 'OK', 1, 'assisted_query_aaa', 'aaa', 1000), ('encrypt_bbb', 'encrypt_2000', 'OK', 1, 'assisted_query_bbb', 'bbb', 2000), ('encrypt_ccc', 'encrypt_3000', 'OK', 1, 'assisted_query_ccc', 'ccc', 3000), ('encrypt_ddd', 'encrypt_4000', 'OK', 1, 'assisted_query_ddd', 'ddd', 4000)" />
......
......@@ -37,9 +37,8 @@
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = 1 AND a.assisted_query_password = 'assisted_query_aaa' AND a.cipher_amount = 'encrypt_1000' AND a.status = 'OK'" />
</rewrite-assertion>
<!-- FIXME -->
<!--<rewrite-assertion id="select_with_table_as_qualified_name">-->
<!--<input sql="SELECT t_account.account_id, t_account.password, t_account.amount AS a, status AS s FROM t_account WHERE t_account.account_id = 1 AND t_account.password = 'aaa' AND t_account.amount = 1000 AND t_account.status = 'OK'" />-->
<!--<output sql="SELECT t_account_1.account_id, t_account_1.password, t_account_1.amount AS a, status AS s FROM t_account_1 WHERE t_account_1.account_id = 1 AND t_account_1.assisted_query_password = 'assisted_query_aaa' AND t_account_1.cipher_amount = 'encrypt_1000' AND t_account_1.status = 'OK'" />-->
<!--</rewrite-assertion>-->
<rewrite-assertion id="select_with_table_as_qualified_name">
<input sql="SELECT t_account.account_id, t_account.password, t_account.amount AS a, status AS s FROM t_account WHERE t_account.account_id = 1 AND t_account.password = 'aaa' AND t_account.amount = 1000 AND t_account.status = 'OK'" />
<output sql="SELECT t_account_1.account_id, t_account_1.cipher_password, t_account_1.cipher_amount AS a, status AS s FROM t_account_1 WHERE t_account_1.account_id = 1 AND t_account_1.assisted_query_password = 'assisted_query_aaa' AND t_account_1.cipher_amount = 'encrypt_1000' AND t_account_1.status = 'OK'" />
</rewrite-assertion>
</rewrite-assertions>
......@@ -21,7 +21,7 @@
<input sql="UPDATE t_account SET account_id = ?, password = ?, amount = ?, status = ? WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK, 1, aaa, 1000, OK" />
<output sql="UPDATE t_account_1 SET account_id = ?, cipher_password = ?, assisted_query_password = ?, cipher_amount = ?, status = ? WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, encrypt_aaa, assisted_query_aaa, encrypt_1000, OK, 1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="update_for_literals">
<input sql="UPDATE t_account SET account_id = 1, password = 'aaa', amount = 1000, status = 'OK' WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="UPDATE t_account_1 SET account_id = 1, cipher_password = 'encrypt_aaa', assisted_query_password = 'assisted_query_aaa', cipher_amount = 'encrypt_1000', status = 'OK' WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
......@@ -31,7 +31,7 @@
<input sql="UPDATE t_account_bak SET account_id = ?, password = ?, amount = ?, status = ? WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK, 1, aaa, 1000, OK" />
<output sql="UPDATE t_account_bak_1 SET account_id = ?, cipher_password = ?, assisted_query_password = ?, plain_password = ?, cipher_amount = ?, plain_amount = ?, status = ? WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, encrypt_aaa, assisted_query_aaa, aaa, encrypt_1000, 1000, OK, 1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="update_plain_for_literals">
<input sql="UPDATE t_account_bak SET account_id = 1, password = 'aaa', amount = 1000, status = 'OK' WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="UPDATE t_account_bak_1 SET account_id = 1, cipher_password = 'encrypt_aaa', assisted_query_password = 'assisted_query_aaa', plain_password = 'aaa', cipher_amount = 'encrypt_1000', plain_amount = 1000, status = 'OK' WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
......
......@@ -21,7 +21,7 @@
<input sql="UPDATE t_account_bak SET account_id = ?, password = ?, amount = ?, status = ? WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK, 1, aaa, 1000, OK" />
<output sql="UPDATE t_account_bak_1 SET account_id = ?, cipher_password = ?, assisted_query_password = ?, plain_password = ?, cipher_amount = ?, plain_amount = ?, status = ? WHERE account_id = ? AND plain_password = ? AND plain_amount = ? AND status = ?" parameters="1, encrypt_aaa, assisted_query_aaa, aaa, encrypt_1000, 1000, OK, 1, aaa, 1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="update_plain_for_literals">
<input sql="UPDATE t_account_bak SET account_id = 1, password = 'aaa', amount = 1000, status = 'OK' WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="UPDATE t_account_bak_1 SET account_id = 1, cipher_password = 'encrypt_aaa', assisted_query_password = 'assisted_query_aaa', plain_password = 'aaa', cipher_amount = 'encrypt_1000', plain_amount = 1000, status = 'OK' WHERE account_id = 1 AND plain_password = 'aaa' AND plain_amount = 1000 AND status = 'OK'" />
......
......@@ -316,4 +316,9 @@
<!--<output sql="SELECT * FROM (SELECT TOP(2147483647) row_number() OVER (ORDER BY o.account_id.id) AS rownum_, o.account_id FROM t_account_0 o WHERE o.account_id IN (100, 101) GROUP BY account_id ORDER BY account_id DESC) AS row_ WHERE row_.rownum_ &gt; 0" />-->
<!--<output sql="SELECT * FROM (SELECT TOP(2147483647) row_number() OVER (ORDER BY o.account_id.id) AS rownum_, o.account_id FROM t_account_1 o WHERE o.account_id IN (100, 101) GROUP BY account_id ORDER BY account_id DESC) AS row_ WHERE row_.rownum_ &gt; 0" />-->
<!--</rewrite-assertion>-->
<rewrite-assertion id="select_with_table_as_qualified_name">
<input sql="SELECT t_account.account_id, t_account.password, t_account.amount AS a, status AS s FROM t_account WHERE t_account.account_id = 1 AND t_account.password = 'aaa' AND t_account.amount = 1000 AND t_account.status = 'OK'" />
<output sql="SELECT t_account_1.account_id, t_account_1.password, t_account_1.amount AS a, status AS s FROM t_account_1 WHERE t_account_1.account_id = 1 AND t_account_1.password = 'aaa' AND t_account_1.amount = 1000 AND t_account_1.status = 'OK'" />
</rewrite-assertion>
</rewrite-assertions>
......@@ -21,7 +21,7 @@
<input sql="UPDATE t_account SET status = ? WHERE account_id = ?" parameters="'OK', 100" />
<output sql="UPDATE t_account_0 SET status = ? WHERE account_id = ?" parameters="'OK', 100" />
</rewrite-assertion>
<rewrite-assertion id="update_with_sharding_value_for_literals">
<input sql="UPDATE t_account SET status = 'OK' WHERE account_id = 100" />
<output sql="UPDATE t_account_0 SET status = 'OK' WHERE account_id = 100" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册