Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
17b4e8c6
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
17b4e8c6
编写于
12月 29, 2020
作者:
Z
zyyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change
上级
646acdb0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
79 addition
and
49 deletion
+79
-49
src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java
.../src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java
+17
-10
src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java
.../src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java
+43
-16
src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/util/HttpClientPoolUtil.java
...in/java/com/taosdata/jdbc/rs/util/HttpClientPoolUtil.java
+6
-16
src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/SQLTest.java
...ctor/jdbc/src/test/java/com/taosdata/jdbc/rs/SQLTest.java
+13
-7
未找到文件。
src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulResultSet.java
浏览文件 @
17b4e8c6
...
...
@@ -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
<
JSONObject
>
fieldJson
)
{
this
(
database
,
statement
,
resultJson
);
ArrayList
<
Field
>
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
<
JSONObject
>
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
;
}
...
...
src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java
浏览文件 @
17b4e8c6
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
<
String
>
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
<
JSONObject
>
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
,
fieldJson
List
);
}
else
{
this
.
resultSet
=
new
RestfulResultSet
(
database
,
this
,
resultJson
);
}
...
...
src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/util/HttpClientPoolUtil.java
浏览文件 @
17b4e8c6
...
...
@@ -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
();
}
}
}
...
...
src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/SQLTest.java
浏览文件 @
17b4e8c6
...
...
@@ -189,43 +189,43 @@ public class SQLTest {
}
@Test
public
void
testCase30
()
{
public
void
testCase
0
30
()
{
String
sql
=
"select location, temperature, ts from restful_test.weather where ts > now"
;
executeQuery
(
sql
);
}
@Test
public
void
testCase31
()
{
public
void
testCase
0
31
()
{
String
sql
=
"select location, temperature, ts from restful_test.weather where ts < now"
;
executeQuery
(
sql
);
}
@Test
public
void
testCase32
()
{
public
void
testCase
0
32
()
{
String
sql
=
"select count(*) from restful_test.weather"
;
executeQuery
(
sql
);
}
@Test
public
void
testCase33
()
{
public
void
testCase
0
33
()
{
String
sql
=
"select first(*) from restful_test.weather"
;
executeQuery
(
sql
);
}
@Test
public
void
testCase34
()
{
public
void
testCase
0
34
()
{
String
sql
=
"select last(*) from restful_test.weather"
;
executeQuery
(
sql
);
}
@Test
public
void
testCase35
()
{
public
void
testCase
0
35
()
{
String
sql
=
"select last_row(*) from restful_test.weather"
;
executeQuery
(
sql
);
}
@Test
public
void
testCase36
()
{
public
void
testCase
0
36
()
{
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
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录