diff --git a/src/connector/jdbc/deploy-pom.xml b/src/connector/jdbc/deploy-pom.xml
index 51db837c7b1149bfc5dca6d69a953ceb6b3eb898..46de54ffb22a739701040fc0aa1aac2ab8210654 100755
--- a/src/connector/jdbc/deploy-pom.xml
+++ b/src/connector/jdbc/deploy-pom.xml
@@ -5,7 +5,7 @@
com.taosdata.jdbc
taos-jdbcdriver
- 2.0.10
+ 2.0.13
jar
JDBCDriver
diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml
index e7124a0599fa80baabba84700eb097bde3e57287..3d1f40243547081d5ada664e0edaf3e9184f3172 100755
--- a/src/connector/jdbc/pom.xml
+++ b/src/connector/jdbc/pom.xml
@@ -3,7 +3,7 @@
4.0.0
com.taosdata.jdbc
taos-jdbcdriver
- 2.0.8
+ 2.0.13
jar
JDBCDriver
https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc
@@ -112,6 +112,13 @@
maven-surefire-plugin
2.12.4
+
+ **/*Test.java
+
+
+ **/BatchInsertTest.java
+ **/FailOverTest.java
+
true
diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java
index b82efca3ef4defb166632e4dd347de528e52d2c6..6b0937a9b7c50c25d03459d18e01b807b5c00c3c 100644
--- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java
+++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java
@@ -15,7 +15,6 @@ public class RestfulConnection implements Connection {
private final String database;
private final String url;
-
public RestfulConnection(String host, String port, Properties props, String database, String url) {
this.host = host;
this.port = Integer.parseInt(port);
@@ -28,7 +27,7 @@ public class RestfulConnection implements Connection {
public Statement createStatement() throws SQLException {
if (isClosed())
throw new SQLException(TSDBConstants.WrapErrMsg("restful TDengine connection is closed."));
- return new RestfulStatement(this, this.database);
+ return new RestfulStatement(this, database);
}
@Override
@@ -104,22 +103,28 @@ public class RestfulConnection implements Connection {
@Override
public void setTransactionIsolation(int level) throws SQLException {
-
+ //transaction is not supported
+ throw new SQLFeatureNotSupportedException("transactions are not supported");
}
+ /**
+ *
+ */
@Override
public int getTransactionIsolation() throws SQLException {
- return 0;
+ //Connection.TRANSACTION_NONE specifies that transactions are not supported.
+ return Connection.TRANSACTION_NONE;
}
@Override
public SQLWarning getWarnings() throws SQLException {
+ //TODO: getWarnings not implemented
return null;
}
@Override
public void clearWarnings() throws SQLException {
-
+ throw new SQLFeatureNotSupportedException("clearWarnings not supported.");
}
@Override
@@ -209,22 +214,26 @@ public class RestfulConnection implements Connection {
@Override
public Clob createClob() throws SQLException {
- return null;
+ //TODO: not supported
+ throw new SQLFeatureNotSupportedException();
}
@Override
public Blob createBlob() throws SQLException {
- return null;
+ //TODO: not supported
+ throw new SQLFeatureNotSupportedException();
}
@Override
public NClob createNClob() throws SQLException {
- return null;
+ //TODO: not supported
+ throw new SQLFeatureNotSupportedException();
}
@Override
public SQLXML createSQLXML() throws SQLException {
- return null;
+ //TODO: not supported
+ throw new SQLFeatureNotSupportedException();
}
@Override
@@ -254,12 +263,14 @@ public class RestfulConnection implements Connection {
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
- return null;
+ //TODO: not supported
+ throw new SQLFeatureNotSupportedException();
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
- return null;
+ //TODO: not supported
+ throw new SQLFeatureNotSupportedException();
}
@Override
@@ -289,12 +300,16 @@ public class RestfulConnection implements Connection {
@Override
public T unwrap(Class iface) throws SQLException {
- return null;
+ try {
+ return iface.cast(this);
+ } catch (ClassCastException cce) {
+ throw new SQLException("Unable to unwrap to " + iface.toString());
+ }
}
@Override
public boolean isWrapperFor(Class> iface) throws SQLException {
- return false;
+ return iface.isInstance(this);
}
public String getHost() {
diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java
index a07d6eb2b6d637a86a9ac0c2a9b03e3ee3f5028c..9e87cfa68015ece9fdb0208b290daaecf75b2191 100644
--- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java
+++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java
@@ -35,7 +35,7 @@ public class RestfulDriver extends AbstractTaosDriver {
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 database = props.containsKey(TSDBDriver.PROPERTY_KEY_DBNAME) ? props.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME) : null;
String loginUrl = "http://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + ":"
+ props.getProperty(TSDBDriver.PROPERTY_KEY_PORT) + "/rest/login/"
diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java
index 20510f01352c6b8a82ed300bd629d3184eb58894..30b56638d880137b7a3394e5282a3edf5178d2b0 100644
--- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java
+++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taosdata.jdbc.TSDBConstants;
import com.taosdata.jdbc.rs.util.HttpClientPoolUtil;
+import com.taosdata.jdbc.utils.SqlSyntaxValidator;
import java.sql.*;
import java.util.Arrays;
@@ -11,19 +12,23 @@ import java.util.List;
public class RestfulStatement implements Statement {
- private final String catalog;
+ private boolean closed;
+ private String database;
private final RestfulConnection conn;
- public RestfulStatement(RestfulConnection c, String catalog) {
+ public RestfulStatement(RestfulConnection c, String database) {
this.conn = c;
- this.catalog = catalog;
+ this.database = database;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
+ if (isClosed())
+ throw new SQLException("statement already closed");
+ if (!SqlSyntaxValidator.isSelectSql(sql))
+ throw new SQLException("not a select sql for executeQuery: " + sql);
- final String url = "http://" + conn.getHost() + ":"+conn.getPort()+"/rest/sql";
-
+ final String url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql";
String result = HttpClientPoolUtil.execute(url, sql);
String fields = "";
List words = Arrays.asList(sql.split(" "));
@@ -65,12 +70,29 @@ public class RestfulStatement implements Statement {
@Override
public int executeUpdate(String sql) throws SQLException {
- return 0;
+ if (isClosed())
+ throw new SQLException("statement already closed");
+ if (!SqlSyntaxValidator.isValidForExecuteUpdate(sql))
+ throw new SQLException("not a valid sql for executeUpdate: " + sql);
+
+ if (this.database == null)
+ throw new SQLException("Database not specified or available");
+
+ final String url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql";
+ HttpClientPoolUtil.execute(url, "use " + conn.getDatabase());
+ String result = HttpClientPoolUtil.execute(url, sql);
+ 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")));
+ }
+ return Integer.parseInt(jsonObject.getString("rows"));
}
@Override
public void close() throws SQLException {
-
+ this.closed = true;
}
@Override
@@ -115,6 +137,7 @@ public class RestfulStatement implements Statement {
@Override
public SQLWarning getWarnings() throws SQLException {
+ //TODO: getWarnings not Implemented
return null;
}
@@ -130,7 +153,29 @@ public class RestfulStatement implements Statement {
@Override
public boolean execute(String sql) throws SQLException {
- return false;
+ if (isClosed()) {
+ throw new SQLException("Invalid method call on a closed statement.");
+ }
+ //如果执行了use操作应该将当前Statement的catalog设置为新的database
+ if (SqlSyntaxValidator.isUseSql(sql)) {
+ this.database = sql.trim().replace("use", "").trim();
+ }
+ if (this.database == null)
+ throw new SQLException("Database not specified or available");
+
+ final String url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql";
+ // use database
+ HttpClientPoolUtil.execute(url, "use " + conn.getDatabase());
+ // execute sql
+ String result = HttpClientPoolUtil.execute(url, sql);
+ // parse result
+ 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")));
+ }
+ return true;
}
@Override
@@ -245,7 +290,7 @@ public class RestfulStatement implements Statement {
@Override
public boolean isClosed() throws SQLException {
- return false;
+ return closed;
}
@Override
@@ -270,11 +315,15 @@ public class RestfulStatement implements Statement {
@Override
public T unwrap(Class iface) throws SQLException {
- return null;
+ try {
+ return iface.cast(this);
+ } catch (ClassCastException cce) {
+ throw new SQLException("Unable to unwrap to " + iface.toString());
+ }
}
@Override
public boolean isWrapperFor(Class> iface) throws SQLException {
- return false;
+ return iface.isInstance(this);
}
}
diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/SqlSyntaxValidator.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/SqlSyntaxValidator.java
index 066dfad5d50fb5fcb31c00a645ccc0487f6a594e..388c3978bef3372e6abca89a70bd428a8bb79d3d 100644
--- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/SqlSyntaxValidator.java
+++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/SqlSyntaxValidator.java
@@ -22,6 +22,9 @@ import java.sql.SQLException;
public class SqlSyntaxValidator {
+ private static final String[] updateSQL = {"insert", "update", "delete", "create", "alter", "drop", "show", "describe", "use"};
+ private static final String[] querySQL = {"select"};
+
private TSDBConnection tsdbConnection;
public SqlSyntaxValidator(Connection connection) {
@@ -34,7 +37,7 @@ public class SqlSyntaxValidator {
if (tsdbConnection == null || tsdbConnection.isClosed()) {
throw new SQLException("invalid connection");
} else {
- TSDBJNIConnector jniConnector = tsdbConnection.getConnection();
+ TSDBJNIConnector jniConnector = tsdbConnection.getConnection();
if (jniConnector == null) {
throw new SQLException("jniConnector is null");
} else {
@@ -43,4 +46,28 @@ public class SqlSyntaxValidator {
}
return res;
}
+
+ public static boolean isValidForExecuteUpdate(String sql) {
+ for (String prefix : updateSQL) {
+ if (sql.trim().toLowerCase().startsWith(prefix))
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isUseSql(String sql) {
+ return sql.trim().toLowerCase().startsWith(updateSQL[8]) || sql.trim().toLowerCase().matches("create\\s*database.*") || sql.toLowerCase().toLowerCase().matches("drop\\s*database.*");
+ }
+
+ public static boolean isUpdateSql(String sql) {
+ return sql.trim().toLowerCase().startsWith(updateSQL[1]);
+ }
+
+ public static boolean isInsertSql(String sql) {
+ return sql.trim().toLowerCase().startsWith(updateSQL[0]);
+ }
+
+ public static boolean isSelectSql(String sql) {
+ return sql.trim().toLowerCase().startsWith(querySQL[0]);
+ }
}
diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDriverTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDriverTest.java
index 768eceab8e66e9d66302cc6579787b2f2b2232a8..d07a6a21796fc2ea7927c04fd0cf5a37e9a49988 100644
--- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDriverTest.java
+++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDriverTest.java
@@ -7,29 +7,6 @@ import java.sql.*;
public class RestfulDriverTest {
- @Test
- public void testCase001() {
- try {
- Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
- 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 connect() {
diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulJDBCTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulJDBCTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..d13475b96dfc96780faaae7b01275f717f8befbf
--- /dev/null
+++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulJDBCTest.java
@@ -0,0 +1,108 @@
+package com.taosdata.jdbc.rs;
+
+import org.junit.*;
+import org.junit.runners.MethodSorters;
+
+import java.sql.*;
+import java.util.Random;
+
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class RestfulJDBCTest {
+
+ private Connection connection;
+
+ @Before
+ public void before() throws ClassNotFoundException, SQLException {
+ Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
+ connection = DriverManager.getConnection("jdbc:TAOS-RS://master:6041/restful_test?user=root&password=taosdata");
+ }
+
+ @After
+ public void after() throws SQLException {
+ if (connection != null)
+ connection.close();
+ }
+
+
+ /**
+ * 查询所有log.log
+ **/
+ @Test
+ public void testCase001() {
+ try {
+ 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();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * create database
+ */
+ @Test
+ public void testCase002() {
+ try (Statement stmt = connection.createStatement()) {
+ stmt.execute("drop database if exists restful_test");
+ stmt.execute("create database if not exists restful_test");
+ stmt.execute("use restful_test");
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * create super table
+ ***/
+ @Test
+ public void testCase003() {
+ try (Statement stmt = connection.createStatement()) {
+ stmt.execute("create table weather(ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int)");
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testCase004() {
+ try (Statement stmt = connection.createStatement()) {
+ for (int i = 1; i <= 100; i++) {
+ stmt.execute("create table t" + i + " using weather tags('beijing', '" + i + "')");
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private Random random = new Random(System.currentTimeMillis());
+
+ @Test
+ public void testCase005() {
+ try (Statement stmt = connection.createStatement()) {
+ int rows = 0;
+ for (int i = 0; i < 10; i++) {
+ for (int j = 1; j <= 100; j++) {
+ long currentTimeMillis = System.currentTimeMillis();
+ int affectRows = stmt.executeUpdate("insert into t" + j + " values(" + currentTimeMillis + "," + (random.nextFloat() * 50) + "," + random.nextInt(100) + ")");
+ Assert.assertEquals(1, affectRows);
+ rows += affectRows;
+ }
+ }
+ Assert.assertEquals(1000, rows);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+}
diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/SqlSyntaxValidatorTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/SqlSyntaxValidatorTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..25c6c4913d5855f0ef48e8113b94a406f814f986
--- /dev/null
+++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/SqlSyntaxValidatorTest.java
@@ -0,0 +1,29 @@
+package com.taosdata.jdbc.utils;
+
+import com.sun.source.tree.AssertTree;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SqlSyntaxValidatorTest {
+
+ @Test
+ public void validateSqlSyntax() {
+ }
+
+ @Test
+ public void isSelectSQL() {
+ Assert.assertTrue(SqlSyntaxValidator.isSelectSql("select * from test.weather"));
+ Assert.assertTrue(SqlSyntaxValidator.isSelectSql(" select * from test.weather"));
+ Assert.assertTrue(SqlSyntaxValidator.isSelectSql(" select * from test.weather "));
+ Assert.assertFalse(SqlSyntaxValidator.isSelectSql("insert into test.weather values(now, 1.1, 2)"));
+ }
+
+ @Test
+ public void isUseSQL() {
+ Assert.assertTrue(SqlSyntaxValidator.isUseSql("use database test"));
+ Assert.assertTrue(SqlSyntaxValidator.isUseSql("create database test"));
+ Assert.assertTrue(SqlSyntaxValidator.isUseSql("create database if not exist test"));
+ Assert.assertTrue(SqlSyntaxValidator.isUseSql("drop database test"));
+ Assert.assertTrue(SqlSyntaxValidator.isUseSql("drop database if exist test"));
+ }
+}
\ No newline at end of file