提交 17b4e8c6 编写于 作者: Z zyyang

change

上级 646acdb0
...@@ -11,6 +11,7 @@ import java.net.URL; ...@@ -11,6 +11,7 @@ import java.net.URL;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import java.util.Map; import java.util.Map;
public class RestfulResultSet implements ResultSet { public class RestfulResultSet implements ResultSet {
...@@ -59,15 +60,17 @@ public class RestfulResultSet implements ResultSet { ...@@ -59,15 +60,17 @@ public class RestfulResultSet implements ResultSet {
} }
/** /**
* 由2个resultSet的JSON构造结果集 * 由多个resultSet的JSON构造结果集
*
* @param resultJson: 包含data信息的结果集,有sql返回的结果集 * @param resultJson: 包含data信息的结果集,有sql返回的结果集
* @param fieldJson: 包含meta信息的结果集,有describe xxx * @param fieldJson: 包含多个(最多2个)meta信息的结果集,有describe xxx
* **/ **/
public RestfulResultSet(String database, Statement statement, JSONObject resultJson, JSONObject fieldJson) { public RestfulResultSet(String database, Statement statement, JSONObject resultJson, List<JSONObject> fieldJson) {
this(database, statement, resultJson); this(database, statement, resultJson);
ArrayList<Field> newColumns = new ArrayList<>(); ArrayList<Field> newColumns = new ArrayList<>();
for (Field column : columns) { for (Field column : columns) {
Field field = findField(column.name, fieldJson.getJSONArray("data")); Field field = findField(column.name, fieldJson);
if (field != null) { if (field != null) {
newColumns.add(field); newColumns.add(field);
} else { } else {
...@@ -78,13 +81,17 @@ public class RestfulResultSet implements ResultSet { ...@@ -78,13 +81,17 @@ public class RestfulResultSet implements ResultSet {
this.metaData = new RestfulResultSetMetaData(this.database, this.columns); this.metaData = new RestfulResultSetMetaData(this.database, this.columns);
} }
public Field findField(String columnName, JSONArray fieldDataJson) { public Field findField(String columnName, List<JSONObject> fieldJsonList) {
for (int i = 0; i < fieldDataJson.size(); i++) { for (JSONObject fieldJSON : fieldJsonList) {
JSONArray field = fieldDataJson.getJSONArray(i); JSONArray fieldDataJson = fieldJSON.getJSONArray("data");
if (columnName.equalsIgnoreCase(field.getString(0))) { for (int i = 0; i < fieldDataJson.size(); i++) {
return new Field(field.getString(0), field.getString(1), field.getInteger(2), field.getString(3)); JSONArray field = fieldDataJson.getJSONArray(i);
if (columnName.equalsIgnoreCase(field.getString(0))) {
return new Field(field.getString(0), field.getString(1), field.getInteger(2), field.getString(3));
}
} }
} }
return null; return null;
} }
......
package com.taosdata.jdbc.rs; package com.taosdata.jdbc.rs;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.taosdata.jdbc.TSDBConstants; import com.taosdata.jdbc.TSDBConstants;
import com.taosdata.jdbc.rs.util.HttpClientPoolUtil; import com.taosdata.jdbc.rs.util.HttpClientPoolUtil;
...@@ -11,6 +10,7 @@ import java.sql.*; ...@@ -11,6 +10,7 @@ import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class RestfulStatement implements Statement { public class RestfulStatement implements Statement {
...@@ -28,14 +28,37 @@ public class RestfulStatement implements Statement { ...@@ -28,14 +28,37 @@ public class RestfulStatement implements Statement {
this.database = database; this.database = database;
} }
private String parseTableIdentifier(String sql) { private String[] parseTableIdentifier(String sql) {
List<String> words = Arrays.asList(sql.trim().toLowerCase().split(" ")); sql = sql.trim().toLowerCase();
if (words.get(0).equalsIgnoreCase("select")) { String[] ret = null;
if (words.contains("from")) { if (sql.contains("where"))
return words.get(words.indexOf("from") + 1); sql = sql.substring(0, sql.indexOf("where"));
} if (sql.contains("interval"))
sql = sql.substring(0, sql.indexOf("interval"));
if (sql.contains("fill"))
sql = sql.substring(0, sql.indexOf("fill"));
if (sql.contains("sliding"))
sql = sql.substring(0, sql.indexOf("sliding"));
if (sql.contains("group by"))
sql = sql.substring(0, sql.indexOf("group by"));
if (sql.contains("order by"))
sql = sql.substring(0, sql.indexOf("order by"));
if (sql.contains("slimit"))
sql = sql.substring(0, sql.indexOf("slimit"));
if (sql.contains("limit"))
sql = sql.substring(0, sql.indexOf("limit"));
// parse
if (sql.contains("from")) {
sql = sql.substring(sql.indexOf("from") + 4).trim();
return Arrays.asList(sql.split(",")).stream()
.map(tableIdentifier -> {
tableIdentifier = tableIdentifier.trim();
if (tableIdentifier.contains(" "))
tableIdentifier = tableIdentifier.substring(0, tableIdentifier.indexOf(" "));
return tableIdentifier;
}).collect(Collectors.joining(",")).split(",");
} }
return null; return ret;
} }
@Override @Override
...@@ -54,15 +77,19 @@ public class RestfulStatement implements Statement { ...@@ -54,15 +77,19 @@ public class RestfulStatement implements Statement {
} }
// parse table name from sql // parse table name from sql
String tableIdentifier = parseTableIdentifier(sql); String[] tableIdentifiers = parseTableIdentifier(sql);
if (tableIdentifier != null) { if (tableIdentifiers != null) {
// field meta List<JSONObject> fieldJsonList = new ArrayList<>();
String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier); for (String tableIdentifier : tableIdentifiers) {
JSONObject fieldJson = JSON.parseObject(fields); // field meta
if (fieldJson.getString("status").equals("error")) { String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier);
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code"))); JSONObject fieldJson = JSON.parseObject(fields);
if (fieldJson.getString("status").equals("error")) {
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code")));
}
fieldJsonList.add(fieldJson);
} }
this.resultSet = new RestfulResultSet(database, this, resultJson, fieldJson); this.resultSet = new RestfulResultSet(database, this, resultJson, fieldJsonList);
} else { } else {
this.resultSet = new RestfulResultSet(database, this, resultJson); this.resultSet = new RestfulResultSet(database, this, resultJson);
} }
......
...@@ -17,6 +17,8 @@ import org.apache.http.protocol.HTTP; ...@@ -17,6 +17,8 @@ import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import java.nio.charset.Charset;
public class HttpClientPoolUtil { public class HttpClientPoolUtil {
public static PoolingHttpClientConnectionManager cm = null; public static PoolingHttpClientConnectionManager cm = null;
...@@ -94,7 +96,8 @@ public class HttpClientPoolUtil { ...@@ -94,7 +96,8 @@ public class HttpClientPoolUtil {
initPools(); initPools();
} }
method = (HttpEntityEnclosingRequestBase) getRequest(uri, HttpPost.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0); method = (HttpEntityEnclosingRequestBase) getRequest(uri, HttpPost.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
method.setEntity(new StringEntity(data)); method.setHeader("Content-Type", "text/plain");
method.setEntity(new StringEntity(data, Charset.forName("UTF-8")));
HttpContext context = HttpClientContext.create(); HttpContext context = HttpClientContext.create();
CloseableHttpResponse httpResponse = httpClient.execute(method, context); CloseableHttpResponse httpResponse = httpClient.execute(method, context);
httpEntity = httpResponse.getEntity(); httpEntity = httpResponse.getEntity();
...@@ -105,26 +108,13 @@ public class HttpClientPoolUtil { ...@@ -105,26 +108,13 @@ public class HttpClientPoolUtil {
if (method != null) { if (method != null) {
method.abort(); method.abort();
} }
// e.printStackTrace(); new Exception("execute post request exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):" + (System.currentTimeMillis() - startTime)).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 { } finally {
if (httpEntity != null) { if (httpEntity != null) {
try { try {
EntityUtils.consumeQuietly(httpEntity); EntityUtils.consumeQuietly(httpEntity);
} catch (Exception e) { } catch (Exception e) {
// e.printStackTrace(); new Exception("close response exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):" + (System.currentTimeMillis() - startTime)).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();
} }
} }
} }
......
...@@ -189,43 +189,43 @@ public class SQLTest { ...@@ -189,43 +189,43 @@ public class SQLTest {
} }
@Test @Test
public void testCase30() { public void testCase030() {
String sql = "select location, temperature, ts from restful_test.weather where ts > now"; String sql = "select location, temperature, ts from restful_test.weather where ts > now";
executeQuery(sql); executeQuery(sql);
} }
@Test @Test
public void testCase31() { public void testCase031() {
String sql = "select location, temperature, ts from restful_test.weather where ts < now"; String sql = "select location, temperature, ts from restful_test.weather where ts < now";
executeQuery(sql); executeQuery(sql);
} }
@Test @Test
public void testCase32() { public void testCase032() {
String sql = "select count(*) from restful_test.weather"; String sql = "select count(*) from restful_test.weather";
executeQuery(sql); executeQuery(sql);
} }
@Test @Test
public void testCase33() { public void testCase033() {
String sql = "select first(*) from restful_test.weather"; String sql = "select first(*) from restful_test.weather";
executeQuery(sql); executeQuery(sql);
} }
@Test @Test
public void testCase34() { public void testCase034() {
String sql = "select last(*) from restful_test.weather"; String sql = "select last(*) from restful_test.weather";
executeQuery(sql); executeQuery(sql);
} }
@Test @Test
public void testCase35() { public void testCase035() {
String sql = "select last_row(*) from restful_test.weather"; String sql = "select last_row(*) from restful_test.weather";
executeQuery(sql); executeQuery(sql);
} }
@Test @Test
public void testCase36() { public void testCase036() {
String sql = "select ts, ts as primary_key from restful_test.weather"; String sql = "select ts, ts as primary_key from restful_test.weather";
executeQuery(sql); executeQuery(sql);
} }
...@@ -316,6 +316,12 @@ public class SQLTest { ...@@ -316,6 +316,12 @@ public class SQLTest {
executeQuery(sql); executeQuery(sql);
} }
@Test
public void testCase051() {
String sql = "select * from restful_test.t1 tt, restful_test.t3 yy where tt.ts = yy.ts";
executeQuery(sql);
}
private void executeUpdate(String sql) { private void executeUpdate(String sql) {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册