Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
suliangchun
dbeaver
提交
7df53159
D
dbeaver
项目概览
suliangchun
/
dbeaver
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dbeaver
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7df53159
编写于
6月 13, 2014
作者:
J
jurgen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cassandra data update + value handlers.
Execution batch refactoring.
上级
27188b98
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
175 addition
and
135 deletion
+175
-135
plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/model/impl/data/ExecuteBatchImpl.java
...c/org/jkiss/dbeaver/model/impl/data/ExecuteBatchImpl.java
+171
-0
plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/model/impl/jdbc/struct/JDBCTable.java
...c/org/jkiss/dbeaver/model/impl/jdbc/struct/JDBCTable.java
+4
-135
未找到文件。
plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/model/impl/data/ExecuteBatchImpl.java
0 → 100644
浏览文件 @
7df53159
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package
org.jkiss.dbeaver.model.impl.data
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.jkiss.code.NotNull
;
import
org.jkiss.code.Nullable
;
import
org.jkiss.dbeaver.model.DBUtils
;
import
org.jkiss.dbeaver.model.data.DBDDataReceiver
;
import
org.jkiss.dbeaver.model.data.DBDValueHandler
;
import
org.jkiss.dbeaver.model.exec.*
;
import
org.jkiss.dbeaver.model.struct.DBSAttributeBase
;
import
org.jkiss.dbeaver.model.struct.DBSDataManipulator
;
import
org.jkiss.utils.ArrayUtils
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
/**
* Execute batch.
* Can be used in JDBC or any other underlying DB APIs
*
*/
public
class
ExecuteBatchImpl
implements
DBSDataManipulator
.
ExecuteBatch
{
static
final
Log
log
=
LogFactory
.
getLog
(
ExecuteBatchImpl
.
class
);
private
DBCStatement
statement
;
private
final
DBSAttributeBase
[]
attributes
;
private
final
List
<
Object
[]>
values
=
new
ArrayList
<
Object
[]>();
private
final
DBDDataReceiver
keysReceiver
;
private
final
boolean
skipSequences
;
public
ExecuteBatchImpl
(
@NotNull
DBCStatement
statement
,
@NotNull
DBSAttributeBase
[]
attributes
,
@Nullable
DBDDataReceiver
keysReceiver
,
boolean
skipSequences
)
{
this
.
statement
=
statement
;
this
.
attributes
=
attributes
;
this
.
keysReceiver
=
keysReceiver
;
this
.
skipSequences
=
skipSequences
;
}
@Override
public
void
add
(
@NotNull
Object
[]
attributeValues
)
throws
DBCException
{
if
(!
ArrayUtils
.
isEmpty
(
attributes
)
&&
ArrayUtils
.
isEmpty
(
attributeValues
))
{
throw
new
DBCException
(
"Bad attribute values: "
+
Arrays
.
toString
(
attributeValues
));
}
values
.
add
(
attributeValues
);
}
@NotNull
@Override
public
DBCStatistics
execute
()
throws
DBCException
{
if
(
statement
==
null
)
{
throw
new
DBCException
(
"Execute batch closed"
);
}
DBDValueHandler
[]
handlers
=
new
DBDValueHandler
[
attributes
.
length
];
for
(
int
i
=
0
;
i
<
attributes
.
length
;
i
++)
{
handlers
[
i
]
=
DBUtils
.
findValueHandler
(
statement
.
getSession
(),
attributes
[
i
]);
}
boolean
useBatch
=
statement
.
getSession
().
getDataSource
().
getInfo
().
supportsBatchUpdates
();
if
(
values
.
size
()
<=
1
)
{
useBatch
=
false
;
}
DBCStatistics
statistics
=
new
DBCStatistics
();
statistics
.
setQueryText
(
statement
.
getQueryString
());
for
(
Object
[]
rowValues
:
values
)
{
int
paramIndex
=
0
;
for
(
int
k
=
0
;
k
<
handlers
.
length
;
k
++)
{
DBDValueHandler
handler
=
handlers
[
k
];
DBSAttributeBase
attribute
=
attributes
[
k
];
if
(
skipSequences
&&
(
attribute
.
isPseudoAttribute
()
||
attribute
.
isAutoGenerated
()))
{
continue
;
}
handler
.
bindValueObject
(
statement
.
getSession
(),
statement
,
attribute
,
paramIndex
++,
rowValues
[
k
]);
}
if
(
useBatch
)
{
statement
.
addToBatch
();
}
else
{
// Execute each row separately
long
startTime
=
System
.
currentTimeMillis
();
statement
.
executeStatement
();
statistics
.
addExecuteTime
(
System
.
currentTimeMillis
()
-
startTime
);
long
rowCount
=
statement
.
getUpdateRowCount
();
if
(
rowCount
>
0
)
{
statistics
.
addRowsUpdated
(
rowCount
);
}
// Read keys
if
(
keysReceiver
!=
null
)
{
readKeys
(
statement
.
getSession
(),
statement
,
keysReceiver
);
}
}
}
values
.
clear
();
if
(
useBatch
)
{
// Process batch
long
startTime
=
System
.
currentTimeMillis
();
int
[]
updatedRows
=
statement
.
executeStatementBatch
();
statistics
.
addExecuteTime
(
System
.
currentTimeMillis
()
-
startTime
);
if
(!
ArrayUtils
.
isEmpty
(
updatedRows
))
{
for
(
int
rows
:
updatedRows
)
{
statistics
.
addRowsUpdated
(
rows
);
}
}
}
return
statistics
;
}
@Override
public
void
close
()
{
statement
.
close
();
statement
=
null
;
}
private
void
readKeys
(
@NotNull
DBCSession
session
,
@NotNull
DBCStatement
dbStat
,
@NotNull
DBDDataReceiver
keysReceiver
)
throws
DBCException
{
DBCResultSet
dbResult
;
try
{
dbResult
=
dbStat
.
openGeneratedKeysResultSet
();
}
catch
(
Throwable
e
)
{
log
.
debug
(
"Error obtaining generated keys"
,
e
);
//$NON-NLS-1$
return
;
}
if
(
dbResult
==
null
)
{
return
;
}
try
{
keysReceiver
.
fetchStart
(
session
,
dbResult
);
try
{
while
(
dbResult
.
nextRow
())
{
keysReceiver
.
fetchRow
(
session
,
dbResult
);
}
}
finally
{
keysReceiver
.
fetchEnd
(
session
);
}
}
finally
{
dbResult
.
close
();
keysReceiver
.
close
();
}
}
}
plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/model/impl/jdbc/struct/JDBCTable.java
浏览文件 @
7df53159
...
...
@@ -30,6 +30,7 @@ import org.jkiss.dbeaver.model.DBUtils;
import
org.jkiss.dbeaver.model.data.*
;
import
org.jkiss.dbeaver.model.exec.*
;
import
org.jkiss.dbeaver.model.impl.DBObjectNameCaseTransformer
;
import
org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl
;
import
org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCStructCache
;
import
org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData
;
import
org.jkiss.dbeaver.model.impl.struct.AbstractTable
;
...
...
@@ -44,8 +45,6 @@ import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import
org.jkiss.utils.ArrayUtils
;
import
org.jkiss.utils.CommonUtils
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
/**
...
...
@@ -328,7 +327,7 @@ public abstract class JDBCTable<DATASOURCE extends DBPDataSource, CONTAINER exte
DBCStatement
dbStat
=
session
.
prepareStatement
(
DBCStatementType
.
QUERY
,
query
.
toString
(),
false
,
false
,
keysReceiver
!=
null
);
dbStat
.
setStatementSource
(
this
);
return
new
BatchImpl
(
dbStat
,
attributes
,
keysReceiver
,
true
);
return
new
Execute
BatchImpl
(
dbStat
,
attributes
,
keysReceiver
,
true
);
}
@NotNull
...
...
@@ -378,7 +377,7 @@ public abstract class JDBCTable<DATASOURCE extends DBPDataSource, CONTAINER exte
DBSAttributeBase
[]
attributes
=
ArrayUtils
.
concatArrays
(
updateAttributes
,
keyAttributes
);
return
new
BatchImpl
(
dbStat
,
attributes
,
keysReceiver
,
false
);
return
new
Execute
BatchImpl
(
dbStat
,
attributes
,
keysReceiver
,
false
);
}
@NotNull
...
...
@@ -412,7 +411,7 @@ public abstract class JDBCTable<DATASOURCE extends DBPDataSource, CONTAINER exte
// Execute
DBCStatement
dbStat
=
session
.
prepareStatement
(
DBCStatementType
.
QUERY
,
query
.
toString
(),
false
,
false
,
false
);
dbStat
.
setStatementSource
(
this
);
return
new
BatchImpl
(
dbStat
,
keyAttributes
,
null
,
false
);
return
new
Execute
BatchImpl
(
dbStat
,
keyAttributes
,
null
,
false
);
}
private
String
getAttributeName
(
@NotNull
DBSAttributeBase
attribute
)
{
...
...
@@ -465,37 +464,6 @@ public abstract class JDBCTable<DATASOURCE extends DBPDataSource, CONTAINER exte
query
.
append
(
"=?"
);
//$NON-NLS-1$
}
private
void
readKeys
(
@NotNull
DBCSession
session
,
@NotNull
DBCStatement
dbStat
,
@NotNull
DBDDataReceiver
keysReceiver
)
throws
DBCException
{
DBCResultSet
dbResult
;
try
{
dbResult
=
dbStat
.
openGeneratedKeysResultSet
();
}
catch
(
Throwable
e
)
{
log
.
debug
(
"Error obtaining generated keys"
,
e
);
//$NON-NLS-1$
return
;
}
if
(
dbResult
==
null
)
{
return
;
}
try
{
keysReceiver
.
fetchStart
(
session
,
dbResult
);
try
{
while
(
dbResult
.
nextRow
())
{
keysReceiver
.
fetchRow
(
session
,
dbResult
);
}
}
finally
{
keysReceiver
.
fetchEnd
(
session
);
}
}
finally
{
dbResult
.
close
();
keysReceiver
.
close
();
}
}
/**
* Reads and caches metadata which is required for data requests
* @param monitor progress monitor
...
...
@@ -512,103 +480,4 @@ public abstract class JDBCTable<DATASOURCE extends DBPDataSource, CONTAINER exte
}
}
private
class
BatchImpl
implements
ExecuteBatch
{
private
DBCStatement
statement
;
private
final
DBSAttributeBase
[]
attributes
;
private
final
List
<
Object
[]>
values
=
new
ArrayList
<
Object
[]>();
private
final
DBDDataReceiver
keysReceiver
;
private
final
boolean
skipSequences
;
private
BatchImpl
(
@NotNull
DBCStatement
statement
,
@NotNull
DBSAttributeBase
[]
attributes
,
@Nullable
DBDDataReceiver
keysReceiver
,
boolean
skipSequences
)
{
this
.
statement
=
statement
;
this
.
attributes
=
attributes
;
this
.
keysReceiver
=
keysReceiver
;
this
.
skipSequences
=
skipSequences
;
}
@Override
public
void
add
(
@NotNull
Object
[]
attributeValues
)
throws
DBCException
{
if
(!
ArrayUtils
.
isEmpty
(
attributes
)
&&
ArrayUtils
.
isEmpty
(
attributeValues
))
{
throw
new
DBCException
(
"Bad attribute values: "
+
Arrays
.
toString
(
attributeValues
));
}
values
.
add
(
attributeValues
);
}
@NotNull
@Override
public
DBCStatistics
execute
()
throws
DBCException
{
if
(
statement
==
null
)
{
throw
new
DBCException
(
"Execute batch closed"
);
}
DBDValueHandler
[]
handlers
=
new
DBDValueHandler
[
attributes
.
length
];
for
(
int
i
=
0
;
i
<
attributes
.
length
;
i
++)
{
handlers
[
i
]
=
DBUtils
.
findValueHandler
(
statement
.
getSession
(),
attributes
[
i
]);
}
boolean
useBatch
=
statement
.
getSession
().
getDataSource
().
getInfo
().
supportsBatchUpdates
();
if
(
values
.
size
()
<=
1
)
{
useBatch
=
false
;
}
DBCStatistics
statistics
=
new
DBCStatistics
();
statistics
.
setQueryText
(
statement
.
getQueryString
());
for
(
Object
[]
rowValues
:
values
)
{
int
paramIndex
=
0
;
for
(
int
k
=
0
;
k
<
handlers
.
length
;
k
++)
{
DBDValueHandler
handler
=
handlers
[
k
];
DBSAttributeBase
attribute
=
attributes
[
k
];
if
(
skipSequences
&&
(
attribute
.
isPseudoAttribute
()
||
attribute
.
isAutoGenerated
()))
{
continue
;
}
handler
.
bindValueObject
(
statement
.
getSession
(),
statement
,
attribute
,
paramIndex
++,
rowValues
[
k
]);
}
if
(
useBatch
)
{
statement
.
addToBatch
();
}
else
{
// Execute each row separately
long
startTime
=
System
.
currentTimeMillis
();
statement
.
executeStatement
();
statistics
.
addExecuteTime
(
System
.
currentTimeMillis
()
-
startTime
);
long
rowCount
=
statement
.
getUpdateRowCount
();
if
(
rowCount
>
0
)
{
statistics
.
addRowsUpdated
(
rowCount
);
}
// Read keys
if
(
keysReceiver
!=
null
)
{
readKeys
(
statement
.
getSession
(),
statement
,
keysReceiver
);
}
}
}
values
.
clear
();
if
(
useBatch
)
{
// Process batch
long
startTime
=
System
.
currentTimeMillis
();
int
[]
updatedRows
=
statement
.
executeStatementBatch
();
statistics
.
addExecuteTime
(
System
.
currentTimeMillis
()
-
startTime
);
if
(!
ArrayUtils
.
isEmpty
(
updatedRows
))
{
for
(
int
rows
:
updatedRows
)
{
statistics
.
addRowsUpdated
(
rows
);
}
}
}
return
statistics
;
}
@Override
public
void
close
()
{
statement
.
close
();
statement
=
null
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录