From 99741286cedc76b07c584fd41e4d5df019d1a1eb Mon Sep 17 00:00:00 2001 From: zyyang Date: Mon, 14 Sep 2020 17:48:27 +0800 Subject: [PATCH 1/4] adapt the calcite for customer GLD --- src/connector/jdbc/pom.xml | 227 +++---- .../com/taosdata/jdbc/TSDBConnection.java | 54 -- .../taosdata/jdbc/TSDBDatabaseMetaData.java | 6 +- .../java/com/taosdata/jdbc/TSDBDriver.java | 560 ++++++++++-------- .../com/taosdata/jdbc/cases/CalciteTest.java | 80 +++ 5 files changed, 510 insertions(+), 417 deletions(-) create mode 100644 src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index da0c5b12d4..82d3136e50 100755 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -1,106 +1,129 @@ - 4.0.0 - com.taosdata.jdbc - taos-jdbcdriver - 2.0.0 - jar - JDBCDriver - https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc - TDengine JDBC Driver - - - GNU AFFERO GENERAL PUBLIC LICENSE Version 3 - https://github.com/taosdata/TDengine/blob/master/LICENSE - repo - - - - scm:git:git://github.com/taosdata/TDengine.git - scm:git:git@github.com:taosdata/TDengine.git + 4.0.0 + com.taosdata.jdbc + taos-jdbcdriver + 2.0.0 + jar + JDBCDriver https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc - HEAD - - - - taosdata - support@taosdata.com - https://www.taosdata.com/ - https://www.taosdata.com/ - - - - UTF-8 - 1.8 - 3.6.0 - 1.1.2 - 3.5 - - - - commons-logging - commons-logging - ${commons-logging.version} - - - * - * - - - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - junit - junit - 4.13 - test - - - - - - org.apache.maven.plugins - maven-assembly-plugin - 3.0.0 - - - src/main/assembly/assembly-jar.xml - - - - - make-assembly - package - - single - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - UTF-8 - ${java.version} - ${java.version} - true - true - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.12.4 - - true - - - - + TDengine JDBC Driver + + + GNU AFFERO GENERAL PUBLIC LICENSE Version 3 + https://github.com/taosdata/TDengine/blob/master/LICENSE + repo + + + + scm:git:git://github.com/taosdata/TDengine.git + scm:git:git@github.com:taosdata/TDengine.git + https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc + HEAD + + + + taosdata + support@taosdata.com + https://www.taosdata.com/ + https://www.taosdata.com/ + + + + UTF-8 + 1.8 + 3.6.0 + 1.1.2 + 3.5 + + + + commons-logging + commons-logging + ${commons-logging.version} + + + * + * + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + junit + junit + 4.13 + test + + + + + org.apache.calcite + calcite-core + 1.23.0 + + + org.apache.commons + commons-dbcp2 + 2.7.0 + + + org.apache.calcite.avatica + avatica-core + 1.17.0 + + + mysql + mysql-connector-java + 5.1.47 + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.0.0 + + + src/main/assembly/assembly-jar.xml + + + + + make-assembly + package + + single + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + UTF-8 + ${java.version} + ${java.version} + true + true + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12.4 + + true + + + + diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java index d8df2fc0d3..ac0e4eb84a 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConnection.java @@ -53,66 +53,12 @@ public class TSDBConnection implements Connection { public TSDBConnection(Properties info, TSDBDatabaseMetaData meta) throws SQLException { 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 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), 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_PASSWORD)); } - private List loadConfigEndpoints(File cfgFile) { - List 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 { this.connector = new TSDBJNIConnector(); this.connector.connect(host, port, dbName, user, password); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java index 804e09c6b3..15f66fa202 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java @@ -68,15 +68,15 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { } public boolean nullsAreSortedLow() throws SQLException { - return false; + return !nullsAreSortedHigh(); } public boolean nullsAreSortedAtStart() throws SQLException { - return false; + return true; } public boolean nullsAreSortedAtEnd() throws SQLException { - return false; + return !nullsAreSortedAtStart(); } public String getDatabaseProductName() throws SQLException { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index 02d642d643..d56f33fe5e 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -16,24 +16,27 @@ package com.taosdata.jdbc; import org.apache.commons.lang3.StringUtils; +import java.io.*; import java.sql.*; +import java.util.ArrayList; +import java.util.List; import java.util.Properties; import java.util.logging.Logger; /** * The Java SQL framework allows for multiple database drivers. Each driver * should supply a class that implements the Driver interface - * + * *

