Merge remote-tracking branch 'origin/master'

master
星期八 5 years ago
commit 3de4d4d8f9
  1. 48
      panda-jdbc/src/main/java/org/panda/jdbc/sql/builder/MysqlBuilder.java

@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* @author qi * @author qi
@ -21,6 +22,14 @@ public class MysqlBuilder extends AbstractDbBuiler {
private static final Logger LOGGER = LoggerFactory.getLogger(MysqlBuilder.class); private static final Logger LOGGER = LoggerFactory.getLogger(MysqlBuilder.class);
private static Map<String,String> dataTypeMap=new HashMap<>(); private static Map<String,String> dataTypeMap=new HashMap<>();
/**
* 数据库数据类型-char
*/
public static final String DB_TYPE_CHAR = "char";
/**
* 数据库数据类型-int
*/
public static final String DB_TYPE_INT = "int";
static { static {
dataTypeMap.put("class java.lang.String","varchar"); dataTypeMap.put("class java.lang.String","varchar");
@ -35,26 +44,19 @@ public class MysqlBuilder extends AbstractDbBuiler {
public String automaticUpdateDb(Set<Class> classSet,List<Table> tableList) { public String automaticUpdateDb(Set<Class> classSet,List<Table> tableList) {
StringBuffer sql = new StringBuffer(); StringBuffer sql = new StringBuffer();
//扫描实体类包的实体class //扫描实体类包的实体class
Set<Class> set = classSet; Map<String, Class> map = classSet.stream().collect(Collectors.toMap(BeanSqlUtil::beanNameToTableName,clazz->clazz));
Map<String, Class> map = new HashMap<>(set.size());
set.forEach((clazz)->{
map.put(BeanSqlUtil.beanNameToTableName(clazz), clazz);
});
//初始化的时候,tableList可能为null //初始化的时候,tableList可能为null
if(tableList!=null) { if(tableList!=null&&tableList.size()>0) {
tableList.forEach((table)->{ tableList.forEach(table->{
if (map.containsKey(table.getTableName())) { if (map.containsKey(table.getTableName())) {
Class clazz = map.get(table.getTableName()); Class clazz = map.get(table.getTableName());
List<Field> fieldsList = BeanUtil.getAllField(clazz); List<Field> fieldsList = BeanUtil.getAllField(clazz);
Map<String, Object> clazzMap = new HashMap<>(fieldsList.size()); Map<String, Object> clazzMap = new HashMap<>(fieldsList.size());
Iterator<Field> fieldsIterator = fieldsList.iterator(); fieldsList.forEach(field -> {
while (fieldsIterator.hasNext()) { clazzMap.put(BeanSqlUtil.caseToHump(field.getName()), field);
Field f = fieldsIterator.next(); });
clazzMap.put(BeanSqlUtil.caseToHump(f.getName()), f);
}
List<Column> columnList = table.getColumns();
List<Column> columnList = table.getColumns();
Iterator<Column> columnIterator = columnList.iterator(); Iterator<Column> columnIterator = columnList.iterator();
while (columnIterator.hasNext()) { while (columnIterator.hasNext()) {
Column column = columnIterator.next(); Column column = columnIterator.next();
@ -62,24 +64,22 @@ public class MysqlBuilder extends AbstractDbBuiler {
clazzMap.remove(column.getColumnName()); clazzMap.remove(column.getColumnName());
columnIterator.remove(); columnIterator.remove();
} }
} }
columnIterator = columnList.iterator(); columnList.forEach(column -> {
while (columnIterator.hasNext()) {
Column column = columnIterator.next();
sql.append("ALTER TABLE " + table.getTableName() + " DROP COLUMN " + column.getColumnName() + ";"); sql.append("ALTER TABLE " + table.getTableName() + " DROP COLUMN " + column.getColumnName() + ";");
} });
Set<String> keySet = clazzMap.keySet();
for (String key : clazzMap.keySet()) { keySet.forEach(key->{
Field f = (Field) clazzMap.get(key); Field f = (Field) clazzMap.get(key);
String column = BeanSqlUtil.caseToHump(f.getName()); String column = BeanSqlUtil.caseToHump(f.getName());
String columnType = dataTypeMap.get(f.getGenericType().toString()); String columnType = dataTypeMap.get(f.getGenericType().toString());
String lengthStr = ""; String lengthStr = "";
if ("varchar".equals(columnType) || "int".equals(columnType)) { if (DB_TYPE_CHAR.equals(columnType) || DB_TYPE_CHAR.equals(columnType)) {
lengthStr = "(100)"; lengthStr = "(100)";
} }
sql.append("ALTER TABLE " + table.getTableName() + " ADD COLUMN " + column + " " + columnType + lengthStr + ";"); sql.append("ALTER TABLE " + table.getTableName() + " ADD COLUMN " + column + " " + columnType + lengthStr + ";");
}
});
map.remove(table.getTableName()); map.remove(table.getTableName());
} }
}); });
@ -111,7 +111,7 @@ public class MysqlBuilder extends AbstractDbBuiler {
for (Map<String,String> map:mapList){ for (Map<String,String> map:mapList){
String dataTypeStr = dataTypeMap.get(map.get("fieldType")); String dataTypeStr = dataTypeMap.get(map.get("fieldType"));
sql.append(BeanSqlUtil.caseToHump(map.get("fieldName"))+" "+dataTypeStr); sql.append(BeanSqlUtil.caseToHump(map.get("fieldName"))+" "+dataTypeStr);
if ("varchar".equals(dataTypeStr)||"int".equals(dataTypeStr)){ if (DB_TYPE_CHAR.equals(dataTypeStr)||DB_TYPE_INT.equals(dataTypeStr)){
if (map.containsKey("fieldLength")) { if (map.containsKey("fieldLength")) {
sql.append("("+map.get("fieldLength")+")"); sql.append("("+map.get("fieldLength")+")");
} }

Loading…
Cancel
Save