Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
W
workflow
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
2
议题
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄夏豪
workflow
Commits
7f21deb8
提交
7f21deb8
authored
5月 14, 2021
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[数据源模块]初始提交
上级
4a54f0a7
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
27 行增加
和
23 行删除
+27
-23
DataSourceInfoRepository.java
.../workflowcore/ds/repository/DataSourceInfoRepository.java
+1
-4
DataOperation.java
.../java/com/tykj/workflowcore/ds/service/DataOperation.java
+26
-19
没有找到文件。
src/main/java/com/tykj/workflowcore/ds/repository/DataSourceInfoRepository.java
浏览文件 @
7f21deb8
...
...
@@ -2,9 +2,6 @@ package com.tykj.workflowcore.ds.repository;
import
com.tykj.workflowcore.ds.entity.DataSourceInfo
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.stereotype.Repository
;
@Repository
public
interface
DataSourceInfoRepository
extends
JpaRepository
<
DataSourceInfo
,
Integer
>
{
public
interface
DataSourceInfoRepository
extends
JpaRepository
<
DataSourceInfo
,
Integer
>
{
}
src/main/java/com/tykj/workflowcore/ds/service/DataOperation.java
浏览文件 @
7f21deb8
package
com
.
tykj
.
workflowcore
.
ds
.
service
;
import
com.tykj.workflowcore.model_layer.entity.ColumnInfo
;
import
com.tykj.workflowcore.model_layer.entity.TableInfo
;
import
com.tykj.workflowcore.model_layer.entity.vo.ColumnVO
;
import
com.tykj.workflowcore.model_layer.entity.vo.TableVO
;
import
com.tykj.workflowcore.model_layer.service.ModelService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.logging.log4j.util.Strings
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jdbc.core.JdbcTemplate
;
...
...
@@ -19,8 +18,9 @@ import static java.lang.String.format;
import
static
java
.
util
.
Objects
.
nonNull
;
/**
*
*
用于数据同步的数据操作执行者
*/
@Slf4j
@Service
public
class
DataOperation
{
...
...
@@ -31,13 +31,10 @@ public class DataOperation {
@Autowired
private
ModelService
modelService
;
public
void
copyTableFromDataSource
(
String
tableName
,
String
dataSourceName
)
{
dataSourceManager
.
switchToDataSource
(
dataSourceName
);
String
sql
=
String
.
valueOf
(
jdbcTemplate
.
queryForMap
(
format
(
"show create table %s"
,
tableName
)).
get
(
"sql"
));
dataSourceManager
.
clear
();
jdbcTemplate
.
execute
(
sql
);
}
/**
* 同步外界数据源中所有表结构
* @param dataSourceName 指定数据源的名字
*/
public
void
syncAllTablesFromDataSource
(
String
dataSourceName
)
{
dataSourceManager
.
switchToDataSource
(
dataSourceName
);
List
<
String
>
tableNames
=
getAllTableNames
();
...
...
@@ -90,31 +87,37 @@ public class DataOperation {
);
}
public
void
syncData
(
String
tableName
)
{
List
<
Map
<
String
,
Object
>>
dataList
=
jdbcTemplate
.
queryForList
(
format
(
"select * from %s"
),
tableName
);
public
void
syncData
(
String
dataSourceName
,
String
tableName
)
{
dataSourceManager
.
switchToDataSource
(
dataSourceName
);
List
<
Map
<
String
,
Object
>>
dataList
=
getData
(
tableName
);
dataSourceManager
.
clear
();
clearTable
(
tableName
);
for
(
Map
<
String
,
Object
>
data
:
dataList
)
{
String
sql
=
insertSql
(
tableName
,
data
);
log
.
info
(
"execute : {}"
,
sql
);
jdbcTemplate
.
execute
(
sql
);
}
}
private
List
<
Map
<
String
,
Object
>>
getData
(
String
tableName
)
{
return
jdbcTemplate
.
queryForList
(
format
(
"select * from %s"
,
tableName
));
}
private
String
insertSql
(
String
tableName
,
Map
<
String
,
Object
>
data
)
{
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
"insert in
f
o "
).
append
(
tableName
).
append
(
" "
);
result
.
append
(
"insert in
t
o "
).
append
(
tableName
).
append
(
" "
);
StringBuilder
names
=
new
StringBuilder
();
names
.
append
(
"("
);
StringBuilder
values
=
new
StringBuilder
();
values
.
append
(
"("
);
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
data
.
entrySet
())
{
if
(
nonNull
(
entry
.
getValue
())){
if
(
nonNull
(
entry
.
getValue
()))
{
names
.
append
(
entry
.
getKey
()).
append
(
","
);
values
.
append
(
entry
.
getValue
()
).
append
(
","
);
values
.
append
(
"'"
).
append
(
entry
.
getValue
()).
append
(
"'"
).
append
(
","
);
}
}
names
.
delete
(
names
.
indexOf
(
","
),
names
.
i
ndexOf
(
","
)
+
1
);
values
.
delete
(
values
.
indexOf
(
","
),
values
.
i
ndexOf
(
","
)
+
1
);
names
.
delete
(
names
.
lastIndexOf
(
","
),
names
.
lastI
ndexOf
(
","
)
+
1
);
values
.
delete
(
values
.
lastIndexOf
(
","
),
values
.
lastI
ndexOf
(
","
)
+
1
);
names
.
append
(
")"
);
values
.
append
(
")"
);
result
.
append
(
names
);
...
...
@@ -123,4 +126,8 @@ public class DataOperation {
return
result
.
toString
();
}
private
void
clearTable
(
String
tableName
)
{
jdbcTemplate
.
execute
(
format
(
"truncate table %s"
,
tableName
));
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论