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 f902114ddeb Add H2 identifier case policy provider (#39650)
f902114ddeb is described below

commit f902114ddeb7fefaa03ee872e13a8486b1e0d694
Author: Haoran Meng <[email protected]>
AuthorDate: Sun Aug 30 21:52:50 2026 +0800

    Add H2 identifier case policy provider (#39650)
---
 .../identifier/H2IdentifierCasePolicyProvider.java | 84 +++++++++++++++++++
 ...etadata.identifier.IdentifierCasePolicyProvider | 18 +++++
 .../H2IdentifierCasePolicyProviderTest.java        | 94 ++++++++++++++++++++++
 3 files changed, 196 insertions(+)

diff --git 
a/database/connector/dialect/h2/src/main/java/org/apache/shardingsphere/database/connector/h2/metadata/identifier/H2IdentifierCasePolicyProvider.java
 
b/database/connector/dialect/h2/src/main/java/org/apache/shardingsphere/database/connector/h2/metadata/identifier/H2IdentifierCasePolicyProvider.java
new file mode 100644
index 00000000000..da2847f3888
--- /dev/null
+++ 
b/database/connector/dialect/h2/src/main/java/org/apache/shardingsphere/database/connector/h2/metadata/identifier/H2IdentifierCasePolicyProvider.java
@@ -0,0 +1,84 @@
+/*
+ * 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.h2.metadata.identifier;
+
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyFactory;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicy;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyProvider;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyProviderContext;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicySet;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierScope;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * H2 provider of identifier case policies.
+ */
+public final class H2IdentifierCasePolicyProvider implements 
IdentifierCasePolicyProvider {
+    
+    private static final IdentifierCasePolicySet UPPER_CASE_POLICY_SET = 
IdentifierCasePolicyFactory.newUpperCasePolicySet();
+    
+    private static final IdentifierCasePolicySet LOWER_CASE_POLICY_SET = 
IdentifierCasePolicyFactory.newLowerCasePolicySet();
+    
+    private static final IdentifierCasePolicySet SENSITIVE_POLICY_SET = 
IdentifierCasePolicyFactory.newSensitivePolicySet();
+    
+    private static final IdentifierCasePolicySet 
CASE_PRESERVING_INSENSITIVE_POLICY_SET = 
IdentifierCasePolicyFactory.newCasePreservingInsensitivePolicySet();
+    
+    private static final IdentifierCasePolicySet DEFAULT_POLICY_SET = 
IdentifierCasePolicyFactory.newInsensitivePolicySet();
+    
+    @Override
+    public IdentifierCasePolicySet provide(final 
IdentifierCasePolicyProviderContext context) {
+        if (null == context.getDataSource()) {
+            return DEFAULT_POLICY_SET;
+        }
+        try (Connection connection = context.getDataSource().getConnection()) {
+            return null == connection ? DEFAULT_POLICY_SET : 
createPolicySet(connection.getMetaData());
+        } catch (final SQLException ignored) {
+            return DEFAULT_POLICY_SET;
+        }
+    }
+    
+    private IdentifierCasePolicySet createPolicySet(final DatabaseMetaData 
metaData) throws SQLException {
+        IdentifierCasePolicySet schemaPolicySet = getSchemaPolicySet(metaData);
+        Map<IdentifierScope, IdentifierCasePolicy> scopedPolicies = new 
EnumMap<>(IdentifierScope.class);
+        scopedPolicies.put(IdentifierScope.SCHEMA, 
schemaPolicySet.getPolicy(IdentifierScope.SCHEMA));
+        return new 
IdentifierCasePolicySet(DEFAULT_POLICY_SET.getPolicy(IdentifierScope.TABLE), 
scopedPolicies);
+    }
+    
+    private IdentifierCasePolicySet getSchemaPolicySet(final DatabaseMetaData 
metaData) throws SQLException {
+        if (metaData.storesUpperCaseIdentifiers()) {
+            return UPPER_CASE_POLICY_SET;
+        }
+        if (metaData.storesLowerCaseIdentifiers()) {
+            return LOWER_CASE_POLICY_SET;
+        }
+        if (metaData.supportsMixedCaseIdentifiers()) {
+            return SENSITIVE_POLICY_SET;
+        }
+        return metaData.storesMixedCaseIdentifiers() ? 
CASE_PRESERVING_INSENSITIVE_POLICY_SET : DEFAULT_POLICY_SET;
+    }
+    
+    @Override
+    public String getDatabaseType() {
+        return "H2";
+    }
+}
diff --git 
a/database/connector/dialect/h2/src/main/resources/META-INF/services/org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyProvider
 
b/database/connector/dialect/h2/src/main/resources/META-INF/services/org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyProvider
new file mode 100644
index 00000000000..58ee278cd31
--- /dev/null
+++ 
b/database/connector/dialect/h2/src/main/resources/META-INF/services/org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyProvider
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.database.connector.h2.metadata.identifier.H2IdentifierCasePolicyProvider
diff --git 
a/database/connector/dialect/h2/src/test/java/org/apache/shardingsphere/database/connector/h2/metadata/identifier/H2IdentifierCasePolicyProviderTest.java
 
b/database/connector/dialect/h2/src/test/java/org/apache/shardingsphere/database/connector/h2/metadata/identifier/H2IdentifierCasePolicyProviderTest.java
new file mode 100644
index 00000000000..deb4bbf7d54
--- /dev/null
+++ 
b/database/connector/dialect/h2/src/test/java/org/apache/shardingsphere/database/connector/h2/metadata/identifier/H2IdentifierCasePolicyProviderTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.h2.metadata.identifier;
+
+import 
org.apache.shardingsphere.database.connector.core.metadata.database.enums.QuoteCharacter;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicy;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyProvider;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicyProviderContext;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierCasePolicySet;
+import 
org.apache.shardingsphere.database.connector.core.metadata.identifier.IdentifierScope;
+import 
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class H2IdentifierCasePolicyProviderTest {
+    
+    private static final DatabaseType DATABASE_TYPE = 
TypedSPILoader.getService(DatabaseType.class, "H2");
+    
+    private final IdentifierCasePolicyProvider provider = 
DatabaseTypedSPILoader.getService(IdentifierCasePolicyProvider.class, 
DATABASE_TYPE);
+    
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("provideArguments")
+    void assertProvide(final String name, final 
IdentifierCasePolicyProviderContext context, final String expectedDefinition, 
final boolean expectedMatch) {
+        IdentifierCasePolicySet actual = provider.provide(context);
+        IdentifierCasePolicy actualSchemaPolicy = 
actual.getPolicy(IdentifierScope.SCHEMA);
+        assertThat(actualSchemaPolicy.normalizeForDefinition("Foo_Schema", 
QuoteCharacter.NONE), is(expectedDefinition));
+        assertThat(actualSchemaPolicy.matches(expectedDefinition, 
"FOO_SCHEMA", QuoteCharacter.NONE), is(expectedMatch));
+        IdentifierCasePolicy actualTablePolicy = 
actual.getPolicy(IdentifierScope.TABLE);
+        assertThat(actualTablePolicy.normalizeForDefinition("Foo_Table", 
QuoteCharacter.NONE), is("foo_table"));
+        assertTrue(actualTablePolicy.matches("foo_table", "FOO_TABLE", 
QuoteCharacter.NONE));
+    }
+    
+    private static Stream<Arguments> provideArguments() throws SQLException {
+        return Stream.of(
+                Arguments.of("null data source", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, null), "foo_schema", true),
+                Arguments.of("null connection", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, mock(DataSource.class)), 
"foo_schema", true),
+                Arguments.of("stores upper case identifiers", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, mockDataSource(true, false, 
false, false)), "FOO_SCHEMA", true),
+                Arguments.of("stores lower case identifiers", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, mockDataSource(false, true, 
false, false)), "foo_schema", true),
+                Arguments.of("supports mixed case identifiers", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, mockDataSource(false, false, 
false, true)), "Foo_Schema", false),
+                Arguments.of("stores mixed case identifiers", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, mockDataSource(false, false, 
true, false)), "Foo_Schema", true),
+                Arguments.of("unknown identifier case behavior", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, mockDataSource(false, false, 
false, false)), "foo_schema", true),
+                Arguments.of("SQL exception", new 
IdentifierCasePolicyProviderContext(DATABASE_TYPE, mockFailingDataSource()), 
"foo_schema", true));
+    }
+    
+    private static DataSource mockDataSource(final boolean 
storesUpperCaseIdentifiers, final boolean storesLowerCaseIdentifiers,
+                                             final boolean 
storesMixedCaseIdentifiers, final boolean supportsMixedCaseIdentifiers) throws 
SQLException {
+        DataSource result = mock(DataSource.class);
+        Connection connection = mock(Connection.class);
+        DatabaseMetaData metaData = mock(DatabaseMetaData.class);
+        when(result.getConnection()).thenReturn(connection);
+        when(connection.getMetaData()).thenReturn(metaData);
+        
when(metaData.storesUpperCaseIdentifiers()).thenReturn(storesUpperCaseIdentifiers);
+        
when(metaData.storesLowerCaseIdentifiers()).thenReturn(storesLowerCaseIdentifiers);
+        
when(metaData.storesMixedCaseIdentifiers()).thenReturn(storesMixedCaseIdentifiers);
+        
when(metaData.supportsMixedCaseIdentifiers()).thenReturn(supportsMixedCaseIdentifiers);
+        return result;
+    }
+    
+    private static DataSource mockFailingDataSource() throws SQLException {
+        DataSource result = mock(DataSource.class);
+        when(result.getConnection()).thenThrow(new SQLException("failed"));
+        return result;
+    }
+}

Reply via email to