Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
d03b5c43
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d03b5c43
编写于
11月 02, 2021
作者:
Z
Zhiyu Yang
提交者:
GitHub
11月 02, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[TS-531]<feature>: jdbc parameter bind multi tables (#8537)
上级
608c3af3
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
167 addition
and
36 deletion
+167
-36
src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
...rc/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
+27
-21
src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java
...main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java
+1
-7
src/connector/jdbc/src/test/java/com/taosdata/jdbc/ParameterBindTest.java
...bc/src/test/java/com/taosdata/jdbc/ParameterBindTest.java
+139
-0
src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/HttpClientPoolUtilTest.java
.../java/com/taosdata/jdbc/utils/HttpClientPoolUtilTest.java
+0
-8
未找到文件。
src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
浏览文件 @
d03b5c43
...
...
@@ -36,15 +36,15 @@ import java.util.regex.Pattern;
* compatibility needs.
*/
public
class
TSDBPreparedStatement
extends
TSDBStatement
implements
PreparedStatement
{
// for jdbc preparedStatement interface
private
String
rawSql
;
private
Object
[]
parameters
;
private
ArrayList
<
ColumnInfo
>
colData
;
// for parameter binding
private
long
nativeStmtHandle
=
0
;
private
String
tableName
;
private
ArrayList
<
TableTagInfo
>
tableTags
;
private
int
tagValueLength
;
private
String
tableName
;
private
long
nativeStmtHandle
=
0
;
private
ArrayList
<
ColumnInfo
>
colData
;
TSDBPreparedStatement
(
TSDBConnection
connection
,
String
sql
)
{
super
(
connection
);
...
...
@@ -72,10 +72,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
preprocessSql
();
}
/*
*
*/
/**
* Some of the SQLs sent by other popular frameworks or tools like Spark, contains syntax that cannot be parsed by
* the TDengine client. Thus, some simple parsers/filters are intentionally added in this JDBC implementation in
...
...
@@ -250,13 +246,10 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
@Override
public
void
setObject
(
int
parameterIndex
,
Object
x
)
throws
SQLException
{
if
(
isClosed
())
{
if
(
isClosed
())
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_STATEMENT_CLOSED
);
}
if
(
parameterIndex
<
1
&&
parameterIndex
>=
parameters
.
length
)
{
if
(
parameterIndex
<
1
&&
parameterIndex
>=
parameters
.
length
)
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_PARAMETER_INDEX_OUT_RANGE
);
}
parameters
[
parameterIndex
-
1
]
=
x
;
}
...
...
@@ -335,7 +328,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
public
void
setTimestamp
(
int
parameterIndex
,
Timestamp
x
,
Calendar
cal
)
throws
SQLException
{
if
(
isClosed
())
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_STATEMENT_CLOSED
);
// TODO:
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_UNSUPPORTED_METHOD
);
}
...
...
@@ -419,7 +411,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
public
void
setObject
(
int
parameterIndex
,
Object
x
,
int
targetSqlType
,
int
scaleOrLength
)
throws
SQLException
{
if
(
isClosed
())
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_STATEMENT_CLOSED
);
//TODO:
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_UNSUPPORTED_METHOD
);
}
...
...
@@ -477,7 +468,6 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
if
(
isClosed
())
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_STATEMENT_CLOSED
);
throw
TSDBError
.
createSQLException
(
TSDBErrorNumbers
.
ERROR_UNSUPPORTED_METHOD
);
}
@Override
...
...
@@ -496,7 +486,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
///////////////////////////////////////////////////////////////////////
// NOTE: the following APIs are not JDBC compatible
//
set the bind table name
//
parameter binding
private
static
class
ColumnInfo
{
@SuppressWarnings
(
"rawtypes"
)
private
ArrayList
data
;
...
...
@@ -539,7 +529,11 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
}
}
public
void
setTableName
(
String
name
)
{
public
void
setTableName
(
String
name
)
throws
SQLException
{
if
(
this
.
tableName
!=
null
)
{
this
.
columnDataExecuteBatch
();
this
.
columnDataClearBatchInternal
();
}
this
.
tableName
=
name
;
}
...
...
@@ -960,17 +954,22 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
public
void
columnDataExecuteBatch
()
throws
SQLException
{
TSDBJNIConnector
connector
=
((
TSDBConnection
)
this
.
getConnection
()).
getConnector
();
connector
.
executeBatch
(
this
.
nativeStmtHandle
);
this
.
columnDataClearBatch
();
this
.
columnDataClearBatch
Internal
();
}
@Deprecated
public
void
columnDataClearBatch
()
{
columnDataClearBatchInternal
();
}
private
void
columnDataClearBatchInternal
()
{
int
size
=
this
.
colData
.
size
();
this
.
colData
.
clear
();
this
.
colData
.
addAll
(
Collections
.
nCopies
(
size
,
null
));
this
.
tableName
=
null
;
// clear the table name
}
public
void
columnDataCloseBatch
()
throws
SQLException
{
TSDBJNIConnector
connector
=
((
TSDBConnection
)
this
.
getConnection
()).
getConnector
();
connector
.
closeBatch
(
this
.
nativeStmtHandle
);
...
...
@@ -978,4 +977,11 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat
this
.
nativeStmtHandle
=
0L
;
this
.
tableName
=
null
;
}
@Override
public
void
close
()
throws
SQLException
{
this
.
columnDataClearBatchInternal
();
this
.
columnDataCloseBatch
();
super
.
close
();
}
}
src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java
浏览文件 @
d03b5c43
...
...
@@ -5,9 +5,7 @@ import com.taosdata.jdbc.TSDBErrorNumbers;
import
org.apache.http.HeaderElement
;
import
org.apache.http.HeaderElementIterator
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.NoHttpResponseException
;
import
org.apache.http.client.ClientProtocolException
;
import
org.apache.http.client.HttpRequestRetryHandler
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.methods.*
;
import
org.apache.http.client.protocol.HttpClientContext
;
...
...
@@ -21,10 +19,7 @@ import org.apache.http.protocol.HTTP;
import
org.apache.http.protocol.HttpContext
;
import
org.apache.http.util.EntityUtils
;
import
javax.net.ssl.SSLException
;
import
java.io.IOException
;
import
java.io.InterruptedIOException
;
import
java.net.UnknownHostException
;
import
java.nio.charset.StandardCharsets
;
import
java.sql.SQLException
;
...
...
@@ -53,10 +48,9 @@ public class HttpClientPoolUtil {
return
DEFAULT_HTTP_KEEP_TIME
*
1000
;
};
private
static
CloseableHttpClient
httpClient
;
private
static
final
CloseableHttpClient
httpClient
;
static
{
PoolingHttpClientConnectionManager
connectionManager
=
new
PoolingHttpClientConnectionManager
();
connectionManager
.
setMaxTotal
(
DEFAULT_MAX_TOTAL
);
connectionManager
.
setDefaultMaxPerRoute
(
DEFAULT_MAX_PER_ROUTE
);
...
...
src/connector/jdbc/src/test/java/com/taosdata/jdbc/ParameterBindTest.java
0 → 100644
浏览文件 @
d03b5c43
package
com.taosdata.jdbc
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.sql.*
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.stream.Collectors
;
public
class
ParameterBindTest
{
private
static
final
String
host
=
"127.0.0.1"
;
private
static
final
String
stable
=
"weather"
;
private
Connection
conn
;
private
final
Random
random
=
new
Random
(
System
.
currentTimeMillis
());
@Test
public
void
test
()
{
// given
String
[]
tbnames
=
{
"t1"
,
"t2"
,
"t3"
};
int
rows
=
10
;
// when
insertIntoTables
(
tbnames
,
10
);
// then
assertRows
(
stable
,
tbnames
.
length
*
rows
);
for
(
String
t
:
tbnames
)
{
assertRows
(
t
,
rows
);
}
}
@Test
public
void
testMultiThreads
()
{
// given
String
[][]
tables
=
{{
"t1"
,
"t2"
,
"t3"
},
{
"t4"
,
"t5"
,
"t6"
},
{
"t7"
,
"t8"
,
"t9"
},
{
"t10"
}};
int
rows
=
10
;
// when
List
<
Thread
>
threads
=
Arrays
.
stream
(
tables
).
map
(
tbnames
->
new
Thread
(()
->
insertIntoTables
(
tbnames
,
rows
))).
collect
(
Collectors
.
toList
());
threads
.
forEach
(
Thread:
:
start
);
for
(
Thread
thread
:
threads
)
{
try
{
thread
.
join
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
// then
for
(
String
[]
table
:
tables
)
{
for
(
String
t
:
table
)
{
assertRows
(
t
,
rows
);
}
}
}
private
void
assertRows
(
String
tbname
,
int
rows
)
{
try
(
Statement
stmt
=
conn
.
createStatement
())
{
ResultSet
rs
=
stmt
.
executeQuery
(
"select count(*) from "
+
tbname
);
while
(
rs
.
next
())
{
int
count
=
rs
.
getInt
(
1
);
Assert
.
assertEquals
(
rows
,
count
);
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
private
void
insertIntoTables
(
String
[]
tbnames
,
int
rowsEachTable
)
{
long
current
=
System
.
currentTimeMillis
();
String
sql
=
"insert into ? using "
+
stable
+
" tags(?, ?) values(?, ?, ?)"
;
try
(
TSDBPreparedStatement
pstmt
=
conn
.
prepareStatement
(
sql
).
unwrap
(
TSDBPreparedStatement
.
class
))
{
for
(
int
i
=
0
;
i
<
tbnames
.
length
;
i
++)
{
pstmt
.
setTableName
(
tbnames
[
i
]);
pstmt
.
setTagInt
(
0
,
random
.
nextInt
(
100
));
pstmt
.
setTagInt
(
1
,
random
.
nextInt
(
100
));
ArrayList
<
Long
>
timestampList
=
new
ArrayList
<>();
for
(
int
j
=
0
;
j
<
rowsEachTable
;
j
++)
{
timestampList
.
add
(
current
+
i
*
1000
+
j
);
}
pstmt
.
setTimestamp
(
0
,
timestampList
);
ArrayList
<
Integer
>
f1List
=
new
ArrayList
<>();
for
(
int
j
=
0
;
j
<
rowsEachTable
;
j
++)
{
f1List
.
add
(
random
.
nextInt
(
100
));
}
pstmt
.
setInt
(
1
,
f1List
);
ArrayList
<
Integer
>
f2List
=
new
ArrayList
<>();
for
(
int
j
=
0
;
j
<
rowsEachTable
;
j
++)
{
f2List
.
add
(
random
.
nextInt
(
100
));
}
pstmt
.
setInt
(
2
,
f2List
);
pstmt
.
columnDataAddBatch
();
}
pstmt
.
columnDataExecuteBatch
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
@Before
public
void
before
()
{
String
url
=
"jdbc:TAOS://"
+
host
+
":6030/?user=root&password=taosdata"
;
try
{
conn
=
DriverManager
.
getConnection
(
url
);
Statement
stmt
=
conn
.
createStatement
();
stmt
.
execute
(
"drop database if exists test_pd"
);
stmt
.
execute
(
"create database if not exists test_pd"
);
stmt
.
execute
(
"use test_pd"
);
stmt
.
execute
(
"create table "
+
stable
+
"(ts timestamp, f1 int, f2 int) tags(t1 int, t2 int)"
);
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
@After
public
void
after
()
{
try
{
// Statement stmt = conn.createStatement();
// stmt.execute("drop database if exists test_pd");
if
(
conn
!=
null
)
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/HttpClientPoolUtilTest.java
浏览文件 @
d03b5c43
...
...
@@ -2,7 +2,6 @@ package com.taosdata.jdbc.utils;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.taosdata.jdbc.TSDBDriver
;
import
com.taosdata.jdbc.TSDBError
;
import
org.junit.Test
;
...
...
@@ -11,7 +10,6 @@ import java.net.URLEncoder;
import
java.nio.charset.StandardCharsets
;
import
java.sql.SQLException
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
...
...
@@ -27,11 +25,6 @@ public class HttpClientPoolUtilTest {
// given
List
<
Thread
>
threads
=
IntStream
.
range
(
0
,
4000
).
mapToObj
(
i
->
new
Thread
(()
->
{
useDB
();
// try {
// TimeUnit.SECONDS.sleep(10);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
})).
collect
(
Collectors
.
toList
());
threads
.
forEach
(
Thread:
:
start
);
...
...
@@ -43,7 +36,6 @@ public class HttpClientPoolUtilTest {
e
.
printStackTrace
();
}
}
}
private
void
useDB
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录