提交 579a29b1 authored 作者: 1239068511@qq.com's avatar 1239068511@qq.com

[数据模型] 新增了对 长文本的支持

上级 0b4607cc
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;
}
......@@ -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 : sqls) {
for (ExecuteSqlVo executeSqlVo : executeSqlVos) {
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.addAll(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.addAll(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.addAll(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.addAll(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
......
......@@ -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");
}
......
......@@ -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 resultSqlVo;
}
/**
......@@ -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 resultSqlVo;
}
/**
......@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论