Skip to content

Commit 7cda118

Browse files
committed
feat: support ipv6
1 parent c84fd78 commit 7cda118

14 files changed

Lines changed: 282 additions & 138 deletions

src/main/java/com/taosdata/jdbc/AbstractDriver.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ protected DriverPropertyInfo[] getPropertyInfo(Properties info) {
5454
propertyInfo[4] = passwordProp;
5555
return propertyInfo;
5656
}
57-
58-
protected Properties parseURL(String url, Properties defaults) {
59-
return StringUtils.parseUrl(url, defaults);
60-
}
61-
62-
63-
6457
protected Connection getWSConnection(String url, ConnectionParam param, Properties props) throws SQLException {
6558
if (log.isDebugEnabled()){
6659
log.debug("getWSConnection, url = {}", StringUtils.getBasicUrl(url));

src/main/java/com/taosdata/jdbc/SchemalessWriter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,18 @@ public SchemalessWriter(String url, String user, String password, String cloudTo
9494

9595
private void init(String url, String user, String password, String cloudToken, String dbName, Boolean useSSL) throws SQLException {
9696
String t;
97+
Properties properties;
98+
9799
if (url.startsWith(TSDBDriver.URL_PREFIX)) {
98100
t = "jni";
101+
properties = StringUtils.parseUrl(url, null, true);
99102
} else if (url.startsWith(RestfulDriver.URL_PREFIX)) {
100103
t = "ws";
104+
properties = StringUtils.parseUrl(url, null, false);
101105
} else {
102106
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "unknown url:" + url);
103107
}
104108

105-
Properties properties = StringUtils.parseUrl(url, null);
106109
if (user != null)
107110
properties.setProperty(TSDBDriver.PROPERTY_KEY_USER, user);
108111
if (password != null)

src/main/java/com/taosdata/jdbc/TSDBDriver.java

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.taosdata.jdbc;
22

3+
import com.taosdata.jdbc.utils.StringUtils;
4+
35
import java.sql.*;
46
import java.util.Properties;
57
import java.util.StringTokenizer;
@@ -222,62 +224,15 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws
222224
/**
223225
* example: jdbc:TAOS://127.0.0.1:0/db?user=root&password=your_password
224226
*/
225-
@Override
226-
public Properties parseURL(String url, Properties defaults) {
227-
Properties urlProps = (defaults != null) ? defaults : new Properties();
227+
public Properties parseURL(String url, Properties defaults) throws SQLException {
228228
if (url == null || url.length() <= 0 || url.trim().length() <= 0)
229-
return null;
229+
return new Properties();
230230
if (!url.startsWith(URL_PREFIX) && !url.startsWith(URL_PREFIX1))
231-
return null;
231+
return new Properties();
232232

233233
// parse properties
234-
String urlForMeta = url;
235-
int beginningOfSlashes = url.indexOf("//");
236-
int index = url.indexOf("?");
237-
if (index != -1) {
238-
String paramString = url.substring(index + 1);
239-
url = url.substring(0, index);
240-
StringTokenizer queryParams = new StringTokenizer(paramString, "&");
241-
while (queryParams.hasMoreElements()) {
242-
String oneToken = queryParams.nextToken();
243-
String[] pair = oneToken.split("=");
244-
245-
if ((pair[0] != null && pair[0].trim().length() > 0) && (pair[1] != null && pair[1].trim().length() > 0)) {
246-
urlProps.setProperty(pair[0].trim(), pair[1].trim());
247-
}
248-
}
249-
}
250-
251-
// parse Product Name
252-
String dbProductName = url.substring(0, beginningOfSlashes);
253-
dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1);
254-
dbProductName = dbProductName.substring(0, dbProductName.indexOf(":"));
255-
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PRODUCT_NAME, dbProductName);
256-
257-
// parse database name
258-
url = url.substring(beginningOfSlashes + 2);
259-
int indexOfSlash = url.indexOf("/");
260-
if (indexOfSlash != -1) {
261-
if (indexOfSlash + 1 < url.length()) {
262-
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME, url.substring(indexOfSlash + 1).toLowerCase());
263-
}
264-
url = url.substring(0, indexOfSlash);
265-
}
266-
267-
// parse port
268-
int indexOfColon = url.indexOf(":");
269-
if (indexOfColon != -1) {
270-
if (indexOfColon + 1 < url.length()) {
271-
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PORT, url.substring(indexOfColon + 1));
272-
}
273-
url = url.substring(0, indexOfColon);
274-
}
275-
276-
if (url.length() > 0 && url.trim().length() > 0) {
277-
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_HOST, url);
278-
}
279-
280-
this.dbMetaData = new TSDBDatabaseMetaData(urlForMeta, urlProps.getProperty(TSDBDriver.PROPERTY_KEY_USER));
234+
Properties urlProps = StringUtils.parseUrl(url, defaults,true);
235+
this.dbMetaData = new TSDBDatabaseMetaData(url, urlProps.getProperty(TSDBDriver.PROPERTY_KEY_USER));
281236
return urlProps;
282237
}
283238

