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 f32c82719fb Move default schema resolver to connector core (#39648)
f32c82719fb is described below
commit f32c82719fbe33d2b6d17483f331f30952830767
Author: Haoran Meng <[email protected]>
AuthorDate: Sun Aug 30 16:14:33 2026 +0800
Move default schema resolver to connector core (#39648)
---
.../identifier/DefaultSchemaNameResolver.java | 61 +++++++++++++++++++
.../identifier/DefaultSchemaNameResolverTest.java | 70 ++++++++++++++++++++++
2 files changed, 131 insertions(+)
diff --git
a/database/connector/core/src/main/java/org/apache/shardingsphere/database/connector/core/metadata/identifier/DefaultSchemaNameResolver.java
b/database/connector/core/src/main/java/org/apache/shardingsphere/database/connector/core/metadata/identifier/DefaultSchemaNameResolver.java
new file mode 100644
index 00000000000..8d383d8ae72
--- /dev/null
+++
b/database/connector/core/src/main/java/org/apache/shardingsphere/database/connector/core/metadata/identifier/DefaultSchemaNameResolver.java
@@ -0,0 +1,61 @@
+/*
+ * 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.database.connector.core.metadata.identifier;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
+
+import javax.sql.DataSource;
+import java.util.function.Supplier;
+
+/**
+ * Default schema name resolver.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DefaultSchemaNameResolver {
+
+ /**
+ * Resolve protocol default schema name.
+ *
+ * @param protocolType protocol type
+ * @param databaseName database name, may be null
+ * @return default schema name, may be null
+ */
+ public static String resolveProtocol(final DatabaseType protocolType,
final String databaseName) {
+ return resolve(protocolType, databaseName, () ->
IdentifierNormalizeEngine.resolvePolicy(protocolType, null,
IdentifierScope.SCHEMA));
+ }
+
+ /**
+ * Resolve storage default schema name.
+ *
+ * @param storageType storage type
+ * @param dataSource data source
+ * @param databaseName database name, may be null
+ * @return default schema name, may be null
+ */
+ public static String resolveStorage(final DatabaseType storageType, final
DataSource dataSource, final String databaseName) {
+ return resolve(storageType, databaseName, () ->
IdentifierNormalizeEngine.resolvePolicy(storageType, dataSource,
IdentifierScope.SCHEMA));
+ }
+
+ private static String resolve(final DatabaseType databaseType, final
String databaseName, final Supplier<IdentifierCasePolicy>
identifierCasePolicySupplier) {
+ return new
DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().getSchemaOption().getDefaultSchema()
+ .orElseGet(() -> null == databaseName ? null :
IdentifierNormalizeEngine.normalize(identifierCasePolicySupplier.get(),
databaseName));
+ }
+}
diff --git
a/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/metadata/identifier/DefaultSchemaNameResolverTest.java
b/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/metadata/identifier/DefaultSchemaNameResolverTest.java
new file mode 100644
index 00000000000..e71ae161fed
--- /dev/null
+++
b/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/metadata/identifier/DefaultSchemaNameResolverTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.database.connector.core.metadata.identifier;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import javax.sql.DataSource;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(IdentifierNormalizeEngine.class)
+class DefaultSchemaNameResolverTest {
+
+ private final DatabaseType trunkDatabaseType =
TypedSPILoader.getService(DatabaseType.class, "TRUNK");
+
+ private final DatabaseType branchDatabaseType =
TypedSPILoader.getService(DatabaseType.class, "BRANCH");
+
+ @Test
+ void assertResolveProtocolWithFixedDefaultSchema() {
+
assertThat(DefaultSchemaNameResolver.resolveProtocol(trunkDatabaseType,
"foo_db"), is("test"));
+ }
+
+ @Test
+ void assertResolveProtocolWithIdentifierCasePolicy() {
+ IdentifierCasePolicy policy =
IdentifierCasePolicyFactory.newUpperCasePolicySet().getPolicy(IdentifierScope.SCHEMA);
+ when(IdentifierNormalizeEngine.resolvePolicy(branchDatabaseType, null,
IdentifierScope.SCHEMA)).thenReturn(policy);
+ when(IdentifierNormalizeEngine.normalize(policy,
"foo_db")).thenCallRealMethod();
+
assertThat(DefaultSchemaNameResolver.resolveProtocol(branchDatabaseType,
"foo_db"), is("FOO_DB"));
+ }
+
+ @Test
+ void assertResolveProtocolWithNullDatabaseName() {
+ when(IdentifierNormalizeEngine.resolvePolicy(branchDatabaseType, null,
IdentifierScope.SCHEMA)).thenThrow(AssertionError.class);
+
assertNull(DefaultSchemaNameResolver.resolveProtocol(branchDatabaseType, null));
+ }
+
+ @Test
+ void assertResolveStorageWithDataSourceIdentifierCasePolicy() {
+ DataSource dataSource = mock(DataSource.class);
+ IdentifierCasePolicy policy =
IdentifierCasePolicyFactory.newLowerCasePolicySet().getPolicy(IdentifierScope.SCHEMA);
+ when(IdentifierNormalizeEngine.resolvePolicy(branchDatabaseType,
dataSource, IdentifierScope.SCHEMA)).thenReturn(policy);
+ when(IdentifierNormalizeEngine.normalize(policy,
"FOO_DB")).thenCallRealMethod();
+
assertThat(DefaultSchemaNameResolver.resolveStorage(branchDatabaseType,
dataSource, "FOO_DB"), is("foo_db"));
+ }
+}