Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
W
workflow
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
2
议题
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄夏豪
workflow
Commits
a2509a4d
提交
a2509a4d
authored
3月 23, 2021
作者:
黄承天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[数据模型]增加修改表功能
上级
2c0fad42
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
207 行增加
和
73 行删除
+207
-73
ModelImpl.java
...tykj/workflowcore/model_layer/service/impl/ModelImpl.java
+73
-72
HqlUtil.java
...java/com/tykj/workflowcore/model_layer/utils/HqlUtil.java
+3
-1
SqlUtil.java
...java/com/tykj/workflowcore/model_layer/utils/SqlUtil.java
+131
-0
没有找到文件。
src/main/java/com/tykj/workflowcore/model_layer/service/impl/ModelImpl.java
浏览文件 @
a2509a4d
...
@@ -10,6 +10,7 @@ import com.tykj.workflowcore.model_layer.entity.vo.*;
...
@@ -10,6 +10,7 @@ import com.tykj.workflowcore.model_layer.entity.vo.*;
import
com.tykj.workflowcore.model_layer.entity.*
;
import
com.tykj.workflowcore.model_layer.entity.*
;
import
com.tykj.workflowcore.model_layer.service.ModelService
;
import
com.tykj.workflowcore.model_layer.service.ModelService
;
import
com.tykj.workflowcore.model_layer.utils.CreateTableUtil
;
import
com.tykj.workflowcore.model_layer.utils.CreateTableUtil
;
import
com.tykj.workflowcore.model_layer.utils.SqlUtil
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -32,19 +33,19 @@ import javax.persistence.Entity;
...
@@ -32,19 +33,19 @@ import javax.persistence.Entity;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Id
;
import
javax.persistence.criteria.*
;
import
javax.persistence.criteria.*
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.sql.SQLException
;
import
java.text.ParseException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
static
com
.
tykj
.
workflowcore
.
model_layer
.
utils
.
ClassTypeLength
.
setLength
;
import
static
com
.
tykj
.
workflowcore
.
model_layer
.
utils
.
ClassTypeLength
.
setLength
;
import
static
com
.
tykj
.
workflowcore
.
model_layer
.
utils
.
CreateTableUtil
.*;
import
static
com
.
tykj
.
workflowcore
.
model_layer
.
utils
.
CreateTableUtil
.*;
import
static
com
.
tykj
.
workflowcore
.
model_layer
.
utils
.
HqlUtil
.
createQuery
;
import
static
com
.
tykj
.
workflowcore
.
model_layer
.
utils
.
HqlUtil
.
createQuery
;
import
static
java
.
util
.
Objects
.
isNull
;
/**
/**
...
@@ -363,23 +364,24 @@ public class ModelImpl implements ModelService {
...
@@ -363,23 +364,24 @@ public class ModelImpl implements ModelService {
* @Date 10:51 2021/3/11
* @Date 10:51 2021/3/11
**/
**/
@Override
@Override
public
List
<
Map
<
String
,
Object
>>
findAllByName
(
String
name
)
{
public
List
<
Map
<
String
,
Object
>>
findAllByName
(
String
name
)
{
if
(
name
!=
null
&&
name
!=
""
)
{
if
(
name
!=
null
&&
name
!=
""
)
{
String
sql
=
"select * from "
+
name
;
String
sql
=
"select * from "
+
name
;
return
jdbcTemplate
.
queryForList
(
sql
);
return
jdbcTemplate
.
queryForList
(
sql
);
}
}
return
null
;
return
null
;
}
}
@Override
@Override
public
List
<
Map
<
String
,
Object
>>
complexQuery
(
String
tableName
,
List
<
QueryCondition
>
queryConditions
)
{
public
List
<
Map
<
String
,
Object
>>
complexQuery
(
String
tableName
,
List
<
QueryCondition
>
queryConditions
)
{
if
(!
""
.
equals
(
tableName
))
{
if
(!
""
.
equals
(
tableName
))
{
String
query
=
createQuery
(
tableName
,
queryConditions
);
String
query
=
createQuery
(
tableName
,
queryConditions
);
return
jdbcTemplate
.
queryForList
(
query
);
return
jdbcTemplate
.
queryForList
(
query
);
}
}
return
null
;
return
null
;
}
}
@Override
@Override
public
List
<
TableInfo
>
listAllEntities
()
{
public
List
<
TableInfo
>
listAllEntities
()
{
return
tableInfoDao
.
findAll
();
return
tableInfoDao
.
findAll
();
...
@@ -388,89 +390,88 @@ public class ModelImpl implements ModelService {
...
@@ -388,89 +390,88 @@ public class ModelImpl implements ModelService {
@Override
@Override
public
void
updateTable
(
UpdateTableInfoVO
updateTableInfoVO
)
{
public
void
updateTable
(
UpdateTableInfoVO
updateTableInfoVO
)
{
//
tableInfo和columnInfo变化
//
tableInfo和columnInfo变化
//查询到TableInfo和ColumnInfo
//查询到TableInfo和ColumnInfo
Integer
dbId
=
updateTableInfoVO
.
getDbId
();
Integer
dbId
=
updateTableInfoVO
.
getDbId
();
TableInfo
tableInfo
=
tableInfoDao
.
findById
(
dbId
).
orElseThrow
(()
->
new
RuntimeException
(
"未找到该id的表信息"
));
Specification
spec
=
(
Specification
)
(
root
,
criteriaQuery
,
criteriaBuilder
)
->
{
Predicate
predicate
=
criteriaBuilder
.
equal
(
root
.
get
(
"dbId"
),
dbId
);
return
predicate
;
};
List
<
ColumnInfo
>
all
=
columnInfoDao
.
findAll
(
spec
);
//删除columnInfo
columnInfoDao
.
deleteInBatch
(
all
);
TableInfo
tableInfo
=
tableInfoDao
.
getOne
(
dbId
);
tableInfo
.
setUpdatedTime
(
new
Date
());
tableInfo
.
setUpdatedTime
(
new
Date
());
TableVO
tableVO
=
updateTableInfoVO
.
getTableVO
();
TableVO
tableVO
=
updateTableInfoVO
.
getTableVO
();
List
<
ColumnVO
>
dataList
=
tableVO
.
getDataList
();
String
xml
=
createTable
(
tableVO
);
String
xml
=
createTable
(
tableVO
);
//重新存xml
//重新存xml
tableInfo
.
setXml
(
xml
);
tableInfo
.
setXml
(
xml
);
tableInfoDao
.
save
(
tableInfo
);
tableInfoDao
.
save
(
tableInfo
);
for
(
ColumnVO
columnVO
:
dataList
)
{
//原来的字段信息
ColumnInfo
columnInfo
=
new
ColumnInfo
();
Specification
spec
=
(
Specification
)
(
root
,
criteriaQuery
,
criteriaBuilder
)
->
{
columnInfo
.
setFieldName
(
columnVO
.
getFieldName
());
Predicate
predicate
=
criteriaBuilder
.
equal
(
root
.
get
(
"dbId"
),
dbId
);
columnInfo
.
setFieldType
(
columnVO
.
getFieldType
());
return
predicate
;
columnInfo
.
setFieldLength
(
columnVO
.
getFieldLength
());
};
columnInfo
.
setFieldTitle
(
columnVO
.
getFieldTitle
());
List
<
ColumnInfo
>
columnInfos
=
columnInfoDao
.
findAll
(
spec
);
setLength
(
columnInfo
,
columnVO
.
getFieldType
());
//新的字段信息
columnInfo
.
setDbName
(
tableInfo
.
getModelName
());
List
<
ColumnInfo
>
columnInfosForSave
=
tableVO
.
getDataList
().
stream
()
columnInfo
.
setDbId
(
tableInfo
.
getId
());
.
map
(
columnVO
->
columnInfo
(
tableInfo
.
getId
(),
tableInfo
.
getModelName
(),
columnVO
))
columnInfoDao
.
save
(
columnInfo
);
.
collect
(
Collectors
.
toList
());
}
//根据ColumnInfo集合得出表实际改动的sql语句集合
// #################### tableInfo和columnInfo变化结束 ###################
List
<
String
>
sqls
=
getTableSqls
(
tableInfo
.
getModelName
(),
columnInfos
,
columnInfosForSave
);
//执行sql语句
// 对应表真实变化
for
(
String
sql
:
sqls
)
{
jdbcTemplate
.
execute
(
sql
);
// 遍历新数据和原来数据对比=> 少了加 多了删
}
/**
* 修改列名
*
* @param tableName
* @param oldColumnName
* @param newColumnName
*/
private
void
UpdateColumnName
(
String
tableName
,
String
oldColumnName
,
String
newColumnName
)
{
// ALTER TABLE stu rename column name to name2;
if
(
""
!=
tableName
&&
tableName
!=
null
){
jdbcTemplate
.
execute
(
" ALTER TABLE "
+
tableName
+
" rename column "
+
oldColumnName
+
" to "
+
newColumnName
+
";"
);
}
else
{
log
.
info
(
"列名:{}或者新列名:{}不合法!"
,
oldColumnName
,
newColumnName
);
}
}
//重新保存字段信息
columnInfoDao
.
deleteInBatch
(
columnInfos
);
columnInfosForSave
.
forEach
(
columnInfo
->
columnInfoDao
.
save
(
columnInfo
));
}
}
/**
/**
* 删除一列
* 对象类型转换
* @param tableName
* ColumnVo -> ColumnInfo
* @param columnName
*/
*/
private
void
delOneColumn
(
String
tableName
,
String
columnName
)
{
private
ColumnInfo
columnInfo
(
Integer
dbId
,
String
dbName
,
ColumnVO
columnVO
)
{
// ALTER TABLE fab2 DROP test1;
ColumnInfo
columnInfo
=
new
ColumnInfo
();
jdbcTemplate
.
execute
(
"ALTER TABLE "
+
tableName
+
" DROP "
+
columnName
+
";"
);
columnInfo
.
setId
(
columnVO
.
getId
());
columnInfo
.
setFieldName
(
columnVO
.
getFieldName
());
columnInfo
.
setFieldType
(
columnVO
.
getFieldType
());
columnInfo
.
setFieldLength
(
columnVO
.
getFieldLength
());
columnInfo
.
setFieldTitle
(
columnVO
.
getFieldTitle
());
columnInfo
.
setFieldLength
(
columnVO
.
getFieldLength
());
columnInfo
.
setDbId
(
dbId
);
columnInfo
.
setDbName
(
dbName
);
return
columnInfo
;
}
}
/**
/**
* 增加一列
* 根据ColumnInfo集合得出表实际改动的sql语句集合
* @param tableName
* 根据ColumnInfo的id来匹配字段变化
* @param columnName
* @param type
*/
*/
private
void
addOneColumn
(
String
tableName
,
String
columnName
,
String
type
)
{
private
List
<
String
>
getTableSqls
(
String
table
,
List
<
ColumnInfo
>
origin
,
List
<
ColumnInfo
>
current
)
{
// alter table fab2 add test1 varchar(10) not Null;
List
<
String
>
result
=
new
ArrayList
<>();
jdbcTemplate
.
execute
(
" alter table "
+
tableName
+
" add "
+
columnName
+
" "
+
type
);
for
(
ColumnInfo
columnInfo
:
current
)
{
ColumnInfo
originColumnInfo
=
origin
.
stream
()
.
filter
(
columnInfo1
->
Objects
.
equals
(
columnInfo
.
getId
(),
columnInfo1
.
getId
()))
.
findAny
()
.
orElse
(
null
);
if
(
isNull
(
originColumnInfo
))
{
String
sql
=
SqlUtil
.
addColumn
(
table
,
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
());
result
.
add
(
sql
);
}
else
{
String
sql
=
SqlUtil
.
updateColumn
(
table
,
originColumnInfo
.
getFieldName
(),
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
());
result
.
add
(
sql
);
}
}
for
(
ColumnInfo
originColumnInfo
:
origin
)
{
boolean
noneMatch
=
current
.
stream
()
.
noneMatch
(
columnInfo1
->
Objects
.
equals
(
originColumnInfo
.
getId
(),
columnInfo1
.
getId
()));
if
(
noneMatch
)
{
String
sql
=
SqlUtil
.
deleteColumn
(
table
,
originColumnInfo
.
getFieldName
());
result
.
add
(
sql
);
}
}
for
(
String
s
:
result
)
{
System
.
out
.
println
(
s
);
}
return
result
;
}
}
}
}
src/main/java/com/tykj/workflowcore/model_layer/utils/HqlUtil.java
浏览文件 @
a2509a4d
package
com
.
tykj
.
workflowcore
.
model_layer
.
utils
;
package
com
.
tykj
.
workflowcore
.
model_layer
.
utils
;
import
com.tykj.workflowcore.model_layer.entity.vo.QueryCondition
;
import
com.tykj.workflowcore.model_layer.entity.vo.QueryCondition
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.Objects
;
/**
/**
* @author
HASEE
* @author
C
*/
*/
@Slf4j
public
class
HqlUtil
{
public
class
HqlUtil
{
public
static
String
createQuery
(
String
tableName
,
List
<
QueryCondition
>
conditions
)
{
public
static
String
createQuery
(
String
tableName
,
List
<
QueryCondition
>
conditions
)
{
...
...
src/main/java/com/tykj/workflowcore/model_layer/utils/SqlUtil.java
0 → 100644
浏览文件 @
a2509a4d
package
com
.
tykj
.
workflowcore
.
model_layer
.
utils
;
import
com.google.common.base.Strings
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.HashMap
;
import
java.util.Map
;
import
static
java
.
util
.
Objects
.
isNull
;
import
static
java
.
util
.
Objects
.
nonNull
;
/**
* @author C
*/
@Slf4j
public
class
SqlUtil
{
/**
* java类型对应sql类型Map
*/
private
static
final
Map
<
String
,
String
>
TYPE_MAP
=
new
HashMap
<>();
static
{
TYPE_MAP
.
put
(
"java.lang.Integer"
,
"int"
);
TYPE_MAP
.
put
(
"java.lang.Long"
,
"bigint"
);
TYPE_MAP
.
put
(
"java.lang.Short"
,
"smallint"
);
TYPE_MAP
.
put
(
"java.lang.Float"
,
"float"
);
TYPE_MAP
.
put
(
"java.lang.Double"
,
"double"
);
TYPE_MAP
.
put
(
"java.lang.BigDecimal"
,
"numeric"
);
TYPE_MAP
.
put
(
"java.lang.Character"
,
"char(1)"
);
TYPE_MAP
.
put
(
"java.lang.String"
,
"varchar"
);
TYPE_MAP
.
put
(
"java.lang.Byte"
,
"tinyint"
);
TYPE_MAP
.
put
(
"java.lang.Boolean"
,
"bit"
);
TYPE_MAP
.
put
(
"java.lang.Class"
,
"varchar"
);
TYPE_MAP
.
put
(
"java.util.Date"
,
"date"
);
TYPE_MAP
.
put
(
"java.util.Calendar"
,
"timestamp"
);
TYPE_MAP
.
put
(
"java.util.Locale"
,
"varchar"
);
TYPE_MAP
.
put
(
"java.util.TimeZone"
,
"varchar"
);
TYPE_MAP
.
put
(
"java.util.Currency"
,
"varchar"
);
TYPE_MAP
.
put
(
"java.sql.Date"
,
"date"
);
TYPE_MAP
.
put
(
"java.sql.Time"
,
"time"
);
TYPE_MAP
.
put
(
"java.sql.Timestamp"
,
"timestamp"
);
TYPE_MAP
.
put
(
"java.sql.Clob"
,
"clob"
);
TYPE_MAP
.
put
(
"java.sql.Blob"
,
"blob"
);
}
/**
* 修改表名的SQL语句
*
* @param table 原表名
* @param newTable 新表名
* @return SQL语句
*/
public
static
String
renameTable
(
String
table
,
String
newTable
)
{
return
String
.
format
(
"rename table %s to %s;"
,
table
,
newTable
);
}
/**
* 增加单个字段的SQL语句
*
* @param table 表名
* @param name 字段名
* @param type 字段类型
* @param length 字段长度
* @return SQL语句
*/
public
static
String
addColumn
(
String
table
,
String
name
,
String
type
,
Integer
length
)
{
type
=
TYPE_MAP
.
get
(
type
);
boolean
hasValue
=
!
Strings
.
isNullOrEmpty
(
table
)
&&
!
Strings
.
isNullOrEmpty
(
name
)
&&
nonNull
(
type
);
StringBuilder
result
=
new
StringBuilder
();
if
(
hasValue
)
{
//基本部分
result
.
append
(
String
.
format
(
"ALTER TABLE %s ADD COLUMN %s %s"
,
table
,
name
,
type
));
//字段长度
if
(
nonNull
(
length
))
{
result
.
append
(
String
.
format
(
"(%s)"
,
length
));
}
result
.
append
(
";"
);
return
result
.
toString
();
}
else
{
log
.
error
(
"表名或字段名不能为空"
);
throw
new
RuntimeException
(
"表名或字段名不能为空"
);
}
}
/**
* 修改表的单个字段的SQL语句
*
* @param table 表名
* @param name 旧字段名
* @param newName 新字段名
* @param newType 字段类型
* @param newLength 字段长度
* @return SQL语句
*/
public
static
String
updateColumn
(
String
table
,
String
name
,
String
newName
,
String
newType
,
Integer
newLength
)
{
newType
=
TYPE_MAP
.
get
(
newType
);
boolean
hasValue
=
!
Strings
.
isNullOrEmpty
(
table
)
&&
nonNull
(
newType
);
StringBuilder
result
=
new
StringBuilder
();
if
(
hasValue
)
{
if
(
isNull
(
newName
))
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s MODIFY %s %s"
,
table
,
name
,
newType
));
}
else
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s CHANGE COLUMN %s %s %s"
,
table
,
name
,
newName
,
newType
));
}
if
(
nonNull
(
newLength
))
{
result
.
append
(
String
.
format
(
"(%s)"
,
newLength
));
}
}
else
{
log
.
error
(
"参数缺失"
);
}
result
.
append
(
";"
);
return
result
.
toString
();
}
/**
* 删除单个字段的SQL语句
*
* @param table 表名
* @param name 字段名
* @return SQL语句
*/
public
static
String
deleteColumn
(
String
table
,
String
name
)
{
return
String
.
format
(
"ALTER TABLE %s DROP %s;"
,
table
,
name
);
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论