Skip to content

Commit

Permalink
Merge 935d407 into eb8b6ac
Browse files Browse the repository at this point in the history
  • Loading branch information
SteNicholas committed Dec 13, 2019
2 parents eb8b6ac + 935d407 commit 1d83546
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Expand Up @@ -19,6 +19,7 @@

import com.google.common.base.Optional;
import lombok.Setter;
import org.apache.shardingsphere.core.constant.ShardingOperator;
import org.apache.shardingsphere.core.metadata.table.TableMetas;
import org.apache.shardingsphere.sql.parser.sql.statement.generic.WhereSegmentAvailable;
import org.apache.shardingsphere.sql.parser.relation.statement.SQLStatementContext;
Expand Down Expand Up @@ -79,8 +80,9 @@ private EncryptPredicateRightValueToken generateSQLToken(final EncryptCondition

private EncryptPredicateRightValueToken generateSQLTokenForQueryWithCipherColumn(final EncryptCondition encryptCondition, final List<Object> originalValues) {
List<Object> encryptedValues = getEncryptedValues(encryptCondition, originalValues);
return new EncryptPredicateRightValueToken(encryptCondition.getStartIndex(), encryptCondition.getStopIndex(),
getPositionValues(encryptCondition.getPositionValueMap().keySet(), encryptedValues), encryptCondition.getPositionIndexMap().keySet(), encryptCondition.getOperator());
int startIndex = encryptCondition.getOperator().equals(ShardingOperator.IN) ? encryptCondition.getStartIndex() - 1 : encryptCondition.getStartIndex();
return new EncryptPredicateRightValueToken(startIndex, encryptCondition.getStopIndex(), getPositionValues(encryptCondition.getPositionValueMap().keySet(), encryptedValues),
encryptCondition.getPositionIndexMap().keySet(), encryptCondition.getOperator());
}

private List<Object> getEncryptedValues(final EncryptCondition encryptCondition, final List<Object> originalValues) {
Expand Down
Expand Up @@ -82,7 +82,7 @@ private String getConjunctionText(final SQLToken sqlToken) {

private int getStartIndex(final SQLToken sqlToken) {
int startIndex = sqlToken instanceof Substitutable ? ((Substitutable) sqlToken).getStopIndex() + 1 : sqlToken.getStartIndex();
return startIndex > logicSQL.length() ? logicSQL.length() : startIndex;
return Math.min(startIndex, logicSQL.length());
}

private int getStopIndex(final SQLToken sqlToken) {
Expand Down
Expand Up @@ -54,6 +54,8 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab

private static final String SELECT_FULL_SQL = "select id, cipher_pwd, plain_pwd, assist_pwd from t_query_and_plain_encrypt";

private static final String SELECT_SQL_WITH_IN_OPERATOR = "select * from t_query_encrypt where pwd IN (?)";

@Test
public void assertSqlShow() throws SQLException {
assertTrue(getEncryptConnectionWithProps().getRuntimeContext().getProps().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
Expand Down Expand Up @@ -220,4 +222,17 @@ public void assertQueryWithEmptyString() throws SQLException {
statement.executeQuery();
}
}

@Test
public void assertSelectWithInOperator() throws SQLException {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(SELECT_SQL_WITH_IN_OPERATOR)) {
statement.setObject(1, 'a');
ResultSetMetaData metaData = statement.executeQuery().getMetaData();
assertThat(metaData.getColumnCount(), is(2));
for (int i = 0; i < metaData.getColumnCount(); i++) {
assertThat(metaData.getColumnLabel(1), is("id"));
assertThat(metaData.getColumnLabel(2), is("pwd"));
}
}
}
}

0 comments on commit 1d83546

Please sign in to comment.