* The DriverManager will try to load as many drivers as it can find and then * for any given connection request, it will ask each driver in turn to try to * connect to the target URL. - * + * *

* It is strongly recommended that each Driver class should be small and stand * alone so that the Driver class can be loaded and queried without bringing in * vast quantities of supporting code. - * + * *

* When a Driver class is loaded, it should create an instance of itself and * register it with the DriverManager. This means that a user can load and @@ -41,39 +44,39 @@ import java.util.logging.Logger; */ public class TSDBDriver implements java.sql.Driver { - @Deprecated - private static final String URL_PREFIX1 = "jdbc:TSDB://"; + @Deprecated + 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 - * to the driver. - */ - public static final String PROPERTY_KEY_DBNAME = "dbname"; + /** + * Key used to retrieve the database value from the properties instance passed + * to the driver. + */ + public static final String PROPERTY_KEY_DBNAME = "dbname"; - /** - * Key used to retrieve the host value from the properties instance passed to - * the driver. - */ - public static final String PROPERTY_KEY_HOST = "host"; - /** - * Key used to retrieve the password value from the properties instance passed - * to the driver. - */ - public static final String PROPERTY_KEY_PASSWORD = "password"; + /** + * Key used to retrieve the host value from the properties instance passed to + * the driver. + */ + public static final String PROPERTY_KEY_HOST = "host"; + /** + * Key used to retrieve the password value from the properties instance passed + * to the driver. + */ + public static final String PROPERTY_KEY_PASSWORD = "password"; - /** - * Key used to retrieve the port number value from the properties instance - * passed to the driver. - */ - public static final String PROPERTY_KEY_PORT = "port"; + /** + * Key used to retrieve the port number value from the properties instance + * passed to the driver. + */ + public static final String PROPERTY_KEY_PORT = "port"; - /** - * Key used to retrieve the user value from the properties instance passed to - * the driver. - */ - public static final String PROPERTY_KEY_USER = "user"; + /** + * Key used to retrieve the user value from the properties instance passed to + * the driver. + */ + public static final String PROPERTY_KEY_USER = "user"; /** * Key for the configuration file directory of TSDB client in properties instance @@ -98,277 +101,318 @@ public class TSDBDriver implements java.sql.Driver { public static final String PROPERTY_KEY_PROTOCOL = "protocol"; - /** - * Index for port coming out of parseHostPortPair(). - */ - public final static int PORT_NUMBER_INDEX = 1; + /** + * Index for port coming out of parseHostPortPair(). + */ + public final static int PORT_NUMBER_INDEX = 1; - /** - * Index for host coming out of parseHostPortPair(). - */ - public final static int HOST_NAME_INDEX = 0; + /** + * Index for host coming out of parseHostPortPair(). + */ + public final static int HOST_NAME_INDEX = 0; - private TSDBDatabaseMetaData dbMetaData = null; + private TSDBDatabaseMetaData dbMetaData = null; - static { - try { - java.sql.DriverManager.registerDriver(new TSDBDriver()); - } catch (SQLException E) { - throw new RuntimeException(TSDBConstants.WrapErrMsg("can't register tdengine jdbc driver!")); - } - } + static { + try { + java.sql.DriverManager.registerDriver(new TSDBDriver()); + } catch (SQLException E) { + throw new RuntimeException(TSDBConstants.WrapErrMsg("can't register tdengine jdbc driver!")); + } + } - public Connection connect(String url, Properties info) throws SQLException { - if (url == null) { - throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!")); - } + private List loadConfigEndpoints(File cfgFile) { + List 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; + } - Properties props = null; + /** + * @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; + } - if ((props = parseURL(url, info)) == null) { - return null; - } + /** + * @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; + } - try { - 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)); - Connection newConn = new TSDBConnection(props, this.dbMetaData); - return newConn; - } catch (SQLWarning sqlWarning) { - sqlWarning.printStackTrace(); - Connection newConn = new TSDBConnection(props, this.dbMetaData); - return newConn; - } catch (SQLException sqlEx) { - throw sqlEx; - } catch (Exception ex) { - SQLException sqlEx = new SQLException("SQLException:" + ex.toString()); - sqlEx.initCause(ex); - throw sqlEx; - } - } + public Connection connect(String url, Properties info) throws SQLException { + if (url == null) { + throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!")); + } - /** - * Parses hostPortPair in the form of [host][:port] into an array, with the - * element of index HOST_NAME_INDEX being the host (or null if not specified), - * and the element of index PORT_NUMBER_INDEX being the port (or null if not - * specified). - * - * @param hostPortPair - * host and port in form of of [host][:port] - * - * @return array containing host and port as Strings - * - * @throws SQLException - * if a parse error occurs - */ - protected static String[] parseHostPortPair(String hostPortPair) throws SQLException { - String[] splitValues = new String[2]; + Properties props = null; + if ((props = parseURL(url, info)) == null) { + return null; + } - int portIndex = hostPortPair.indexOf(":"); + //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 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]); + } + } - String hostname = null; + try { + 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)); + Connection newConn = new TSDBConnection(props, this.dbMetaData); + return newConn; + } catch (SQLWarning sqlWarning) { + sqlWarning.printStackTrace(); + Connection newConn = new TSDBConnection(props, this.dbMetaData); + return newConn; + } catch (SQLException sqlEx) { + throw sqlEx; + } catch (Exception ex) { + SQLException sqlEx = new SQLException("SQLException:" + ex.toString()); + sqlEx.initCause(ex); + throw sqlEx; + } + } - if (portIndex != -1) { - if ((portIndex + 1) < hostPortPair.length()) { - String portAsString = hostPortPair.substring(portIndex + 1); - hostname = hostPortPair.substring(0, portIndex); + /** + * Parses hostPortPair in the form of [host][:port] into an array, with the + * element of index HOST_NAME_INDEX being the host (or null if not specified), + * and the element of index PORT_NUMBER_INDEX being the port (or null if not + * specified). + * + * @param hostPortPair host and port in form of of [host][:port] + * @return array containing host and port as Strings + * @throws SQLException if a parse error occurs + */ + protected static String[] parseHostPortPair(String hostPortPair) throws SQLException { + String[] splitValues = new String[2]; - splitValues[HOST_NAME_INDEX] = hostname; + int portIndex = hostPortPair.indexOf(":"); - splitValues[PORT_NUMBER_INDEX] = portAsString; - } else { - throw new SQLException(TSDBConstants.WrapErrMsg("port is not proper!")); - } - } else { - splitValues[HOST_NAME_INDEX] = hostPortPair; - splitValues[PORT_NUMBER_INDEX] = null; - } + String hostname = null; - return splitValues; - } + if (portIndex != -1) { + if ((portIndex + 1) < hostPortPair.length()) { + String portAsString = hostPortPair.substring(portIndex + 1); + hostname = hostPortPair.substring(0, portIndex); - public boolean acceptsURL(String url) throws SQLException { - return StringUtils.isNotBlank(url) && url.startsWith(URL_PREFIX); - } + splitValues[HOST_NAME_INDEX] = hostname; - public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { - if (info == null) { - info = new Properties(); - } + splitValues[PORT_NUMBER_INDEX] = portAsString; + } else { + throw new SQLException(TSDBConstants.WrapErrMsg("port is not proper!")); + } + } else { + splitValues[HOST_NAME_INDEX] = hostPortPair; + splitValues[PORT_NUMBER_INDEX] = null; + } - if ((url != null) && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1))) { - info = parseURL(url, info); - } + return splitValues; + } - DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST)); - hostProp.required = true; + public boolean acceptsURL(String url) throws SQLException { + return StringUtils.isNotBlank(url) && url.startsWith(URL_PREFIX); + } - DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, - info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); - portProp.required = false; + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + if (info == null) { + info = new Properties(); + } - DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME)); - dbProp.required = false; - dbProp.description = "Database name"; + if ((url != null) && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1))) { + info = parseURL(url, info); + } - DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER)); - userProp.required = true; + DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST)); + hostProp.required = true; - DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, - info.getProperty(PROPERTY_KEY_PASSWORD)); - passwordProp.required = true; + DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); + portProp.required = false; - DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5]; - propertyInfo[0] = hostProp; - propertyInfo[1] = portProp; - propertyInfo[2] = dbProp; - propertyInfo[3] = userProp; - propertyInfo[4] = passwordProp; + DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME)); + dbProp.required = false; + dbProp.description = "Database name"; - return propertyInfo; - } + DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER)); + userProp.required = true; - /** - * example: jdbc:TSDB://127.0.0.1:0/db?user=root&password=your_password - */ + DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, info.getProperty(PROPERTY_KEY_PASSWORD)); + passwordProp.required = true; - public Properties parseURL(String url, Properties defaults) throws java.sql.SQLException { - Properties urlProps = (defaults != null) ? defaults : new Properties(); - if (url == null) { - return null; - } + DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5]; + propertyInfo[0] = hostProp; + propertyInfo[1] = portProp; + propertyInfo[2] = dbProp; + propertyInfo[3] = userProp; + propertyInfo[4] = passwordProp; - if (!StringUtils.startsWithIgnoreCase(url, URL_PREFIX) && !StringUtils.startsWithIgnoreCase(url, URL_PREFIX1)) { - return null; - } + return propertyInfo; + } - String urlForMeta = url; + /** + * 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 { + Properties urlProps = (defaults != null) ? defaults : new Properties(); + if (url == null) { + return null; + } - String dbProductName = url.substring(url.indexOf(":") + 1); - dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); - int beginningOfSlashes = url.indexOf("//"); - url = url.substring(beginningOfSlashes + 2); + if (!StringUtils.startsWithIgnoreCase(url, URL_PREFIX) && !StringUtils.startsWithIgnoreCase(url, URL_PREFIX1)) { + return null; + } - String host = url.substring(0, url.indexOf(":")); - url = url.substring(url.indexOf(":") + 1); - urlProps.setProperty(PROPERTY_KEY_HOST, host); + String urlForMeta = url; - String port = url.substring(0, url.indexOf("/")); - urlProps.setProperty(PROPERTY_KEY_PORT, port); - url = url.substring(url.indexOf("/") + 1); + String dbProductName = url.substring(url.indexOf(":") + 1); + dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); + int beginningOfSlashes = url.indexOf("//"); + url = url.substring(beginningOfSlashes + 2); - if (url.indexOf("?") != -1) { - String dbName = url.substring(0, url.indexOf("?")); - urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName); - url = url.trim().substring(url.indexOf("?") + 1); - } else { - // without user & password so return - if(!url.trim().isEmpty()) { - String dbName = url.trim(); - urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName); - } - this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, urlProps.getProperty("user")); - return urlProps; - } + String host = url.substring(0, url.indexOf(":")); + url = url.substring(url.indexOf(":") + 1); + urlProps.setProperty(PROPERTY_KEY_HOST, host); - String user = ""; + String port = url.substring(0, url.indexOf("/")); + urlProps.setProperty(PROPERTY_KEY_PORT, port); + url = url.substring(url.indexOf("/") + 1); - if (url.indexOf("&") == -1) { - String[] kvPair = url.trim().split("="); - if (kvPair.length == 2) { - setPropertyValue(urlProps, kvPair); - return urlProps; - } - } + if (url.indexOf("?") != -1) { + String dbName = url.substring(0, url.indexOf("?")); + urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName); + url = url.trim().substring(url.indexOf("?") + 1); + } else { + // without user & password so return + if (!url.trim().isEmpty()) { + String dbName = url.trim(); + urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName); + } + this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, urlProps.getProperty("user")); + return urlProps; + } - String[] queryStrings = url.trim().split("&"); - for (String queryStr : queryStrings) { - String[] kvPair = queryStr.trim().split("="); - if (kvPair.length < 2){ - continue; - } - setPropertyValue(urlProps, kvPair); - } + String user = ""; - user = urlProps.getProperty(PROPERTY_KEY_USER).toString(); - this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, user); + if (url.indexOf("&") == -1) { + String[] kvPair = url.trim().split("="); + if (kvPair.length == 2) { + setPropertyValue(urlProps, kvPair); + return urlProps; + } + } - return urlProps; - } + String[] queryStrings = url.trim().split("&"); + for (String queryStr : queryStrings) { + String[] kvPair = queryStr.trim().split("="); + if (kvPair.length < 2) { + continue; + } + setPropertyValue(urlProps, kvPair); + } - public void setPropertyValue(Properties property, String[] keyValuePair) { - switch (keyValuePair[0].toLowerCase()) { - case PROPERTY_KEY_USER: - property.setProperty(PROPERTY_KEY_USER, keyValuePair[1]); - break; - case PROPERTY_KEY_PASSWORD: - property.setProperty(PROPERTY_KEY_PASSWORD, keyValuePair[1]); - break; - case PROPERTY_KEY_TIME_ZONE: - property.setProperty(PROPERTY_KEY_TIME_ZONE, keyValuePair[1]); - break; - case PROPERTY_KEY_LOCALE: - property.setProperty(PROPERTY_KEY_LOCALE, keyValuePair[1]); - break; - case PROPERTY_KEY_CHARSET: - property.setProperty(PROPERTY_KEY_CHARSET, keyValuePair[1]); - break; - case PROPERTY_KEY_CONFIG_DIR: - property.setProperty(PROPERTY_KEY_CONFIG_DIR, keyValuePair[1]); - break; - } - } + user = urlProps.getProperty(PROPERTY_KEY_USER).toString(); + this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, user); + + return urlProps; + } + + public void setPropertyValue(Properties property, String[] keyValuePair) { + switch (keyValuePair[0].toLowerCase()) { + case PROPERTY_KEY_USER: + property.setProperty(PROPERTY_KEY_USER, keyValuePair[1]); + break; + case PROPERTY_KEY_PASSWORD: + property.setProperty(PROPERTY_KEY_PASSWORD, keyValuePair[1]); + break; + case PROPERTY_KEY_TIME_ZONE: + property.setProperty(PROPERTY_KEY_TIME_ZONE, keyValuePair[1]); + break; + case PROPERTY_KEY_LOCALE: + property.setProperty(PROPERTY_KEY_LOCALE, keyValuePair[1]); + break; + case PROPERTY_KEY_CHARSET: + property.setProperty(PROPERTY_KEY_CHARSET, keyValuePair[1]); + break; + case PROPERTY_KEY_CONFIG_DIR: + property.setProperty(PROPERTY_KEY_CONFIG_DIR, keyValuePair[1]); + break; + } + } - public int getMajorVersion() { - return 1; - } + public int getMajorVersion() { + return 1; + } - public int getMinorVersion() { - return 1; - } + public int getMinorVersion() { + return 1; + } - public boolean jdbcCompliant() { - return false; - } + public boolean jdbcCompliant() { + return false; + } - public Logger getParentLogger() throws SQLFeatureNotSupportedException { - return null; - } + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } - /** - * Returns the host property - * - * @param props - * the java.util.Properties instance to retrieve the hostname from. - * - * @return the host - */ - public String host(Properties props) { - return props.getProperty(PROPERTY_KEY_HOST, "localhost"); - } + /** + * Returns the host property + * + * @param props the java.util.Properties instance to retrieve the hostname from. + * @return the host + */ + public String host(Properties props) { + return props.getProperty(PROPERTY_KEY_HOST, "localhost"); + } - /** - * Returns the port number property - * - * @param props - * the properties to get the port number from - * - * @return the port number - */ - public int port(Properties props) { - return Integer.parseInt(props.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); - } + /** + * Returns the port number property + * + * @param props the properties to get the port number from + * @return the port number + */ + public int port(Properties props) { + return Integer.parseInt(props.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); + } - /** - * Returns the database property from props - * - * @param props - * the Properties to look for the database property. - * - * @return the database name. - */ - public String database(Properties props) { - return props.getProperty(PROPERTY_KEY_DBNAME); - } + /** + * Returns the database property from props + * + * @param props the Properties to look for the database property. + * @return the database name. + */ + public String database(Properties props) { + return props.getProperty(PROPERTY_KEY_DBNAME); + } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java new file mode 100644 index 0000000000..039250dd96 --- /dev/null +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java @@ -0,0 +1,80 @@ +package com.taosdata.jdbc.cases; + +import org.apache.calcite.adapter.jdbc.JdbcSchema; +import org.apache.calcite.jdbc.CalciteConnection; +import org.apache.calcite.schema.SchemaPlus; +import org.apache.calcite.sql.parser.SqlParseException; +import org.apache.commons.dbcp2.BasicDataSource; + +import java.sql.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class CalciteTest { + + public static void main(String[] args) throws SqlParseException, ClassNotFoundException, SQLException { + + //创建Calcite Connection对象 + Class.forName("org.apache.calcite.jdbc.Driver"); + Properties info = new Properties(); + info.setProperty("caseSensitive", "false"); + Connection connection = DriverManager.getConnection("jdbc:calcite:", info); + CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); + + // JDBC adapter + Class.forName("com.mysql.jdbc.Driver"); + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setUrl("jdbc:mysql://192.168.56.101:3306"); + dataSource.setUsername("root"); + dataSource.setPassword("123456"); + Map map = new HashMap<>(); + JdbcSchema schema = JdbcSchema.create(rootSchema, "hr", dataSource, null, null); + rootSchema.add("hr", schema); + + Statement statement = calciteConnection.createStatement(); + ResultSet resultSet = statement.executeQuery("select * from hr.depts"); + + while (resultSet.next()) { + ResultSetMetaData metaData = resultSet.getMetaData(); + for (int i = 1; i <= metaData.getColumnCount(); i++) { + String columnLabel = metaData.getColumnLabel(i); + System.out.println(columnLabel + " : " + resultSet.getString(i)); + } + } + resultSet.close(); + statement.close(); + connection.close(); + + + //创建TDengine的数据源schema +// Class.forName("com.taosdata.jdbc.TSDBDriver"); +// String url = "jdbc:TAOS://127.0.0.1:6030/hdb"; +// dataSource.setUrl(url); +// dataSource.setUsername("root"); +// dataSource.setPassword("taosdata"); + +// Class.forName("com.mysql.jdbc.Driver"); +// String url = "jdbc:mysql://localhost:3306/hdb"; +// BasicDataSource dataSource = new BasicDataSource(); +// dataSource.setUrl(url); +// dataSource.setUsername("root"); +// dataSource.setPassword("123456"); + //这里hdb是在tdengine中创建的数据库名 +// JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); +// Schema schema = JdbcSchema.create(rootSchema, "test", dataSource, "hdb", null); + //创建新的schema自动映射到原来的hdb数据库 +// rootSchema.add("test", schema); + +// Statement stmt = calciteConnection.createStatement(); + //查询schema test中的表,表名是tdengine中的表 +// ResultSet rs = stmt.executeQuery("select * from test.t"); +// for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { +// System.out.println(rs.getMetaData().getColumnName(i)); +// } +// while (rs.next()) { +// System.out.println(rs.getObject(1)); +// } + } +} From 8a39f899b60fbf211649931ec548a7a8ac50471c Mon Sep 17 00:00:00 2001 From: zyyang-taosdata Date: Tue, 15 Sep 2020 00:04:15 +0800 Subject: [PATCH 2/4] add calcite test case --- .../java/com/taosdata/jdbc/TSDBStatement.java | 2 +- .../com/taosdata/jdbc/cases/CalciteTest.java | 91 +++++++++---------- 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java index a8d6ceb713..5c6b0545e9 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java @@ -242,7 +242,7 @@ public class TSDBStatement implements Statement { public void addBatch(String sql) throws SQLException { if (batchedArgs == null) { - batchedArgs = new ArrayList(); + batchedArgs = new ArrayList<>(); } batchedArgs.add(sql); } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java index 039250dd96..0d0e7ac067 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java @@ -7,34 +7,17 @@ import org.apache.calcite.sql.parser.SqlParseException; import org.apache.commons.dbcp2.BasicDataSource; import java.sql.*; -import java.util.HashMap; -import java.util.Map; import java.util.Properties; public class CalciteTest { public static void main(String[] args) throws SqlParseException, ClassNotFoundException, SQLException { - //创建Calcite Connection对象 - Class.forName("org.apache.calcite.jdbc.Driver"); - Properties info = new Properties(); - info.setProperty("caseSensitive", "false"); - Connection connection = DriverManager.getConnection("jdbc:calcite:", info); - CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); - SchemaPlus rootSchema = calciteConnection.getRootSchema(); - - // JDBC adapter - Class.forName("com.mysql.jdbc.Driver"); - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setUrl("jdbc:mysql://192.168.56.101:3306"); - dataSource.setUsername("root"); - dataSource.setPassword("123456"); - Map map = new HashMap<>(); - JdbcSchema schema = JdbcSchema.create(rootSchema, "hr", dataSource, null, null); - rootSchema.add("hr", schema); +// CalciteConnection calciteConnection = testMyqsl(); + CalciteConnection calciteConnection = testTSDB(); Statement statement = calciteConnection.createStatement(); - ResultSet resultSet = statement.executeQuery("select * from hr.depts"); + ResultSet resultSet = statement.executeQuery("select * from test.t"); while (resultSet.next()) { ResultSetMetaData metaData = resultSet.getMetaData(); @@ -45,36 +28,50 @@ public class CalciteTest { } resultSet.close(); statement.close(); - connection.close(); + calciteConnection.close(); + } + private static CalciteConnection testMyqsl() throws ClassNotFoundException, SQLException { + //创建Calcite Connection对象 + Class.forName("org.apache.calcite.jdbc.Driver"); + Properties info = new Properties(); + info.setProperty("lex", "JAVA"); + info.setProperty("caseSensitive", "false"); + Connection connection = DriverManager.getConnection("jdbc:calcite:", info); + CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); - //创建TDengine的数据源schema -// Class.forName("com.taosdata.jdbc.TSDBDriver"); -// String url = "jdbc:TAOS://127.0.0.1:6030/hdb"; -// dataSource.setUrl(url); -// dataSource.setUsername("root"); -// dataSource.setPassword("taosdata"); + // JDBC adapter + Class.forName("com.mysql.jdbc.Driver"); + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setUrl("jdbc:mysql://192.168.236.135:3306/test"); + dataSource.setUsername("root"); + dataSource.setPassword("123456"); + JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); + rootSchema.add("test", schema); -// Class.forName("com.mysql.jdbc.Driver"); -// String url = "jdbc:mysql://localhost:3306/hdb"; -// BasicDataSource dataSource = new BasicDataSource(); -// dataSource.setUrl(url); -// dataSource.setUsername("root"); -// dataSource.setPassword("123456"); - //这里hdb是在tdengine中创建的数据库名 -// JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); -// Schema schema = JdbcSchema.create(rootSchema, "test", dataSource, "hdb", null); - //创建新的schema自动映射到原来的hdb数据库 -// rootSchema.add("test", schema); + return calciteConnection; + } -// Statement stmt = calciteConnection.createStatement(); - //查询schema test中的表,表名是tdengine中的表 -// ResultSet rs = stmt.executeQuery("select * from test.t"); -// for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { -// System.out.println(rs.getMetaData().getColumnName(i)); -// } -// while (rs.next()) { -// System.out.println(rs.getObject(1)); -// } + private static CalciteConnection testTSDB() throws SQLException, ClassNotFoundException { + //创建Calcite Connection对象 + Class.forName("org.apache.calcite.jdbc.Driver"); + Properties info = new Properties(); + info.setProperty("lex", "JAVA"); + info.setProperty("caseSensitive", "false"); + Connection connection = DriverManager.getConnection("jdbc:calcite:", info); + CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); + SchemaPlus rootSchema = calciteConnection.getRootSchema(); + + // JDBC adapter + Class.forName("com.taosdata.jdbc.TSDBDriver"); + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setUrl("jdbc:TAOS://192.168.236.135:6030/test"); + dataSource.setUsername("root"); + dataSource.setPassword("taosdata"); + JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); + rootSchema.add("test", schema); + + return calciteConnection; } } From e406aedbb466bb2b85ef7b566e399c1665a94857 Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 17 Sep 2020 13:32:12 +0800 Subject: [PATCH 3/4] clean unused codes --- src/connector/jdbc/pom.xml | 23 ------ .../com/taosdata/jdbc/cases/CalciteTest.java | 77 ------------------- 2 files changed, 100 deletions(-) delete mode 100644 src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index 82d3136e50..43b77559e8 100755 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -59,29 +59,6 @@ 4.13 test - - - - org.apache.calcite - calcite-core - 1.23.0 - - - org.apache.commons - commons-dbcp2 - 2.7.0 - - - org.apache.calcite.avatica - avatica-core - 1.17.0 - - - mysql - mysql-connector-java - 5.1.47 - - diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java deleted file mode 100644 index 0d0e7ac067..0000000000 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/cases/CalciteTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.taosdata.jdbc.cases; - -import org.apache.calcite.adapter.jdbc.JdbcSchema; -import org.apache.calcite.jdbc.CalciteConnection; -import org.apache.calcite.schema.SchemaPlus; -import org.apache.calcite.sql.parser.SqlParseException; -import org.apache.commons.dbcp2.BasicDataSource; - -import java.sql.*; -import java.util.Properties; - -public class CalciteTest { - - public static void main(String[] args) throws SqlParseException, ClassNotFoundException, SQLException { - -// CalciteConnection calciteConnection = testMyqsl(); - CalciteConnection calciteConnection = testTSDB(); - - Statement statement = calciteConnection.createStatement(); - ResultSet resultSet = statement.executeQuery("select * from test.t"); - - while (resultSet.next()) { - ResultSetMetaData metaData = resultSet.getMetaData(); - for (int i = 1; i <= metaData.getColumnCount(); i++) { - String columnLabel = metaData.getColumnLabel(i); - System.out.println(columnLabel + " : " + resultSet.getString(i)); - } - } - resultSet.close(); - statement.close(); - calciteConnection.close(); - } - - private static CalciteConnection testMyqsl() throws ClassNotFoundException, SQLException { - //创建Calcite Connection对象 - Class.forName("org.apache.calcite.jdbc.Driver"); - Properties info = new Properties(); - info.setProperty("lex", "JAVA"); - info.setProperty("caseSensitive", "false"); - Connection connection = DriverManager.getConnection("jdbc:calcite:", info); - CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); - SchemaPlus rootSchema = calciteConnection.getRootSchema(); - - // JDBC adapter - Class.forName("com.mysql.jdbc.Driver"); - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setUrl("jdbc:mysql://192.168.236.135:3306/test"); - dataSource.setUsername("root"); - dataSource.setPassword("123456"); - JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); - rootSchema.add("test", schema); - - return calciteConnection; - } - - private static CalciteConnection testTSDB() throws SQLException, ClassNotFoundException { - //创建Calcite Connection对象 - Class.forName("org.apache.calcite.jdbc.Driver"); - Properties info = new Properties(); - info.setProperty("lex", "JAVA"); - info.setProperty("caseSensitive", "false"); - Connection connection = DriverManager.getConnection("jdbc:calcite:", info); - CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); - SchemaPlus rootSchema = calciteConnection.getRootSchema(); - - // JDBC adapter - Class.forName("com.taosdata.jdbc.TSDBDriver"); - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setUrl("jdbc:TAOS://192.168.236.135:6030/test"); - dataSource.setUsername("root"); - dataSource.setPassword("taosdata"); - JdbcSchema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test"); - rootSchema.add("test", schema); - - return calciteConnection; - } -} From 804045682cbfee0be5382a702ab1d86d1976aa1d Mon Sep 17 00:00:00 2001 From: zyyang Date: Thu, 17 Sep 2020 14:06:39 +0800 Subject: [PATCH 4/4] solve conflict --- src/connector/jdbc/pom.xml | 5 ----- .../jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java | 7 +++---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index 841b0d3c74..36e2fa426b 100755 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -49,11 +49,6 @@ - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - junit junit diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index c356df8bf7..bc649a31e1 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -15,8 +15,6 @@ package com.taosdata.jdbc; -import org.apache.commons.lang3.StringUtils; - import java.io.*; import java.sql.*; @@ -246,7 +244,7 @@ public class TSDBDriver implements java.sql.Driver { } public boolean acceptsURL(String url) throws SQLException { - return StringUtils.isNotBlank(url) && url.startsWith(URL_PREFIX); + return (url != null && url.length() > 0 && url.trim().length() > 0) && url.toLowerCase().startsWith(URL_PREFIX); } public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { @@ -293,7 +291,8 @@ public class TSDBDriver implements java.sql.Driver { return null; } - if (!StringUtils.startsWithIgnoreCase(url, URL_PREFIX) && !StringUtils.startsWithIgnoreCase(url, URL_PREFIX1)) { + String lowerUrl = url.toLowerCase(); + if (!lowerUrl.startsWith(URL_PREFIX) && !lowerUrl.startsWith(URL_PREFIX1)) { return null; }