Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
dataWareHose
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄夏豪
dataWareHose
Commits
579a29b1
提交
579a29b1
authored
8月 03, 2021
作者:
1239068511@qq.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[数据模型] 新增了对 长文本的支持
上级
0b4607cc
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
76 行增加
和
25 行删除
+76
-25
ExecuteSqlVo.java
src/main/java/com/tykj/model/entity/vo/ExecuteSqlVo.java
+21
-0
ModelImpl.java
src/main/java/com/tykj/model/service/impl/ModelImpl.java
+30
-13
MysqlSqlType.java
src/main/java/com/tykj/model/sqlType/MysqlSqlType.java
+1
-1
SqlUtil.java
src/main/java/com/tykj/model/utils/SqlUtil.java
+24
-11
没有找到文件。
src/main/java/com/tykj/model/entity/vo/ExecuteSqlVo.java
0 → 100644
浏览文件 @
579a29b1
package
com
.
tykj
.
model
.
entity
.
vo
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
@Data
@AllArgsConstructor
public
class
ExecuteSqlVo
{
private
String
sql
;
private
String
tableName
;
private
String
oldField
;
private
String
newField
;
private
String
oldType
;
private
String
newType
;
}
src/main/java/com/tykj/model/service/impl/ModelImpl.java
浏览文件 @
579a29b1
...
...
@@ -32,11 +32,13 @@ import org.hibernate.query.Query;
import
org.hibernate.type.*
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.env.Environment
;
import
org.springframework.dao.DataIntegrityViolationException
;
import
org.springframework.data.domain.Page
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
javax.sql.rowset.serial.SerialClob
;
import
java.text.SimpleDateFormat
;
import
java.time.LocalDateTime
;
import
java.time.ZoneId
;
...
...
@@ -414,13 +416,19 @@ public class ModelImpl implements ModelService {
.
collect
(
Collectors
.
toList
());
//根据ColumnInfo集合得出表实际改动的sql语句集合
List
<
String
>
sqls
=
getTableSqls
(
tableInfo
.
getModelName
(),
originalColumnInfos
,
currentColumnInfos
);
List
<
ExecuteSqlVo
>
executeSqlVos
=
getTableExecuteSqlVo
(
tableInfo
.
getModelName
(),
originalColumnInfos
,
currentColumnInfos
);
//执行sql语句
for
(
String
sql
:
sql
s
)
{
for
(
ExecuteSqlVo
executeSqlVo
:
executeSqlVo
s
)
{
try
{
jdbcTemplate
.
execute
(
sql
);
jdbcTemplate
.
execute
(
executeSqlVo
.
getSql
()
);
}
catch
(
Exception
exception
)
{
//todo
if
(
exception
instanceof
DataIntegrityViolationException
){
String
message
=
exception
.
getMessage
();
if
(
message
.
contains
(
"MODIFY"
)){
throw
new
ApiException
(
String
.
format
(
"字段:%s 无法转换为 %s 类型"
,
executeSqlVo
.
getNewField
(),
executeSqlVo
.
getNewType
()));
}
}
exception
.
printStackTrace
();
throw
new
ApiException
(
"SQL执行出错"
);
...
...
@@ -477,11 +485,11 @@ public class ModelImpl implements ModelService {
* 根据ColumnInfo集合得出表实际改动的sql语句集合
* 根据ColumnInfo的id来匹配字段变化
*/
private
List
<
String
>
getTableSqls
(
String
table
,
List
<
ColumnInfo
>
origin
,
List
<
ColumnInfo
>
current
)
{
private
List
<
ExecuteSqlVo
>
getTableExecuteSqlVo
(
String
table
,
List
<
ColumnInfo
>
origin
,
List
<
ColumnInfo
>
current
)
{
//取到当前环境名
String
driverType
=
env
.
getProperty
(
"spring.datasource.driver-class-name"
);
Map
<
String
,
String
>
sqlTypeMap
=
SqlTypeUtil
.
getSqlTypeMap
(
driverType
);
List
<
String
>
result
=
new
ArrayList
<>();
List
<
ExecuteSqlVo
>
result
=
new
ArrayList
<>();
//遍历获取新增和更新列的情况
for
(
ColumnInfo
columnInfo
:
current
)
{
ColumnInfo
originColumnInfo
=
origin
.
stream
()
...
...
@@ -489,19 +497,19 @@ public class ModelImpl implements ModelService {
.
findAny
()
.
orElse
(
null
);
if
(
isNull
(
originColumnInfo
))
{
String
sql
=
SqlUtil
.
addColumn
(
table
,
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
(),
sqlTypeMap
);
result
.
add
(
sql
);
List
<
ExecuteSqlVo
>
executeSqlVos
=
SqlUtil
.
addColumn
(
table
,
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
(),
sqlTypeMap
);
result
.
add
All
(
executeSqlVos
);
}
else
{
//判断 originColumnInfo 和 columnInfo 是否存在不一样的地方
if
(!
columnInfo
.
getFieldType
().
equals
(
originColumnInfo
.
getFieldType
())
||
!
columnInfo
.
getFieldLength
().
equals
(
originColumnInfo
.
getFieldLength
())
)
{
String
sql
=
SqlUtil
.
updateColumnType
(
table
,
originColumnInfo
.
getFieldName
(),
columnInfo
.
getFieldName
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
(),
driverType
,
sqlTypeMap
);
result
.
add
(
sql
);
List
<
ExecuteSqlVo
>
executeSqlVos
=
SqlUtil
.
updateColumnType
(
table
,
originColumnInfo
.
getFieldName
(),
columnInfo
.
getFieldName
(),
originColumnInfo
.
getFieldType
(),
columnInfo
.
getFieldType
(),
columnInfo
.
getFieldLength
()
,
sqlTypeMap
);
result
.
add
All
(
executeSqlVos
);
}
if
(!
columnInfo
.
getFieldName
().
equals
(
originColumnInfo
.
getFieldName
()))
{
String
sql
=
SqlUtil
.
updateColumnName
(
table
,
originColumnInfo
.
getFieldName
(),
columnInfo
.
getFieldName
());
result
.
add
(
sql
);
List
<
ExecuteSqlVo
>
executeSqlVos
=
SqlUtil
.
updateColumnName
(
table
,
originColumnInfo
.
getFieldName
(),
columnInfo
.
getFieldName
());
result
.
add
All
(
executeSqlVos
);
}
}
...
...
@@ -512,8 +520,8 @@ public class ModelImpl implements ModelService {
boolean
noneMatch
=
current
.
stream
()
.
noneMatch
(
columnInfo1
->
Objects
.
equals
(
originColumnInfo
.
getId
(),
columnInfo1
.
getId
()));
if
(
noneMatch
)
{
String
sql
=
SqlUtil
.
deleteColumn
(
table
,
originColumnInfo
.
getFieldName
());
result
.
add
(
sql
);
List
<
ExecuteSqlVo
>
executeSqlVos
=
SqlUtil
.
deleteColumn
(
table
,
originColumnInfo
.
getFieldName
());
result
.
add
All
(
executeSqlVos
);
}
}
...
...
@@ -678,6 +686,15 @@ public class ModelImpl implements ModelService {
throw
new
ApiException
(
"字段:"
+
propertyName
+
"的类型是浮点数类型的。他的值不能为:"
+
value
);
}
}
if
(
propertyType
instanceof
ClobType
)
{
try
{
SerialClob
i1
=
new
SerialClob
((
value
+
""
).
toCharArray
());
map
.
put
(
propertyName
,
i1
);
}
catch
(
Exception
e
)
{
throw
new
ApiException
(
"字段:"
+
propertyName
+
"的类型是浮点数类型的。他的值不能为:"
+
value
);
}
}
}
@Override
...
...
src/main/java/com/tykj/model/sqlType/MysqlSqlType.java
浏览文件 @
579a29b1
...
...
@@ -33,7 +33,7 @@ public class MysqlSqlType {
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.Clob"
,
"
text
"
);
TYPE_MAP
.
put
(
"java.sql.Blob"
,
"blob"
);
}
...
...
src/main/java/com/tykj/model/utils/SqlUtil.java
浏览文件 @
579a29b1
...
...
@@ -2,10 +2,13 @@ package com.tykj.model.utils;
import
com.google.common.base.Strings
;
import
com.tykj.model.entity.vo.ExecuteSqlVo
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.env.Environment
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
static
java
.
util
.
Objects
.
isNull
;
...
...
@@ -40,8 +43,10 @@ public class SqlUtil {
* @param length 字段长度
* @return SQL语句
*/
public
static
String
addColumn
(
String
table
,
String
name
,
String
type
,
Integer
length
,
Map
<
String
,
String
>
sqlTypeMap
)
{
public
static
List
<
ExecuteSqlVo
>
addColumn
(
String
table
,
String
name
,
String
type
,
Integer
length
,
Map
<
String
,
String
>
sqlTypeMap
)
{
List
<
ExecuteSqlVo
>
resultSqlVo
=
new
ArrayList
<>();
type
=
sqlTypeMap
.
get
(
type
);
boolean
hasValue
=
!
Strings
.
isNullOrEmpty
(
table
)
&&
!
Strings
.
isNullOrEmpty
(
name
)
&&
nonNull
(
type
);
...
...
@@ -54,11 +59,13 @@ public class SqlUtil {
result
.
append
(
String
.
format
(
"(%s)"
,
length
));
}
result
.
append
(
";"
);
return
result
.
toString
();
String
resultSql
=
result
.
toString
();
resultSqlVo
.
add
(
new
ExecuteSqlVo
(
resultSql
,
table
,
""
,
name
,
""
,
type
));
}
else
{
log
.
error
(
"表名或字段名不能为空"
);
throw
new
RuntimeException
(
"表名或字段名不能为空"
);
}
return
resultSqlVo
;
}
/**
...
...
@@ -71,7 +78,8 @@ public class SqlUtil {
* @param newLength 字段长度
* @return SQL语句
*/
public
static
String
updateColumnType
(
String
table
,
String
name
,
String
newName
,
String
newType
,
Integer
newLength
,
String
driverType
,
Map
<
String
,
String
>
sqlTypeMap
)
{
public
static
List
<
ExecuteSqlVo
>
updateColumnType
(
String
table
,
String
name
,
String
newName
,
String
oldType
,
String
newType
,
Integer
newLength
,
Map
<
String
,
String
>
sqlTypeMap
)
{
List
<
ExecuteSqlVo
>
resultSqlVo
=
new
ArrayList
<>();
newType
=
sqlTypeMap
.
get
(
newType
);
boolean
hasValue
=
!
Strings
.
isNullOrEmpty
(
table
)
&&
nonNull
(
newType
);
...
...
@@ -82,13 +90,12 @@ public class SqlUtil {
}
else
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s MODIFY %s %s ;"
,
table
,
name
,
newType
));
}
if
(
isNull
(
newName
))
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s RENAME COLUMN %s TO %s ;"
,
table
,
name
,
newName
));
}
String
resultSql
=
result
.
toString
();
resultSqlVo
.
add
(
new
ExecuteSqlVo
(
resultSql
,
table
,
name
,
newName
,
oldType
,
newType
));
}
else
{
log
.
error
(
"参数缺失"
);
}
return
result
.
toString
()
;
return
result
SqlVo
;
}
/**
...
...
@@ -99,17 +106,20 @@ public class SqlUtil {
* @param newName 新字段名
* @return SQL语句
*/
public
static
String
updateColumnName
(
String
table
,
String
name
,
String
newName
)
{
public
static
List
<
ExecuteSqlVo
>
updateColumnName
(
String
table
,
String
name
,
String
newName
)
{
List
<
ExecuteSqlVo
>
resultSqlVo
=
new
ArrayList
<>();
boolean
hasValue
=
!
Strings
.
isNullOrEmpty
(
table
);
StringBuilder
result
=
new
StringBuilder
();
if
(
hasValue
)
{
if
(!
isNull
(
newName
))
{
result
.
append
(
String
.
format
(
"ALTER TABLE %s RENAME COLUMN %s TO %s ;"
,
table
,
name
,
newName
));
}
String
resultSql
=
result
.
toString
();
resultSqlVo
.
add
(
new
ExecuteSqlVo
(
resultSql
,
table
,
name
,
newName
,
""
,
""
));
}
else
{
log
.
error
(
"参数缺失"
);
}
return
result
.
toString
()
;
return
result
SqlVo
;
}
/**
...
...
@@ -119,8 +129,11 @@ public class SqlUtil {
* @param name 字段名
* @return SQL语句
*/
public
static
String
deleteColumn
(
String
table
,
String
name
)
{
return
String
.
format
(
"ALTER TABLE %s DROP %s;"
,
table
,
name
);
public
static
List
<
ExecuteSqlVo
>
deleteColumn
(
String
table
,
String
name
)
{
List
<
ExecuteSqlVo
>
resultSqlVo
=
new
ArrayList
<>();
String
resultSql
=
String
.
format
(
"ALTER TABLE %s DROP %s;"
,
table
,
name
);
resultSqlVo
.
add
(
new
ExecuteSqlVo
(
resultSql
,
table
,
name
,
name
,
""
,
""
));
return
resultSqlVo
;
}
private
static
boolean
needLengthColumn
(
String
type
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论