From 92bd924e761f32dbc7137ed677c569957ce0ed98 Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Thu, 11 Jan 2024 13:56:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20#1093=20=E4=BF=AE=E5=A4=8D=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E8=A1=A8=E5=AD=97=E6=AE=B5=E5=90=8D=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=94=9F=E6=88=90SQL=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/mysql/builder/MysqlSqlBuilder.java | 117 +++++++++++------- 1 file changed, 70 insertions(+), 47 deletions(-) diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java index 1a97c843b..c98905b24 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/builder/MysqlSqlBuilder.java @@ -94,11 +94,13 @@ public String buildModifyTaleSql(Table oldTable, Table newTable) { script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n"); } + boolean modifyFlag = false; // append modify column for (TableColumn tableColumn : newTable.getColumnList()) { if (StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType()) && StringUtils.isNotBlank(tableColumn.getName())) { MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(tableColumn.getColumnType()); script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n"); + modifyFlag = true; } } @@ -107,11 +109,18 @@ public String buildModifyTaleSql(Table oldTable, Table newTable) { if (StringUtils.isNotBlank(tableIndex.getEditStatus()) && StringUtils.isNotBlank(tableIndex.getType())) { MysqlIndexTypeEnum mysqlIndexTypeEnum = MysqlIndexTypeEnum.getByType(tableIndex.getType()); script.append("\t").append(mysqlIndexTypeEnum.buildModifyIndex(tableIndex)).append(",\n"); + modifyFlag = true; } } // append reorder column - script.append(buildGenerateReorderColumnSql(oldTable, newTable)); + String reorderColumnSql = buildGenerateReorderColumnSql(oldTable, newTable, modifyFlag); + if (reorderColumnSql.length() > 0) { + script = new StringBuilder(script.substring(0, script.length() - 2)); + script.append(";\n"); + } + script.append(reorderColumnSql); + if (script.length() > 2) { script = new StringBuilder(script.substring(0, script.length() - 2)); @@ -152,16 +161,16 @@ public String buildCreateDatabaseSql(Database database) { return sqlBuilder.toString(); } - public String buildGenerateReorderColumnSql(Table oldTable, Table newTable) { + public String buildGenerateReorderColumnSql(Table oldTable, Table newTable, boolean modifyFlag) { StringBuilder sql = new StringBuilder(); - int n = 0; + int n = modifyFlag? 1 : 0; // Create a map to store the index of each column in the old table's column list Map oldColumnIndexMap = new HashMap<>(); for (int i = 0; i < oldTable.getColumnList().size(); i++) { oldColumnIndexMap.put(oldTable.getColumnList().get(i).getName(), i); } String[] oldColumnArray = oldTable.getColumnList().stream().map(TableColumn::getName).toArray(String[]::new); - String[] newColumnArray = newTable.getColumnList().stream().map(TableColumn::getName).toArray(String[]::new); + String[] newColumnArray = newTable.getColumnList().stream().map(TableColumn::getOldName).toArray(String[]::new); buildSql(oldColumnArray, newColumnArray, sql, oldTable, newTable, n); @@ -172,20 +181,28 @@ private String[] buildSql(String[] originalArray, String[] targetArray, StringBu // 先完成首位移动 if (!originalArray[0].equals(targetArray[0])) { int a = findIndex(originalArray, targetArray[0]); - TableColumn column = oldTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getName(), originalArray[a])).findFirst().get(); - String[] newArray = moveElement(originalArray, a, 0); - System.out.println(ArrayUtil.toString(newArray)); - sql.append(" MODIFY COLUMN "); - MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(column.getColumnType()); - sql.append(typeEnum.buildColumn(column)); - sql.append(" FIRST;\n"); - n++; - if (Arrays.equals(newArray, targetArray)) { - return newArray; - } - String[] resultArray = buildSql(newArray, targetArray, sql, oldTable, newTable, n); - if (Arrays.equals(resultArray, targetArray)) { - return resultArray; + if (a != -1) { + TableColumn column = newTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getOldName(), originalArray[a])).findFirst().get(); + String[] newArray = moveElement(originalArray, a, 0); + if (n > 0) { + sql.append("ALTER TABLE "); + if (StringUtils.isNotBlank(oldTable.getDatabaseName())) { + sql.append("`").append(oldTable.getDatabaseName()).append("`").append("."); + } + sql.append("`").append(oldTable.getName()).append("`").append("\n"); + } + sql.append(" MODIFY COLUMN "); + MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(column.getColumnType()); + sql.append(typeEnum.buildColumn(column)); + sql.append(" FIRST;\n"); + n++; + if (Arrays.equals(newArray, targetArray)) { + return newArray; + } + String[] resultArray = buildSql(newArray, targetArray, sql, oldTable, newTable, n); + if (Arrays.equals(resultArray, targetArray)) { + return resultArray; + } } } @@ -194,40 +211,44 @@ private String[] buildSql(String[] originalArray, String[] targetArray, StringBu if (!originalArray[max].equals(targetArray[max])) { int a = findIndex(originalArray, targetArray[max]); //System.out.println("Move " + originalArray[a] + " after " + (a > 0 ? originalArray[max] : "start")); - TableColumn column = oldTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getName(), originalArray[a])).findFirst().get(); - String[] newArray = moveElement(originalArray, a, max); - System.out.println(ArrayUtil.toString(newArray)); - if (n > 0) { - sql.append("ALTER TABLE "); - if (StringUtils.isNotBlank(oldTable.getDatabaseName())) { - sql.append("`").append(oldTable.getDatabaseName()).append("`").append("."); + if (a != -1) { + TableColumn column = newTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getOldName(), originalArray[a])).findFirst().get(); + String[] newArray = moveElement(originalArray, a, max); + //System.out.println(ArrayUtil.toString(newArray)); + if (n > 0) { + sql.append("ALTER TABLE "); + if (StringUtils.isNotBlank(oldTable.getDatabaseName())) { + sql.append("`").append(oldTable.getDatabaseName()).append("`").append("."); + } + sql.append("`").append(oldTable.getName()).append("`").append("\n"); + } + sql.append(" MODIFY COLUMN "); + MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(column.getColumnType()); + sql.append(typeEnum.buildColumn(column)); + sql.append(" "); + sql.append(" AFTER "); + String oldMaxName = oldTable.getColumnList().get(max).getName(); + TableColumn newOldColumn = newTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getOldName(), oldMaxName)).findFirst().get(); + sql.append(newOldColumn.getName()); + sql.append(";\n"); + n++; + if (Arrays.equals(newArray, targetArray)) { + return newArray; + } + String[] resultArray = buildSql(newArray, targetArray, sql, oldTable, newTable, n); + if (Arrays.equals(resultArray, targetArray)) { + return resultArray; } - sql.append("`").append(oldTable.getName()).append("`").append("\n"); - } - sql.append(" MODIFY COLUMN "); - MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(column.getColumnType()); - sql.append(typeEnum.buildColumn(column)); - sql.append(" "); - sql.append(" AFTER "); - sql.append(oldTable.getColumnList().get(max).getName()); - sql.append(";\n"); - n++; - if (Arrays.equals(newArray, targetArray)) { - return newArray; - } - String[] resultArray = buildSql(newArray, targetArray, sql, oldTable, newTable, n); - if (Arrays.equals(resultArray, targetArray)) { - return resultArray; } } for (int i = 0; i < originalArray.length; i++) { int a = findIndex(targetArray, originalArray[i]); - if (i != a && isMoveValid(originalArray, targetArray, i, a)) { + if (i != a && a != -1 && isMoveValid(originalArray, targetArray, i, a)) { // oldTable.getColumnList中查找name为a int finalI = i; - TableColumn column = oldTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getName(), originalArray[finalI])).findFirst().get(); + TableColumn column = newTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getOldName(), originalArray[finalI])).findFirst().get(); if (n > 0) { sql.append("ALTER TABLE "); if (StringUtils.isNotBlank(oldTable.getDatabaseName())) { @@ -241,9 +262,13 @@ private String[] buildSql(String[] originalArray, String[] targetArray, StringBu sql.append(" "); sql.append(" AFTER "); if (i < a) { - sql.append(originalArray[a]); + String oldName = oldTable.getColumnList().get(a).getName(); + TableColumn newOldColumn = newTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getOldName(), oldName)).findFirst().get(); + sql.append(newOldColumn.getName()); } else { - sql.append(originalArray[a - 1]); + String oldName = oldTable.getColumnList().get(a - 1).getName(); + TableColumn newOldColumn = newTable.getColumnList().stream().filter(col -> StringUtils.equals(col.getOldName(), oldName)).findFirst().get(); + sql.append(newOldColumn.getName()); } sql.append(";\n"); @@ -271,7 +296,6 @@ private static int findIndex(String[] array, String element) { } private static boolean isMoveValid(String[] originalArray, String[] targetArray, int i, int a) { - System.out.println("i : " + i + " a:" + a); return (i == 0 || a == 0 || !originalArray[i - 1].equals(targetArray[a - 1])) && (i >= originalArray.length - 1 || a >= targetArray.length - 1 || !originalArray[i + 1].equals(targetArray[a + 1])); } @@ -286,7 +310,6 @@ private static String[] moveElement(String[] originalArray, int from, int to) { System.arraycopy(originalArray, to, newArray, to + 1, from - to); } newArray[to] = temp; - System.out.println(ArrayUtil.toString(newArray)); return newArray; }