提交 99741286 编写于 作者: Z zyyang

adapt the calcite for customer GLD

上级 21584e30
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.taosdata.jdbc</groupId> <groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId> <artifactId>taos-jdbcdriver</artifactId>
<version>2.0.0</version> <version>2.0.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>JDBCDriver</name> <name>JDBCDriver</name>
<url>https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc</url>
<description>TDengine JDBC Driver</description>
<licenses>
<license>
<name>GNU AFFERO GENERAL PUBLIC LICENSE Version 3</name>
<url>https://github.com/taosdata/TDengine/blob/master/LICENSE</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:git:git://github.com/taosdata/TDengine.git</connection>
<developerConnection>scm:git:git@github.com:taosdata/TDengine.git</developerConnection>
<url>https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc</url> <url>https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc</url>
<tag>HEAD</tag> <description>TDengine JDBC Driver</description>
</scm> <licenses>
<developers> <license>
<developer> <name>GNU AFFERO GENERAL PUBLIC LICENSE Version 3</name>
<name>taosdata</name> <url>https://github.com/taosdata/TDengine/blob/master/LICENSE</url>
<email>support@taosdata.com</email> <distribution>repo</distribution>
<organization>https://www.taosdata.com/</organization> </license>
<organizationUrl>https://www.taosdata.com/</organizationUrl> </licenses>
</developer> <scm>
</developers> <connection>scm:git:git://github.com/taosdata/TDengine.git</connection>
<properties> <developerConnection>scm:git:git@github.com:taosdata/TDengine.git</developerConnection>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <url>https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc</url>
<java.version>1.8</java.version> <tag>HEAD</tag>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version> </scm>
<commons-logging.version>1.1.2</commons-logging.version> <developers>
<commons-lang3.version>3.5</commons-lang3.version> <developer>
</properties> <name>taosdata</name>
<dependencies> <email>support@taosdata.com</email>
<dependency> <organization>https://www.taosdata.com/</organization>
<groupId>commons-logging</groupId> <organizationUrl>https://www.taosdata.com/</organizationUrl>
<artifactId>commons-logging</artifactId> </developer>
<version>${commons-logging.version}</version> </developers>
<exclusions> <properties>
<exclusion> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<groupId>*</groupId> <java.version>1.8</java.version>
<artifactId>*</artifactId> <maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
</exclusion> <commons-logging.version>1.1.2</commons-logging.version>
</exclusions> <commons-lang3.version>3.5</commons-lang3.version>
</dependency> </properties>
<dependency> <dependencies>
<groupId>org.apache.commons</groupId> <dependency>
<artifactId>commons-lang3</artifactId> <groupId>commons-logging</groupId>
<version>${commons-lang3.version}</version> <artifactId>commons-logging</artifactId>
</dependency> <version>${commons-logging.version}</version>
<dependency> <exclusions>
<groupId>junit</groupId> <exclusion>
<artifactId>junit</artifactId> <groupId>*</groupId>
<version>4.13</version> <artifactId>*</artifactId>
<scope>test</scope> </exclusion>
</dependency> </exclusions>
</dependencies> </dependency>
<build> <dependency>
<plugins> <groupId>org.apache.commons</groupId>
<plugin> <artifactId>commons-lang3</artifactId>
<groupId>org.apache.maven.plugins</groupId> <version>${commons-lang3.version}</version>
<artifactId>maven-assembly-plugin</artifactId> </dependency>
<version>3.0.0</version> <dependency>
<configuration> <groupId>junit</groupId>
<descriptors> <artifactId>junit</artifactId>
<descriptor>src/main/assembly/assembly-jar.xml</descriptor> <version>4.13</version>
</descriptors> <scope>test</scope>
</configuration> </dependency>
<executions>
<execution> <!-- calcite -->
<id>make-assembly</id> <dependency>
<phase>package</phase> <groupId>org.apache.calcite</groupId>
<goals> <artifactId>calcite-core</artifactId>
<goal>single</goal> <version>1.23.0</version>
</goals> </dependency>
</execution> <dependency>
</executions> <groupId>org.apache.commons</groupId>
</plugin> <artifactId>commons-dbcp2</artifactId>
<plugin> <version>2.7.0</version>
<groupId>org.apache.maven.plugins</groupId> </dependency>
<artifactId>maven-compiler-plugin</artifactId> <dependency>
<version>${maven-compiler-plugin.version}</version> <groupId>org.apache.calcite.avatica</groupId>
<configuration> <artifactId>avatica-core</artifactId>
<encoding>UTF-8</encoding> <version>1.17.0</version>
<source>${java.version}</source> </dependency>
<target>${java.version}</target> <dependency>
<debug>true</debug> <groupId>mysql</groupId>
<showDeprecation>true</showDeprecation> <artifactId>mysql-connector-java</artifactId>
</configuration> <version>5.1.47</version>
</plugin> </dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId> </dependencies>
<artifactId>maven-surefire-plugin</artifactId> <build>
<version>2.12.4</version> <plugins>
<configuration> <plugin>
<testFailureIgnore>true</testFailureIgnore> <groupId>org.apache.maven.plugins</groupId>
</configuration> <artifactId>maven-assembly-plugin</artifactId>
</plugin> <version>3.0.0</version>
</plugins> <configuration>
</build> <descriptors>
<descriptor>src/main/assembly/assembly-jar.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
<debug>true</debug>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>
...@@ -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 {
......
...@@ -16,24 +16,27 @@ package com.taosdata.jdbc; ...@@ -16,24 +16,27 @@ package com.taosdata.jdbc;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
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;
/** /**
* The Java SQL framework allows for multiple database drivers. Each driver * The Java SQL framework allows for multiple database drivers. Each driver
* should supply a class that implements the Driver interface * should supply a class that implements the Driver interface
* *
* <p> * <p>
* The DriverManager will try to load as many drivers as it can find and then * 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 * for any given connection request, it will ask each driver in turn to try to
* connect to the target URL. * connect to the target URL.
* *
* <p> * <p>
* It is strongly recommended that each Driver class should be small and stand * 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 * alone so that the Driver class can be loaded and queried without bringing in
* vast quantities of supporting code. * vast quantities of supporting code.
* *
* <p> * <p>
* When a Driver class is loaded, it should create an instance of itself and * 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 * register it with the DriverManager. This means that a user can load and
...@@ -41,39 +44,39 @@ import java.util.logging.Logger; ...@@ -41,39 +44,39 @@ 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
* to the driver. * to the driver.
*/ */
public static final String PROPERTY_KEY_DBNAME = "dbname"; public static final String PROPERTY_KEY_DBNAME = "dbname";
/** /**
* Key used to retrieve the host value from the properties instance passed to * Key used to retrieve the host value from the properties instance passed to
* the driver. * the driver.
*/ */
public static final String PROPERTY_KEY_HOST = "host"; public static final String PROPERTY_KEY_HOST = "host";
/** /**
* Key used to retrieve the password value from the properties instance passed * Key used to retrieve the password value from the properties instance passed
* to the driver. * to the driver.
*/ */
public static final String PROPERTY_KEY_PASSWORD = "password"; public static final String PROPERTY_KEY_PASSWORD = "password";
/** /**
* Key used to retrieve the port number value from the properties instance * Key used to retrieve the port number value from the properties instance
* passed to the driver. * passed to the driver.
*/ */
public static final String PROPERTY_KEY_PORT = "port"; public static final String PROPERTY_KEY_PORT = "port";
/** /**
* Key used to retrieve the user value from the properties instance passed to * Key used to retrieve the user value from the properties instance passed to
* the driver. * the 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
...@@ -98,277 +101,318 @@ public class TSDBDriver implements java.sql.Driver { ...@@ -98,277 +101,318 @@ 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().
*/ */
public final static int PORT_NUMBER_INDEX = 1; public final static int PORT_NUMBER_INDEX = 1;
/** /**
* Index for host coming out of parseHostPortPair(). * Index for host coming out of parseHostPortPair().
*/ */
public final static int HOST_NAME_INDEX = 0; public final static int HOST_NAME_INDEX = 0;
private TSDBDatabaseMetaData dbMetaData = null; private TSDBDatabaseMetaData dbMetaData = null;
static { static {
try { try {
java.sql.DriverManager.registerDriver(new TSDBDriver()); java.sql.DriverManager.registerDriver(new TSDBDriver());
} catch (SQLException E) { } catch (SQLException E) {
throw new RuntimeException(TSDBConstants.WrapErrMsg("can't register tdengine jdbc driver!")); throw new RuntimeException(TSDBConstants.WrapErrMsg("can't register tdengine jdbc driver!"));
} }
} }
public Connection connect(String url, Properties info) throws SQLException { private List<String> loadConfigEndpoints(File cfgFile) {
if (url == null) { List<String> endpoints = new ArrayList<>();
throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!")); try (BufferedReader reader = new BufferedReader(new FileReader(cfgFile))) {
} String line = null;
while ((line = reader.readLine()) != null) {
Properties props = null; if (line.trim().startsWith("firstEp") || line.trim().startsWith("secondEp")) {
endpoints.add(line.substring(line.indexOf('p') + 1).trim());
if ((props = parseURL(url, info)) == null) { }
return null; if (endpoints.size() > 1)
} break;
}
try { } catch (FileNotFoundException e) {
TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), (String) props.get(PROPERTY_KEY_CHARSET), e.printStackTrace();
(String) props.get(PROPERTY_KEY_TIME_ZONE)); } catch (IOException e) {
Connection newConn = new TSDBConnection(props, this.dbMetaData); e.printStackTrace();
return newConn; }
} catch (SQLWarning sqlWarning) { return endpoints;
sqlWarning.printStackTrace(); }
Connection newConn = new TSDBConnection(props, this.dbMetaData);
return newConn; /**
} catch (SQLException sqlEx) { * @param cfgDirPath
throw sqlEx; * @return return the config dir
} catch (Exception ex) { **/
SQLException sqlEx = new SQLException("SQLException:" + ex.toString()); private File loadConfigDir(String cfgDirPath) {
sqlEx.initCause(ex); if (cfgDirPath == null)
throw sqlEx; return loadDefaultConfigDir();
} File cfgDir = new File(cfgDirPath);
} if (!cfgDir.exists())
return loadDefaultConfigDir();
/** return cfgDir;
* 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). * @return search the default config dir, if the config dir is not exist will return null
* */
* @param hostPortPair private File loadDefaultConfigDir() {
* host and port in form of of [host][:port] File cfgDir;
* File cfgDir_linux = new File("/etc/taos");
* @return array containing host and port as Strings cfgDir = cfgDir_linux.exists() ? cfgDir_linux : null;
* File cfgDir_windows = new File("C:\\TDengine\\cfg");
* @throws SQLException cfgDir = (cfgDir == null && cfgDir_windows.exists()) ? cfgDir_windows : cfgDir;
* if a parse error occurs return cfgDir;
*/ }
protected static String[] parseHostPortPair(String hostPortPair) throws SQLException {
String[] splitValues = new String[2]; public Connection connect(String url, Properties info) throws SQLException {
if (url == null) {
int portIndex = hostPortPair.indexOf(":"); throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!"));
}
String hostname = null;
Properties props = null;
if (portIndex != -1) { if ((props = parseURL(url, info)) == null) {
if ((portIndex + 1) < hostPortPair.length()) { return null;
String portAsString = hostPortPair.substring(portIndex + 1); }
hostname = hostPortPair.substring(0, portIndex);
//load taos.cfg start
splitValues[HOST_NAME_INDEX] = hostname; 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));
splitValues[PORT_NUMBER_INDEX] = portAsString; File cfgFile = cfgDir.listFiles((dir, name) -> "taos.cfg".equalsIgnoreCase(name))[0];
} else { List<String> endpoints = loadConfigEndpoints(cfgFile);
throw new SQLException(TSDBConstants.WrapErrMsg("port is not proper!")); if (!endpoints.isEmpty()) {
} info.setProperty(TSDBDriver.PROPERTY_KEY_HOST, endpoints.get(0).split(":")[0]);
} else { info.setProperty(TSDBDriver.PROPERTY_KEY_PORT, endpoints.get(0).split(":")[1]);
splitValues[HOST_NAME_INDEX] = hostPortPair; }
splitValues[PORT_NUMBER_INDEX] = null; }
}
try {
return splitValues; 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);
public boolean acceptsURL(String url) throws SQLException { return newConn;
return StringUtils.isNotBlank(url) && url.startsWith(URL_PREFIX); } catch (SQLWarning sqlWarning) {
} sqlWarning.printStackTrace();
Connection newConn = new TSDBConnection(props, this.dbMetaData);
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { return newConn;
if (info == null) { } catch (SQLException sqlEx) {
info = new Properties(); throw sqlEx;
} } catch (Exception ex) {
SQLException sqlEx = new SQLException("SQLException:" + ex.toString());
if ((url != null) && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1))) { sqlEx.initCause(ex);
info = parseURL(url, info); throw sqlEx;
} }
}
DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST));
hostProp.required = true; /**
* Parses hostPortPair in the form of [host][:port] into an array, with the
DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, * element of index HOST_NAME_INDEX being the host (or null if not specified),
info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); * and the element of index PORT_NUMBER_INDEX being the port (or null if not
portProp.required = false; * specified).
*
DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME)); * @param hostPortPair host and port in form of of [host][:port]
dbProp.required = false; * @return array containing host and port as Strings
dbProp.description = "Database name"; * @throws SQLException if a parse error occurs
*/
DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER)); protected static String[] parseHostPortPair(String hostPortPair) throws SQLException {
userProp.required = true; String[] splitValues = new String[2];
DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, int portIndex = hostPortPair.indexOf(":");
info.getProperty(PROPERTY_KEY_PASSWORD));
passwordProp.required = true; String hostname = null;
DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5]; if (portIndex != -1) {
propertyInfo[0] = hostProp; if ((portIndex + 1) < hostPortPair.length()) {
propertyInfo[1] = portProp; String portAsString = hostPortPair.substring(portIndex + 1);
propertyInfo[2] = dbProp; hostname = hostPortPair.substring(0, portIndex);
propertyInfo[3] = userProp;
propertyInfo[4] = passwordProp; splitValues[HOST_NAME_INDEX] = hostname;
return propertyInfo; splitValues[PORT_NUMBER_INDEX] = portAsString;
} } else {
throw new SQLException(TSDBConstants.WrapErrMsg("port is not proper!"));
/** }
* example: jdbc:TSDB://127.0.0.1:0/db?user=root&password=your_password } else {
*/ splitValues[HOST_NAME_INDEX] = hostPortPair;
splitValues[PORT_NUMBER_INDEX] = null;
public Properties parseURL(String url, Properties defaults) throws java.sql.SQLException { }
Properties urlProps = (defaults != null) ? defaults : new Properties();
if (url == null) { return splitValues;
return null; }
}
public boolean acceptsURL(String url) throws SQLException {
if (!StringUtils.startsWithIgnoreCase(url, URL_PREFIX) && !StringUtils.startsWithIgnoreCase(url, URL_PREFIX1)) { return StringUtils.isNotBlank(url) && url.startsWith(URL_PREFIX);
return null; }
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
String urlForMeta = url; if (info == null) {
info = new Properties();
String dbProductName = url.substring(url.indexOf(":") + 1); }
dbProductName = dbProductName.substring(0, dbProductName.indexOf(":"));
int beginningOfSlashes = url.indexOf("//"); if ((url != null) && (url.startsWith(URL_PREFIX) || url.startsWith(URL_PREFIX1))) {
url = url.substring(beginningOfSlashes + 2); info = parseURL(url, info);
}
String host = url.substring(0, url.indexOf(":"));
url = url.substring(url.indexOf(":") + 1); DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST));
urlProps.setProperty(PROPERTY_KEY_HOST, host); hostProp.required = true;
String port = url.substring(0, url.indexOf("/")); DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT));
urlProps.setProperty(PROPERTY_KEY_PORT, port); portProp.required = false;
url = url.substring(url.indexOf("/") + 1);
DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME));
if (url.indexOf("?") != -1) { dbProp.required = false;
String dbName = url.substring(0, url.indexOf("?")); dbProp.description = "Database name";
urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName);
url = url.trim().substring(url.indexOf("?") + 1); DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER));
} else { userProp.required = true;
// without user & password so return
if(!url.trim().isEmpty()) { DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, info.getProperty(PROPERTY_KEY_PASSWORD));
String dbName = url.trim(); passwordProp.required = true;
urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName);
} DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5];
this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, urlProps.getProperty("user")); propertyInfo[0] = hostProp;
return urlProps; propertyInfo[1] = portProp;
} propertyInfo[2] = dbProp;
propertyInfo[3] = userProp;
String user = ""; propertyInfo[4] = passwordProp;
if (url.indexOf("&") == -1) { return propertyInfo;
String[] kvPair = url.trim().split("="); }
if (kvPair.length == 2) {
setPropertyValue(urlProps, kvPair); /**
return urlProps; * 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();
String[] queryStrings = url.trim().split("&"); if (url == null) {
for (String queryStr : queryStrings) { return null;
String[] kvPair = queryStr.trim().split("="); }
if (kvPair.length < 2){
continue; if (!StringUtils.startsWithIgnoreCase(url, URL_PREFIX) && !StringUtils.startsWithIgnoreCase(url, URL_PREFIX1)) {
} return null;
setPropertyValue(urlProps, kvPair); }
}
String urlForMeta = url;
user = urlProps.getProperty(PROPERTY_KEY_USER).toString();
this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, user); String dbProductName = url.substring(url.indexOf(":") + 1);
dbProductName = dbProductName.substring(0, dbProductName.indexOf(":"));
return urlProps; int beginningOfSlashes = url.indexOf("//");
} url = url.substring(beginningOfSlashes + 2);
public void setPropertyValue(Properties property, String[] keyValuePair) { String host = url.substring(0, url.indexOf(":"));
switch (keyValuePair[0].toLowerCase()) { url = url.substring(url.indexOf(":") + 1);
case PROPERTY_KEY_USER: urlProps.setProperty(PROPERTY_KEY_HOST, host);
property.setProperty(PROPERTY_KEY_USER, keyValuePair[1]);
break; String port = url.substring(0, url.indexOf("/"));
case PROPERTY_KEY_PASSWORD: urlProps.setProperty(PROPERTY_KEY_PORT, port);
property.setProperty(PROPERTY_KEY_PASSWORD, keyValuePair[1]); url = url.substring(url.indexOf("/") + 1);
break;
case PROPERTY_KEY_TIME_ZONE: if (url.indexOf("?") != -1) {
property.setProperty(PROPERTY_KEY_TIME_ZONE, keyValuePair[1]); String dbName = url.substring(0, url.indexOf("?"));
break; urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName);
case PROPERTY_KEY_LOCALE: url = url.trim().substring(url.indexOf("?") + 1);
property.setProperty(PROPERTY_KEY_LOCALE, keyValuePair[1]); } else {
break; // without user & password so return
case PROPERTY_KEY_CHARSET: if (!url.trim().isEmpty()) {
property.setProperty(PROPERTY_KEY_CHARSET, keyValuePair[1]); String dbName = url.trim();
break; urlProps.setProperty(PROPERTY_KEY_DBNAME, dbName);
case PROPERTY_KEY_CONFIG_DIR: }
property.setProperty(PROPERTY_KEY_CONFIG_DIR, keyValuePair[1]); this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, urlProps.getProperty("user"));
break; return urlProps;
} }
}
String user = "";
public int getMajorVersion() { if (url.indexOf("&") == -1) {
return 1; String[] kvPair = url.trim().split("=");
} if (kvPair.length == 2) {
setPropertyValue(urlProps, kvPair);
public int getMinorVersion() { return urlProps;
return 1; }
} }
public boolean jdbcCompliant() { String[] queryStrings = url.trim().split("&");
return false; for (String queryStr : queryStrings) {
} String[] kvPair = queryStr.trim().split("=");
if (kvPair.length < 2) {
public Logger getParentLogger() throws SQLFeatureNotSupportedException { continue;
return null; }
} setPropertyValue(urlProps, kvPair);
}
/**
* Returns the host property user = urlProps.getProperty(PROPERTY_KEY_USER).toString();
* this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, user);
* @param props
* the java.util.Properties instance to retrieve the hostname from. return urlProps;
* }
* @return the host
*/ public void setPropertyValue(Properties property, String[] keyValuePair) {
public String host(Properties props) { switch (keyValuePair[0].toLowerCase()) {
return props.getProperty(PROPERTY_KEY_HOST, "localhost"); case PROPERTY_KEY_USER:
} property.setProperty(PROPERTY_KEY_USER, keyValuePair[1]);
break;
/** case PROPERTY_KEY_PASSWORD:
* Returns the port number property property.setProperty(PROPERTY_KEY_PASSWORD, keyValuePair[1]);
* break;
* @param props case PROPERTY_KEY_TIME_ZONE:
* the properties to get the port number from property.setProperty(PROPERTY_KEY_TIME_ZONE, keyValuePair[1]);
* break;
* @return the port number case PROPERTY_KEY_LOCALE:
*/ property.setProperty(PROPERTY_KEY_LOCALE, keyValuePair[1]);
public int port(Properties props) { break;
return Integer.parseInt(props.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT)); case PROPERTY_KEY_CHARSET:
} property.setProperty(PROPERTY_KEY_CHARSET, keyValuePair[1]);
break;
/** case PROPERTY_KEY_CONFIG_DIR:
* Returns the database property from <code>props</code> property.setProperty(PROPERTY_KEY_CONFIG_DIR, keyValuePair[1]);
* break;
* @param props }
* the Properties to look for the database property. }
*
* @return the database name.
*/ public int getMajorVersion() {
public String database(Properties props) { return 1;
return props.getProperty(PROPERTY_KEY_DBNAME); }
}
public int getMinorVersion() {
return 1;
}
public boolean jdbcCompliant() {
return false;
}
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 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 <code>props</code>
*
* @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);
}
} }
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<String, String> 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));
// }
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册