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
The following commit(s) were added to refs/heads/codex/jdbc-driver-info by this
push:
new d49e1836b38 Handle null JDBC data source properties
d49e1836b38 is described below
commit d49e1836b3802dc48807e87cb44a49f0a92608b8
Author: Caideyipi <[email protected]>
AuthorDate: Mon Jun 8 17:08:02 2026 +0800
Handle null JDBC data source properties
---
.../org/apache/iotdb/jdbc/IoTDBDataSource.java | 25 +++++---
.../apache/iotdb/jdbc/IoTDBDataSourceFactory.java | 10 ++-
.../iotdb/jdbc/IoTDBDataSourceFactoryTest.java | 73 ++++++++++++++++++++++
3 files changed, 98 insertions(+), 10 deletions(-)
diff --git
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSource.java
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSource.java
index fe31896eb25..625098acd84 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSource.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSource.java
@@ -37,7 +37,6 @@ public class IoTDBDataSource implements DataSource {
private String url;
private String user;
private String password;
- private static final String PWD_STR = "password";
private Properties properties;
private Integer port = 6667;
@@ -48,8 +47,8 @@ public class IoTDBDataSource implements DataSource {
public IoTDBDataSource(String url, String user, String password, Integer
port) {
this.url = url;
this.properties = new Properties();
- properties.setProperty("user", user);
- properties.setProperty(PWD_STR, password);
+ setUser(user);
+ setPassword(password);
if (port != 0) {
this.port = port;
}
@@ -61,7 +60,7 @@ public class IoTDBDataSource implements DataSource {
public void setUser(String user) {
this.user = user;
- properties.setProperty("user", user);
+ setProperty(Config.AUTH_USER, user);
}
public String getPassword() {
@@ -70,7 +69,7 @@ public class IoTDBDataSource implements DataSource {
public void setPassword(String password) {
this.password = password;
- properties.setProperty(PWD_STR, password);
+ setProperty(Config.AUTH_PASSWORD, password);
}
public Integer getPort() {
@@ -103,8 +102,8 @@ public class IoTDBDataSource implements DataSource {
public Connection getConnection(String username, String password) {
try {
Properties newProp = new Properties();
- newProp.setProperty("user", username);
- newProp.setProperty(PWD_STR, password);
+ setProperty(newProp, Config.AUTH_USER, username);
+ setProperty(newProp, Config.AUTH_PASSWORD, password);
return new IoTDBConnection(url, newProp);
} catch (Exception e) {
LOGGER.error(JdbcMessages.GET_CONNECTION_ERROR, e);
@@ -146,4 +145,16 @@ public class IoTDBDataSource implements DataSource {
public boolean isWrapperFor(Class<?> aClass) {
return false;
}
+
+ private void setProperty(String key, String value) {
+ setProperty(properties, key, value);
+ }
+
+ private static void setProperty(Properties properties, String key, String
value) {
+ if (value == null) {
+ properties.remove(key);
+ } else {
+ properties.setProperty(key, value);
+ }
+ }
}
diff --git
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSourceFactory.java
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSourceFactory.java
index 2e5994ca426..f81245d16b8 100644
---
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSourceFactory.java
+++
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDataSourceFactory.java
@@ -43,17 +43,21 @@ public class IoTDBDataSourceFactory implements
DataSourceFactory {
}
public void setProperties(IoTDBDataSource ds, Properties prop) {
- Properties properties = (Properties) prop.clone();
+ Properties properties = prop == null ? new Properties() : (Properties)
prop.clone();
String url = (String) properties.remove(DataSourceFactory.JDBC_URL);
if (url != null) {
ds.setUrl(url);
}
String user = (String) properties.remove(DataSourceFactory.JDBC_USER);
- ds.setUser(user);
+ if (user != null) {
+ ds.setUser(user);
+ }
String password = (String)
properties.remove(DataSourceFactory.JDBC_PASSWORD);
- ds.setPassword(password);
+ if (password != null) {
+ ds.setPassword(password);
+ }
logger.info(JdbcMessages.REMAINING_PROPERTIES, properties.size());
diff --git
a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBDataSourceFactoryTest.java
b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBDataSourceFactoryTest.java
new file mode 100644
index 00000000000..fbf750d7057
--- /dev/null
+++
b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBDataSourceFactoryTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.iotdb.jdbc;
+
+import org.junit.Test;
+import org.osgi.service.jdbc.DataSourceFactory;
+
+import javax.sql.DataSource;
+
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class IoTDBDataSourceFactoryTest {
+
+ @Test
+ public void testCreateDataSourceAllowsNullProperties() {
+ DataSource dataSource = new
IoTDBDataSourceFactory().createDataSource(null);
+
+ assertTrue(dataSource instanceof IoTDBDataSource);
+ IoTDBDataSource iotdbDataSource = (IoTDBDataSource) dataSource;
+ assertNull(iotdbDataSource.getUrl());
+ assertNull(iotdbDataSource.getUser());
+ assertNull(iotdbDataSource.getPassword());
+ }
+
+ @Test
+ public void testCreateDataSourceAllowsUrlOnlyProperties() {
+ String url = "jdbc:iotdb://localhost:6667";
+ Properties properties = new Properties();
+ properties.setProperty(DataSourceFactory.JDBC_URL, url);
+
+ IoTDBDataSource dataSource =
+ (IoTDBDataSource) new
IoTDBDataSourceFactory().createDataSource(properties);
+
+ assertEquals(url, dataSource.getUrl());
+ assertNull(dataSource.getUser());
+ assertNull(dataSource.getPassword());
+ assertEquals(url, properties.getProperty(DataSourceFactory.JDBC_URL));
+ }
+
+ @Test
+ public void testDataSourceAllowsClearingUserAndPassword() {
+ IoTDBDataSource dataSource = new IoTDBDataSource();
+
+ dataSource.setUser("root");
+ dataSource.setPassword("root");
+ dataSource.setUser(null);
+ dataSource.setPassword(null);
+
+ assertNull(dataSource.getUser());
+ assertNull(dataSource.getPassword());
+ }
+}