change
This commit is contained in:
parent
646acdb0f6
commit
17b4e8c6c8
|
@ -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 {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 由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 {
|
||||||
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 (JSONObject fieldJSON : fieldJsonList) {
|
||||||
|
JSONArray fieldDataJson = fieldJSON.getJSONArray("data");
|
||||||
for (int i = 0; i < fieldDataJson.size(); i++) {
|
for (int i = 0; i < fieldDataJson.size(); i++) {
|
||||||
JSONArray field = fieldDataJson.getJSONArray(i);
|
JSONArray field = fieldDataJson.getJSONArray(i);
|
||||||
if (columnName.equalsIgnoreCase(field.getString(0))) {
|
if (columnName.equalsIgnoreCase(field.getString(0))) {
|
||||||
return new Field(field.getString(0), field.getString(1), field.getInteger(2), field.getString(3));
|
return new Field(field.getString(0), field.getString(1), field.getInteger(2), field.getString(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
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.*;
|
||||||
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 {
|
||||||
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 ret;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -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) {
|
||||||
|
List<JSONObject> fieldJsonList = new ArrayList<>();
|
||||||
|
for (String tableIdentifier : tableIdentifiers) {
|
||||||
// field meta
|
// field meta
|
||||||
String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier);
|
String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier);
|
||||||
JSONObject fieldJson = JSON.parseObject(fields);
|
JSONObject fieldJson = JSON.parseObject(fields);
|
||||||
if (fieldJson.getString("status").equals("error")) {
|
if (fieldJson.getString("status").equals("error")) {
|
||||||
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code")));
|
throw new SQLException(TSDBConstants.WrapErrMsg("SQL execution error: " + fieldJson.getString("desc") + "\n" + "error code: " + fieldJson.getString("code")));
|
||||||
}
|
}
|
||||||
this.resultSet = new RestfulResultSet(database, this, resultJson, fieldJson);
|
fieldJsonList.add(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;
|
||||||
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 {
|
||||||
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 {
|
||||||
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 {
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 {
|
||||||
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();
|
||||||
|
|
Loading…
Reference in New Issue