From 6043b04bd312da461f4584602de8c26557c5c775 Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Wed, 22 Jan 2025 14:10:44 +0800 Subject: [PATCH] Remove DatabaseAware and refactor encrypt token generator logic --- .../EncryptSQLRewriteContextDecorator.java | 2 +- .../token/EncryptTokenGenerateBuilder.java | 20 +++++------ ...EncryptInsertAssignmentTokenGenerator.java | 5 ++- ...EncryptUpdateAssignmentTokenGenerator.java | 5 ++- .../EncryptInsertOnUpdateTokenGenerator.java | 5 ++- .../EncryptInsertValuesTokenGenerator.java | 7 ++-- ...yptInsertPredicateValueTokenGenerator.java | 10 +++--- .../EncryptPredicateColumnTokenGenerator.java | 10 +++--- .../EncryptPredicateValueTokenGenerator.java | 7 ++-- ...tInsertSelectProjectionTokenGenerator.java | 4 +-- .../EncryptProjectionTokenGenerator.java | 4 +-- ...EncryptSelectProjectionTokenGenerator.java | 2 +- .../EncryptGroupByItemTokenGenerator.java | 4 +-- .../EncryptTokenGenerateBuilderTest.java | 2 +- ...yptInsertAssignmentTokenGeneratorTest.java | 3 +- ...yptUpdateAssignmentTokenGeneratorTest.java | 3 +- ...cryptInsertOnUpdateTokenGeneratorTest.java | 3 +- ...EncryptInsertValuesTokenGeneratorTest.java | 8 ++--- ...cryptPredicateValueTokenGeneratorTest.java | 5 ++- .../EncryptProjectionTokenGeneratorTest.java | 6 +++- .../common/generator/aware/DatabaseAware.java | 33 ------------------- .../SubstitutableColumnNameTokenTest.java | 10 ++++-- 22 files changed, 61 insertions(+), 97 deletions(-) delete mode 100644 infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/generator/aware/DatabaseAware.java diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java index bfcaa9c4890d3..05678e43ac144 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java @@ -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()); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java index d8aea0caf6e9b..a13fc0ec593c3 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java @@ -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; @@ -53,12 +52,12 @@ @RequiredArgsConstructor public final class EncryptTokenGenerateBuilder implements SQLTokenGeneratorBuilder { - private final EncryptRule rule; - private final SQLStatementContext sqlStatementContext; private final Collection encryptConditions; + private final EncryptRule rule; + private final ShardingSphereDatabase database; @Override @@ -66,17 +65,17 @@ public Collection getSQLTokenGenerators() { Collection 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)); @@ -92,9 +91,6 @@ private void addSQLTokenGenerator(final Collection sqlTokenGe } private void setUpSQLTokenGenerator(final SQLTokenGenerator toBeAddedSQLTokenGenerator) { - if (toBeAddedSQLTokenGenerator instanceof DatabaseAware) { - ((DatabaseAware) toBeAddedSQLTokenGenerator).setDatabase(database); - } if (toBeAddedSQLTokenGenerator instanceof EncryptConditionsAware) { ((EncryptConditionsAware) toBeAddedSQLTokenGenerator).setEncryptConditions(encryptConditions); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGenerator.java index 7afea2b2fa959..5547fda947c25 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGenerator.java @@ -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; @@ -37,11 +36,11 @@ @HighFrequencyInvocation @RequiredArgsConstructor @Setter -public final class EncryptInsertAssignmentTokenGenerator implements CollectionSQLTokenGenerator, DatabaseAware { +public final class EncryptInsertAssignmentTokenGenerator implements CollectionSQLTokenGenerator { private final EncryptRule rule; - private ShardingSphereDatabase database; + private final ShardingSphereDatabase database; @Override public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) { diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGenerator.java index 9677c6a0565ea..ca41790067da2 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGenerator.java @@ -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; @@ -37,11 +36,11 @@ @HighFrequencyInvocation @RequiredArgsConstructor @Setter -public final class EncryptUpdateAssignmentTokenGenerator implements CollectionSQLTokenGenerator, DatabaseAware { +public final class EncryptUpdateAssignmentTokenGenerator implements CollectionSQLTokenGenerator { private final EncryptRule rule; - private ShardingSphereDatabase database; + private final ShardingSphereDatabase database; @Override public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) { diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGenerator.java index ba0fb916d0c03..75d98a2bb8fc4 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGenerator.java @@ -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; @@ -57,11 +56,11 @@ @HighFrequencyInvocation @RequiredArgsConstructor @Setter -public final class EncryptInsertOnUpdateTokenGenerator implements CollectionSQLTokenGenerator, DatabaseAware { +public final class EncryptInsertOnUpdateTokenGenerator implements CollectionSQLTokenGenerator { private final EncryptRule rule; - private ShardingSphereDatabase database; + private final ShardingSphereDatabase database; @Override public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) { diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGenerator.java index 964fc69678453..f7799392089e7 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGenerator.java @@ -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; @@ -58,13 +57,13 @@ */ @RequiredArgsConstructor @Setter -public final class EncryptInsertValuesTokenGenerator implements OptionalSQLTokenGenerator, PreviousSQLTokensAware, DatabaseAware { +public final class EncryptInsertValuesTokenGenerator implements OptionalSQLTokenGenerator, PreviousSQLTokensAware { private final EncryptRule rule; - private List previousSQLTokens; + private final ShardingSphereDatabase database; - private ShardingSphereDatabase database; + private List previousSQLTokens; @Override public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) { diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptInsertPredicateValueTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptInsertPredicateValueTokenGenerator.java index 28d809f7572b1..a288e9af23947 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptInsertPredicateValueTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptInsertPredicateValueTokenGenerator.java @@ -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; @@ -40,16 +39,16 @@ @HighFrequencyInvocation @RequiredArgsConstructor @Setter -public final class EncryptInsertPredicateValueTokenGenerator implements CollectionSQLTokenGenerator, ParametersAware, EncryptConditionsAware, DatabaseAware { +public final class EncryptInsertPredicateValueTokenGenerator implements CollectionSQLTokenGenerator, ParametersAware, EncryptConditionsAware { private final EncryptRule rule; + private final ShardingSphereDatabase database; + private List parameters; private Collection encryptConditions; - private ShardingSphereDatabase database; - @Override public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) { return sqlStatementContext instanceof InsertStatementContext && null != ((InsertStatementContext) sqlStatementContext).getInsertSelectContext() @@ -58,10 +57,9 @@ public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) @Override public Collection 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()); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateColumnTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateColumnTokenGenerator.java index 8497bb70a9860..e06d6b836614e 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateColumnTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateColumnTokenGenerator.java @@ -100,12 +100,12 @@ private SubstitutableColumnNameToken buildSubstitutableColumnNameToken(final Enc if (includesLike(andPredicates, columnSegment)) { Optional 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 columnProjections = - encryptColumn.getAssistedQuery().map(optional -> createColumnProjections(optional.getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType)) - .orElseGet(() -> createColumnProjections(encryptColumn.getCipher().getName(), columnSegment.getIdentifier().getQuoteCharacter(), databaseType)); + Collection 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); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGenerator.java index bf963a3f665ee..cbc4d23f3738b 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGenerator.java @@ -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; @@ -57,16 +56,16 @@ @HighFrequencyInvocation @RequiredArgsConstructor @Setter -public final class EncryptPredicateValueTokenGenerator implements CollectionSQLTokenGenerator, ParametersAware, EncryptConditionsAware, DatabaseAware { +public final class EncryptPredicateValueTokenGenerator implements CollectionSQLTokenGenerator, ParametersAware, EncryptConditionsAware { private final EncryptRule rule; + private final ShardingSphereDatabase database; + private List parameters; private Collection encryptConditions; - private ShardingSphereDatabase database; - @Override public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) { return sqlStatementContext instanceof WhereAvailable; diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptInsertSelectProjectionTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptInsertSelectProjectionTokenGenerator.java index 90a75eeab37a3..e6f5801b2224c 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptInsertSelectProjectionTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptInsertSelectProjectionTokenGenerator.java @@ -49,7 +49,7 @@ public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) @Override public Collection 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()); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java index 5877e355428f7..14aa6a20cad06 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java @@ -57,10 +57,10 @@ public final class EncryptProjectionTokenGenerator { private final List previousSQLTokens; - private final EncryptRule rule; - private final DatabaseType databaseType; + private final EncryptRule rule; + /** * Generate SQL tokens. * diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptSelectProjectionTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptSelectProjectionTokenGenerator.java index 0fe07b7e103ab..a4bc8f6677b56 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptSelectProjectionTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptSelectProjectionTokenGenerator.java @@ -49,6 +49,6 @@ public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) @Override public Collection generateSQLTokens(final SelectStatementContext sqlStatementContext) { - return new EncryptProjectionTokenGenerator(previousSQLTokens, rule, sqlStatementContext.getDatabaseType()).generateSQLTokens(sqlStatementContext); + return new EncryptProjectionTokenGenerator(previousSQLTokens, sqlStatementContext.getDatabaseType(), rule).generateSQLTokens(sqlStatementContext); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/select/EncryptGroupByItemTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/select/EncryptGroupByItemTokenGenerator.java index cfc5d86a68956..b7cbec1fde8e6 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/select/EncryptGroupByItemTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/select/EncryptGroupByItemTokenGenerator.java @@ -91,8 +91,8 @@ private Optional 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))); } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java index 3b55e6741e810..5b1f712cb327b 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java @@ -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 sqlTokenGenerators = encryptTokenGenerateBuilder.getSQLTokenGenerators(); assertThat(sqlTokenGenerators.size(), is(3)); Iterator iterator = sqlTokenGenerators.iterator(); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGeneratorTest.java index 6c882557e37ad..b23b7578feefa 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptInsertAssignmentTokenGeneratorTest.java @@ -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; @@ -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)); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGeneratorTest.java index 2038b25a38e6f..c8e7efa75c3cc 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/assignment/EncryptUpdateAssignmentTokenGeneratorTest.java @@ -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; @@ -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)); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGeneratorTest.java index a84e756620de3..f7f1947997ca5 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertOnUpdateTokenGeneratorTest.java @@ -59,8 +59,7 @@ class EncryptInsertOnUpdateTokenGeneratorTest { @BeforeEach void setup() { - generator = new EncryptInsertOnUpdateTokenGenerator(mockEncryptRule()); - generator.setDatabase(mock(ShardingSphereDatabase.class)); + generator = new EncryptInsertOnUpdateTokenGenerator(mockEncryptRule(), mock(ShardingSphereDatabase.class)); } private EncryptRule mockEncryptRule() { diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGeneratorTest.java index 93e2c434d8b53..24a6c92781160 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/insert/EncryptInsertValuesTokenGeneratorTest.java @@ -36,7 +36,7 @@ class EncryptInsertValuesTokenGeneratorTest { @BeforeEach void setup() { - generator = new EncryptInsertValuesTokenGenerator(EncryptGeneratorFixtureBuilder.createEncryptRule()); + generator = new EncryptInsertValuesTokenGenerator(EncryptGeneratorFixtureBuilder.createEncryptRule(), new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), Collections.emptyList())); } @Test @@ -47,14 +47,12 @@ void assertIsGenerateSQLToken() { @Test void assertGenerateSQLTokenFromGenerateNewSQLToken() { generator.setPreviousSQLTokens(Collections.emptyList()); - generator.setDatabase(new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), Collections.emptyList())); - assertThat(generator.generateSQLToken(EncryptGeneratorFixtureBuilder.createInsertStatementContext(Arrays.asList(1, "Tom", 0, "123456"))).toString(), is("(?, ?, ?, ?, ?, ?)")); + assertThat(generator.generateSQLToken(EncryptGeneratorFixtureBuilder.createInsertStatementContext(Arrays.asList(1, "Tom", 0, "123456"))).toString(), is("(?, ?, ?, ?, ?, ?, ?)")); } @Test void assertGenerateSQLTokenFromPreviousSQLTokens() { generator.setPreviousSQLTokens(EncryptGeneratorFixtureBuilder.getPreviousSQLTokens()); - generator.setDatabase(new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), Collections.emptyList())); - assertThat(generator.generateSQLToken(EncryptGeneratorFixtureBuilder.createInsertStatementContext(Arrays.asList(1, "Tom", 0, "123456"))).toString(), is("(?, ?, ?, ?, ?, ?)")); + assertThat(generator.generateSQLToken(EncryptGeneratorFixtureBuilder.createInsertStatementContext(Arrays.asList(1, "Tom", 0, "123456"))).toString(), is("(?, ?, ?, ?, ?, ?, ?)")); } } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGeneratorTest.java index 29a88eae73239..43eaf4af5029e 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/predicate/EncryptPredicateValueTokenGeneratorTest.java @@ -40,19 +40,18 @@ class EncryptPredicateValueTokenGeneratorTest { @BeforeEach void setup() { - generator = new EncryptPredicateValueTokenGenerator(EncryptGeneratorFixtureBuilder.createEncryptRule()); + generator = new EncryptPredicateValueTokenGenerator(EncryptGeneratorFixtureBuilder.createEncryptRule(), + new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), Collections.emptyList())); } @Test void assertIsGenerateSQLToken() { - generator.setDatabase(new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), Collections.emptyList())); assertTrue(generator.isGenerateSQLToken(EncryptGeneratorFixtureBuilder.createUpdateStatementContext())); } @Test void assertGenerateSQLTokenFromGenerateNewSQLToken() { UpdateStatementContext updateStatementContext = EncryptGeneratorFixtureBuilder.createUpdateStatementContext(); - generator.setDatabase(new ShardingSphereDatabase("foo_db", mock(), mock(), mock(), Collections.emptyList())); generator.setEncryptConditions(getEncryptConditions(updateStatementContext)); Collection sqlTokens = generator.generateSQLTokens(updateStatementContext); assertThat(sqlTokens.size(), is(1)); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGeneratorTest.java index 5648193a7d5c0..cdfcea25991f6 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGeneratorTest.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; @@ -58,7 +59,10 @@ class EncryptProjectionTokenGeneratorTest { @BeforeEach void setup() { - generator = new EncryptProjectionTokenGenerator(Collections.emptyList(), mockEncryptRule(), databaseType); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); + when(database.getRuleMetaData().getRules()).thenReturn(Collections.emptyList()); + generator = new EncryptProjectionTokenGenerator(Collections.emptyList(), databaseType, mockEncryptRule()); } private EncryptRule mockEncryptRule() { diff --git a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/generator/aware/DatabaseAware.java b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/generator/aware/DatabaseAware.java deleted file mode 100644 index 1d10afc64af83..0000000000000 --- a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/generator/aware/DatabaseAware.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.infra.rewrite.sql.token.common.generator.aware; - -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; - -/** - * Database aware. - */ -public interface DatabaseAware { - - /** - * Set database. - * - * @param database database - */ - void setDatabase(ShardingSphereDatabase database); -} diff --git a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/pojo/generic/SubstitutableColumnNameTokenTest.java b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/pojo/generic/SubstitutableColumnNameTokenTest.java index 31df3869ece43..4e7144c1ceaf7 100644 --- a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/pojo/generic/SubstitutableColumnNameTokenTest.java +++ b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/common/pojo/generic/SubstitutableColumnNameTokenTest.java @@ -41,14 +41,20 @@ class SubstitutableColumnNameTokenTest { @Test void assertToString() { Collection projections = Collections.singletonList(new ColumnProjection(null, "id", null, mock(DatabaseType.class))); - assertThat(new SubstitutableColumnNameToken(0, 1, projections, TypedSPILoader.getService(DatabaseType.class, "MySQL")).toString(mock(RouteUnit.class)), is("id")); + assertThat( + new SubstitutableColumnNameToken(0, 1, projections, TypedSPILoader.getService(DatabaseType.class, "MySQL")) + .toString(mock(RouteUnit.class)), + is("id")); } @Test void assertToStringWithQuote() { Collection projections = Collections.singletonList(new ColumnProjection(null, new IdentifierValue("id", QuoteCharacter.BACK_QUOTE), new IdentifierValue("id", QuoteCharacter.BACK_QUOTE), mock(DatabaseType.class))); - assertThat(new SubstitutableColumnNameToken(0, 1, projections, TypedSPILoader.getService(DatabaseType.class, "MySQL")).toString(mock(RouteUnit.class)), is("`id` AS `id`")); + assertThat( + new SubstitutableColumnNameToken(0, 1, projections, TypedSPILoader.getService(DatabaseType.class, "MySQL")) + .toString(mock(RouteUnit.class)), + is("`id` AS `id`")); } @Test