Skip to content

Commit

Permalink
Merge pull request #2342 from tristaZero/dev
Browse files Browse the repository at this point in the history
Move encrypting processing in SQLRewriteEngine to ShardingEncryptorEngine.
  • Loading branch information
terrymanu authored May 7, 2019
2 parents 7380165 + c012463 commit 23ffeb3
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

package org.apache.shardingsphere.core.strategy.encrypt;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptorRuleConfiguration;
import org.apache.shardingsphere.core.rule.ColumnNode;
import org.apache.shardingsphere.spi.encrypt.ShardingEncryptor;
import org.apache.shardingsphere.spi.encrypt.ShardingQueryAssistedEncryptor;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -123,4 +129,43 @@ public Collection<String> getEncryptTableNames() {
}
return result;
}

/**
* Get encrypt assisted column values.
*
* @param columnNode column node
* @param originalColumnValues original column values
* @return assisted column values
*/
public List<Comparable<?>> getEncryptAssistedColumnValues(final ColumnNode columnNode, final List<Comparable<?>> originalColumnValues) {
final Optional<ShardingEncryptor> shardingEncryptor = getShardingEncryptor(columnNode.getTableName(), columnNode.getColumnName());
Preconditions.checkArgument(shardingEncryptor.isPresent() && shardingEncryptor.get() instanceof ShardingQueryAssistedEncryptor,
String.format("Can not find ShardingQueryAssistedEncryptor by %s.", columnNode));
return Lists.transform(originalColumnValues, new Function<Comparable<?>, Comparable<?>>() {

@Override
public Comparable<?> apply(final Comparable<?> input) {
return ((ShardingQueryAssistedEncryptor) shardingEncryptor.get()).queryAssistedEncrypt(input.toString());
}
});
}

/**
* get encrypt column values.
*
* @param columnNode column node
* @param originalColumnValues original column values
* @return encrypt column values
*/
public List<Comparable<?>> getEncryptColumnValues(final ColumnNode columnNode, final List<Comparable<?>> originalColumnValues) {
final Optional<ShardingEncryptor> shardingEncryptor = getShardingEncryptor(columnNode.getTableName(), columnNode.getColumnName());
Preconditions.checkArgument(shardingEncryptor.isPresent(), String.format("Can not find ShardingEncryptor by %s.", columnNode));
return Lists.transform(originalColumnValues, new Function<Comparable<?>, Comparable<?>>() {

@Override
public Comparable<?> apply(final Comparable<?> input) {
return String.valueOf(shardingEncryptor.get().encrypt(input.toString()));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
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.antlr.sql.token.EncryptColumnToken;
import org.apache.shardingsphere.core.parse.antlr.sql.token.SQLToken;
import org.apache.shardingsphere.core.parse.old.parser.context.condition.Condition;
import org.apache.shardingsphere.core.parse.old.parser.context.condition.Conditions;
import org.apache.shardingsphere.core.parse.old.parser.context.table.Tables;

Expand Down Expand Up @@ -80,4 +83,33 @@ public final List<SQLToken> getSQLTokens() {
Collections.sort(sqlTokens);
return sqlTokens;
}

/**
* Get encrypt condition.
*
* @param encryptColumnToken encrypt column token
* @return encrypt condition
*/
public Optional<Condition> getEncryptCondition(final EncryptColumnToken encryptColumnToken) {
List<Condition> conditions = encryptConditions.getOrCondition().findConditions(encryptColumnToken.getColumn());
if (0 == conditions.size()) {
return Optional.absent();
}
if (1 == conditions.size()) {
return Optional.of(conditions.iterator().next());
}
return Optional.of(conditions.get(getEncryptConditionIndex(encryptColumnToken)));
}

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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.parse.old.parser.context.condition.Column;
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLExpression;
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLNumberExpression;
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLParameterMarkerExpression;
import org.apache.shardingsphere.core.parse.old.parser.expression.SQLTextExpression;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Update statement.
*
* @author zhangliang
* @author panjuan
*/
@ToString(callSuper = true)
@Getter
Expand All @@ -45,4 +51,35 @@ public final class UpdateStatement extends DMLStatement {
private int whereParameterStartIndex;

private int whereParameterEndIndex;

/**
* Get column value.
*
* @param column column
* @param parameters parameters
* @return column value
*/
public Comparable<?> getColumnValue(final Column column, final List<Object> parameters) {
SQLExpression sqlExpression = assignments.get(column);
if (sqlExpression instanceof SQLParameterMarkerExpression) {
return parameters.get(((SQLParameterMarkerExpression) sqlExpression).getIndex()).toString();
}
if (sqlExpression instanceof SQLTextExpression) {
return ((SQLTextExpression) sqlExpression).getText();
}
if (sqlExpression instanceof SQLNumberExpression) {
return (Comparable) ((SQLNumberExpression) sqlExpression).getNumber();
}
throw new ShardingException("Can not find column value by %s.", column);
}

/**
* Is SQL parameter marker expression.
*
* @param column column
* @return SQL parameter marker expression or nott
*/
public boolean isSQLParameterMarkerExpression(final Column column) {
return assignments.get(column) instanceof SQLParameterMarkerExpression;
}
}
Loading

0 comments on commit 23ffeb3

Please sign in to comment.