提交 24cb0cd8 编写于 作者: H huolibo

[TD-10424]<feature>:add openTSDBStatement for openTSDB format

上级 092aecb5
package com.taosdata.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AbstractStatementWrapper extends AbstractStatement{
protected Statement statement;
public AbstractStatementWrapper(Statement statement) {
this.statement = statement;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
return statement.executeQuery(sql);
}
@Override
public int executeUpdate(String sql) throws SQLException {
return statement.executeUpdate(sql);
}
@Override
public void close() throws SQLException {
statement.close();
}
@Override
public boolean execute(String sql) throws SQLException {
return statement.execute(sql);
}
@Override
public ResultSet getResultSet() throws SQLException {
return statement.getResultSet();
}
@Override
public int getUpdateCount() throws SQLException {
return statement.getUpdateCount();
}
@Override
public Connection getConnection() throws SQLException {
return statement.getConnection();
}
@Override
public boolean isClosed() throws SQLException {
return statement.isClosed();
}
}
package com.taosdata.jdbc;
import com.taosdata.jdbc.rs.RestfulConnection;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class OpenTSDBStatement extends AbstractStatementWrapper {
public OpenTSDBStatement(Statement statement) {
super(statement);
}
public void executeTelnetPut(String[] strings) throws SQLException {
Connection connection = this.getConnection();
if (connection instanceof TSDBConnection) {
TSDBConnection tsdbConnection = (TSDBConnection) connection;
tsdbConnection.getConnector().insertTelnetLines(strings);
} else if (connection instanceof RestfulConnection) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD, "restful connection is not supported currently");
} else {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_CONNECTION, "unknown connection:" + connection.getCatalog());
}
}
public void executeTelnetPut(String sql) throws SQLException {
executeTelnetPut(new String[]{sql});
}
public void executeInsertJsonPayload(String json) throws SQLException {
Connection connection = this.getConnection();
if (connection instanceof TSDBConnection) {
TSDBConnection tsdbConnection = (TSDBConnection) connection;
tsdbConnection.getConnector().insertJsonPayload(json);
} else if (connection instanceof RestfulConnection) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD, "restful connection is not supported currently");
} else {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN_CONNECTION, "unknown connection:" + connection.getCatalog());
}
}
}
...@@ -32,6 +32,7 @@ public class TSDBErrorNumbers { ...@@ -32,6 +32,7 @@ public class TSDBErrorNumbers {
public static final int ERROR_USER_IS_REQUIRED = 0x2319; // user is required public static final int ERROR_USER_IS_REQUIRED = 0x2319; // user is required
public static final int ERROR_PASSWORD_IS_REQUIRED = 0x231a; // password is required public static final int ERROR_PASSWORD_IS_REQUIRED = 0x231a; // password is required
public static final int ERROR_INVALID_JSON_FORMAT = 0x231b; public static final int ERROR_INVALID_JSON_FORMAT = 0x231b;
public static final int ERROR_UNKNOWN_CONNECTION = 0x231c;
public static final int ERROR_UNKNOWN = 0x2350; //unknown error public static final int ERROR_UNKNOWN = 0x2350; //unknown error
...@@ -74,6 +75,7 @@ public class TSDBErrorNumbers { ...@@ -74,6 +75,7 @@ public class TSDBErrorNumbers {
errorNumbers.add(ERROR_USER_IS_REQUIRED); errorNumbers.add(ERROR_USER_IS_REQUIRED);
errorNumbers.add(ERROR_PASSWORD_IS_REQUIRED); errorNumbers.add(ERROR_PASSWORD_IS_REQUIRED);
errorNumbers.add(ERROR_INVALID_JSON_FORMAT); errorNumbers.add(ERROR_INVALID_JSON_FORMAT);
errorNumbers.add(ERROR_UNKNOWN_CONNECTION);
errorNumbers.add(ERROR_RESTFul_Client_Protocol_Exception); errorNumbers.add(ERROR_RESTFul_Client_Protocol_Exception);
......
...@@ -23,7 +23,8 @@ public class OpenTSDBMigrateTest { ...@@ -23,7 +23,8 @@ public class OpenTSDBMigrateTest {
}; };
// when // when
conn.getConnector().insertTelnetLines(lines); OpenTSDBStatement statement = new OpenTSDBStatement(conn.createStatement());
statement.executeTelnetPut(lines);
// then // then
Set<String> collect = Arrays.stream(lines) Set<String> collect = Arrays.stream(lines)
...@@ -31,7 +32,6 @@ public class OpenTSDBMigrateTest { ...@@ -31,7 +32,6 @@ public class OpenTSDBMigrateTest {
.map(s -> s.split("\\s+")[0].replaceAll("\\.", "_")) .map(s -> s.split("\\s+")[0].replaceAll("\\.", "_"))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("show stables"); ResultSet rs = statement.executeQuery("show stables");
Assert.assertNotNull(rs); Assert.assertNotNull(rs);
ResultSetMetaData metaData = rs.getMetaData(); ResultSetMetaData metaData = rs.getMetaData();
...@@ -60,11 +60,11 @@ public class OpenTSDBMigrateTest { ...@@ -60,11 +60,11 @@ public class OpenTSDBMigrateTest {
"}"; "}";
// when // when
conn.getConnector().insertJsonPayload(json); OpenTSDBStatement statement = new OpenTSDBStatement(conn.createStatement());
statement.executeInsertJsonPayload(json);
// then // then
JSONObject jObject = JSONObject.parseObject(json); JSONObject jObject = JSONObject.parseObject(json);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("show stables"); ResultSet rs = statement.executeQuery("show stables");
Assert.assertNotNull(rs); Assert.assertNotNull(rs);
ResultSetMetaData metaData = rs.getMetaData(); ResultSetMetaData metaData = rs.getMetaData();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册