mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 02:32:33 +08:00
mysql修改列顺序测试
This commit is contained in:
@ -104,13 +104,18 @@ public class MysqlSqlBuilder extends DefaultSqlBuilder {
|
||||
|
||||
// 判断移动的字段
|
||||
List<TableColumn> moveColumnList = new ArrayList<>();
|
||||
movedElements(oldTable.getColumnList(), newTable.getColumnList());
|
||||
moveColumnList = movedElements(oldTable.getColumnList(), newTable.getColumnList());
|
||||
|
||||
// append modify column
|
||||
for (TableColumn tableColumn : newTable.getColumnList()) {
|
||||
if (StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType()) && StringUtils.isNotBlank(tableColumn.getName())) {
|
||||
if ((StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType())
|
||||
&& StringUtils.isNotBlank(tableColumn.getName())) || moveColumnList.contains(tableColumn)) {
|
||||
MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(tableColumn.getColumnType());
|
||||
script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n");
|
||||
if (moveColumnList.contains(tableColumn)) {
|
||||
script.append("\t").append(typeEnum.buildModifyColumn(tableColumn, true, findPrevious(tableColumn, newTable))).append(",\n");
|
||||
} else {
|
||||
script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,6 +138,13 @@ public class MysqlSqlBuilder extends DefaultSqlBuilder {
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
private String findPrevious(TableColumn tableColumn, Table newTable) {
|
||||
int index = newTable.getColumnList().indexOf(tableColumn);
|
||||
if (index == 0) {
|
||||
return "-1";
|
||||
}
|
||||
return newTable.getColumnList().get(index - 1).getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String pageLimit(String sql, int offset, int pageNo, int pageSize) {
|
||||
|
@ -199,6 +199,30 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String buildModifyColumn(TableColumn tableColumn, boolean isMove, String columnName) {
|
||||
if (EditStatus.DELETE.name().equals(tableColumn.getEditStatus())) {
|
||||
return StringUtils.join("DROP COLUMN `", tableColumn.getName() + "`");
|
||||
}
|
||||
if (EditStatus.ADD.name().equals(tableColumn.getEditStatus())) {
|
||||
if (isMove){
|
||||
if (columnName.equals("-1")) {
|
||||
return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn)," FIRST");
|
||||
} else {
|
||||
return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn), " AFTER ", columnName);
|
||||
}
|
||||
}
|
||||
return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn));
|
||||
}
|
||||
if (EditStatus.MODIFY.name().equals(tableColumn.getEditStatus())) {
|
||||
if (!StringUtils.equalsIgnoreCase(tableColumn.getOldName(), tableColumn.getName())) {
|
||||
return StringUtils.join("CHANGE COLUMN `", tableColumn.getOldName(), "` ", buildCreateColumnSql(tableColumn));
|
||||
} else {
|
||||
return StringUtils.join("MODIFY COLUMN ", buildCreateColumnSql(tableColumn));
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private String buildAutoIncrement(TableColumn column, MysqlColumnTypeEnum type) {
|
||||
if(!type.getColumnType().isSupportAutoIncrement()){
|
||||
return "";
|
||||
|
Reference in New Issue
Block a user