提交 561d0a6c authored 作者: 黄承天's avatar 黄承天

[模型模块]修改表语法改动

上级 43b8cd86
package com.tykj.model_layer.service.impl;
import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
......@@ -15,19 +14,14 @@ import com.tykj.model_layer.entity.customEnums.ConnectionType;
import com.tykj.model_layer.entity.vo.*;
import com.tykj.model_layer.myEnum.ModelType;
import com.tykj.model_layer.service.ModelService;
import com.tykj.model_layer.utils.ObjectUtil;
import com.tykj.model_layer.utils.SessionUtil;
import com.tykj.model_layer.utils.SqlUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.StaleStateException;
import org.hibernate.dialect.lock.OptimisticEntityLockException;
import org.hibernate.internal.SessionImpl;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.Query;
......@@ -35,8 +29,8 @@ import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.hibernate.type.TimestampType;
import org.hibernate.type.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
......@@ -45,7 +39,6 @@ import org.springframework.stereotype.Service;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
......@@ -87,6 +80,9 @@ public class ModelImpl implements ModelService {
@Autowired
private SessionUtil sessionUtil;
@Autowired
private Environment env;
/**
* @param
* @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo>
......@@ -205,7 +201,7 @@ public class ModelImpl implements ModelService {
* @Date 16:17 2021/3/5
**/
@Override
public int putValueByEntityName(Map<String, Object> map,Boolean isDelete) {
public int putValueByEntityName(Map<String, Object> map, Boolean isDelete) {
for (String tableName :
map.keySet()) {
//查找对应的表
......@@ -225,23 +221,23 @@ public class ModelImpl implements ModelService {
Object values = map.get(tableName);
if (values instanceof Map) {
//插入数据
insertValue(tableInfo.getModelName(), (Map) values, session, true, false,isDelete);
insertValue(tableInfo.getModelName(), (Map) values, session, true, false, isDelete);
} else {
//循环插入数据
List valuesList = (List) values;
for (int i = 0; i < valuesList.size(); i++) {
insertValue(tableInfo.getModelName(), (Map) valuesList.get(i), session, true, false,isDelete);
insertValue(tableInfo.getModelName(), (Map) valuesList.get(i), session, true, false, isDelete);
}
}
try {
session.getTransaction().commit();
}catch (Exception e){
} catch (Exception e) {
//todo 等会捕捉一下
if (e.getMessage().contains("Batch update returned unexpected row count from")){
if (e.getMessage().contains("Batch update returned unexpected row count from")) {
throw new ApiException("要操作对象的主键不存在");
}
}finally {
} finally {
session.close();
}
......@@ -258,7 +254,7 @@ public class ModelImpl implements ModelService {
* @Date 16:17 2021/3/5
**/
@Override
public int putValueByEntityNameList(List<Map<String, Object>> mapList,Boolean isDelete) {
public int putValueByEntityNameList(List<Map<String, Object>> mapList, Boolean isDelete) {
SessionImpl session = (SessionImpl) sessionUtil.getSession();
session.getTransaction().begin();
for (Map<String, Object> map : mapList) {
......@@ -279,12 +275,12 @@ public class ModelImpl implements ModelService {
Object values = map.get(tableName);
if (values instanceof Map) {
//插入数据
insertValue(tableInfo.getModelName(), (Map) values, session, true, false,isDelete);
insertValue(tableInfo.getModelName(), (Map) values, session, true, false, isDelete);
} else {
//循环插入数据
List valuesList = (List) values;
for (int i = 0; i < valuesList.size(); i++) {
insertValue(tableInfo.getModelName(), (Map) valuesList.get(i), session, true, false,isDelete);
insertValue(tableInfo.getModelName(), (Map) valuesList.get(i), session, true, false, isDelete);
}
}
......@@ -292,11 +288,11 @@ public class ModelImpl implements ModelService {
}
try {
session.getTransaction().commit();
}catch (StaleStateException staleStateException){
if (staleStateException.getMessage().contains("Batch update returned unexpected row count from")){
} catch (StaleStateException staleStateException) {
if (staleStateException.getMessage().contains("Batch update returned unexpected row count from")) {
throw new ApiException("要操作对象的主键不存在");
}
}finally {
} finally {
session.close();
}
return 0;
......@@ -311,12 +307,12 @@ public class ModelImpl implements ModelService {
* @Description 新增参数的方法
* @Date 16:17 2021/3/5
**/
public void insertValue(String tableName, Map map, SessionImpl session, boolean back, boolean saveOrSaveAndUpdate,boolean isDelete) {
public void insertValue(String tableName, Map map, SessionImpl session, boolean back, boolean saveOrSaveAndUpdate, boolean isDelete) {
EntityPersister entityPersister = session.getEntityPersister(tableName, map);
//主键类型推断
String identifierPropertyName = entityPersister.getIdentifierPropertyName();
if (map.containsKey(identifierPropertyName)){
changeValueToTargetType(map,map.get(identifierPropertyName),entityPersister.getIdentifierType(),identifierPropertyName);
if (map.containsKey(identifierPropertyName)) {
changeValueToTargetType(map, map.get(identifierPropertyName), entityPersister.getIdentifierType(), identifierPropertyName);
}
//类型推断 根据目标值的类型强转目标值的目标类型
Type[] propertyTypes = entityPersister.getPropertyTypes();
......@@ -326,12 +322,12 @@ public class ModelImpl implements ModelService {
Object value = propertyValuesToInsert[i];
Type propertyType = propertyTypes[i];
//根据目标Type转换
changeValueToTargetType(map,value,propertyType,propertyNames[i]);
changeValueToTargetType(map, value, propertyType, propertyNames[i]);
}
HashMap hashMap = new HashMap();
hashMap.putAll(map);
//到这里直接执行删除操作
if (isDelete){
if (isDelete) {
session.delete(tableName, map);
return;
}
......@@ -570,6 +566,7 @@ public class ModelImpl implements ModelService {
* 根据ColumnInfo的id来匹配字段变化
*/
private List<String> getTableSqls(String table, List<ColumnInfo> origin, List<ColumnInfo> current) {
String driverType = env.getProperty("spring.datasource.driver-class-name");
List<String> result = new ArrayList<>();
for (ColumnInfo columnInfo : current) {
ColumnInfo originColumnInfo = origin.stream()
......@@ -580,7 +577,7 @@ public class ModelImpl implements ModelService {
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());
String sql = SqlUtil.updateColumn(table, originColumnInfo.getFieldName(), columnInfo.getFieldName(), columnInfo.getFieldType(), columnInfo.getFieldLength(), driverType);
result.add(sql);
}
}
......@@ -689,7 +686,7 @@ public class ModelImpl implements ModelService {
}
void changeValueToTargetType(Map map, Object value,Type propertyType,String propertyName){
void changeValueToTargetType(Map map, Object value, Type propertyType, String propertyName) {
if (propertyType instanceof TimestampType) {
try {
if (value instanceof String) {
......@@ -714,5 +711,4 @@ public class ModelImpl implements ModelService {
}
}
......@@ -3,9 +3,12 @@ package com.tykj.model_layer.utils;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
......@@ -16,6 +19,9 @@ import static java.util.Objects.nonNull;
@Slf4j
public class SqlUtil {
@Autowired
private Environment env;
/**
* java类型对应sql类型Map
*/
......@@ -96,7 +102,7 @@ public class SqlUtil {
* @param newLength 字段长度
* @return SQL语句
*/
public static String updateColumn(String table, String name, String newName, String newType, Integer newLength) {
public static String updateColumn(String table, String name, String newName, String newType, Integer newLength,String driverType) {
newType = TYPE_MAP.get(newType);
boolean hasValue = !Strings.isNullOrEmpty(table)
&& nonNull(newType);
......@@ -104,12 +110,27 @@ public class SqlUtil {
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));
switch (driverType){
case "com.mysql.cj.jdbc.Driver":
result.append(String.format("ALTER TABLE %s MODIFY %s %s", table, name, newType));
break;
case "org.h2.Driver":
case "com.oscar.Driver":
result.append(String.format("ALTER TABLE %s ALTER COLUMN %s %s", table, name, newType));
}
if (nonNull(newLength)) {
result.append(String.format("(%s)", newLength));
}
} else {
switch (driverType){
case "com.mysql.cj.jdbc.Driver":
result.append(String.format("ALTER TABLE %s CHANGE COLUMN %s %s %s", table, name, newName, newType));
break;
case "org.h2.Driver":
case "com.oscar.Driver":
result.append(String.format("ALTER TABLE %s RENAME COLUMN %s TO %s", table, name, newName));
}
}
} else {
log.error("参数缺失");
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论