src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Connection connect(String url, Properties info) throws SQLException {
4343
if (!acceptsURL(url))
4444
return null;
4545

46-
Properties props = parseURL(url, info);
46+
Properties props = StringUtils.parseUrl(url, info, false);
4747
String batchLoad = info.getProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD);
4848
if (Boolean.parseBoolean(batchLoad)) {
4949
ConnectionParam param = ConnectionParam.getParamWs(props);
@@ -82,7 +82,7 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws
8282
info = new Properties();
8383
}
8484
if (acceptsURL(url)) {
85-
info = parseURL(url, info);
85+
info = StringUtils.parseUrl(url, info, false);
8686
}
8787
return getPropertyInfo(info);
8888
}

src/main/java/com/taosdata/jdbc/tmq/JNIConsumer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.taosdata.jdbc.TSDBConstants;
44
import com.taosdata.jdbc.common.Consumer;
55
import com.taosdata.jdbc.enums.TmqMessageType;
6+
import com.taosdata.jdbc.utils.StringUtils;
67

78
import java.sql.SQLException;
89
import java.time.Duration;
@@ -21,6 +22,16 @@ public JNIConsumer() {
2122

2223
@Override
2324
public void create(Properties properties) throws SQLException {
25+
if (!StringUtils.isEmpty(properties.getProperty(TMQConstants.CONNECT_IP)) &&
26+
properties.getProperty(TMQConstants.CONNECT_IP).startsWith("[")
27+
) {
28+
// IPv6 address
29+
String ip = properties.getProperty(TMQConstants.CONNECT_IP);
30+
if (ip.endsWith("]")) {
31+
ip = ip.substring(1, ip.length() - 1);
32+
}
33+
properties.setProperty(TMQConstants.CONNECT_IP, ip);
34+
}
2435
long config = connector.createConfig(properties);
2536
try {
2637
connector.createConsumer(config);

src/main/java/com/taosdata/jdbc/tmq/TaosConsumer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.taosdata.jdbc.tmq;
22

3+
import com.taosdata.jdbc.TSDBDriver;
34
import com.taosdata.jdbc.TSDBError;
45
import com.taosdata.jdbc.TSDBErrorNumbers;
56
import com.taosdata.jdbc.common.Consumer;
@@ -37,14 +38,13 @@ public TaosConsumer(Properties properties) throws SQLException {
3738

3839
String servers = properties.getProperty(TMQConstants.BOOTSTRAP_SERVERS);
3940
if (!StringUtils.isEmpty(servers)) {
40-
Arrays.stream(servers.split(",")).filter(s -> !StringUtils.isEmpty(s))
41-
.findFirst().ifPresent(s -> {
42-
String[] host = s.split(":");
43-
properties.setProperty(TMQConstants.CONNECT_IP, host[0]);
44-
if (host.length > 1) {
45-
properties.setProperty(TMQConstants.CONNECT_PORT, host[1]);
46-
}
47-
});
41+
Properties tempProp = StringUtils.parseHostPort(servers, false);
42+
if (!StringUtils.isEmpty(tempProp.getProperty(TSDBDriver.PROPERTY_KEY_HOST))) {
43+
properties.setProperty(TMQConstants.CONNECT_IP, tempProp.getProperty(TSDBDriver.PROPERTY_KEY_HOST));
44+
}
45+
if (!StringUtils.isEmpty(tempProp.getProperty(TSDBDriver.PROPERTY_KEY_PORT))) {
46+
properties.setProperty(TMQConstants.CONNECT_PORT, tempProp.getProperty(TSDBDriver.PROPERTY_KEY_PORT));
47+
}
4848
}
4949

5050
String s = properties.getProperty(TMQConstants.VALUE_DESERIALIZER);

src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,6 @@ public static void init(Properties props) {
8686
}
8787
}
8888

89-
/*** execute GET request ***/
90-
// public static String execute(String uri) throws SQLException {
91-
// HttpEntity httpEntity = null;
92-
// String responseBody = "";
93-
// HttpRequestBase method = getRequest(uri, HttpGet.METHOD_NAME);
94-
// HttpContext context = HttpClientContext.create();
95-
//
96-
// try (CloseableHttpResponse httpResponse = httpClient.execute(method, context)) {
97-
// httpEntity = httpResponse.getEntity();
98-
// if (httpEntity != null) {
99-
// responseBody = EntityUtils.toString(httpEntity, StandardCharsets.UTF_8);
100-
// }
101-
// } catch (ClientProtocolException e) {
102-
// throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESTFul_Client_Protocol_Exception, e.getMessage());
103-
// } catch (IOException exception) {
104-
// throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESTFul_Client_IOException, exception.getMessage());
105-
// } finally {
106-
// if (httpEntity != null) {
107-
// EntityUtils.consumeQuietly(httpEntity);
108-
// }
109-
// }
110-
// return responseBody;
111-
// }
112-
11389
public static String execute(String uri, String data, String auth) throws SQLException {
11490
return execute(uri, data, auth, null);
11591
}

src/main/java/com/taosdata/jdbc/utils/StringUtils.java

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.taosdata.jdbc.utils;
22

33
import com.taosdata.jdbc.TSDBDriver;
4+
import com.taosdata.jdbc.TSDBError;
5+
import com.taosdata.jdbc.TSDBErrorNumbers;
46

7+
import java.sql.SQLException;
58
import java.util.Properties;
69
import java.util.StringTokenizer;
710

@@ -31,62 +34,109 @@ public static boolean isNumeric(String str) {
3134
return true;
3235
}
3336

34-
public static Properties parseUrl(String url, Properties defaults) {
37+
public static Properties parseHostPort(String hostPortDb, boolean isNative) throws SQLException {
38+
Properties properties = new Properties();
39+
40+
// parse host and port
41+
String host = hostPortDb;
42+
String port = null;
43+
44+
// ipv6 address
45+
if (hostPortDb.startsWith("[")) {
46+
int endBracket = hostPortDb.indexOf(']');
47+
if (endBracket != -1) {
48+
// extract host
49+
if (isNative){
50+
host = hostPortDb.substring(1, endBracket);
51+
} else {
52+
host = hostPortDb.substring(0, endBracket + 1);
53+
}
54+
55+
// if have port
56+
if (endBracket + 1 < hostPortDb.length() &&
57+
hostPortDb.charAt(endBracket + 1) == ':') {
58+
port = hostPortDb.substring(endBracket + 2);
59+
}
60+
} else {
61+
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "Invalid IPv6 address: " + hostPortDb);
62+
}
63+
}
64+
// ipv4 address or hostname
65+
else {
66+
// find last ':'
67+
int lastColon = hostPortDb.lastIndexOf(':');
68+
69+
if (lastColon != -1) {
70+
host = hostPortDb.substring(0, lastColon);
71+
port = hostPortDb.substring(lastColon + 1);
72+
}
73+
}
74+
75+
// handle Scope Identifier like fe80::1%eth0
76+
if (host.contains("%")) {
77+
host = host.replace("%", "%25");
78+
}
79+
80+
// set property
81+
if (!host.trim().isEmpty()) {
82+
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
83+
}
84+
if (port != null && !port.trim().isEmpty()) {
85+
properties.setProperty(TSDBDriver.PROPERTY_KEY_PORT, port);
86+
}
87+
88+
return properties;
89+
}
90+
91+
public static Properties parseUrl(String url, Properties defaults, boolean isNative) throws SQLException {
3592
Properties urlProps = (defaults != null) ? defaults : new Properties();
3693
if (StringUtils.isEmpty(url)) {
3794
return urlProps;
3895
}
3996

40-
// parse properties in url
41-
int beginningOfSlashes = url.indexOf("//");
42-
int index = url.indexOf("?");
43-
if (index != -1) {
44-
String paramString = url.substring(index + 1);
45-
url = url.substring(0, index);
97+
// parse parameters
98+
int paramStart = url.indexOf("?");
99+
if (paramStart != -1) {
100+
String paramString = url.substring(paramStart + 1);
101+
url = url.substring(0, paramStart);
46102
StringTokenizer queryParams = new StringTokenizer(paramString, "&");
47103
while (queryParams.hasMoreElements()) {
48-
String parameterValuePair = queryParams.nextToken();
49-
int indexOfEqual = parameterValuePair.indexOf("=");
50-
String parameter = null;
51-
String value = null;
52-
if (indexOfEqual != -1) {
53-
parameter = parameterValuePair.substring(0, indexOfEqual);
54-
if (indexOfEqual + 1 < parameterValuePair.length()) {
55-
value = parameterValuePair.substring(indexOfEqual + 1);
104+
String pair = queryParams.nextToken();
105+
int eqIdx = pair.indexOf("=");
106+
if (eqIdx != -1) {
107+
String key = pair.substring(0, eqIdx);
108+
String value = eqIdx + 1 < pair.length() ? pair.substring(eqIdx + 1) : "";
109+
if (!key.isEmpty() && !value.isEmpty()) {
110+
urlProps.setProperty(key, value);
56111
}
57112
}
58-
if (value != null && value.length() > 0 && parameter.length() > 0) {
59-
urlProps.setProperty(parameter, value);
60-
}
61113
}
62114
}
63115

64-
// parse Product Name
65-
String dbProductName = url.substring(0, beginningOfSlashes);
116+
// parse product name
117+
int slashesStart = url.indexOf("//");
118+
String dbProductName = url.substring(0, slashesStart);
66119
dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1);
67120
dbProductName = dbProductName.substring(0, dbProductName.indexOf(":"));
68121
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PRODUCT_NAME, dbProductName);
122+
123+
// extract host,port and dbname
124+
String hostPortDb = url.substring(slashesStart + 2);
125+
69126
// parse dbname
70-
url = url.substring(beginningOfSlashes + 2);
71-
int indexOfSlash = url.indexOf("/");
72-
if (indexOfSlash != -1) {
73-
if (indexOfSlash + 1 < url.length()) {
74-
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME, url.substring(indexOfSlash + 1).toLowerCase());
127+
int dbStart = hostPortDb.indexOf("/");
128+
if (dbStart != -1) {
129+
if (dbStart + 1 < hostPortDb.length()) {
130+
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME,
131+
hostPortDb.substring(dbStart + 1).toLowerCase());
75132
}
76-
url = url.substring(0, indexOfSlash);
77-
}
78-
// parse port
79-
int indexOfColon = url.indexOf(":");
80-
if (indexOfColon != -1) {
81-
if (indexOfColon + 1 < url.length()) {
82-
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PORT, url.substring(indexOfColon + 1));
83-
}
84-
url = url.substring(0, indexOfColon);
85-
}
86-
// parse host
87-
if (url.length() > 0 && url.trim().length() > 0) {
88-
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_HOST, url);
133+
hostPortDb = hostPortDb.substring(0, dbStart);
89134
}
135+
136+
// parse host and port
137+
Properties hostPortProps = parseHostPort(hostPortDb, isNative);
138+
urlProps.putAll(hostPortProps);
139+
90140
return urlProps;
91141
}
92142

src/main/java/com/taosdata/jdbc/ws/WebSocketDriver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.taosdata.jdbc.TSDBError;
66
import com.taosdata.jdbc.TSDBErrorNumbers;
77
import com.taosdata.jdbc.rs.ConnectionParam;
8+
import com.taosdata.jdbc.utils.StringUtils;
89

910
import java.sql.*;
1011
import java.util.Properties;
@@ -31,7 +32,7 @@ public Connection connect(String url, Properties info) throws SQLException {
3132
if (!acceptsURL(url))
3233
return null;
3334

34-
Properties props = parseURL(url, info);
35+
Properties props = StringUtils.parseUrl(url, info, false);
3536
ConnectionParam param = ConnectionParam.getParamWs(props);
3637
return getWSConnection(url, param, props);
3738

@@ -50,7 +51,7 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws
5051
info = new Properties();
5152
}
5253
if (acceptsURL(url)) {
53-
info = parseURL(url, info);
54+
info = StringUtils.parseUrl(url, info, false);
5455
}
5556
return getPropertyInfo(info);
5657
}

0 commit comments

Comments
 (0)