This is an automated email from the ASF dual-hosted git repository.
menghaoranss pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 76a123c3370 Use database default schema in encrypt assignment rewriter
(#39601)
76a123c3370 is described below
commit 76a123c33704a16cf1a891596be92073193b080a
Author: Haoran Meng <[email protected]>
AuthorDate: Thu Aug 27 14:09:14 2026 +0800
Use database default schema in encrypt assignment rewriter (#39601)
---
.../EncryptParameterRewritersRegistry.java | 2 +-
.../EncryptAssignmentParameterRewriter.java | 12 +--
.../EncryptAssignmentParameterRewriterTest.java | 88 ++++++++++++++++++++++
3 files changed, 95 insertions(+), 7 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewritersRegistry.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewritersRegistry.java
index 0300e7dee96..5d4b299b9a0 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewritersRegistry.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewritersRegistry.java
@@ -48,7 +48,7 @@ public final class EncryptParameterRewritersRegistry
implements ParameterRewrite
public Collection<ParameterRewriter> getParameterRewriters() {
String databaseName = sqlRewriteContext.getDatabase().getName();
return Arrays.asList(
- new EncryptAssignmentParameterRewriter(rule, databaseName),
+ new EncryptAssignmentParameterRewriter(rule,
sqlRewriteContext.getDatabase()),
new EncryptPredicateParameterRewriter(rule, encryptConditions),
new EncryptInsertPredicateParameterRewriter(rule,
encryptConditions),
new EncryptInsertValueParameterRewriter(rule, databaseName),
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java
index 364a8a54214..e931e193dcc 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java
@@ -19,12 +19,12 @@ package
org.apache.shardingsphere.encrypt.rewrite.parameter.rewriter;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
-import
org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.dml.InsertStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.type.dml.UpdateStatementContext;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
import
org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
import
org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
@@ -51,7 +51,7 @@ public final class EncryptAssignmentParameterRewriter
implements ParameterRewrit
private final EncryptRule rule;
- private final String databaseName;
+ private final ShardingSphereDatabase database;
@Override
public boolean isNeedRewrite(final SQLStatementContext
sqlStatementContext) {
@@ -67,7 +67,7 @@ public final class EncryptAssignmentParameterRewriter
implements ParameterRewrit
@Override
public void rewrite(final ParameterBuilder paramBuilder, final
SQLStatementContext sqlStatementContext, final List<Object> params) {
String schemaName =
sqlStatementContext.getTablesContext().getSchemaName()
- .orElseGet(() -> new
DatabaseTypeRegistry(sqlStatementContext.getSqlStatement().getDatabaseType()).getDefaultSchemaName(databaseName));
+ .orElseGet(database::getDefaultSchemaName);
for (ColumnAssignmentSegment each :
getSetAssignmentSegment(sqlStatementContext.getSqlStatement()).getAssignments())
{
String columnName =
each.getColumns().get(0).getIdentifier().getValue();
String tableName =
each.getColumns().get(0).getColumnBoundInfo().getOriginalTable().getValue();
@@ -98,14 +98,14 @@ public final class EncryptAssignmentParameterRewriter
implements ParameterRewrit
final String tableName, final EncryptColumn
encryptColumn, final int parameterMarkerIndex, final List<Object> params) {
String columnName = encryptColumn.getName();
Object originalValue = params.get(parameterMarkerIndex);
- Object cipherValue = encryptColumn.getCipher().encrypt(databaseName,
schemaName, tableName, columnName,
Collections.singletonList(originalValue)).iterator().next();
+ Object cipherValue =
encryptColumn.getCipher().encrypt(database.getName(), schemaName, tableName,
columnName, Collections.singletonList(originalValue)).iterator().next();
paramBuilder.addReplacedParameters(parameterMarkerIndex, cipherValue);
Collection<Object> addedParams = new LinkedList<>();
if (encryptColumn.getAssistedQuery().isPresent()) {
-
addedParams.add(encryptColumn.getAssistedQuery().get().encrypt(databaseName,
schemaName, tableName, columnName,
Collections.singletonList(originalValue)).iterator().next());
+
addedParams.add(encryptColumn.getAssistedQuery().get().encrypt(database.getName(),
schemaName, tableName, columnName,
Collections.singletonList(originalValue)).iterator().next());
}
if (encryptColumn.getLikeQuery().isPresent()) {
-
addedParams.add(encryptColumn.getLikeQuery().get().encrypt(databaseName,
schemaName, tableName, columnName,
Collections.singletonList(originalValue)).iterator().next());
+
addedParams.add(encryptColumn.getLikeQuery().get().encrypt(database.getName(),
schemaName, tableName, columnName,
Collections.singletonList(originalValue)).iterator().next());
}
if (!addedParams.isEmpty()) {
paramBuilder.addAddedParameters(parameterMarkerIndex, addedParams);
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriterTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriterTest.java
new file mode 100644
index 00000000000..d615b01b842
--- /dev/null
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriterTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.encrypt.rewrite.parameter.rewriter;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn;
+import org.apache.shardingsphere.encrypt.rule.column.item.CipherColumnItem;
+import org.apache.shardingsphere.encrypt.rule.table.EncryptTable;
+import
org.apache.shardingsphere.infra.binder.context.statement.type.dml.UpdateStatementContext;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.enums.TableSourceType;
+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.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.UpdateStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class EncryptAssignmentParameterRewriterTest {
+
+ @Test
+ void assertRewriteWithDefaultSchema() {
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getName()).thenReturn("foo_db");
+ when(database.getDefaultSchemaName()).thenReturn("foo_default_schema");
+ CipherColumnItem cipherColumnItem = mock(CipherColumnItem.class);
+ when(cipherColumnItem.encrypt("foo_db", "foo_default_schema",
"foo_table", "foo_column", Collections.singletonList("foo_value")))
+ .thenReturn(Collections.singletonList("foo_cipher_value"));
+ EncryptRule rule = mockEncryptRule(cipherColumnItem);
+ StandardParameterBuilder paramBuilder = new
StandardParameterBuilder(Collections.singletonList("foo_value"));
+ new EncryptAssignmentParameterRewriter(rule,
database).rewrite(paramBuilder, createSQLStatementContext(),
Collections.singletonList("foo_value"));
+ assertThat(paramBuilder.getParameters(),
is(Collections.singletonList("foo_cipher_value")));
+ }
+
+ private EncryptRule mockEncryptRule(final CipherColumnItem
cipherColumnItem) {
+ EncryptColumn encryptColumn = new EncryptColumn("foo_column",
cipherColumnItem);
+ EncryptTable encryptTable = mock(EncryptTable.class);
+ when(encryptTable.isEncryptColumn("foo_column")).thenReturn(true);
+
when(encryptTable.getEncryptColumn("foo_column")).thenReturn(encryptColumn);
+ EncryptRule result = mock(EncryptRule.class);
+
when(result.findEncryptTable("foo_table")).thenReturn(Optional.of(encryptTable));
+ when(result.getEncryptTable("foo_table")).thenReturn(encryptTable);
+ return result;
+ }
+
+ private UpdateStatementContext createSQLStatementContext() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("foo_column"));
+ columnSegment.setColumnBoundInfo(new ColumnSegmentBoundInfo(new
TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("")),
+ new IdentifierValue("foo_table"), new
IdentifierValue("foo_column"), TableSourceType.PHYSICAL_TABLE));
+ ColumnAssignmentSegment assignmentSegment = new
ColumnAssignmentSegment(0, 0, Collections.singletonList(columnSegment), new
ParameterMarkerExpressionSegment(0, 0, 0));
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+ UpdateStatement updateStatement =
UpdateStatement.builder().databaseType(databaseType)
+ .table(new SimpleTableSegment(new TableNameSegment(0, 0, new
IdentifierValue("foo_table"))))
+ .setAssignment(new SetAssignmentSegment(0, 0,
Collections.singletonList(assignmentSegment))).build();
+ return new UpdateStatementContext(updateStatement);
+ }
+}