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

Merge pull request #3179 from taosdata/TD-1174_feature

TD-1174: jdbc without host ip
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
*****************************************************************************/ *****************************************************************************/
package com.taosdata.jdbc; package com.taosdata.jdbc;
import java.io.*;
import java.sql.Array; import java.sql.Array;
import java.sql.Blob; import java.sql.Blob;
import java.sql.CallableStatement; import java.sql.CallableStatement;
...@@ -30,9 +31,7 @@ import java.sql.SQLXML; ...@@ -30,9 +31,7 @@ import java.sql.SQLXML;
import java.sql.Savepoint; import java.sql.Savepoint;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Struct; import java.sql.Struct;
import java.util.Enumeration; import java.util.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
public class TSDBConnection implements Connection { public class TSDBConnection implements Connection {
...@@ -53,12 +52,67 @@ public class TSDBConnection implements Connection { ...@@ -53,12 +52,67 @@ 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);
...@@ -146,8 +200,7 @@ public class TSDBConnection implements Connection { ...@@ -146,8 +200,7 @@ public class TSDBConnection implements Connection {
* DatabaseMetaData object. * DatabaseMetaData object.
* *
* @return a DatabaseMetaData object for this connection * @return a DatabaseMetaData object for this connection
* @exception SQLException * @throws SQLException if a database access error occurs
* if a database access error occurs
*/ */
public DatabaseMetaData getMetaData() throws SQLException { public DatabaseMetaData getMetaData() throws SQLException {
return this.dbMetaData; return this.dbMetaData;
...@@ -156,6 +209,7 @@ public class TSDBConnection implements Connection { ...@@ -156,6 +209,7 @@ public class TSDBConnection implements Connection {
/** /**
* This readOnly option is not supported by TDengine. However, the method is intentionally left blank here to * This readOnly option is not supported by TDengine. However, the method is intentionally left blank here to
* support HikariCP connection. * support HikariCP connection.
*
* @param readOnly * @param readOnly
* @throws SQLException * @throws SQLException
*/ */
...@@ -177,6 +231,7 @@ public class TSDBConnection implements Connection { ...@@ -177,6 +231,7 @@ public class TSDBConnection implements Connection {
/** /**
* The transaction isolation level option is not supported by TDengine. * The transaction isolation level option is not supported by TDengine.
* This method is intentionally left empty to support HikariCP connection. * This method is intentionally left empty to support HikariCP connection.
*
* @param level * @param level
* @throws SQLException * @throws SQLException
*/ */
...@@ -185,6 +240,7 @@ public class TSDBConnection implements Connection { ...@@ -185,6 +240,7 @@ public class TSDBConnection implements Connection {
/** /**
* The transaction isolation level option is not supported by TDengine. * The transaction isolation level option is not supported by TDengine.
*
* @return * @return
* @throws SQLException * @throws SQLException
*/ */
...@@ -237,6 +293,7 @@ public class TSDBConnection implements Connection { ...@@ -237,6 +293,7 @@ public class TSDBConnection implements Connection {
/** /**
* the transaction is not supported by TDengine, so the opened ResultSet Objects will remain open * the transaction is not supported by TDengine, so the opened ResultSet Objects will remain open
*
* @return * @return
* @throws SQLException * @throws SQLException
*/ */
...@@ -313,7 +370,7 @@ public class TSDBConnection implements Connection { ...@@ -313,7 +370,7 @@ public class TSDBConnection implements Connection {
} }
public void setClientInfo(Properties properties) throws SQLClientInfoException { public void setClientInfo(Properties properties) throws SQLClientInfoException {
for (Enumeration<Object> enumer = properties.keys(); enumer.hasMoreElements();) { for (Enumeration<Object> enumer = properties.keys(); enumer.hasMoreElements(); ) {
String name = (String) enumer.nextElement(); String name = (String) enumer.nextElement();
clientInfoProps.put(name, properties.getProperty(name)); clientInfoProps.put(name, properties.getProperty(name));
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册