Skip to content

Commit c6a13c7

Browse files
committed
Improve JDBC driver metadata
1 parent eb4409d commit c6a13c7

4 files changed

Lines changed: 177 additions & 9 deletions

File tree

iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Config.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ private Config() {
5151
static final int RETRY_NUM = 3;
5252
static final long RETRY_INTERVAL_MS = 1000;
5353

54+
static final int DRIVER_MAJOR_VERSION = 4;
55+
static final int DRIVER_MINOR_VERSION = 3;
56+
5457
public static final int DEFAULT_FETCH_SIZE = 5000;
5558
static final int DEFAULT_CONNECTION_TIMEOUT_MS = 0;
5659

iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBAbstractDatabaseMetadata.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,12 @@ public String getDriverName() throws SQLException {
606606

607607
@Override
608608
public int getDriverMajorVersion() {
609-
return 4;
609+
return Config.DRIVER_MAJOR_VERSION;
610610
}
611611

612612
@Override
613613
public int getDriverMinorVersion() {
614-
return 3;
614+
return Config.DRIVER_MINOR_VERSION;
615615
}
616616

617617
@Override

iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDriver.java

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
package org.apache.iotdb.jdbc;
2121

2222
import org.apache.iotdb.jdbc.i18n.JdbcMessages;
23+
import org.apache.iotdb.rpc.RpcUtils;
2324

2425
import org.apache.thrift.transport.TTransportException;
26+
import org.apache.tsfile.common.conf.TSFileConfig;
2527
import org.osgi.service.component.annotations.Component;
2628

2729
import java.sql.Connection;
@@ -30,6 +32,8 @@
3032
import java.sql.DriverPropertyInfo;
3133
import java.sql.SQLException;
3234
import java.sql.SQLFeatureNotSupportedException;
35+
import java.time.ZoneId;
36+
import java.util.Arrays;
3337
import java.util.Properties;
3438
import java.util.logging.Logger;
3539
import java.util.regex.Pattern;
@@ -42,6 +46,11 @@ public class IoTDBDriver implements Driver {
4246
/** Is this driver JDBC compliant. */
4347
private static final boolean TSFILE_JDBC_COMPLIANT = false;
4448

49+
private static final String[] BOOLEAN_CHOICES = {"true", "false"};
50+
private static final String[] SQL_DIALECT_CHOICES = {Constant.TREE, Constant.TABLE};
51+
private static final String[] VERSION_CHOICES =
52+
Arrays.stream(Constant.Version.values()).map(Enum::name).toArray(String[]::new);
53+
4554
static {
4655
try {
4756
DriverManager.registerDriver(new IoTDBDriver());
@@ -58,7 +67,7 @@ public IoTDBDriver() {
5867

5968
@Override
6069
public boolean acceptsURL(String url) {
61-
return Pattern.matches(TSFILE_URL_PREFIX, url);
70+
return url != null && Pattern.matches(TSFILE_URL_PREFIX, url);
6271
}
6372

6473
@Override
@@ -75,14 +84,12 @@ public Connection connect(String url, Properties info) throws SQLException {
7584

7685
@Override
7786
public int getMajorVersion() {
78-
// TODO Auto-generated method stub
79-
return 0;
87+
return Config.DRIVER_MAJOR_VERSION;
8088
}
8189

8290
@Override
8391
public int getMinorVersion() {
84-
// TODO Auto-generated method stub
85-
return 0;
92+
return Config.DRIVER_MINOR_VERSION;
8693
}
8794

8895
@Override
@@ -92,12 +99,74 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
9299

93100
@Override
94101
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
95-
// TODO Auto-generated method stub
96-
return new DriverPropertyInfo[0];
102+
Properties properties = info == null ? new Properties() : info;
103+
return new DriverPropertyInfo[] {
104+
createProperty(
105+
Config.AUTH_USER, Config.DEFAULT_USER, "User name for authentication.", properties),
106+
createProperty(
107+
Config.AUTH_PASSWORD,
108+
Config.DEFAULT_PASSWORD,
109+
"Password for authentication.",
110+
properties),
111+
createProperty(
112+
Config.DEFAULT_BUFFER_CAPACITY,
113+
String.valueOf(RpcUtils.THRIFT_DEFAULT_BUF_CAPACITY),
114+
"Thrift default buffer capacity in bytes.",
115+
properties),
116+
createProperty(
117+
Config.THRIFT_FRAME_MAX_SIZE,
118+
String.valueOf(RpcUtils.THRIFT_FRAME_MAX_SIZE),
119+
"Thrift max frame size in bytes.",
120+
properties),
121+
createProperty(
122+
Config.VERSION,
123+
Config.DEFAULT_VERSION.name(),
124+
VERSION_CHOICES,
125+
"Client compatibility version.",
126+
properties),
127+
createProperty(
128+
Config.NETWORK_TIMEOUT,
129+
String.valueOf(Config.DEFAULT_CONNECTION_TIMEOUT_MS),
130+
"Network timeout in milliseconds.",
131+
properties),
132+
createProperty(
133+
Config.TIME_ZONE, ZoneId.systemDefault().toString(), "Connection time zone.", properties),
134+
createProperty(
135+
Config.CHARSET, TSFileConfig.STRING_CHARSET.name(), "Connection charset.", properties),
136+
createProperty(
137+
Config.USE_SSL, "false", BOOLEAN_CHOICES, "Whether to enable SSL.", properties),
138+
createProperty(Config.TRUST_STORE, null, "SSL trust store path.", properties),
139+
createProperty(Config.TRUST_STORE_PWD, null, "SSL trust store password.", properties),
140+
createProperty(
141+
Config.SQL_DIALECT,
142+
Constant.TREE,
143+
SQL_DIALECT_CHOICES,
144+
"SQL dialect for the connection.",
145+
properties)
146+
};
97147
}
98148

99149
@Override
100150
public boolean jdbcCompliant() {
101151
return TSFILE_JDBC_COMPLIANT;
102152
}
153+
154+
private static DriverPropertyInfo createProperty(
155+
String name, String defaultValue, String description, Properties properties) {
156+
return createProperty(name, defaultValue, null, description, properties);
157+
}
158+
159+
private static DriverPropertyInfo createProperty(
160+
String name,
161+
String defaultValue,
162+
String[] choices,
163+
String description,
164+
Properties properties) {
165+
DriverPropertyInfo propertyInfo =
166+
new DriverPropertyInfo(name, properties.getProperty(name, defaultValue));
167+
propertyInfo.required = false;
168+
propertyInfo.choices = choices;
169+
propertyInfo.description = description;
170+
return propertyInfo;
171+
}
103172
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.jdbc;
21+
22+
import org.junit.Test;
23+
24+
import java.sql.DriverPropertyInfo;
25+
import java.util.Arrays;
26+
import java.util.Properties;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertFalse;
30+
import static org.junit.Assert.assertNotNull;
31+
import static org.junit.Assert.assertTrue;
32+
import static org.junit.Assert.fail;
33+
34+
public class IoTDBDriverTest {
35+
36+
@Test
37+
public void testAcceptsUrl() {
38+
IoTDBDriver driver = new IoTDBDriver();
39+
40+
assertTrue(driver.acceptsURL("jdbc:iotdb://localhost:6667"));
41+
assertFalse(driver.acceptsURL(null));
42+
assertFalse(driver.acceptsURL("jdbc:mysql://localhost:3306"));
43+
}
44+
45+
@Test
46+
public void testDriverVersion() {
47+
IoTDBDriver driver = new IoTDBDriver();
48+
49+
assertEquals(Config.DRIVER_MAJOR_VERSION, driver.getMajorVersion());
50+
assertEquals(Config.DRIVER_MINOR_VERSION, driver.getMinorVersion());
51+
}
52+
53+
@Test
54+
public void testGetPropertyInfo() {
55+
IoTDBDriver driver = new IoTDBDriver();
56+
Properties properties = new Properties();
57+
properties.setProperty(Config.AUTH_USER, "root");
58+
properties.setProperty(Config.USE_SSL, "true");
59+
60+
DriverPropertyInfo[] propertyInfos =
61+
driver.getPropertyInfo("jdbc:iotdb://localhost:6667", properties);
62+
63+
assertTrue(propertyInfos.length > 0);
64+
assertEquals("root", findProperty(propertyInfos, Config.AUTH_USER).value);
65+
assertEquals(Config.DEFAULT_PASSWORD, findProperty(propertyInfos, Config.AUTH_PASSWORD).value);
66+
assertEquals("true", findProperty(propertyInfos, Config.USE_SSL).value);
67+
assertEquals(
68+
Arrays.asList("true", "false"),
69+
Arrays.asList(findProperty(propertyInfos, Config.USE_SSL).choices));
70+
assertEquals(
71+
Arrays.asList(Constant.TREE, Constant.TABLE),
72+
Arrays.asList(findProperty(propertyInfos, Config.SQL_DIALECT).choices));
73+
assertEquals(Config.DEFAULT_VERSION.name(), findProperty(propertyInfos, Config.VERSION).value);
74+
}
75+
76+
@Test
77+
public void testGetPropertyInfoAllowsNullProperties() {
78+
IoTDBDriver driver = new IoTDBDriver();
79+
80+
DriverPropertyInfo[] propertyInfos =
81+
driver.getPropertyInfo("jdbc:iotdb://localhost:6667", null);
82+
83+
assertNotNull(propertyInfos);
84+
assertEquals(Config.DEFAULT_USER, findProperty(propertyInfos, Config.AUTH_USER).value);
85+
}
86+
87+
private static DriverPropertyInfo findProperty(DriverPropertyInfo[] propertyInfos, String name) {
88+
for (DriverPropertyInfo propertyInfo : propertyInfos) {
89+
if (name.equals(propertyInfo.name)) {
90+
return propertyInfo;
91+
}
92+
}
93+
fail("Missing driver property: " + name);
94+
return null;
95+
}
96+
}

0 commit comments

Comments
 (0)