未验证 提交 a50a50c0 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #3563 from taosdata/feature/TD-1313

Feature/td 1313
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<name>JDBCDriver</name> <name>JDBCDriver</name>
<url>https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc</url> <url>https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc</url>
<description>TDengine JDBC Driver</description> <description>TDengine JDBC Driver</description>
<licenses> <licenses>
<license> <license>
......
...@@ -53,66 +53,12 @@ public class TSDBConnection implements Connection { ...@@ -53,66 +53,12 @@ public class TSDBConnection implements Connection {
public TSDBConnection(Properties info, TSDBDatabaseMetaData meta) throws SQLException { public TSDBConnection(Properties info, TSDBDatabaseMetaData meta) throws SQLException {
this.dbMetaData = meta; this.dbMetaData = meta;
//load taos.cfg start
File cfgDir = loadConfigDir(info.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR));
File cfgFile = cfgDir.listFiles((dir, name) -> "taos.cfg".equalsIgnoreCase(name))[0];
List<String> endpoints = loadConfigEndpoints(cfgFile);
if (!endpoints.isEmpty()) {
info.setProperty(TSDBDriver.PROPERTY_KEY_HOST, endpoints.get(0).split(":")[0]);
info.setProperty(TSDBDriver.PROPERTY_KEY_PORT, endpoints.get(0).split(":")[1]);
}
//load taos.cfg end
connect(info.getProperty(TSDBDriver.PROPERTY_KEY_HOST), connect(info.getProperty(TSDBDriver.PROPERTY_KEY_HOST),
Integer.parseInt(info.getProperty(TSDBDriver.PROPERTY_KEY_PORT, "0")), Integer.parseInt(info.getProperty(TSDBDriver.PROPERTY_KEY_PORT, "0")),
info.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME), info.getProperty(TSDBDriver.PROPERTY_KEY_USER), info.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME), info.getProperty(TSDBDriver.PROPERTY_KEY_USER),
info.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD)); info.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD));
} }
private List<String> loadConfigEndpoints(File cfgFile) {
List<String> endpoints = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(cfgFile))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("firstEp") || line.trim().startsWith("secondEp")) {
endpoints.add(line.substring(line.indexOf('p') + 1).trim());
}
if (endpoints.size() > 1)
break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return endpoints;
}
/**
* @param cfgDirPath
* @return return the config dir
**/
private File loadConfigDir(String cfgDirPath) {
if (cfgDirPath == null)
return loadDefaultConfigDir();
File cfgDir = new File(cfgDirPath);
if (!cfgDir.exists())
return loadDefaultConfigDir();
return cfgDir;
}
/**
* @return search the default config dir, if the config dir is not exist will return null
*/
private File loadDefaultConfigDir() {
File cfgDir;
File cfgDir_linux = new File("/etc/taos");
cfgDir = cfgDir_linux.exists() ? cfgDir_linux : null;
File cfgDir_windows = new File("C:\\TDengine\\cfg");
cfgDir = (cfgDir == null && cfgDir_windows.exists()) ? cfgDir_windows : cfgDir;
return cfgDir;
}
private void connect(String host, int port, String dbName, String user, String password) throws SQLException { private void connect(String host, int port, String dbName, String user, String password) throws SQLException {
this.connector = new TSDBJNIConnector(); this.connector = new TSDBJNIConnector();
this.connector.connect(host, port, dbName, user, password); this.connector.connect(host, port, dbName, user, password);
......
...@@ -68,15 +68,15 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { ...@@ -68,15 +68,15 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
} }
public boolean nullsAreSortedLow() throws SQLException { public boolean nullsAreSortedLow() throws SQLException {
return false; return !nullsAreSortedHigh();
} }
public boolean nullsAreSortedAtStart() throws SQLException { public boolean nullsAreSortedAtStart() throws SQLException {
return false; return true;
} }
public boolean nullsAreSortedAtEnd() throws SQLException { public boolean nullsAreSortedAtEnd() throws SQLException {
return false; return !nullsAreSortedAtStart();
} }
public String getDatabaseProductName() throws SQLException { public String getDatabaseProductName() throws SQLException {
......
...@@ -14,7 +14,12 @@ ...@@ -14,7 +14,12 @@
*****************************************************************************/ *****************************************************************************/
package com.taosdata.jdbc; package com.taosdata.jdbc;
import java.io.*;
import java.sql.*; import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -39,9 +44,11 @@ import java.util.logging.Logger; ...@@ -39,9 +44,11 @@ import java.util.logging.Logger;
*/ */
public class TSDBDriver implements java.sql.Driver { public class TSDBDriver implements java.sql.Driver {
@Deprecated @Deprecated
private static final String URL_PREFIX1 = "jdbc:tsdb://"; private static final String URL_PREFIX1 = "jdbc:TSDB://";
private static final String URL_PREFIX = "jdbc:taos://";
private static final String URL_PREFIX = "jdbc:TAOS://";
/** /**
* Key used to retrieve the database value from the properties instance passed * Key used to retrieve the database value from the properties instance passed
...@@ -72,6 +79,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -72,6 +79,7 @@ public class TSDBDriver implements java.sql.Driver {
*/ */
public static final String PROPERTY_KEY_USER = "user"; public static final String PROPERTY_KEY_USER = "user";
/** /**
* Key for the configuration file directory of TSDB client in properties instance * Key for the configuration file directory of TSDB client in properties instance
*/ */
...@@ -95,6 +103,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -95,6 +103,7 @@ public class TSDBDriver implements java.sql.Driver {
public static final String PROPERTY_KEY_PROTOCOL = "protocol"; public static final String PROPERTY_KEY_PROTOCOL = "protocol";
/** /**
* Index for port coming out of parseHostPortPair(). * Index for port coming out of parseHostPortPair().
*/ */
...@@ -115,17 +124,71 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -115,17 +124,71 @@ public class TSDBDriver implements java.sql.Driver {
} }
} }
private List<String> loadConfigEndpoints(File cfgFile) {
List<String> endpoints = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(cfgFile))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("firstEp") || line.trim().startsWith("secondEp")) {
endpoints.add(line.substring(line.indexOf('p') + 1).trim());
}
if (endpoints.size() > 1)
break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return endpoints;
}
/**
* @param cfgDirPath
* @return return the config dir
**/
private File loadConfigDir(String cfgDirPath) {
if (cfgDirPath == null)
return loadDefaultConfigDir();
File cfgDir = new File(cfgDirPath);
if (!cfgDir.exists())
return loadDefaultConfigDir();
return cfgDir;
}
/**
* @return search the default config dir, if the config dir is not exist will return null
*/
private File loadDefaultConfigDir() {
File cfgDir;
File cfgDir_linux = new File("/etc/taos");
cfgDir = cfgDir_linux.exists() ? cfgDir_linux : null;
File cfgDir_windows = new File("C:\\TDengine\\cfg");
cfgDir = (cfgDir == null && cfgDir_windows.exists()) ? cfgDir_windows : cfgDir;
return cfgDir;
}
public Connection connect(String url, Properties info) throws SQLException { public Connection connect(String url, Properties info) throws SQLException {
if (url == null) { if (url == null) {
throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!")); throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!"));
} }
Properties props = null; Properties props = null;
if ((props = parseURL(url, info)) == null) { if ((props = parseURL(url, info)) == null) {
return null; return null;
} }
//load taos.cfg start
if (info.getProperty(TSDBDriver.PROPERTY_KEY_HOST) == null && info.getProperty(TSDBDriver.PROPERTY_KEY_PORT) == null){
File cfgDir = loadConfigDir(info.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR));
File cfgFile = cfgDir.listFiles((dir, name) -> "taos.cfg".equalsIgnoreCase(name))[0];
List<String> endpoints = loadConfigEndpoints(cfgFile);
if (!endpoints.isEmpty()) {
info.setProperty(TSDBDriver.PROPERTY_KEY_HOST, endpoints.get(0).split(":")[0]);
info.setProperty(TSDBDriver.PROPERTY_KEY_PORT, endpoints.get(0).split(":")[1]);
}
}
try { try {
TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), (String) props.get(PROPERTY_KEY_CHARSET), TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), (String) props.get(PROPERTY_KEY_CHARSET),
(String) props.get(PROPERTY_KEY_TIME_ZONE)); (String) props.get(PROPERTY_KEY_TIME_ZONE));
...@@ -150,13 +213,9 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -150,13 +213,9 @@ public class TSDBDriver implements java.sql.Driver {
* and the element of index PORT_NUMBER_INDEX being the port (or null if not * and the element of index PORT_NUMBER_INDEX being the port (or null if not
* specified). * specified).
* *
* @param hostPortPair * @param hostPortPair host and port in form of of [host][:port]
* host and port in form of of [host][:port]
*
* @return array containing host and port as Strings * @return array containing host and port as Strings
* * @throws SQLException if a parse error occurs
* @throws SQLException
* if a parse error occurs
*/ */
protected static String[] parseHostPortPair(String hostPortPair) throws SQLException { protected static String[] parseHostPortPair(String hostPortPair) throws SQLException {
String[] splitValues = new String[2]; String[] splitValues = new String[2];
...@@ -200,8 +259,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -200,8 +259,7 @@ public class TSDBDriver implements java.sql.Driver {
DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST)); DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST));
hostProp.required = true; hostProp.required = true;
DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT));
info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT));
portProp.required = false; portProp.required = false;
DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME)); DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME));
...@@ -211,8 +269,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -211,8 +269,7 @@ public class TSDBDriver implements java.sql.Driver {
DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER)); DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER));
userProp.required = true; userProp.required = true;
DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, info.getProperty(PROPERTY_KEY_PASSWORD));
info.getProperty(PROPERTY_KEY_PASSWORD));
passwordProp.required = true; passwordProp.required = true;
DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5]; DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5];
...@@ -228,7 +285,6 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -228,7 +285,6 @@ public class TSDBDriver implements java.sql.Driver {
/** /**
* example: jdbc:TSDB://127.0.0.1:0/db?user=root&password=your_password * example: jdbc:TSDB://127.0.0.1:0/db?user=root&password=your_password
*/ */
public Properties parseURL(String url, Properties defaults) throws java.sql.SQLException { public Properties parseURL(String url, Properties defaults) throws java.sql.SQLException {
Properties urlProps = (defaults != null) ? defaults : new Properties(); Properties urlProps = (defaults != null) ? defaults : new Properties();
if (url == null) { if (url == null) {
...@@ -261,7 +317,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -261,7 +317,7 @@ public class TSDBDriver implements java.sql.Driver {
url = url.trim().substring(url.indexOf("?") + 1); url = url.trim().substring(url.indexOf("?") + 1);
} else { } else {
// without user & password so return // without user & password so return
if(!url.trim().isEmpty()) { if (!url.trim().isEmpty()) {
String dbName = url.trim(); String dbName = url.trim();
urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName); urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName);
} }
...@@ -282,7 +338,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -282,7 +338,7 @@ public class TSDBDriver implements java.sql.Driver {
String[] queryStrings = url.trim().split("&"); String[] queryStrings = url.trim().split("&");
for (String queryStr : queryStrings) { for (String queryStr : queryStrings) {
String[] kvPair = queryStr.trim().split("="); String[] kvPair = queryStr.trim().split("=");
if (kvPair.length < 2){ if (kvPair.length < 2) {
continue; continue;
} }
setPropertyValue(urlProps, kvPair); setPropertyValue(urlProps, kvPair);
...@@ -337,9 +393,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -337,9 +393,7 @@ public class TSDBDriver implements java.sql.Driver {
/** /**
* Returns the host property * Returns the host property
* *
* @param props * @param props the java.util.Properties instance to retrieve the hostname from.
* the java.util.Properties instance to retrieve the hostname from.
*
* @return the host * @return the host
*/ */
public String host(Properties props) { public String host(Properties props) {
...@@ -349,9 +403,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -349,9 +403,7 @@ public class TSDBDriver implements java.sql.Driver {
/** /**
* Returns the port number property * Returns the port number property
* *
* @param props * @param props the properties to get the port number from
* the properties to get the port number from
*
* @return the port number * @return the port number
*/ */
public int port(Properties props) { public int port(Properties props) {
...@@ -361,9 +413,7 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -361,9 +413,7 @@ public class TSDBDriver implements java.sql.Driver {
/** /**
* Returns the database property from <code>props</code> * Returns the database property from <code>props</code>
* *
* @param props * @param props the Properties to look for the database property.
* the Properties to look for the database property.
*
* @return the database name. * @return the database name.
*/ */
public String database(Properties props) { public String database(Properties props) {
......
...@@ -242,7 +242,7 @@ public class TSDBStatement implements Statement { ...@@ -242,7 +242,7 @@ public class TSDBStatement implements Statement {
public void addBatch(String sql) throws SQLException { public void addBatch(String sql) throws SQLException {
if (batchedArgs == null) { if (batchedArgs == null) {
batchedArgs = new ArrayList<String>(); batchedArgs = new ArrayList<>();
} }
batchedArgs.add(sql); batchedArgs.add(sql);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册