未验证 提交 e1d53e5f 编写于 作者: M Minglei Jin 提交者: GitHub

Merge pull request #8858 from taosdata/fix/TS-601

[TS-601]<fix>: no invalid database error in jdbc-restful with wrong db
...@@ -66,7 +66,11 @@ public class RestfulStatement extends AbstractStatement { ...@@ -66,7 +66,11 @@ public class RestfulStatement extends AbstractStatement {
boolean result = true; boolean result = true;
if (SqlSyntaxValidator.isUseSql(sql)) { if (SqlSyntaxValidator.isUseSql(sql)) {
HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getToken()); String ret = HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getToken());
JSONObject resultJson = JSON.parseObject(ret);
if (resultJson.getString("status").equals("error")) {
throw TSDBError.createSQLException(resultJson.getInteger("code"), "sql: " + sql + ", desc: " + resultJson.getString("desc"));
}
this.database = sql.trim().replace("use", "").trim(); this.database = sql.trim().replace("use", "").trim();
this.conn.setCatalog(this.database); this.conn.setCatalog(this.database);
result = false; result = false;
...@@ -115,7 +119,7 @@ public class RestfulStatement extends AbstractStatement { ...@@ -115,7 +119,7 @@ public class RestfulStatement extends AbstractStatement {
String result = HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getToken()); String result = HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getToken());
JSONObject resultJson = JSON.parseObject(result); JSONObject resultJson = JSON.parseObject(result);
if (resultJson.getString("status").equals("error")) { if (resultJson.getString("status").equals("error")) {
throw TSDBError.createSQLException(resultJson.getInteger("code"), resultJson.getString("desc")); throw TSDBError.createSQLException(resultJson.getInteger("code"), "sql: " + sql + ", desc: " + resultJson.getString("desc"));
} }
this.resultSet = new RestfulResultSet(database, this, resultJson); this.resultSet = new RestfulResultSet(database, this, resultJson);
this.affectedRows = 0; this.affectedRows = 0;
...@@ -126,7 +130,7 @@ public class RestfulStatement extends AbstractStatement { ...@@ -126,7 +130,7 @@ public class RestfulStatement extends AbstractStatement {
String result = HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getToken()); String result = HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getToken());
JSONObject jsonObject = JSON.parseObject(result); JSONObject jsonObject = JSON.parseObject(result);
if (jsonObject.getString("status").equals("error")) { if (jsonObject.getString("status").equals("error")) {
throw TSDBError.createSQLException(jsonObject.getInteger("code"), jsonObject.getString("desc")); throw TSDBError.createSQLException(jsonObject.getInteger("code"), "sql: " + sql + ", desc: " + jsonObject.getString("desc"));
} }
this.resultSet = null; this.resultSet = null;
this.affectedRows = getAffectedRows(jsonObject); this.affectedRows = getAffectedRows(jsonObject);
...@@ -134,16 +138,13 @@ public class RestfulStatement extends AbstractStatement { ...@@ -134,16 +138,13 @@ public class RestfulStatement extends AbstractStatement {
} }
private int getAffectedRows(JSONObject jsonObject) throws SQLException { private int getAffectedRows(JSONObject jsonObject) throws SQLException {
// create ... SQLs should return 0 , and Restful result like this:
// {"status": "succ", "head": ["affected_rows"], "data": [[0]], "rows": 1}
JSONArray head = jsonObject.getJSONArray("head"); JSONArray head = jsonObject.getJSONArray("head");
if (head.size() != 1 || !"affected_rows".equals(head.getString(0))) if (head.size() != 1 || !"affected_rows".equals(head.getString(0)))
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "invalid variable: [" + head.toJSONString() + "]");
JSONArray data = jsonObject.getJSONArray("data"); JSONArray data = jsonObject.getJSONArray("data");
if (data != null) if (data != null)
return data.getJSONArray(0).getInteger(0); return data.getJSONArray(0).getInteger(0);
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "invalid variable: [" + jsonObject.toJSONString() + "]");
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
} }
@Override @Override
......
...@@ -57,21 +57,17 @@ public class AuthenticationTest { ...@@ -57,21 +57,17 @@ public class AuthenticationTest {
@Ignore @Ignore
@Test @Test
public void test() { public void test() throws SQLException {
// change password // change password
try { conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=" + user + "&password=taosdata");
conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=" + user + "&password=taosdata");
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
stmt.execute("alter user " + user + " pass '" + password + "'"); stmt.execute("alter user " + user + " pass '" + password + "'");
stmt.close(); stmt.close();
conn.close(); conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// use new to login and execute query // use new to login and execute query
try { conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=" + user + "&password=" + password);
conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=" + user + "&password=" + password); stmt = conn.createStatement();
Statement stmt = conn.createStatement();
stmt.execute("show databases"); stmt.execute("show databases");
ResultSet rs = stmt.getResultSet(); ResultSet rs = stmt.getResultSet();
ResultSetMetaData meta = rs.getMetaData(); ResultSetMetaData meta = rs.getMetaData();
...@@ -81,29 +77,13 @@ public class AuthenticationTest { ...@@ -81,29 +77,13 @@ public class AuthenticationTest {
} }
System.out.println(); System.out.println();
} }
} catch (SQLException e) {
e.printStackTrace();
}
// change password back // change password back
try { conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=" + user + "&password=" + password);
conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=" + user + "&password=" + password); stmt = conn.createStatement();
Statement stmt = conn.createStatement();
stmt.execute("alter user " + user + " pass 'taosdata'"); stmt.execute("alter user " + user + " pass 'taosdata'");
stmt.close(); stmt.close();
conn.close(); conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Before
public void before() {
try {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} }
} }
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import com.taosdata.jdbc.TSDBErrorNumbers;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -9,16 +7,14 @@ import java.sql.SQLException; ...@@ -9,16 +7,14 @@ import java.sql.SQLException;
public class ConnectWrongDatabaseTest { public class ConnectWrongDatabaseTest {
@Test @Test(expected = SQLException.class)
public void connect() { public void connectByJni() throws SQLException {
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
DriverManager.getConnection("jdbc:TAOS://localhost:6030/wrong_db?user=root&password=taosdata"); DriverManager.getConnection("jdbc:TAOS://localhost:6030/wrong_db?user=root&password=taosdata");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
Assert.assertEquals(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL, e.getErrorCode());
} }
@Test(expected = SQLException.class)
public void connectByRestful() throws SQLException {
DriverManager.getConnection("jdbc:TAOS-RS://localhost:6041/wrong_db?user=root&password=taosdata");
} }
} }
...@@ -18,9 +18,8 @@ public class InsertDbwithoutUseDbTest { ...@@ -18,9 +18,8 @@ public class InsertDbwithoutUseDbTest {
private static final Random random = new Random(System.currentTimeMillis()); private static final Random random = new Random(System.currentTimeMillis());
@Test @Test
public void case001() throws ClassNotFoundException, SQLException { public void case001() throws SQLException {
// prepare schema // prepare schema
Class.forName("com.taosdata.jdbc.TSDBDriver");
String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata"; String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata";
Connection conn = DriverManager.getConnection(url, properties); Connection conn = DriverManager.getConnection(url, properties);
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
...@@ -51,9 +50,8 @@ public class InsertDbwithoutUseDbTest { ...@@ -51,9 +50,8 @@ public class InsertDbwithoutUseDbTest {
} }
@Test @Test
public void case002() throws ClassNotFoundException, SQLException { public void case002() throws SQLException {
// prepare the schema // prepare the schema
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
final String url = "jdbc:TAOS-RS://" + host + ":6041/inWithoutDb?user=root&password=taosdata"; final String url = "jdbc:TAOS-RS://" + host + ":6041/inWithoutDb?user=root&password=taosdata";
Connection conn = DriverManager.getConnection(url, properties); Connection conn = DriverManager.getConnection(url, properties);
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
......
...@@ -374,22 +374,17 @@ public class RestfulConnectionTest { ...@@ -374,22 +374,17 @@ public class RestfulConnectionTest {
} }
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() throws SQLException {
try {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/log?user=root&password=taosdata", properties); conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata", properties);
// create test database for test cases // create test database for test cases
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
stmt.execute("create database if not exists test"); stmt.execute("create database if not exists test");
} }
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
} }
@AfterClass @AfterClass
......
...@@ -10,11 +10,11 @@ import java.util.Random; ...@@ -10,11 +10,11 @@ import java.util.Random;
public class RestfulJDBCTest { public class RestfulJDBCTest {
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private final Random random = new Random(System.currentTimeMillis()); private static final Random random = new Random(System.currentTimeMillis());
private Connection connection; private static Connection connection;
@Test @Test
public void testCase001() { public void testCase001() throws SQLException {
// given // given
String sql = "drop database if exists restful_test"; String sql = "drop database if exists restful_test";
// when // when
...@@ -38,7 +38,7 @@ public class RestfulJDBCTest { ...@@ -38,7 +38,7 @@ public class RestfulJDBCTest {
} }
@Test @Test
public void testCase002() { public void testCase002() throws SQLException {
// given // given
String sql = "create table weather(ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int)"; String sql = "create table weather(ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int)";
// when // when
...@@ -48,7 +48,7 @@ public class RestfulJDBCTest { ...@@ -48,7 +48,7 @@ public class RestfulJDBCTest {
} }
@Test @Test
public void testCase004() { public void testCase004() throws SQLException {
for (int i = 1; i <= 100; i++) { for (int i = 1; i <= 100; i++) {
// given // given
String sql = "create table t" + i + " using weather tags('beijing', '" + i + "')"; String sql = "create table t" + i + " using weather tags('beijing', '" + i + "')";
...@@ -60,7 +60,7 @@ public class RestfulJDBCTest { ...@@ -60,7 +60,7 @@ public class RestfulJDBCTest {
} }
@Test @Test
public void testCase005() { public void testCase005() throws SQLException {
int rows = 0; int rows = 0;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
for (int j = 1; j <= 100; j++) { for (int j = 1; j <= 100; j++) {
...@@ -99,7 +99,7 @@ public class RestfulJDBCTest { ...@@ -99,7 +99,7 @@ public class RestfulJDBCTest {
} }
@Test @Test
public void testCase007() { public void testCase007() throws SQLException {
// given // given
String sql = "drop database restful_test"; String sql = "drop database restful_test";
...@@ -110,50 +110,41 @@ public class RestfulJDBCTest { ...@@ -110,50 +110,41 @@ public class RestfulJDBCTest {
Assert.assertFalse(execute); Assert.assertFalse(execute);
} }
private int executeUpdate(Connection connection, String sql) { private int executeUpdate(Connection connection, String sql) throws SQLException {
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
return stmt.executeUpdate(sql); return stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} }
return 0;
} }
private boolean execute(Connection connection, String sql) { private boolean execute(Connection connection, String sql) throws SQLException {
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
return stmt.execute(sql); return stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
} }
return false;
} }
private ResultSet executeQuery(Connection connection, String sql) { private ResultSet executeQuery(Connection connection, String sql) throws SQLException {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
return statement.executeQuery(sql); return statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
} }
return null;
} }
@Before @BeforeClass
public void before() { public static void beforeClass() {
try { try {
connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=root&password=taosdata"); connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@After @AfterClass
public void after() { public static void afterClass() throws SQLException {
try { if (connection != null) {
if (connection != null) Statement stmt = connection.createStatement();
stmt.execute("drop database if exists restful_test");
stmt.close();
connection.close(); connection.close();
} catch (SQLException e) {
e.printStackTrace();
} }
} }
......
...@@ -186,10 +186,8 @@ public class RestfulResultSetMetaDataTest { ...@@ -186,10 +186,8 @@ public class RestfulResultSetMetaDataTest {
} }
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() throws SQLException {
try { conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata");
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=root&password=taosdata");
stmt = conn.createStatement(); stmt = conn.createStatement();
stmt.execute("create database if not exists restful_test"); stmt.execute("create database if not exists restful_test");
stmt.execute("use restful_test"); stmt.execute("use restful_test");
...@@ -199,9 +197,6 @@ public class RestfulResultSetMetaDataTest { ...@@ -199,9 +197,6 @@ public class RestfulResultSetMetaDataTest {
rs = stmt.executeQuery("select * from restful_test.weather"); rs = stmt.executeQuery("select * from restful_test.weather");
rs.next(); rs.next();
meta = rs.getMetaData(); meta = rs.getMetaData();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
} }
@AfterClass @AfterClass
......
...@@ -658,11 +658,10 @@ public class RestfulResultSetTest { ...@@ -658,11 +658,10 @@ public class RestfulResultSetTest {
} }
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() throws SQLException {
try { conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata");
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=root&password=taosdata");
stmt = conn.createStatement(); stmt = conn.createStatement();
stmt.execute("drop database if exists restful_test");
stmt.execute("create database if not exists restful_test"); stmt.execute("create database if not exists restful_test");
stmt.execute("use restful_test"); stmt.execute("use restful_test");
stmt.execute("drop table if exists weather"); stmt.execute("drop table if exists weather");
...@@ -670,24 +669,18 @@ public class RestfulResultSetTest { ...@@ -670,24 +669,18 @@ public class RestfulResultSetTest {
stmt.execute("insert into restful_test.weather values('2021-01-01 00:00:00.000', 1, 100, 3.1415, 3.1415926, 'abc', 10, 10, true, '涛思数据')"); stmt.execute("insert into restful_test.weather values('2021-01-01 00:00:00.000', 1, 100, 3.1415, 3.1415926, 'abc', 10, 10, true, '涛思数据')");
rs = stmt.executeQuery("select * from restful_test.weather"); rs = stmt.executeQuery("select * from restful_test.weather");
rs.next(); rs.next();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
} }
@AfterClass @AfterClass
public static void afterClass() { public static void afterClass() throws SQLException {
try {
if (rs != null) if (rs != null)
rs.close(); rs.close();
if (stmt != null) if (stmt != null) {
stmt.execute("drop database if exists restful_test");
stmt.close(); stmt.close();
}
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
\ No newline at end of file
...@@ -581,11 +581,14 @@ public class SQLTest { ...@@ -581,11 +581,14 @@ public class SQLTest {
@BeforeClass @BeforeClass
public static void before() throws SQLException { public static void before() throws SQLException {
connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/restful_test?user=root&password=taosdata"); connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata");
} }
@AfterClass @AfterClass
public static void after() throws SQLException { public static void after() throws SQLException {
Statement stmt = connection.createStatement();
stmt.execute("drop database if exists restful_test");
stmt.close();
connection.close(); connection.close();
} }
......
...@@ -249,7 +249,7 @@ cd ../../../debug; make ...@@ -249,7 +249,7 @@ cd ../../../debug; make
#======================b4-end=============== #======================b4-end===============
#======================b5-start=============== #======================b5-start===============
./test.sh -f unique/dnode/alternativeRole.sim #./test.sh -f unique/dnode/alternativeRole.sim
./test.sh -f unique/dnode/balance1.sim ./test.sh -f unique/dnode/balance1.sim
./test.sh -f unique/dnode/balance2.sim ./test.sh -f unique/dnode/balance2.sim
./test.sh -f unique/dnode/balance3.sim ./test.sh -f unique/dnode/balance3.sim
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册