fuweng11 commented on code in PR #9012:
URL: https://github.com/apache/inlong/pull/9012#discussion_r1349657650


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/ck/ClickHouseJdbcUtils.java:
##########
@@ -39,37 +39,153 @@
 public class ClickHouseJdbcUtils {
 
     private static final String CLICKHOUSE_DRIVER_CLASS = 
"ru.yandex.clickhouse.ClickHouseDriver";
-    private static final String METADATA_TYPE = "TABLE";
     private static final String COLUMN_LABEL = "TABLE_NAME";
     private static final String CLICKHOUSE_JDBC_PREFIX = "jdbc:clickhouse";
 
     private static final Logger LOG = 
LoggerFactory.getLogger(ClickHouseJdbcUtils.class);
 
     /**
-     * Get ClickHouse connection from clickhouse url and user
+     * Get ClickHouse connection from ClickHouse URL and user.
+     *
+     * @param url      JDBC URL, such as jdbc:clickhouse://host:port/database
+     * @param user     Username for JDBC URL
+     * @param password User password
+     * @return {@link Connection}
+     * @throws Exception on get connection error
      */
     public static Connection getConnection(String url, String user, String 
password) throws Exception {
-        if (StringUtils.isBlank(url) || 
!url.startsWith(CLICKHOUSE_JDBC_PREFIX)) {
-            throw new Exception("ClickHouse server URL was invalid, it should 
start with jdbc:clickhouse");
+        // Non-empty validation
+        validateInput(url, user, password);
+        validateUrlFormat(url);
+        String host = extractHostFromUrl(url);
+        String port = extractPortFromUrl(url);
+        validateHost(host);
+        validatePort(port);
+
+        Connection conn = establishConnection(url, user, password);
+        return conn;
+    }
+
+    /**
+     * Validates the format of the ClickHouse JDBC URL.
+     *
+     * @param url The ClickHouse JDBC URL to validate
+     * @throws Exception If the URL format is invalid
+     */
+    private static void validateUrlFormat(String url) throws Exception {
+        if (!url.startsWith(CLICKHOUSE_JDBC_PREFIX)) {
+            throw new Exception("ClickHouse JDBC URL is invalid, it should 
start with " + CLICKHOUSE_JDBC_PREFIX);
+        }
+
+        String[] hostPortParts = 
url.substring(CLICKHOUSE_JDBC_PREFIX.length()).split("/");
+        if (hostPortParts.length < 1) {
+            throw new Exception("Invalid ClickHouse JDBC URL format");
+        }
+    }
+
+    /**
+     * Extracts the host from the ClickHouse JDBC URL.
+     *
+     * @param url The ClickHouse JDBC URL from which to extract the host
+     * @return The extracted host
+     * @throws Exception If the URL format is invalid
+     */
+    private static String extractHostFromUrl(String url) throws Exception {
+        String hostPortPart = 
url.substring(CLICKHOUSE_JDBC_PREFIX.length()).split("/")[0];

Review Comment:
   If `url = "jdbc:clickhouse://host:port/database"` and 
`CLICKHOUSE_JDBC_PREFIX="dbc:clickhouse"`
   then ` hostPortPart = ":"`
   Whether this is in line with expectations ?



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/ck/ClickHouseJdbcUtils.java:
##########
@@ -39,37 +39,153 @@
 public class ClickHouseJdbcUtils {
 
     private static final String CLICKHOUSE_DRIVER_CLASS = 
"ru.yandex.clickhouse.ClickHouseDriver";
-    private static final String METADATA_TYPE = "TABLE";
     private static final String COLUMN_LABEL = "TABLE_NAME";
     private static final String CLICKHOUSE_JDBC_PREFIX = "jdbc:clickhouse";
 
     private static final Logger LOG = 
LoggerFactory.getLogger(ClickHouseJdbcUtils.class);
 
     /**
-     * Get ClickHouse connection from clickhouse url and user
+     * Get ClickHouse connection from ClickHouse URL and user.
+     *
+     * @param url      JDBC URL, such as jdbc:clickhouse://host:port/database
+     * @param user     Username for JDBC URL
+     * @param password User password
+     * @return {@link Connection}
+     * @throws Exception on get connection error
      */
     public static Connection getConnection(String url, String user, String 
password) throws Exception {
-        if (StringUtils.isBlank(url) || 
!url.startsWith(CLICKHOUSE_JDBC_PREFIX)) {
-            throw new Exception("ClickHouse server URL was invalid, it should 
start with jdbc:clickhouse");
+        // Non-empty validation
+        validateInput(url, user, password);
+        validateUrlFormat(url);
+        String host = extractHostFromUrl(url);
+        String port = extractPortFromUrl(url);
+        validateHost(host);
+        validatePort(port);
+
+        Connection conn = establishConnection(url, user, password);
+        return conn;
+    }
+
+    /**
+     * Validates the format of the ClickHouse JDBC URL.
+     *
+     * @param url The ClickHouse JDBC URL to validate
+     * @throws Exception If the URL format is invalid
+     */
+    private static void validateUrlFormat(String url) throws Exception {
+        if (!url.startsWith(CLICKHOUSE_JDBC_PREFIX)) {
+            throw new Exception("ClickHouse JDBC URL is invalid, it should 
start with " + CLICKHOUSE_JDBC_PREFIX);
+        }
+
+        String[] hostPortParts = 
url.substring(CLICKHOUSE_JDBC_PREFIX.length()).split("/");
+        if (hostPortParts.length < 1) {
+            throw new Exception("Invalid ClickHouse JDBC URL format");
+        }
+    }
+
+    /**
+     * Extracts the host from the ClickHouse JDBC URL.
+     *
+     * @param url The ClickHouse JDBC URL from which to extract the host
+     * @return The extracted host
+     * @throws Exception If the URL format is invalid
+     */
+    private static String extractHostFromUrl(String url) throws Exception {
+        String hostPortPart = 
url.substring(CLICKHOUSE_JDBC_PREFIX.length()).split("/")[0];

Review Comment:
   If `url = "jdbc:clickhouse://host:port/database"` and 
`CLICKHOUSE_JDBC_PREFIX="jdbc:clickhouse"`
   then ` hostPortPart = ":"`
   Whether this is in line with expectations ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to