This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 f343591970a Add DialectJdbcUrlFetcher (#36937)
f343591970a is described below

commit f343591970a6ec3c57d3927b1f44abe5127c81df
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Oct 24 23:23:34 2025 +0800

    Add DialectJdbcUrlFetcher (#36937)
    
    * Add DialectJdbcUrlFetcher
    
    * Add DialectJdbcUrlFetcher
    
    * Add DialectJdbcUrlFetcher
---
 .../core/jdbcurl/DialectJdbcUrlFetcher.java        | 47 ++++++++++++++++++++++
 .../connector/hive/jdbcurl/HiveJdbcUrlFetcher.java | 40 ++++++++++++++++++
 ...se.connector.core.jdbcurl.DialectJdbcUrlFetcher | 18 +++++++++
 .../infra/database/DatabaseTypeEngine.java         | 24 +++++------
 4 files changed, 114 insertions(+), 15 deletions(-)

diff --git 
a/database/connector/core/src/main/java/org/apache/shardingsphere/database/connector/core/jdbcurl/DialectJdbcUrlFetcher.java
 
b/database/connector/core/src/main/java/org/apache/shardingsphere/database/connector/core/jdbcurl/DialectJdbcUrlFetcher.java
new file mode 100644
index 00000000000..814118f6f32
--- /dev/null
+++ 
b/database/connector/core/src/main/java/org/apache/shardingsphere/database/connector/core/jdbcurl/DialectJdbcUrlFetcher.java
@@ -0,0 +1,47 @@
+/*
+ * 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.jdbcurl;
+
+import org.apache.shardingsphere.infra.spi.ShardingSphereSPI;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * Dialect JDBC URL fetcher.
+ */
+@SingletonSPI
+public interface DialectJdbcUrlFetcher extends ShardingSphereSPI {
+    
+    /**
+     * Fetch JDBC URL.
+     *
+     * @param connection connection
+     * @return JDBC URL
+     * @throws SQLException SQL exception
+     */
+    String fetch(Connection connection) throws SQLException;
+    
+    /**
+     * Get connection class.
+     *
+     * @return connection class
+     */
+    Class<? extends Connection> getConnectionClass();
+}
diff --git 
a/database/connector/dialect/hive/src/main/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveJdbcUrlFetcher.java
 
b/database/connector/dialect/hive/src/main/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveJdbcUrlFetcher.java
new file mode 100644
index 00000000000..d18cc4b9535
--- /dev/null
+++ 
b/database/connector/dialect/hive/src/main/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveJdbcUrlFetcher.java
@@ -0,0 +1,40 @@
+/*
+ * 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.hive.jdbcurl;
+
+import org.apache.hive.jdbc.HiveConnection;
+import 
org.apache.shardingsphere.database.connector.core.jdbcurl.DialectJdbcUrlFetcher;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * JDBC URL fetcher for Hive.
+ */
+public final class HiveJdbcUrlFetcher implements DialectJdbcUrlFetcher {
+    
+    @Override
+    public String fetch(final Connection connection) throws SQLException {
+        return connection.unwrap(HiveConnection.class).getConnectedUrl();
+    }
+    
+    @Override
+    public Class<? extends Connection> getConnectionClass() {
+        return HiveConnection.class;
+    }
+}
diff --git 
a/database/connector/dialect/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.database.connector.core.jdbcurl.DialectJdbcUrlFetcher
 
b/database/connector/dialect/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.database.connector.core.jdbcurl.DialectJdbcUrlFetcher
new file mode 100644
index 00000000000..174ab6c0e90
--- /dev/null
+++ 
b/database/connector/dialect/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.database.connector.core.jdbcurl.DialectJdbcUrlFetcher
@@ -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.hive.jdbcurl.HiveJdbcUrlFetcher
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
index aed941918ca..4d641ee08c8 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java
@@ -19,18 +19,18 @@ package org.apache.shardingsphere.infra.database;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import 
org.apache.shardingsphere.database.connector.core.jdbcurl.DialectJdbcUrlFetcher;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import 
org.apache.shardingsphere.database.connector.core.type.DatabaseTypeFactory;
 import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import 
org.apache.shardingsphere.infra.exception.external.sql.type.wrapper.SQLWrapperException;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import javax.sql.DataSource;
-import java.lang.reflect.InvocationTargetException;
 import java.sql.Connection;
-import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
 import java.util.Collection;
@@ -100,9 +100,6 @@ public final class DatabaseTypeEngine {
     
     /**
      * Get storage type.
-     * Similar to <a 
href="https://github.com/apache/hive/pull/5554";>apache/hive#5554</a>,
-     * apache/hive 4.0.1's `org.apache.hive.jdbc.HiveDatabaseMetaData` does 
not implement {@link DatabaseMetaData#getURL()}.
-     * So use {@link java.sql.Wrapper#isWrapperFor(Class)} to try fuzzy 
matching.
      *
      * @param dataSource data source
      * @return storage type
@@ -113,26 +110,23 @@ public final class DatabaseTypeEngine {
         try (Connection connection = dataSource.getConnection()) {
             return DatabaseTypeFactory.get(connection.getMetaData().getURL());
         } catch (final SQLFeatureNotSupportedException 
sqlFeatureNotSupportedException) {
-            return getStorageType(dataSource, sqlFeatureNotSupportedException);
+            return findStorageType(dataSource).orElseThrow(() -> new 
SQLWrapperException(sqlFeatureNotSupportedException));
         } catch (final SQLException ex) {
             throw new SQLWrapperException(ex);
         }
     }
     
-    private static DatabaseType getStorageType(final DataSource dataSource, 
final SQLFeatureNotSupportedException sqlFeatureNotSupportedException) {
+    private static Optional<DatabaseType> findStorageType(final DataSource 
dataSource) {
         try (Connection connection = dataSource.getConnection()) {
-            Class<?> hiveConnectionClass = 
Class.forName("org.apache.hive.jdbc.HiveConnection");
-            if (connection.isWrapperFor(hiveConnectionClass)) {
-                Object hiveConnection = connection.unwrap(hiveConnectionClass);
-                String connectedUrl = (String) 
hiveConnectionClass.getMethod("getConnectedUrl").invoke(hiveConnection);
-                return DatabaseTypeFactory.get(connectedUrl);
+            for (DialectJdbcUrlFetcher each : 
ShardingSphereServiceLoader.getServiceInstances(DialectJdbcUrlFetcher.class)) {
+                if (connection.isWrapperFor(each.getConnectionClass())) {
+                    return 
Optional.of(DatabaseTypeFactory.get(each.fetch(connection)));
+                }
             }
-            throw new SQLWrapperException(sqlFeatureNotSupportedException);
         } catch (final SQLException ex) {
             throw new SQLWrapperException(ex);
-        } catch (final ClassNotFoundException | NoSuchMethodException | 
InvocationTargetException | IllegalAccessException ex) {
-            throw new SQLWrapperException(new SQLException(ex));
         }
+        return Optional.empty();
     }
     
     /**

Reply via email to