提交 af51dce4 编写于 作者: Z zyyang

[TD-1859]<feature>: a JDBC implementation use TDengine Restful Interface

上级 df74e517
......@@ -56,6 +56,23 @@
<scope>test</scope>
</dependency>
<!-- for restful -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package com.taosdata.jdbc;
import java.io.*;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
public abstract class AbstractTaosDriver implements Driver {
private static final String TAOS_CFG_FILENAME = "taos.cfg";
/**
* @param cfgDirPath
* @return return the config dir
**/
protected 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
*/
protected 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;
}
protected 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;
}
protected void loadTaosConfig(Properties info) {
if ((info.getProperty(TSDBDriver.PROPERTY_KEY_HOST) == null ||
info.getProperty(TSDBDriver.PROPERTY_KEY_HOST).isEmpty()) && (
info.getProperty(TSDBDriver.PROPERTY_KEY_PORT) == null ||
info.getProperty(TSDBDriver.PROPERTY_KEY_PORT).isEmpty())) {
File cfgDir = loadConfigDir(info.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR));
File cfgFile = cfgDir.listFiles((dir, name) -> TAOS_CFG_FILENAME.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]);
}
}
}
protected DriverPropertyInfo[] getPropertyInfo(Properties info) {
DriverPropertyInfo hostProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_HOST, info.getProperty(TSDBDriver.PROPERTY_KEY_HOST));
hostProp.required = false;
hostProp.description = "Hostname";
DriverPropertyInfo portProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_PORT, info.getProperty(TSDBDriver.PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT));
portProp.required = false;
portProp.description = "Port";
DriverPropertyInfo dbProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_DBNAME, info.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME));
dbProp.required = false;
dbProp.description = "Database name";
DriverPropertyInfo userProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_USER, info.getProperty(TSDBDriver.PROPERTY_KEY_USER));
userProp.required = true;
userProp.description = "User";
DriverPropertyInfo passwordProp = new DriverPropertyInfo(TSDBDriver.PROPERTY_KEY_PASSWORD, info.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD));
passwordProp.required = true;
passwordProp.description = "Password";
DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5];
propertyInfo[0] = hostProp;
propertyInfo[1] = portProp;
propertyInfo[2] = dbProp;
propertyInfo[3] = userProp;
propertyInfo[4] = passwordProp;
return propertyInfo;
}
protected Properties parseURL(String url, Properties defaults) {
Properties urlProps = (defaults != null) ? defaults : new Properties();
// parse properties
int beginningOfSlashes = url.indexOf("//");
int index = url.indexOf("?");
if (index != -1) {
String paramString = url.substring(index + 1, url.length());
url = url.substring(0, index);
StringTokenizer queryParams = new StringTokenizer(paramString, "&");
while (queryParams.hasMoreElements()) {
String parameterValuePair = queryParams.nextToken();
int indexOfEqual = parameterValuePair.indexOf("=");
String parameter = null;
String value = null;
if (indexOfEqual != -1) {
parameter = parameterValuePair.substring(0, indexOfEqual);
if (indexOfEqual + 1 < parameterValuePair.length()) {
value = parameterValuePair.substring(indexOfEqual + 1);
}
}
if ((value != null && value.length() > 0) && (parameter != null && parameter.length() > 0)) {
urlProps.setProperty(parameter, value);
}
}
}
// parse Product Name
String dbProductName = url.substring(0, beginningOfSlashes);
dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1);
dbProductName = dbProductName.substring(0, dbProductName.indexOf(":"));
// parse dbname
url = url.substring(beginningOfSlashes + 2);
int indexOfSlash = url.indexOf("/");
if (indexOfSlash != -1) {
if (indexOfSlash + 1 < url.length()) {
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_DBNAME, url.substring(indexOfSlash + 1));
}
url = url.substring(0, indexOfSlash);
}
// parse port
int indexOfColon = url.indexOf(":");
if (indexOfColon != -1) {
if (indexOfColon + 1 < url.length()) {
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PORT, url.substring(indexOfColon + 1));
}
url = url.substring(0, indexOfColon);
}
// parse host
if (url != null && url.length() > 0 && url.trim().length() > 0) {
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_HOST, url);
}
return urlProps;
}
}
......@@ -14,7 +14,6 @@
*****************************************************************************/
package com.taosdata.jdbc;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Logger;
......@@ -38,7 +37,7 @@ import java.util.logging.Logger;
* register it with the DriverManager. This means that a user can load and
* register a driver by doing Class.forName("foo.bah.Driver")
*/
public class TSDBDriver implements java.sql.Driver {
public class TSDBDriver extends AbstractTaosDriver {
@Deprecated
private static final String URL_PREFIX1 = "jdbc:TSDB://";
......@@ -97,50 +96,6 @@ 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 {
if (url == null)
throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!"));
......@@ -152,26 +107,12 @@ public class TSDBDriver implements java.sql.Driver {
if ((props = parseURL(url, info)) == null) {
return null;
}
//load taos.cfg start
if ((info.getProperty(TSDBDriver.PROPERTY_KEY_HOST) == null ||
info.getProperty(TSDBDriver.PROPERTY_KEY_HOST).isEmpty()) && (
info.getProperty(TSDBDriver.PROPERTY_KEY_PORT) == null ||
info.getProperty(TSDBDriver.PROPERTY_KEY_PORT).isEmpty())) {
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]);
}
}
loadTaosConfig(info);
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));
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) {
......@@ -208,39 +149,13 @@ public class TSDBDriver implements java.sql.Driver {
info = parseURL(url, info);
}
DriverPropertyInfo hostProp = new DriverPropertyInfo(PROPERTY_KEY_HOST, info.getProperty(PROPERTY_KEY_HOST));
hostProp.required = false;
hostProp.description = "Hostname";
DriverPropertyInfo portProp = new DriverPropertyInfo(PROPERTY_KEY_PORT, info.getProperty(PROPERTY_KEY_PORT, TSDBConstants.DEFAULT_PORT));
portProp.required = false;
portProp.description = "Port";
DriverPropertyInfo dbProp = new DriverPropertyInfo(PROPERTY_KEY_DBNAME, info.getProperty(PROPERTY_KEY_DBNAME));
dbProp.required = false;
dbProp.description = "Database name";
DriverPropertyInfo userProp = new DriverPropertyInfo(PROPERTY_KEY_USER, info.getProperty(PROPERTY_KEY_USER));
userProp.required = true;
userProp.description = "User";
DriverPropertyInfo passwordProp = new DriverPropertyInfo(PROPERTY_KEY_PASSWORD, info.getProperty(PROPERTY_KEY_PASSWORD));
passwordProp.required = true;
passwordProp.description = "Password";
DriverPropertyInfo[] propertyInfo = new DriverPropertyInfo[5];
propertyInfo[0] = hostProp;
propertyInfo[1] = portProp;
propertyInfo[2] = dbProp;
propertyInfo[3] = userProp;
propertyInfo[4] = passwordProp;
return propertyInfo;
return getPropertyInfo(info);
}
/**
* example: jdbc:TAOS://127.0.0.1:0/db?user=root&password=your_password
*/
@Override
public Properties parseURL(String url, Properties defaults) {
Properties urlProps = (defaults != null) ? defaults : new Properties();
if (url == null || url.length() <= 0 || url.trim().length() <= 0)
......@@ -296,86 +211,10 @@ public class TSDBDriver implements java.sql.Driver {
if (url != null && url.length() > 0 && url.trim().length() > 0) {
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_HOST, url);
}
this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, urlProps.getProperty(TSDBDriver.PROPERTY_KEY_USER));
/*
String urlForMeta = url;
String dbProductName = url.substring(url.indexOf(":") + 1);
dbProductName = dbProductName.substring(0, dbProductName.indexOf(":"));
int beginningOfSlashes = url.indexOf("//");
url = url.substring(beginningOfSlashes + 2);
String host = url.substring(0, url.indexOf(":"));
url = url.substring(url.indexOf(":") + 1);
urlProps.setProperty(PROPERTY_KEY_HOST, host);
String port = url.substring(0, url.indexOf("/"));
urlProps.setProperty(PROPERTY_KEY_PORT, port);
url = url.substring(url.indexOf("/") + 1);
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 user = "";
if (url.indexOf("&") == -1) {
String[] kvPair = url.trim().split("=");
if (kvPair.length == 2) {
setPropertyValue(urlProps, kvPair);
return urlProps;
}
}
String[] queryStrings = url.trim().split("&");
for (String queryStr : queryStrings) {
String[] kvPair = queryStr.trim().split("=");
if (kvPair.length < 2) {
continue;
}
setPropertyValue(urlProps, kvPair);
}
user = urlProps.getProperty(PROPERTY_KEY_USER).toString();
this.dbMetaData = new TSDBDatabaseMetaData(dbProductName, urlForMeta, user);
*/
return urlProps;
}
private 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 2;
}
......
package com.taosdata.jdbc.rs;
import com.taosdata.jdbc.TSDBConstants;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
public class TaosRestfulConnection implements Connection {
private final String host;
private final int port;
private final Properties props;
private final String database;
private final String url;
public TaosRestfulConnection(String host, String port, Properties props, String database, String url) {
this.host = host;
this.port = Integer.parseInt(port);
this.props = props;
this.database = database;
this.url = url;
}
@Override
public Statement createStatement() throws SQLException {
if (isClosed())
throw new SQLException(TSDBConstants.WrapErrMsg("restful TDengine connection is closed."));
return new TaosRestfulStatement(this, this.database);
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return null;
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
return null;
}
@Override
public String nativeSQL(String sql) throws SQLException {
return null;
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
}
@Override
public boolean getAutoCommit() throws SQLException {
return false;
}
@Override
public void commit() throws SQLException {
}
@Override
public void rollback() throws SQLException {
}
@Override
public void close() throws SQLException {
}
@Override
public boolean isClosed() throws SQLException {
return false;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return null;
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
}
@Override
public boolean isReadOnly() throws SQLException {
return false;
}
@Override
public void setCatalog(String catalog) throws SQLException {
}
@Override
public String getCatalog() throws SQLException {
return null;
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
}
@Override
public int getTransactionIsolation() throws SQLException {
return 0;
}
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null;
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
return null;
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
}
@Override
public void setHoldability(int holdability) throws SQLException {
}
@Override
public int getHoldability() throws SQLException {
return 0;
}
@Override
public Savepoint setSavepoint() throws SQLException {
return null;
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
return null;
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return null;
}
@Override
public Clob createClob() throws SQLException {
return null;
}
@Override
public Blob createBlob() throws SQLException {
return null;
}
@Override
public NClob createNClob() throws SQLException {
return null;
}
@Override
public SQLXML createSQLXML() throws SQLException {
return null;
}
@Override
public boolean isValid(int timeout) throws SQLException {
return false;
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
}
@Override
public String getClientInfo(String name) throws SQLException {
return null;
}
@Override
public Properties getClientInfo() throws SQLException {
return null;
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
return null;
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return null;
}
@Override
public void setSchema(String schema) throws SQLException {
}
@Override
public String getSchema() throws SQLException {
return null;
}
@Override
public void abort(Executor executor) throws SQLException {
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
}
@Override
public int getNetworkTimeout() throws SQLException {
return 0;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public Properties getProps() {
return props;
}
public String getDatabase() {
return database;
}
public String getUrl() {
return url;
}
}
package com.taosdata.jdbc.rs;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taosdata.jdbc.AbstractTaosDriver;
import com.taosdata.jdbc.TSDBConstants;
import com.taosdata.jdbc.TSDBDriver;
import com.taosdata.jdbc.rs.util.HttpClientPoolUtil;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;
public class TaosRestfulDriver extends AbstractTaosDriver {
private static final String URL_PREFIX = "jdbc:TAOS-RS://";
static {
try {
DriverManager.registerDriver(new TaosRestfulDriver());
} catch (SQLException e) {
throw new RuntimeException(TSDBConstants.WrapErrMsg("can not register Restful JDBC driver"), e);
}
}
@Override
public Connection connect(String url, Properties info) throws SQLException {
// throw SQLException if url is null
if (url == null)
throw new SQLException(TSDBConstants.WrapErrMsg("url is not set!"));
// return null if url is not be accepted
if (!acceptsURL(url))
return null;
Properties props = parseURL(url, info);
String host = props.getProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost");
String port = props.getProperty(TSDBDriver.PROPERTY_KEY_PORT, "6041");
String database = props.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME);
String loginUrl = "http://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + ":"
+ props.getProperty(TSDBDriver.PROPERTY_KEY_PORT) + "/rest/login/"
+ props.getProperty(TSDBDriver.PROPERTY_KEY_USER) + "/"
+ props.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD) + "";
String result = HttpClientPoolUtil.execute(loginUrl);
JSONObject jsonResult = JSON.parseObject(result);
String status = jsonResult.getString("status");
if (!status.equals("succ")) {
throw new SQLException(jsonResult.getString("desc"));
}
return new TaosRestfulConnection(host, port, props, database, url);
}
@Override
public boolean acceptsURL(String url) throws SQLException {
if (url == null)
throw new SQLException(TSDBConstants.WrapErrMsg("url is null"));
return (url != null && url.length() > 0 && url.trim().length() > 0) && url.startsWith(URL_PREFIX);
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
if (info == null) {
info = new Properties();
}
if (acceptsURL(url)) {
info = parseURL(url, info);
}
return getPropertyInfo(info);
}
@Override
public int getMajorVersion() {
return 2;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public boolean jdbcCompliant() {
return false;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
package com.taosdata.jdbc.rs;
import com.taosdata.jdbc.TSDBConstants;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
public class TaosRestfulResultSet implements ResultSet {
private boolean isClosed = false;
private int pos = -1;
private ArrayList<ArrayList<String>> data;
private ArrayList<String> fields;
public TaosRestfulResultSet(String str, String fieldData) {
data = new ArrayList<>();
str = str.substring(2, str.length() - 2);
ArrayList<String> strTemp = new ArrayList<>(Arrays.asList(str.split("],\\[")));
for (String s : strTemp) {
ArrayList<String> curr = new ArrayList<>(Arrays.asList(s.split(",")));
data.add(curr);
}
if (!StringUtils.isBlank(fieldData)) {
fields = new ArrayList<>();
fieldData = fieldData.substring(2, fieldData.length() - 2);
ArrayList<String> fieldTemp = new ArrayList<>(Arrays.asList(fieldData.split("],\\[")));
for (String s : fieldTemp) {
String curr = Arrays.asList(s.split(",")).get(0);
fields.add(curr.substring(1, curr.length() - 1)); // 去掉双引号
}
}
}
@Override
public boolean next() throws SQLException {
if (isClosed) throw new SQLException(TSDBConstants.WrapErrMsg("Result is Closed!!!"));
if (pos < data.size() - 1) {
pos++;
return true;
}
return false;
}
@Override
public void close() throws SQLException {
this.isClosed = true;
}
@Override
public boolean wasNull() throws SQLException {
return data.isEmpty();
}
@Override
public String getString(int columnIndex) throws SQLException {
if (columnIndex > data.get(pos).size()) {
throw new SQLException(TSDBConstants.WrapErrMsg("Column Index out of range, " + columnIndex + " > " + data.get(pos).size()));
}
return data.get(pos).get(columnIndex - 1);
}
@Override
public boolean getBoolean(int columnIndex) throws SQLException {
String result = getString(columnIndex);
if (!(result.equals("true") || result.equals("false"))) {
throw new SQLException("not boolean value");
}
return result.equals("true");
}
@Override
public byte getByte(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public short getShort(int columnIndex) throws SQLException {
return Short.parseShort(getString(columnIndex));
}
@Override
public int getInt(int columnIndex) throws SQLException {
String result = getString(columnIndex);
return Integer.parseInt(result);
}
@Override
public long getLong(int columnIndex) throws SQLException {
String result = getString(columnIndex);
return Long.parseLong(result);
}
@Override
public float getFloat(int columnIndex) throws SQLException {
String result = getString(columnIndex);
return Float.parseFloat(result);
}
@Override
public double getDouble(int columnIndex) throws SQLException {
String result = getString(columnIndex);
return Double.parseDouble(result);
}
@Override
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public byte[] getBytes(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Date getDate(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Time getTime(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Timestamp getTimestamp(int columnIndex) throws SQLException {
String strDate = getString(columnIndex);
strDate = strDate.substring(1, strDate.length() - 1);
return Timestamp.valueOf(strDate);
}
@Override
public InputStream getAsciiStream(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public InputStream getBinaryStream(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public String getString(String columnLabel) throws SQLException {
return getString(findColumn(columnLabel) + 1);
}
@Override
public boolean getBoolean(String columnLabel) throws SQLException {
return Boolean.parseBoolean(getString(columnLabel));
}
@Override
public byte getByte(String columnLabel) throws SQLException {
return 0;
}
@Override
public short getShort(String columnLabel) throws SQLException {
return Short.parseShort(getString(columnLabel));
}
@Override
public int getInt(String columnLabel) throws SQLException {
return Integer.parseInt(getString(columnLabel));
}
@Override
public long getLong(String columnLabel) throws SQLException {
return Long.parseLong(getString(columnLabel));
}
@Override
public float getFloat(String columnLabel) throws SQLException {
String result = getString(columnLabel);
return Float.parseFloat(result);
}
@Override
public double getDouble(String columnLabel) throws SQLException {
return Double.parseDouble(getString(columnLabel));
}
@Override
public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException {
return null;
}
@Override
public byte[] getBytes(String columnLabel) throws SQLException {
return new byte[0];
}
@Override
public Date getDate(String columnLabel) throws SQLException {
return null;
}
@Override
public Time getTime(String columnLabel) throws SQLException {
return null;
}
@Override
public Timestamp getTimestamp(String columnLabel) throws SQLException {
return Timestamp.valueOf(getString(columnLabel));
}
@Override
public InputStream getAsciiStream(String columnLabel) throws SQLException {
return null;
}
@Override
public InputStream getUnicodeStream(String columnLabel) throws SQLException {
return null;
}
@Override
public InputStream getBinaryStream(String columnLabel) throws SQLException {
return null;
}
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
//TODO: SQLFeature Not Supported
// throw new SQLFeatureNotSupportedException();
}
@Override
public void clearWarnings() throws SQLException {
return;
//TODO: SQLFeature Not Supported
// throw new SQLFeatureNotSupportedException();
}
@Override
public String getCursorName() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
return new TaosRestfulResultSetMetaData(fields);
}
@Override
public Object getObject(int columnIndex) throws SQLException {
// return null;
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Object getObject(String columnLabel) throws SQLException {
// return null;
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public int findColumn(String columnLabel) throws SQLException {
return fields.indexOf(columnLabel);
}
@Override
public Reader getCharacterStream(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Reader getCharacterStream(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public BigDecimal getBigDecimal(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isBeforeFirst() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isAfterLast() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isFirst() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isLast() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void beforeFirst() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void afterLast() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean first() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean last() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public int getRow() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean absolute(int row) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean relative(int rows) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean previous() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void setFetchDirection(int direction) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public int getFetchDirection() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void setFetchSize(int rows) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public int getFetchSize() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public int getType() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public int getConcurrency() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean rowUpdated() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean rowInserted() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean rowDeleted() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNull(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateByte(int columnIndex, byte x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateShort(int columnIndex, short x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateInt(int columnIndex, int x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateLong(int columnIndex, long x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateFloat(int columnIndex, float x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateDouble(int columnIndex, double x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateString(int columnIndex, String x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateDate(int columnIndex, Date x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateTime(int columnIndex, Time x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateObject(int columnIndex, Object x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNull(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBoolean(String columnLabel, boolean x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateByte(String columnLabel, byte x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateShort(String columnLabel, short x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateInt(String columnLabel, int x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateLong(String columnLabel, long x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateFloat(String columnLabel, float x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateDouble(String columnLabel, double x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBigDecimal(String columnLabel, BigDecimal x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateString(String columnLabel, String x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBytes(String columnLabel, byte[] x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateDate(String columnLabel, Date x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateTime(String columnLabel, Time x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateObject(String columnLabel, Object x, int scaleOrLength) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateObject(String columnLabel, Object x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void insertRow() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateRow() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void deleteRow() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void refreshRow() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void cancelRowUpdates() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void moveToInsertRow() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void moveToCurrentRow() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Statement getStatement() throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Object getObject(int columnIndex, Map<String, Class<?>> map) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Ref getRef(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Blob getBlob(int columnIndex) throws SQLException {
return null;
}
@Override
public Clob getClob(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Array getArray(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Ref getRef(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Blob getBlob(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Clob getClob(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Array getArray(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Time getTime(String columnLabel, Calendar cal) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public URL getURL(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public URL getURL(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateRef(int columnIndex, Ref x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateRef(String columnLabel, Ref x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBlob(int columnIndex, Blob x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBlob(String columnLabel, Blob x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateClob(int columnIndex, Clob x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateClob(String columnLabel, Clob x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateArray(int columnIndex, Array x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateArray(String columnLabel, Array x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public RowId getRowId(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public RowId getRowId(String columnLabel) throws SQLException {
return null;
}
@Override
public void updateRowId(int columnIndex, RowId x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateRowId(String columnLabel, RowId x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public int getHoldability() throws SQLException {
// return 0;
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isClosed() throws SQLException {
return false;
//TODO: SQLFeature Not Supported
// throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNString(int columnIndex, String nString) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNString(String columnLabel, String nString) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNClob(int columnIndex, NClob nClob) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNClob(String columnLabel, NClob nClob) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public NClob getNClob(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public NClob getNClob(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public SQLXML getSQLXML(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public SQLXML getSQLXML(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public String getNString(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public String getNString(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Reader getNCharacterStream(int columnIndex) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public Reader getNCharacterStream(String columnLabel) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateClob(int columnIndex, Reader reader, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateClob(String columnLabel, Reader reader, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateCharacterStream(int columnIndex, Reader x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateClob(int columnIndex, Reader reader) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateClob(String columnLabel, Reader reader) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNClob(int columnIndex, Reader reader) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public void updateNClob(String columnLabel, Reader reader) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
//TODO: SQLFeature Not Supported
throw new SQLFeatureNotSupportedException();
}
}
package com.taosdata.jdbc.rs;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
public class TaosRestfulResultSetMetaData implements ResultSetMetaData {
private List<String> fields;
public TaosRestfulResultSetMetaData(List<String> fields) {
this.fields = fields;
}
@Override
public int getColumnCount() throws SQLException {
return fields.size();
}
@Override
public boolean isAutoIncrement(int column) throws SQLException {
return false;
}
@Override
public boolean isCaseSensitive(int column) throws SQLException {
return false;
}
@Override
public boolean isSearchable(int column) throws SQLException {
return false;
}
@Override
public boolean isCurrency(int column) throws SQLException {
return false;
}
@Override
public int isNullable(int column) throws SQLException {
return 0;
}
@Override
public boolean isSigned(int column) throws SQLException {
return false;
}
@Override
public int getColumnDisplaySize(int column) throws SQLException {
return 0;
}
@Override
public String getColumnLabel(int column) throws SQLException {
return fields.get(column - 1);
}
@Override
public String getColumnName(int column) throws SQLException {
return null;
}
@Override
public String getSchemaName(int column) throws SQLException {
return null;
}
@Override
public int getPrecision(int column) throws SQLException {
return 0;
}
@Override
public int getScale(int column) throws SQLException {
return 0;
}
@Override
public String getTableName(int column) throws SQLException {
return null;
}
@Override
public String getCatalogName(int column) throws SQLException {
return null;
}
@Override
public int getColumnType(int column) throws SQLException {
return 0;
}
@Override
public String getColumnTypeName(int column) throws SQLException {
return null;
}
@Override
public boolean isReadOnly(int column) throws SQLException {
return false;
}
@Override
public boolean isWritable(int column) throws SQLException {
return false;
}
@Override
public boolean isDefinitelyWritable(int column) throws SQLException {
return false;
}
@Override
public String getColumnClassName(int column) throws SQLException {
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}
package com.taosdata.jdbc.rs;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taosdata.jdbc.TSDBConstants;
import com.taosdata.jdbc.rs.util.HttpClientPoolUtil;
import java.sql.*;
import java.util.Arrays;
import java.util.List;
public class TaosRestfulStatement implements Statement {
private final String catalog;
private final TaosRestfulConnection conn;
public TaosRestfulStatement(TaosRestfulConnection c, String catalog) {
this.conn = c;
this.catalog = catalog;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
final String url = "http://" + conn.getHost() + ":"+conn.getPort()+"/rest/sql";
String result = HttpClientPoolUtil.execute(url, sql);
String fields = "";
List<String> words = Arrays.asList(sql.split(" "));
if (words.get(0).equalsIgnoreCase("select")) {
int index = 0;
if (words.contains("from")) {
index = words.indexOf("from");
}
if (words.contains("FROM")) {
index = words.indexOf("FROM");
}
fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + words.get(index + 1));
}
JSONObject jsonObject = JSON.parseObject(result);
if (jsonObject.getString("status").equals("error")) {
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " +
jsonObject.getString("desc") + "\n" +
"error code: " + jsonObject.getString("code")));
}
String dataStr = jsonObject.getString("data");
if ("use".equalsIgnoreCase(fields.split(" ")[0])) {
return new TaosRestfulResultSet(dataStr, "");
}
JSONObject jsonField = JSON.parseObject(fields);
if (jsonField == null) {
return new TaosRestfulResultSet(dataStr, "");
}
if (jsonField.getString("status").equals("error")) {
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " +
jsonField.getString("desc") + "\n" +
"error code: " + jsonField.getString("code")));
}
String fieldData = jsonField.getString("data");
return new TaosRestfulResultSet(dataStr, fieldData);
}
@Override
public int executeUpdate(String sql) throws SQLException {
return 0;
}
@Override
public void close() throws SQLException {
}
@Override
public int getMaxFieldSize() throws SQLException {
return 0;
}
@Override
public void setMaxFieldSize(int max) throws SQLException {
}
@Override
public int getMaxRows() throws SQLException {
return 0;
}
@Override
public void setMaxRows(int max) throws SQLException {
}
@Override
public void setEscapeProcessing(boolean enable) throws SQLException {
}
@Override
public int getQueryTimeout() throws SQLException {
return 0;
}
@Override
public void setQueryTimeout(int seconds) throws SQLException {
}
@Override
public void cancel() throws SQLException {
}
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public void setCursorName(String name) throws SQLException {
}
@Override
public boolean execute(String sql) throws SQLException {
return false;
}
@Override
public ResultSet getResultSet() throws SQLException {
return null;
}
@Override
public int getUpdateCount() throws SQLException {
return 0;
}
@Override
public boolean getMoreResults() throws SQLException {
return false;
}
@Override
public void setFetchDirection(int direction) throws SQLException {
}
@Override
public int getFetchDirection() throws SQLException {
return 0;
}
@Override
public void setFetchSize(int rows) throws SQLException {
}
@Override
public int getFetchSize() throws SQLException {
return 0;
}
@Override
public int getResultSetConcurrency() throws SQLException {
return 0;
}
@Override
public int getResultSetType() throws SQLException {
return 0;
}
@Override
public void addBatch(String sql) throws SQLException {
}
@Override
public void clearBatch() throws SQLException {
}
@Override
public int[] executeBatch() throws SQLException {
return new int[0];
}
@Override
public Connection getConnection() throws SQLException {
return null;
}
@Override
public boolean getMoreResults(int current) throws SQLException {
return false;
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
return null;
}
@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
return 0;
}
@Override
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
return 0;
}
@Override
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
return 0;
}
@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
return false;
}
@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
return false;
}
@Override
public boolean execute(String sql, String[] columnNames) throws SQLException {
return false;
}
@Override
public int getResultSetHoldability() throws SQLException {
return 0;
}
@Override
public boolean isClosed() throws SQLException {
return false;
}
@Override
public void setPoolable(boolean poolable) throws SQLException {
}
@Override
public boolean isPoolable() throws SQLException {
return false;
}
@Override
public void closeOnCompletion() throws SQLException {
}
@Override
public boolean isCloseOnCompletion() throws SQLException {
return false;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}
package com.taosdata.jdbc.rs.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class HttpClientPoolUtil {
public static PoolingHttpClientConnectionManager cm = null;
public static CloseableHttpClient httpClient = null;
/**
* 默认content 类型
*/
private static final String DEFAULT_CONTENT_TYPE = "application/json";
/**
* 默认请求超时时间30s
*/
private static final int DEFAULT_TIME_OUT = 15000;
private static final int count = 32;
private static final int totalCount = 1000;
private static final int Http_Default_Keep_Time = 15000;
/**
* 初始化连接池
*/
public static synchronized void initPools() {
if (httpClient == null) {
cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(count);
cm.setMaxTotal(totalCount);
httpClient = HttpClients.custom().setKeepAliveStrategy(defaultStrategy).setConnectionManager(cm).build();
}
}
/**
* Http connection keepAlive 设置
*/
public static ConnectionKeepAliveStrategy defaultStrategy = (response, context) -> {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
int keepTime = Http_Default_Keep_Time * 1000;
while (it.hasNext()) {
HeaderElement headerElement = it.nextElement();
String param = headerElement.getName();
String value = headerElement.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (Exception e) {
new Exception(
"format KeepAlive timeout exception, exception:" + e.toString())
.printStackTrace();
}
}
}
return keepTime;
};
public static CloseableHttpClient getHttpClient() {
return httpClient;
}
public static PoolingHttpClientConnectionManager getHttpConnectionManager() {
return cm;
}
/**
* 执行http post请求
* 默认采用Content-Type:application/json,Accept:application/json
*
* @param uri 请求地址
* @param data 请求数据
* @return responseBody
*/
public static String execute(String uri, String data) {
long startTime = System.currentTimeMillis();
HttpEntity httpEntity = null;
HttpEntityEnclosingRequestBase method = null;
String responseBody = "";
try {
if (httpClient == null) {
initPools();
}
method = (HttpEntityEnclosingRequestBase) getRequest(uri, HttpPost.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
method.setEntity(new StringEntity(data));
HttpContext context = HttpClientContext.create();
CloseableHttpResponse httpResponse = httpClient.execute(method, context);
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseBody = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
if (method != null) {
method.abort();
}
// e.printStackTrace();
// logger.error("execute post request exception, url:" + uri + ", exception:" + e.toString()
// + ", cost time(ms):" + (System.currentTimeMillis() - startTime));
new Exception("execute post request exception, url:"
+ uri + ", exception:" + e.toString() +
", cost time(ms):" + (System.currentTimeMillis() - startTime))
.printStackTrace();
} finally {
if (httpEntity != null) {
try {
EntityUtils.consumeQuietly(httpEntity);
} catch (Exception e) {
// e.printStackTrace();
// logger.error("close response exception, url:" + uri + ", exception:" + e.toString()
// + ", cost time(ms):" + (System.currentTimeMillis() - startTime));
new Exception(
"close response exception, url:" + uri +
", exception:" + e.toString()
+ ", cost time(ms):" + (System.currentTimeMillis() - startTime))
.printStackTrace();
}
}
}
return responseBody;
}
/**
* * 创建请求
*
* @param uri 请求url
* @param methodName 请求的方法类型
* @param contentType contentType类型
* @param timeout 超时时间
* @return HttpRequestBase 返回类型
* @author lisc
*/
public static HttpRequestBase getRequest(String uri, String methodName, String contentType, int timeout) {
if (httpClient == null) {
initPools();
}
HttpRequestBase method;
if (timeout <= 0) {
timeout = DEFAULT_TIME_OUT;
}
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout * 1000)
.setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000)
.setExpectContinueEnabled(false).build();
if (HttpPut.METHOD_NAME.equalsIgnoreCase(methodName)) {
method = new HttpPut(uri);
} else if (HttpPost.METHOD_NAME.equalsIgnoreCase(methodName)) {
method = new HttpPost(uri);
} else if (HttpGet.METHOD_NAME.equalsIgnoreCase(methodName)) {
method = new HttpGet(uri);
} else {
method = new HttpPost(uri);
}
if (StringUtils.isBlank(contentType)) {
contentType = DEFAULT_CONTENT_TYPE;
}
method.addHeader("Content-Type", contentType);
method.addHeader("Accept", contentType);
method.setConfig(requestConfig);
return method;
}
/**
* 执行GET 请求
*
* @param uri 网址
* @return responseBody
*/
public static String execute(String uri) {
long startTime = System.currentTimeMillis();
HttpEntity httpEntity = null;
HttpRequestBase method = null;
String responseBody = "";
try {
if (httpClient == null) {
initPools();
}
method = getRequest(uri, HttpGet.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
HttpContext context = HttpClientContext.create();
CloseableHttpResponse httpResponse = httpClient.execute(method, context);
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseBody = EntityUtils.toString(httpEntity, "UTF-8");
// logger.info("请求URL: " + uri + "+ 返回状态码:" + httpResponse.getStatusLine().getStatusCode());
}
} catch (Exception e) {
if (method != null) {
method.abort();
}
e.printStackTrace();
// logger.error("execute get request exception, url:" + uri + ", exception:" + e.toString() + ",cost time(ms):"
// + (System.currentTimeMillis() - startTime));
System.out.println("log:调用 HttpClientPoolUtil execute get request exception, url:" + uri + ", exception:" + e.toString() + ",cost time(ms):"
+ (System.currentTimeMillis() - startTime));
} finally {
if (httpEntity != null) {
try {
EntityUtils.consumeQuietly(httpEntity);
} catch (Exception e) {
// e.printStackTrace();
// logger.error("close response exception, url:" + uri + ", exception:" + e.toString()
// + ",cost time(ms):" + (System.currentTimeMillis() - startTime));
new Exception("close response exception, url:" + uri + ", exception:" + e.toString()
+ ",cost time(ms):" + (System.currentTimeMillis() - startTime))
.printStackTrace();
}
}
}
return responseBody;
}
}
\ No newline at end of file
package com.taosdata.jdbc.rs;
import org.junit.Assert;
import org.junit.Test;
import java.sql.*;
public class TaosRestfulDriverTest {
@Test
public void testCase001() {
try {
Class.forName("com.taosdata.jdbc.rs.TaosRestfulDriver");
Connection connection = DriverManager.getConnection("jdbc:TAOS-RS://master:6041/?user=root&password=taosdata");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from log.log");
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String column = metaData.getColumnLabel(i);
String value = resultSet.getString(i);
System.out.print(column + ":" + value + "\t");
}
System.out.println();
}
statement.close();
connection.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void testAcceptUrl() throws SQLException {
Driver driver = new TaosRestfulDriver();
boolean isAccept = driver.acceptsURL("jdbc:TAOS-RS://master:6041");
Assert.assertTrue(isAccept);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册