Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
data-house
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄承天
data-house
Commits
c885a88b
提交
c885a88b
authored
10月 25, 2021
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
初始提交
上级
be225009
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
24 行增加
和
18 行删除
+24
-18
TableInfoController.java
...va/com/tykj/datahouse/controller/TableInfoController.java
+4
-3
ColumnInfo.java
src/main/java/com/tykj/datahouse/entity/ColumnInfo.java
+0
-1
TableInfoService.java
...ain/java/com/tykj/datahouse/service/TableInfoService.java
+9
-3
MysqlSqlCreator.java
...ain/java/com/tykj/datahouse/sql/impl/MysqlSqlCreator.java
+11
-11
没有找到文件。
src/main/java/com/tykj/datahouse/controller/TableInfoController.java
浏览文件 @
c885a88b
package
com
.
tykj
.
datahouse
.
controller
;
import
com.google.common.collect.ImmutableMap
;
import
com.tykj.datahouse.base.result.ResultObj
;
import
com.tykj.datahouse.base.result.ResultUtil
;
import
com.tykj.datahouse.entity.TableInfo
;
...
...
@@ -13,7 +14,7 @@ import org.springframework.web.bind.annotation.*;
import
java.util.List
;
@Api
(
tags
=
"表
信息
API"
)
@Api
(
tags
=
"表
操作
API"
)
@CrossOrigin
@RequestMapping
(
"/table"
)
@RestController
...
...
@@ -25,9 +26,9 @@ public class TableInfoController {
@PostMapping
@ApiOperation
(
"创建一个新表"
)
public
ResponseEntity
<
ResultObj
<
Object
>>
createTable
(
@RequestBody
TableInfo
tableInfo
)
{
public
ResponseEntity
createTable
(
@RequestBody
TableInfo
tableInfo
)
{
tableInfoService
.
createTable
(
tableInfo
);
return
Res
ultUtil
.
success
(
"操作成功"
);
return
Res
ponseEntity
.
ok
(
ImmutableMap
.
of
(
"message"
,
"操作成功"
)
);
}
@PutMapping
...
...
src/main/java/com/tykj/datahouse/entity/ColumnInfo.java
浏览文件 @
c885a88b
...
...
@@ -20,7 +20,6 @@ public class ColumnInfo {
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@ApiModelProperty
(
"主键"
)
@JsonIgnore
private
Integer
id
;
//-----------------------------//
...
...
src/main/java/com/tykj/datahouse/service/TableInfoService.java
浏览文件 @
c885a88b
...
...
@@ -14,12 +14,14 @@ import java.util.*;
import
java.util.stream.Collectors
;
import
static
java
.
lang
.
String
.
format
;
import
static
java
.
util
.
Objects
.
isNull
;
import
static
java
.
util
.
Objects
.
nonNull
;
/**
* 表信息相关操作的Service
* 涉及到对表的操作 均先进行对实际表的操作(执行sql语句) 再更新表信息
*/
@SuppressWarnings
(
"Duplicates"
)
@Service
public
class
TableInfoService
{
...
...
@@ -78,6 +80,9 @@ public class TableInfoService {
public
void
alterTable
(
TableInfo
tableInfo
)
{
//根据id查到更新之前的原表信息
dataSourceManager
.
clear
();
if
(
isNull
(
tableInfo
.
getId
())){
throw
new
RuntimeException
(
"修改数据必须附带id"
);
}
TableInfo
oTableInfo
=
tableInfoRepository
.
findById
(
tableInfo
.
getId
())
.
map
(
this
::
getColumns
)
.
orElseThrow
(()
->
new
RuntimeException
(
format
(
"id为%s的表不存在"
,
tableInfo
.
getId
())));
...
...
@@ -90,7 +95,8 @@ public class TableInfoService {
Set
<
Integer
>
columnInfoIds
=
columnInfos
.
stream
()
.
map
(
ColumnInfo:
:
getId
)
.
collect
(
Collectors
.
toSet
());
Map
<
Integer
,
ColumnInfo
>
columnInfoMap
=
columnInfos
.
stream
()
Map
<
Integer
,
ColumnInfo
>
columnInfoMapForAlter
=
columnInfos
.
stream
()
.
filter
(
Objects:
:
nonNull
)
.
collect
(
Collectors
.
toMap
(
ColumnInfo:
:
getId
,
columnInfo
->
columnInfo
));
List
<
ColumnInfo
>
columnsForAdd
=
columnInfos
.
stream
()
.
filter
(
columnInfo
->
isForAdd
(
columnInfo
.
getId
(),
oColumnInfoIds
))
...
...
@@ -111,10 +117,10 @@ public class TableInfoService {
}
for
(
ColumnInfo
oColumnInfo
:
columnsForAlter
)
{
dataSourceManager
.
switchToDataSource
(
"target"
);
String
alterColumnSql
=
sqlCreator
.
alterColumnSql
(
tableInfo
,
oColumnInfo
,
columnInfoMap
.
get
(
oColumnInfo
.
getId
()));
String
alterColumnSql
=
sqlCreator
.
alterColumnSql
(
tableInfo
,
oColumnInfo
,
columnInfoMap
ForAlter
.
get
(
oColumnInfo
.
getId
()));
jdbcTemplate
.
execute
(
alterColumnSql
);
dataSourceManager
.
clear
();
columnInfoRepository
.
save
(
columnInfoMap
.
get
(
oColumnInfo
.
getId
()));
columnInfoRepository
.
save
(
columnInfoMap
ForAlter
.
get
(
oColumnInfo
.
getId
()));
}
for
(
ColumnInfo
oColumnInfo
:
columnsForDelete
)
{
dataSourceManager
.
switchToDataSource
(
"target"
);
...
...
src/main/java/com/tykj/datahouse/sql/impl/MysqlSqlCreator.java
浏览文件 @
c885a88b
...
...
@@ -21,10 +21,10 @@ public class MysqlSqlCreator implements SqlCreator {
.
orElseThrow
(()
->
new
RuntimeException
(
"没有主键"
));
StringBuilder
result
=
new
StringBuilder
();
//起始
result
.
append
(
format
(
"CREATE TABLE
%s
(\n"
,
tableInfo
.
getName
()));
result
.
append
(
format
(
"CREATE TABLE
`%s`
(\n"
,
tableInfo
.
getName
()));
//其他字段
for
(
ColumnInfo
columnInfo
:
columnInfos
)
{
result
.
append
(
format
(
"
%s
%s"
,
columnInfo
.
getName
(),
columnInfo
.
getType
()));
result
.
append
(
format
(
"
`%s`
%s"
,
columnInfo
.
getName
(),
columnInfo
.
getType
()));
if
(
nonNull
(
columnInfo
.
getLength
()))
{
result
.
append
(
format
(
"(%s)"
,
columnInfo
.
getLength
()));
}
...
...
@@ -56,7 +56,7 @@ public class MysqlSqlCreator implements SqlCreator {
@Override
public
String
alterColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
oColumnInfo
,
ColumnInfo
columnInfo
)
{
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
format
(
"ALTER TABLE
%s CHANGE COLUMN %s %s
%s"
,
tableInfo
.
getName
(),
oColumnInfo
.
getName
(),
columnInfo
.
getName
(),
columnInfo
.
getType
()));
result
.
append
(
format
(
"ALTER TABLE
`%s` CHANGE COLUMN `%s` `%s`
%s"
,
tableInfo
.
getName
(),
oColumnInfo
.
getName
(),
columnInfo
.
getName
(),
columnInfo
.
getType
()));
if
(
nonNull
(
columnInfo
.
getLength
()))
{
result
.
append
(
format
(
"(%s)"
,
columnInfo
.
getLength
()));
}
...
...
@@ -68,12 +68,12 @@ public class MysqlSqlCreator implements SqlCreator {
@Override
public
String
removeColumnSql
(
TableInfo
tableInfo
,
ColumnInfo
columnInfo
)
{
return
format
(
"ALTER TABLE
%s DROP COLUMN %s
"
,
tableInfo
.
getName
(),
columnInfo
.
getName
());
return
format
(
"ALTER TABLE
`%s` DROP COLUMN `%s`
"
,
tableInfo
.
getName
(),
columnInfo
.
getName
());
}
@Override
public
String
dropTableSql
(
TableInfo
tableInfo
)
{
return
format
(
"DROP TABLE
%s
"
,
tableInfo
.
getName
());
return
format
(
"DROP TABLE
`%s`
"
,
tableInfo
.
getName
());
}
@Override
...
...
@@ -83,9 +83,9 @@ public class MysqlSqlCreator implements SqlCreator {
StringBuilder
columns
=
new
StringBuilder
();
StringBuilder
values
=
new
StringBuilder
();
//起始
result
.
append
(
format
(
"INSERT INTO
%s
"
,
tableInfo
.
getName
()));
result
.
append
(
format
(
"INSERT INTO
`%s`
"
,
tableInfo
.
getName
()));
for
(
ColumnInfo
columnInfo
:
columnInfos
)
{
columns
.
append
(
columnInfo
.
getName
(
)).
append
(
","
);
columns
.
append
(
format
(
"`%s`"
,
columnInfo
.
getName
()
)).
append
(
","
);
values
.
append
(
format
(
"\'%s\',"
,
row
.
get
(
columnInfo
.
getName
())));
}
int
columnLastIndex
=
columns
.
lastIndexOf
(
","
);
...
...
@@ -109,15 +109,15 @@ public class MysqlSqlCreator implements SqlCreator {
.
orElseThrow
(()
->
new
RuntimeException
(
"没有主键"
));
columnInfos
.
remove
(
primary
);
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
format
(
"UPDATE
%s
SET "
,
tableInfo
.
getName
()));
result
.
append
(
format
(
"UPDATE
`%s`
SET "
,
tableInfo
.
getName
()));
for
(
ColumnInfo
columnInfo
:
columnInfos
)
{
result
.
append
(
format
(
"
%s
= \'%s\',"
,
columnInfo
.
getName
(),
row
.
get
(
columnInfo
.
getName
())));
result
.
append
(
format
(
"
`%s`
= \'%s\',"
,
columnInfo
.
getName
(),
row
.
get
(
columnInfo
.
getName
())));
}
int
lastIndex
=
result
.
lastIndexOf
(
","
);
if
(
lastIndex
>
0
)
{
result
.
delete
(
lastIndex
,
result
.
length
());
}
result
.
append
(
format
(
" WHERE (
%s
= \'%s\')"
,
primary
.
getName
(),
row
.
get
(
primary
.
getName
())));
result
.
append
(
format
(
" WHERE (
`%s`
= \'%s\')"
,
primary
.
getName
(),
row
.
get
(
primary
.
getName
())));
return
result
.
toString
();
}
...
...
@@ -128,7 +128,7 @@ public class MysqlSqlCreator implements SqlCreator {
.
filter
(
ColumnInfo:
:
getIsPrimary
)
.
findAny
()
.
orElseThrow
(()
->
new
RuntimeException
(
"没有主键"
));
result
.
append
(
format
(
"DELETE FROM
%s WHERE (%s
= \'%s\')"
,
tableInfo
.
getName
(),
primary
.
getName
(),
row
.
get
(
primary
.
getName
())));
result
.
append
(
format
(
"DELETE FROM
`%s` WHERE (`%s`
= \'%s\')"
,
tableInfo
.
getName
(),
primary
.
getName
(),
row
.
get
(
primary
.
getName
())));
return
result
.
toString
();
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论