Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
2add95e9
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
2add95e9
编写于
10月 24, 2019
作者:
X
xieyinglin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
format the code
上级
76f82481
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
451 addition
and
439 deletion
+451
-439
src/connector/jdbc/src/main/java/com/taosdata/jdbc/SavedPreparedStatement.java
...c/main/java/com/taosdata/jdbc/SavedPreparedStatement.java
+58
-48
src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
...rc/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
+391
-389
src/connector/jdbc/src/main/java/com/taosdata/jdbc/bean/TSDBPreparedParam.java
...c/main/java/com/taosdata/jdbc/bean/TSDBPreparedParam.java
+2
-2
未找到文件。
src/connector/jdbc/src/main/java/com/taosdata/jdbc/SavedPreparedStatement.java
浏览文件 @
2add95e9
...
...
@@ -33,9 +33,9 @@ public class SavedPreparedStatement {
/**
* insert or import sql template pattern, the template are the following:
*
*
<p>
* insert/import into tableName [(field1, field2, ...)] [using stables tags(?, ?, ...) ] values(?, ?, ...) (?, ?, ...)
*
*
<p>
* we split it to three part:
* 1. prefix, insert/import
* 2. middle, tableName [(field1, field2, ...)] [using stables tags(?, ?, ...) ]
...
...
@@ -70,9 +70,9 @@ public class SavedPreparedStatement {
/**
* default param value
*/
private
static
final
String
DEFAULT_VALUE
=
"NULL"
;
private
static
final
String
DEFAULT_VALUE
=
"NULL"
;
private
static
final
String
PLACEHOLDER
=
"?"
;
private
static
final
String
PLACEHOLDER
=
"?"
;
private
String
tableName
;
...
...
@@ -81,7 +81,7 @@ public class SavedPreparedStatement {
*/
private
boolean
isAddBatch
;
public
SavedPreparedStatement
(
String
sql
,
TSDBPreparedStatement
tsdbPreparedStatement
)
throws
SQLException
{
public
SavedPreparedStatement
(
String
sql
,
TSDBPreparedStatement
tsdbPreparedStatement
)
throws
SQLException
{
this
.
sql
=
sql
;
this
.
tsdbPreparedStatement
=
tsdbPreparedStatement
;
this
.
sqlParamList
=
new
ArrayList
<>();
...
...
@@ -91,17 +91,18 @@ public class SavedPreparedStatement {
/**
* parse the init param according the sql param
*
* @param sql
*/
private
void
parsePreparedParam
(
String
sql
)
throws
SQLException
{
private
void
parsePreparedParam
(
String
sql
)
throws
SQLException
{
Matcher
matcher
=
sqlPattern
.
matcher
(
sql
);
if
(
matcher
.
find
())
{
if
(
matcher
.
find
())
{
tableName
=
matcher
.
group
(
"tablename"
);
if
(
tableName
!=
null
&&
PLACEHOLDER
.
equals
(
tableName
)){
if
(
tableName
!=
null
&&
PLACEHOLDER
.
equals
(
tableName
))
{
// the table name is dynamic
this
.
isTableNameDynamic
=
true
;
}
...
...
@@ -110,26 +111,26 @@ public class SavedPreparedStatement {
middle
=
matcher
.
group
(
2
);
valueList
=
matcher
.
group
(
"valueList"
);
if
(
middle
!=
null
&&
!
""
.
equals
(
middle
))
{
if
(
middle
!=
null
&&
!
""
.
equals
(
middle
))
{
middleParamSize
=
parsePlaceholder
(
middle
);
}
if
(
valueList
!=
null
&&
!
""
.
equals
(
valueList
))
{
if
(
valueList
!=
null
&&
!
""
.
equals
(
valueList
))
{
valueListSize
=
parsePlaceholder
(
valueList
);
}
initPreparedParam
=
initDefaultParam
(
tableName
,
middleParamSize
,
valueListSize
);
}
else
{
}
else
{
// not match
throw
new
SQLException
(
TSDBConstants
.
WrapErrMsg
(
"the sql is not complete!"
));
}
}
private
TSDBPreparedParam
initDefaultParam
(
String
tableName
,
int
middleParamSize
,
int
valueListSize
){
private
TSDBPreparedParam
initDefaultParam
(
String
tableName
,
int
middleParamSize
,
int
valueListSize
)
{
TSDBPreparedParam
tsdbPreparedParam
=
new
TSDBPreparedParam
(
tableName
);
TSDBPreparedParam
tsdbPreparedParam
=
new
TSDBPreparedParam
(
tableName
);
tsdbPreparedParam
.
setMiddleParamList
(
getDefaultParamList
(
middleParamSize
));
...
...
@@ -140,14 +141,15 @@ public class SavedPreparedStatement {
/**
* generate the default param value list
*
* @param paramSize
* @return
*/
private
List
<
Object
>
getDefaultParamList
(
int
paramSize
){
private
List
<
Object
>
getDefaultParamList
(
int
paramSize
)
{
List
<
Object
>
paramList
=
new
ArrayList
<>(
paramSize
);
if
(
paramSize
>
0
){
for
(
int
i
=
0
;
i
<
paramSize
;
i
++){
if
(
paramSize
>
0
)
{
for
(
int
i
=
0
;
i
<
paramSize
;
i
++)
{
paramList
.
add
(
i
,
DEFAULT_VALUE
);
}
}
...
...
@@ -157,17 +159,18 @@ public class SavedPreparedStatement {
/**
* calculate the placeholder num
*
* @param value
* @return
*/
private
int
parsePlaceholder
(
String
value
){
private
int
parsePlaceholder
(
String
value
)
{
Pattern
pattern
=
Pattern
.
compile
(
"[?]"
);
Matcher
matcher
=
pattern
.
matcher
(
value
);
int
result
=
0
;
while
(
matcher
.
find
()){
while
(
matcher
.
find
())
{
result
++;
}
return
result
;
...
...
@@ -175,34 +178,35 @@ public class SavedPreparedStatement {
/**
* set current row params
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
*/
public
void
setParam
(
int
parameterIndex
,
Object
x
)
throws
SQLException
{
public
void
setParam
(
int
parameterIndex
,
Object
x
)
throws
SQLException
{
int
paramSize
=
this
.
middleParamSize
+
this
.
valueListSize
;
String
errorMsg
=
String
.
format
(
"the parameterIndex %s out of the range [1, %s]"
,
parameterIndex
,
this
.
middleParamSize
+
this
.
valueListSize
);
if
(
parameterIndex
<
1
||
parameterIndex
>
paramSize
){
if
(
parameterIndex
<
1
||
parameterIndex
>
paramSize
)
{
throw
new
SQLException
(
TSDBConstants
.
WrapErrMsg
(
errorMsg
));
}
this
.
isAddBatch
=
false
;
//set isAddBatch to false
if
(
x
==
null
){
if
(
x
==
null
)
{
x
=
DEFAULT_VALUE
;
// set default null string
}
parameterIndex
=
parameterIndex
-
1
;
// start from 0 in param list
parameterIndex
=
parameterIndex
-
1
;
// start from 0 in param list
if
(
this
.
middleParamSize
!=
0
&&
parameterIndex
>=
0
&&
parameterIndex
<
this
.
middleParamSize
){
if
(
this
.
middleParamSize
!=
0
&&
parameterIndex
>=
0
&&
parameterIndex
<
this
.
middleParamSize
)
{
this
.
initPreparedParam
.
setMiddleParam
(
parameterIndex
,
x
);
return
;
}
if
(
this
.
valueListSize
!=
0
&&
parameterIndex
>=
this
.
middleParamSize
&&
parameterIndex
<
(
this
.
middleParamSize
+
this
.
valueListSize
))
{
if
(
this
.
valueListSize
!=
0
&&
parameterIndex
>=
this
.
middleParamSize
&&
parameterIndex
<
paramSize
)
{
this
.
initPreparedParam
.
setValueParam
(
parameterIndex
-
this
.
middleParamSize
,
x
);
return
;
...
...
@@ -220,9 +224,9 @@ public class SavedPreparedStatement {
/**
* add current param to batch list
*/
private
void
addCurrentRowParamToList
(){
private
void
addCurrentRowParamToList
()
{
if
(
initPreparedParam
!=
null
&&
(
this
.
middleParamSize
>
0
||
this
.
valueListSize
>
0
))
{
if
(
initPreparedParam
!=
null
&&
(
this
.
middleParamSize
>
0
||
this
.
valueListSize
>
0
))
{
this
.
sqlParamList
.
add
(
initPreparedParam
);
// add current param to batch list
}
this
.
isAddBatch
=
true
;
...
...
@@ -231,6 +235,7 @@ public class SavedPreparedStatement {
/**
* execute the sql with batch sql
*
* @return
* @throws SQLException
*/
...
...
@@ -238,13 +243,13 @@ public class SavedPreparedStatement {
int
result
=
executeBatchInternal
();
return
new
int
[]{
result
};
return
new
int
[]{
result
};
}
public
int
executeBatchInternal
()
throws
SQLException
{
public
int
executeBatchInternal
()
throws
SQLException
{
if
(!
isAddBatch
){
if
(!
isAddBatch
)
{
addCurrentRowParamToList
();
// add current param to batch list
}
...
...
@@ -262,26 +267,27 @@ public class SavedPreparedStatement {
/**
* generate the batch sql
*
* @return
*/
private
String
generateExecuteSql
(){
private
String
generateExecuteSql
()
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
stringBuilder
.
append
(
prefix
);
stringBuilder
.
append
(
" into "
);
if
(!
isTableNameDynamic
){
if
(!
isTableNameDynamic
)
{
// tablename will not need to be replaced
stringBuilder
.
append
(
middle
);
stringBuilder
.
append
(
" values"
);
stringBuilder
.
append
(
replaceValueListParam
(
valueList
,
sqlParamList
));
}
else
{
}
else
{
// need to replace tablename
if
(
sqlParamList
.
size
()
>
0
)
{
if
(
sqlParamList
.
size
()
>
0
)
{
TSDBPreparedParam
firstPreparedParam
=
sqlParamList
.
get
(
0
);
...
...
@@ -292,12 +298,12 @@ public class SavedPreparedStatement {
//the first param in the middleParamList is the tableName
String
lastTableName
=
firstPreparedParam
.
getMiddleParamList
().
get
(
0
).
toString
();
if
(
sqlParamList
.
size
()
>
1
){
if
(
sqlParamList
.
size
()
>
1
)
{
for
(
int
i
=
1
;
i
<
sqlParamList
.
size
();
i
++){
for
(
int
i
=
1
;
i
<
sqlParamList
.
size
();
i
++)
{
TSDBPreparedParam
currentParam
=
sqlParamList
.
get
(
i
);
String
currentTableName
=
currentParam
.
getMiddleParamList
().
get
(
0
).
toString
();
if
(
lastTableName
.
equalsIgnoreCase
(
currentTableName
)){
if
(
lastTableName
.
equalsIgnoreCase
(
currentTableName
))
{
// tablename is same with the last row ,so only need to append the part of value
String
values
=
replaceTemplateParam
(
valueList
,
currentParam
.
getValueList
());
...
...
@@ -313,7 +319,7 @@ public class SavedPreparedStatement {
}
}
}
else
{
}
else
{
stringBuilder
.
append
(
middle
);
stringBuilder
.
append
(
" values"
);
...
...
@@ -327,10 +333,11 @@ public class SavedPreparedStatement {
/**
* replace the middle and value part
*
* @param tsdbPreparedParam
* @return
*/
private
String
replaceMiddleAndValuePart
(
TSDBPreparedParam
tsdbPreparedParam
){
private
String
replaceMiddleAndValuePart
(
TSDBPreparedParam
tsdbPreparedParam
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
(
" "
);
...
...
@@ -348,17 +355,18 @@ public class SavedPreparedStatement {
/**
* replace the placeholder of the template with TSDBPreparedParam list
*
* @param template
* @param sqlParamList
* @return
*/
private
String
replaceValueListParam
(
String
template
,
List
<
TSDBPreparedParam
>
sqlParamList
){
private
String
replaceValueListParam
(
String
template
,
List
<
TSDBPreparedParam
>
sqlParamList
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
if
(
sqlParamList
.
size
()
>
0
)
{
if
(
sqlParamList
.
size
()
>
0
)
{
for
(
TSDBPreparedParam
tsdbPreparedParam
:
sqlParamList
){
for
(
TSDBPreparedParam
tsdbPreparedParam
:
sqlParamList
)
{
String
tmp
=
replaceTemplateParam
(
template
,
tsdbPreparedParam
.
getValueList
());
...
...
@@ -374,13 +382,14 @@ public class SavedPreparedStatement {
/**
* replace the placeholder of the template with paramList
*
* @param template
* @param paramList
* @return
*/
private
String
replaceTemplateParam
(
String
template
,
List
<
Object
>
paramList
){
private
String
replaceTemplateParam
(
String
template
,
List
<
Object
>
paramList
)
{
if
(
paramList
.
size
()
>
0
){
if
(
paramList
.
size
()
>
0
)
{
String
tmp
=
template
;
...
...
@@ -402,20 +411,21 @@ public class SavedPreparedStatement {
/**
* get the string of param object
*
* @param paramObj
* @return
*/
private
String
getParamString
(
Object
paramObj
){
private
String
getParamString
(
Object
paramObj
)
{
String
paraStr
=
paramObj
.
toString
();
if
(
paramObj
instanceof
Timestamp
||
(
paramObj
instanceof
String
&&
!
DEFAULT_VALUE
.
equalsIgnoreCase
(
paraStr
)))
{
paraStr
=
"'"
+
paraStr
+
"'"
;
}
return
paraStr
;
return
paraStr
;
}
private
int
executeSql
(
String
sql
)
throws
SQLException
{
private
int
executeSql
(
String
sql
)
throws
SQLException
{
return
tsdbPreparedStatement
.
executeUpdate
(
sql
);
}
...
...
src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
浏览文件 @
2add95e9
此差异已折叠。
点击以展开。
src/connector/jdbc/src/main/java/com/taosdata/jdbc/bean/TSDBPreparedParam.java
浏览文件 @
2add95e9
...
...
@@ -42,7 +42,7 @@ public class TSDBPreparedParam {
this
.
middleParamList
=
middleParamList
;
}
public
void
setMiddleParam
(
int
parameterIndex
,
Object
x
){
public
void
setMiddleParam
(
int
parameterIndex
,
Object
x
)
{
this
.
middleParamList
.
set
(
parameterIndex
,
x
);
}
...
...
@@ -55,7 +55,7 @@ public class TSDBPreparedParam {
}
public
void
setValueParam
(
int
parameterIndex
,
Object
x
){
public
void
setValueParam
(
int
parameterIndex
,
Object
x
)
{
this
.
valueList
.
set
(
parameterIndex
,
x
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录