This is an automated email from the ASF dual-hosted git repository. Caideyipi pushed a commit to branch codex/jdbc-driver-info in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 7e01b4e50c48abc096397ccb869af935fc4be40d Author: Caideyipi <[email protected]> AuthorDate: Mon Jun 8 16:59:59 2026 +0800 Fix JDBC URL parsing defaults --- .../org/apache/iotdb/jdbc/IoTDBConnection.java | 5 +- .../java/org/apache/iotdb/jdbc/IoTDBDriver.java | 4 +- .../src/main/java/org/apache/iotdb/jdbc/Utils.java | 56 ++++++++++++---------- .../test/java/org/apache/iotdb/jdbc/UtilsTest.java | 15 ++++++ 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java index 7f37e2206fb..c0f239c2625 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java @@ -127,9 +127,10 @@ public class IoTDBConnection implements Connection { if (url == null) { throw new IoTDBURLException(JdbcMessages.INPUT_URL_NULL); } - params = Utils.parseUrl(url, info); + Properties properties = info == null ? new Properties() : info; + params = Utils.parseUrl(url, properties); this.url = url; - this.userName = info.get("user").toString(); + this.userName = params.getUsername(); this.networkTimeout = params.getNetworkTimeout(); this.zoneId = ZoneId.of(params.getTimeZone()); this.charset = params.getCharset(); diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDriver.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDriver.java index 2c0b6d4c7f1..71d69d0eb3c 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDriver.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDriver.java @@ -59,7 +59,7 @@ public class IoTDBDriver implements Driver { } } - private static final String TSFILE_URL_PREFIX = Config.IOTDB_URL_PREFIX + ".*"; + private static final String IOTDB_URL_PATTERN = Config.IOTDB_URL_PREFIX + ".*"; public IoTDBDriver() { // This is a constructor. @@ -67,7 +67,7 @@ public class IoTDBDriver implements Driver { @Override public boolean acceptsURL(String url) { - return url != null && Pattern.matches(TSFILE_URL_PREFIX, url); + return url != null && Pattern.matches(IOTDB_URL_PATTERN, url); } @Override diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java index 00e46cc340d..d45ab312f76 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java @@ -46,6 +46,7 @@ public class Utils { * jdbc:iotdb://localhost:6667/. */ static IoTDBConnectionParams parseUrl(String url, Properties info) throws IoTDBURLException { + Properties properties = info == null ? new Properties() : info; IoTDBConnectionParams params = new IoTDBConnectionParams(url); if (url.trim().equalsIgnoreCase(Config.IOTDB_URL_PREFIX)) { return params; @@ -58,6 +59,11 @@ public class Utils { if (url.startsWith(Config.IOTDB_URL_PREFIX)) { String subURL = url.substring(Config.IOTDB_URL_PREFIX.length()); int i = subURL.lastIndexOf(COLON); + if (i <= 0) { + throw new IoTDBURLException( + "Error url format, url should be jdbc:iotdb://anything:port/[database] or jdbc:iotdb://anything:port[/database]?property1=value1&property2=value2, current url is " + + url); + } host = subURL.substring(0, i); params.setHost(host); i++; @@ -90,7 +96,7 @@ public class Utils { } matcher = SUFFIX_URL_PATTERN.matcher(suffixURL); - if (matcher.matches() && parseUrlParam(subURL, info)) { + if (matcher.matches() && parseUrlParam(subURL, properties)) { isUrlLegal = true; } } @@ -101,43 +107,43 @@ public class Utils { + url); } - if (info.containsKey(Config.AUTH_USER)) { - params.setUsername(info.getProperty(Config.AUTH_USER)); + if (properties.containsKey(Config.AUTH_USER)) { + params.setUsername(properties.getProperty(Config.AUTH_USER)); } - if (info.containsKey(Config.AUTH_PASSWORD)) { - params.setPassword(info.getProperty(Config.AUTH_PASSWORD)); + if (properties.containsKey(Config.AUTH_PASSWORD)) { + params.setPassword(properties.getProperty(Config.AUTH_PASSWORD)); } - if (info.containsKey(Config.DEFAULT_BUFFER_CAPACITY)) { + if (properties.containsKey(Config.DEFAULT_BUFFER_CAPACITY)) { params.setThriftDefaultBufferSize( - Integer.parseInt(info.getProperty(Config.DEFAULT_BUFFER_CAPACITY))); + Integer.parseInt(properties.getProperty(Config.DEFAULT_BUFFER_CAPACITY))); } - if (info.containsKey(Config.THRIFT_FRAME_MAX_SIZE)) { + if (properties.containsKey(Config.THRIFT_FRAME_MAX_SIZE)) { params.setThriftMaxFrameSize( - Integer.parseInt(info.getProperty(Config.THRIFT_FRAME_MAX_SIZE))); + Integer.parseInt(properties.getProperty(Config.THRIFT_FRAME_MAX_SIZE))); } - if (info.containsKey(Config.VERSION)) { - params.setVersion(Constant.Version.valueOf(info.getProperty(Config.VERSION))); + if (properties.containsKey(Config.VERSION)) { + params.setVersion(Constant.Version.valueOf(properties.getProperty(Config.VERSION))); } - if (info.containsKey(Config.NETWORK_TIMEOUT)) { - params.setNetworkTimeout(Integer.parseInt(info.getProperty(Config.NETWORK_TIMEOUT))); + if (properties.containsKey(Config.NETWORK_TIMEOUT)) { + params.setNetworkTimeout(Integer.parseInt(properties.getProperty(Config.NETWORK_TIMEOUT))); } - if (info.containsKey(Config.TIME_ZONE)) { - params.setTimeZone(info.getProperty(Config.TIME_ZONE)); + if (properties.containsKey(Config.TIME_ZONE)) { + params.setTimeZone(properties.getProperty(Config.TIME_ZONE)); } - if (info.containsKey(Config.CHARSET)) { - params.setCharset(info.getProperty(Config.CHARSET)); + if (properties.containsKey(Config.CHARSET)) { + params.setCharset(properties.getProperty(Config.CHARSET)); } - if (info.containsKey(Config.USE_SSL)) { - params.setUseSSL(Boolean.parseBoolean(info.getProperty(Config.USE_SSL))); + if (properties.containsKey(Config.USE_SSL)) { + params.setUseSSL(Boolean.parseBoolean(properties.getProperty(Config.USE_SSL))); } - if (info.containsKey(Config.TRUST_STORE)) { - params.setTrustStore(info.getProperty(Config.TRUST_STORE)); + if (properties.containsKey(Config.TRUST_STORE)) { + params.setTrustStore(properties.getProperty(Config.TRUST_STORE)); } - if (info.containsKey(Config.TRUST_STORE_PWD)) { - params.setTrustStorePwd(info.getProperty(Config.TRUST_STORE_PWD)); + if (properties.containsKey(Config.TRUST_STORE_PWD)) { + params.setTrustStorePwd(properties.getProperty(Config.TRUST_STORE_PWD)); } - if (info.containsKey(Config.SQL_DIALECT)) { - params.setSqlDialect(info.getProperty(Config.SQL_DIALECT)); + if (properties.containsKey(Config.SQL_DIALECT)) { + params.setSqlDialect(properties.getProperty(Config.SQL_DIALECT)); } return params; diff --git a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java index 4c401b88017..b049d8b75b0 100644 --- a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java +++ b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/UtilsTest.java @@ -105,6 +105,16 @@ public class UtilsTest { assertEquals(6667, params.getPort()); } + @Test + public void testParseUrlAllowsNullProperties() throws IoTDBURLException { + IoTDBConnectionParams params = Utils.parseUrl("jdbc:iotdb://test:6667", null); + + assertEquals("test", params.getHost()); + assertEquals(6667, params.getPort()); + assertEquals(Config.DEFAULT_USER, params.getUsername()); + assertEquals(Config.DEFAULT_PASSWORD, params.getPassword()); + } + @Test(expected = IoTDBURLException.class) public void testParseWrongUrl2() throws IoTDBURLException { Properties properties = new Properties(); @@ -123,6 +133,11 @@ public class UtilsTest { Utils.parseUrl("jdbc:iotdb//6667?rpc_compress=true&aaa=bbb", properties); } + @Test(expected = IoTDBURLException.class) + public void testParseWrongUrlWithoutPort() throws IoTDBURLException { + Utils.parseUrl("jdbc:iotdb://test", new Properties()); + } + @Test(expected = IoTDBURLException.class) public void testParseWrongPort() throws IoTDBURLException { String userName = "test";
