diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java index 6985aebe31bc316c4cffb38a86fd70596438a031..1aa3d5b3cefe2524f0246b500af6687a79d6b20c 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java @@ -11,6 +11,7 @@ import java.net.URL; import java.sql.*; import java.util.ArrayList; import java.util.Calendar; +import java.util.List; import java.util.Map; public class RestfulResultSet implements ResultSet { @@ -59,15 +60,17 @@ public class RestfulResultSet implements ResultSet { } /** - * 由2个resultSet的JSON构造结果集 + * 由多个resultSet的JSON构造结果集 + * * @param resultJson: 包含data信息的结果集,有sql返回的结果集 - * @param fieldJson: 包含meta信息的结果集,有describe xxx - * **/ - public RestfulResultSet(String database, Statement statement, JSONObject resultJson, JSONObject fieldJson) { + * @param fieldJson: 包含多个(最多2个)meta信息的结果集,有describe xxx + **/ + public RestfulResultSet(String database, Statement statement, JSONObject resultJson, List fieldJson) { this(database, statement, resultJson); ArrayList newColumns = new ArrayList<>(); + for (Field column : columns) { - Field field = findField(column.name, fieldJson.getJSONArray("data")); + Field field = findField(column.name, fieldJson); if (field != null) { newColumns.add(field); } else { @@ -78,13 +81,17 @@ public class RestfulResultSet implements ResultSet { this.metaData = new RestfulResultSetMetaData(this.database, this.columns); } - public Field findField(String columnName, JSONArray fieldDataJson) { - for (int i = 0; i < fieldDataJson.size(); i++) { - 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)); + public Field findField(String columnName, List fieldJsonList) { + for (JSONObject fieldJSON : fieldJsonList) { + JSONArray fieldDataJson = fieldJSON.getJSONArray("data"); + for (int i = 0; i < fieldDataJson.size(); i++) { + 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; } 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 b622673ffa83b0283b65977ee8122514ce6a34b6..690b8a17e672700f9e1aaf9effe165f987b86b54 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 @@ -1,7 +1,6 @@ package com.taosdata.jdbc.rs; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.taosdata.jdbc.TSDBConstants; import com.taosdata.jdbc.rs.util.HttpClientPoolUtil; @@ -11,6 +10,7 @@ import java.sql.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; public class RestfulStatement implements Statement { @@ -28,14 +28,37 @@ public class RestfulStatement implements Statement { this.database = database; } - private String parseTableIdentifier(String sql) { - List words = Arrays.asList(sql.trim().toLowerCase().split(" ")); - if (words.get(0).equalsIgnoreCase("select")) { - if (words.contains("from")) { - return words.get(words.indexOf("from") + 1); - } + private String[] parseTableIdentifier(String sql) { + sql = sql.trim().toLowerCase(); + String[] ret = null; + if (sql.contains("where")) + 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 @@ -54,15 +77,19 @@ public class RestfulStatement implements Statement { } // parse table name from sql - String tableIdentifier = parseTableIdentifier(sql); - if (tableIdentifier != null) { - // field meta - String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier); - 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"))); + String[] tableIdentifiers = parseTableIdentifier(sql); + if (tableIdentifiers != null) { + List fieldJsonList = new ArrayList<>(); + for (String tableIdentifier : tableIdentifiers) { + // field meta + String fields = HttpClientPoolUtil.execute(url, "DESCRIBE " + tableIdentifier); + 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 { this.resultSet = new RestfulResultSet(database, this, resultJson); } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/util/HttpClientPoolUtil.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/util/HttpClientPoolUtil.java index 65399b122d97254b88b6bc2ef08910d7badc5061..7bf8efffc14bc7f3098f77788fe4197a598c3e35 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/util/HttpClientPoolUtil.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/util/HttpClientPoolUtil.java @@ -17,6 +17,8 @@ import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; +import java.nio.charset.Charset; + public class HttpClientPoolUtil { public static PoolingHttpClientConnectionManager cm = null; @@ -94,7 +96,8 @@ public class HttpClientPoolUtil { initPools(); } 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(); CloseableHttpResponse httpResponse = httpClient.execute(method, context); httpEntity = httpResponse.getEntity(); @@ -105,26 +108,13 @@ public class HttpClientPoolUtil { if (method != null) { method.abort(); } -// e.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(); + new Exception("execute post request exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):" + (System.currentTimeMillis() - startTime)).printStackTrace(); } finally { if (httpEntity != null) { try { EntityUtils.consumeQuietly(httpEntity); } catch (Exception e) { -// e.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(); + new Exception("close response exception, url:" + uri + ", exception:" + e.toString() + ", cost time(ms):" + (System.currentTimeMillis() - startTime)).printStackTrace(); } } } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/SQLTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/SQLTest.java index d414bc4b09e952b45ab4586fa57c8e8d058380bb..8ff308f8546b39519b9b24412e772a60a3ef68f0 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/SQLTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/SQLTest.java @@ -189,43 +189,43 @@ public class SQLTest { } @Test - public void testCase30() { + public void testCase030() { String sql = "select location, temperature, ts from restful_test.weather where ts > now"; executeQuery(sql); } @Test - public void testCase31() { + public void testCase031() { String sql = "select location, temperature, ts from restful_test.weather where ts < now"; executeQuery(sql); } @Test - public void testCase32() { + public void testCase032() { String sql = "select count(*) from restful_test.weather"; executeQuery(sql); } @Test - public void testCase33() { + public void testCase033() { String sql = "select first(*) from restful_test.weather"; executeQuery(sql); } @Test - public void testCase34() { + public void testCase034() { String sql = "select last(*) from restful_test.weather"; executeQuery(sql); } @Test - public void testCase35() { + public void testCase035() { String sql = "select last_row(*) from restful_test.weather"; executeQuery(sql); } @Test - public void testCase36() { + public void testCase036() { String sql = "select ts, ts as primary_key from restful_test.weather"; executeQuery(sql); } @@ -316,6 +316,12 @@ public class SQLTest { 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) { try (Statement statement = connection.createStatement()) { long start = System.currentTimeMillis();