Skip to content

Commit

Permalink
Remove DatabaseAware and refactor encrypt token generator logic
Browse files Browse the repository at this point in the history
  • Loading branch information
strongduanmu committed Jan 22, 2025
1 parent d2f80d8 commit 6043b04
Show file tree
Hide file tree
Showing 22 changed files with 61 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void decorate(final EncryptRule rule, final ConfigurationProperties props
.build(new EncryptParameterRewritersRegistry(rule, sqlRewriteContext.getDatabase().getName(), encryptConditions));
rewriteParameters(sqlRewriteContext, parameterRewriters);
}
SQLTokenGeneratorBuilder sqlTokenGeneratorBuilder = new EncryptTokenGenerateBuilder(rule, sqlStatementContext, encryptConditions, sqlRewriteContext.getDatabase());
SQLTokenGeneratorBuilder sqlTokenGeneratorBuilder = new EncryptTokenGenerateBuilder(sqlStatementContext, encryptConditions, rule, sqlRewriteContext.getDatabase());
sqlRewriteContext.addSQLTokenGenerators(sqlTokenGeneratorBuilder.getSQLTokenGenerators());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.SQLTokenGenerator;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.DatabaseAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.builder.SQLTokenGeneratorBuilder;

import java.util.Collection;
Expand All @@ -53,30 +52,30 @@
@RequiredArgsConstructor
public final class EncryptTokenGenerateBuilder implements SQLTokenGeneratorBuilder {

private final EncryptRule rule;

private final SQLStatementContext sqlStatementContext;

private final Collection<EncryptCondition> encryptConditions;

private final EncryptRule rule;

private final ShardingSphereDatabase database;

@Override
public Collection<SQLTokenGenerator> getSQLTokenGenerators() {
Collection<SQLTokenGenerator> result = new LinkedList<>();
addSQLTokenGenerator(result, new EncryptSelectProjectionTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertSelectProjectionTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertAssignmentTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptUpdateAssignmentTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertAssignmentTokenGenerator(rule, database));
addSQLTokenGenerator(result, new EncryptUpdateAssignmentTokenGenerator(rule, database));
addSQLTokenGenerator(result, new EncryptPredicateColumnTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertPredicateColumnTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptPredicateValueTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertPredicateValueTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertValuesTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptPredicateValueTokenGenerator(rule, database));
addSQLTokenGenerator(result, new EncryptInsertPredicateValueTokenGenerator(rule, database));
addSQLTokenGenerator(result, new EncryptInsertValuesTokenGenerator(rule, database));
addSQLTokenGenerator(result, new EncryptInsertDefaultColumnsTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertCipherNameTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertDerivedColumnsTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertOnUpdateTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptInsertOnUpdateTokenGenerator(rule, database));
addSQLTokenGenerator(result, new EncryptGroupByItemTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptIndexColumnTokenGenerator(rule));
addSQLTokenGenerator(result, new EncryptCreateTableTokenGenerator(rule));
Expand All @@ -92,9 +91,6 @@ private void addSQLTokenGenerator(final Collection<SQLTokenGenerator> sqlTokenGe
}

private void setUpSQLTokenGenerator(final SQLTokenGenerator toBeAddedSQLTokenGenerator) {
if (toBeAddedSQLTokenGenerator instanceof DatabaseAware) {
((DatabaseAware) toBeAddedSQLTokenGenerator).setDatabase(database);
}
if (toBeAddedSQLTokenGenerator instanceof EncryptConditionsAware) {
((EncryptConditionsAware) toBeAddedSQLTokenGenerator).setEncryptConditions(encryptConditions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.DatabaseAware;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
Expand All @@ -37,11 +36,11 @@
@HighFrequencyInvocation
@RequiredArgsConstructor
@Setter
public final class EncryptInsertAssignmentTokenGenerator implements CollectionSQLTokenGenerator<InsertStatementContext>, DatabaseAware {
public final class EncryptInsertAssignmentTokenGenerator implements CollectionSQLTokenGenerator<InsertStatementContext> {

private final EncryptRule rule;

private ShardingSphereDatabase database;
private final ShardingSphereDatabase database;

@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.DatabaseAware;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
Expand All @@ -37,11 +36,11 @@
@HighFrequencyInvocation
@RequiredArgsConstructor
@Setter
public final class EncryptUpdateAssignmentTokenGenerator implements CollectionSQLTokenGenerator<UpdateStatementContext>, DatabaseAware {
public final class EncryptUpdateAssignmentTokenGenerator implements CollectionSQLTokenGenerator<UpdateStatementContext> {

private final EncryptRule rule;

private ShardingSphereDatabase database;
private final ShardingSphereDatabase database;

@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.encrypt.exception.syntax.UnsupportedEncryptSQLException;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.DatabaseAware;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptAssignmentToken;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptFunctionAssignmentToken;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptLiteralAssignmentToken;
Expand Down Expand Up @@ -57,11 +56,11 @@
@HighFrequencyInvocation
@RequiredArgsConstructor
@Setter
public final class EncryptInsertOnUpdateTokenGenerator implements CollectionSQLTokenGenerator<InsertStatementContext>, DatabaseAware {
public final class EncryptInsertOnUpdateTokenGenerator implements CollectionSQLTokenGenerator<InsertStatementContext> {

private final EncryptRule rule;

private ShardingSphereDatabase database;
private final ShardingSphereDatabase database;

@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.DatabaseAware;
import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptInsertValuesToken;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn;
Expand Down Expand Up @@ -58,13 +57,13 @@
*/
@RequiredArgsConstructor
@Setter
public final class EncryptInsertValuesTokenGenerator implements OptionalSQLTokenGenerator<InsertStatementContext>, PreviousSQLTokensAware, DatabaseAware {
public final class EncryptInsertValuesTokenGenerator implements OptionalSQLTokenGenerator<InsertStatementContext>, PreviousSQLTokensAware {

private final EncryptRule rule;

private List<SQLToken> previousSQLTokens;
private final ShardingSphereDatabase database;

private ShardingSphereDatabase database;
private List<SQLToken> previousSQLTokens;

@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.DatabaseAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.ParametersAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken;

Expand All @@ -40,16 +39,16 @@
@HighFrequencyInvocation
@RequiredArgsConstructor
@Setter
public final class EncryptInsertPredicateValueTokenGenerator implements CollectionSQLTokenGenerator<SQLStatementContext>, ParametersAware, EncryptConditionsAware, DatabaseAware {
public final class EncryptInsertPredicateValueTokenGenerator implements CollectionSQLTokenGenerator<SQLStatementContext>, ParametersAware, EncryptConditionsAware {

private final EncryptRule rule;

private final ShardingSphereDatabase database;

private List<Object> parameters;

private Collection<EncryptCondition> encryptConditions;

private ShardingSphereDatabase database;

@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
return sqlStatementContext instanceof InsertStatementContext && null != ((InsertStatementContext) sqlStatementContext).getInsertSelectContext()
Expand All @@ -58,10 +57,9 @@ public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext)

@Override
public Collection<SQLToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
EncryptPredicateValueTokenGenerator generator = new EncryptPredicateValueTokenGenerator(rule);
EncryptPredicateValueTokenGenerator generator = new EncryptPredicateValueTokenGenerator(rule, database);
generator.setParameters(parameters);
generator.setEncryptConditions(encryptConditions);
generator.setDatabase(database);
return generator.generateSQLTokens(((InsertStatementContext) sqlStatementContext).getInsertSelectContext().getSelectStatementContext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ private SubstitutableColumnNameToken buildSubstitutableColumnNameToken(final Enc
if (includesLike(andPredicates, columnSegment)) {
Optional<LikeQueryColumnItem> likeQueryColumnItem = encryptColumn.getLikeQuery();
Preconditions.checkState(likeQueryColumnItem.isPresent());
return new SubstitutableColumnNameToken(
startIndex, stopIndex, createColumnProjections(likeQueryColumnItem.get().getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType), databaseType);
return new SubstitutableColumnNameToken(startIndex, stopIndex,
createColumnProjections(likeQueryColumnItem.get().getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType), databaseType);
}
Collection<Projection> columnProjections =
encryptColumn.getAssistedQuery().map(optional -> createColumnProjections(optional.getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType))
.orElseGet(() -> createColumnProjections(encryptColumn.getCipher().getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType));
Collection<Projection> columnProjections = encryptColumn.getAssistedQuery()
.map(optional -> createColumnProjections(optional.getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType))
.orElseGet(() -> createColumnProjections(encryptColumn.getCipher().getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType));
return new SubstitutableColumnNameToken(startIndex, stopIndex, columnProjections, databaseType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.DatabaseAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.generator.aware.ParametersAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
Expand All @@ -57,16 +56,16 @@
@HighFrequencyInvocation
@RequiredArgsConstructor
@Setter
public final class EncryptPredicateValueTokenGenerator implements CollectionSQLTokenGenerator<SQLStatementContext>, ParametersAware, EncryptConditionsAware, DatabaseAware {
public final class EncryptPredicateValueTokenGenerator implements CollectionSQLTokenGenerator<SQLStatementContext>, ParametersAware, EncryptConditionsAware {

private final EncryptRule rule;

private final ShardingSphereDatabase database;

private List<Object> parameters;

private Collection<EncryptCondition> encryptConditions;

private ShardingSphereDatabase database;

@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
return sqlStatementContext instanceof WhereAvailable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext)

@Override
public Collection<SQLToken> generateSQLTokens(final InsertStatementContext sqlStatementContext) {
return new EncryptProjectionTokenGenerator(
previousSQLTokens, rule, sqlStatementContext.getDatabaseType()).generateSQLTokens(sqlStatementContext.getInsertSelectContext().getSelectStatementContext());
return new EncryptProjectionTokenGenerator(previousSQLTokens, sqlStatementContext.getDatabaseType(), rule)
.generateSQLTokens(sqlStatementContext.getInsertSelectContext().getSelectStatementContext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public final class EncryptProjectionTokenGenerator {

private final List<SQLToken> previousSQLTokens;

private final EncryptRule rule;

private final DatabaseType databaseType;

private final EncryptRule rule;

/**
* Generate SQL tokens.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext)

@Override
public Collection<SQLToken> generateSQLTokens(final SelectStatementContext sqlStatementContext) {
return new EncryptProjectionTokenGenerator(previousSQLTokens, rule, sqlStatementContext.getDatabaseType()).generateSQLTokens(sqlStatementContext);
return new EncryptProjectionTokenGenerator(previousSQLTokens, sqlStatementContext.getDatabaseType(), rule).generateSQLTokens(sqlStatementContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private Optional<SubstitutableColumnNameToken> generateSQLToken(final ColumnSegm
int startIndex = columnSegment.getOwner().isPresent() ? columnSegment.getOwner().get().getStopIndex() + 2 : columnSegment.getStartIndex();
int stopIndex = columnSegment.getStopIndex();
return Optional.of(encryptColumn.getAssistedQuery()
.map(optional -> new SubstitutableColumnNameToken(
startIndex, stopIndex, createColumnProjections(optional.getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType), databaseType))
.map(optional -> new SubstitutableColumnNameToken(startIndex, stopIndex, createColumnProjections(optional.getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType),
databaseType))
.orElseGet(() -> new SubstitutableColumnNameToken(startIndex, stopIndex,
createColumnProjections(encryptColumn.getCipher().getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType), databaseType)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void assertGetSQLTokenGenerators() {
when(selectStatementContext.getOrderByContext().getItems()).thenReturn(Collections.singleton(mock(OrderByItem.class)));
when(selectStatementContext.getGroupByContext().getItems()).thenReturn(Collections.emptyList());
when(selectStatementContext.getWhereSegments()).thenReturn(Collections.emptyList());
EncryptTokenGenerateBuilder encryptTokenGenerateBuilder = new EncryptTokenGenerateBuilder(rule, selectStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class));
EncryptTokenGenerateBuilder encryptTokenGenerateBuilder = new EncryptTokenGenerateBuilder(selectStatementContext, Collections.emptyList(), rule, mock(ShardingSphereDatabase.class));
Collection<SQLTokenGenerator> sqlTokenGenerators = encryptTokenGenerateBuilder.getSQLTokenGenerators();
assertThat(sqlTokenGenerators.size(), is(3));
Iterator<SQLTokenGenerator> iterator = sqlTokenGenerators.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.table.EncryptTable;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -49,7 +50,7 @@ class EncryptInsertAssignmentTokenGeneratorTest {

@BeforeEach
void setup() {
tokenGenerator = new EncryptInsertAssignmentTokenGenerator(mockEncryptRule());
tokenGenerator = new EncryptInsertAssignmentTokenGenerator(mockEncryptRule(), mock(ShardingSphereDatabase.class));
when(insertStatementContext.getTablesContext().getSimpleTables().iterator().next().getTableName().getIdentifier().getValue()).thenReturn("table");
ColumnAssignmentSegment assignmentSegment = mock(ColumnAssignmentSegment.class);
when(setAssignmentSegment.getAssignments()).thenReturn(Collections.singleton(assignmentSegment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.table.EncryptTable;
import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -45,7 +46,7 @@ class EncryptUpdateAssignmentTokenGeneratorTest {

@BeforeEach
void setup() {
tokenGenerator = new EncryptUpdateAssignmentTokenGenerator(mockEncryptRule());
tokenGenerator = new EncryptUpdateAssignmentTokenGenerator(mockEncryptRule(), mock(ShardingSphereDatabase.class));
when(updateStatementContext.getTablesContext().getSimpleTables().iterator().next().getTableName().getIdentifier().getValue()).thenReturn("table");
ColumnAssignmentSegment assignmentSegment = mock(ColumnAssignmentSegment.class);
when(updateStatementContext.getSqlStatement().getSetAssignment().getAssignments()).thenReturn(Collections.singleton(assignmentSegment));
Expand Down
Loading

0 comments on commit 6043b04

Please sign in to comment.