mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 10:43:06 +08:00
support table online edit
This commit is contained in:
@ -4,7 +4,9 @@ import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ai.chat2db.plugin.mysql.builder.MysqlSqlBuilder;
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.SqlBuilder;
|
||||
import ai.chat2db.spi.jdbc.DefaultMetaService;
|
||||
import ai.chat2db.spi.model.Function;
|
||||
import ai.chat2db.spi.model.Procedure;
|
||||
@ -133,4 +135,8 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
|
||||
return table;
|
||||
});
|
||||
}
|
||||
@Override
|
||||
public SqlBuilder getSqlBuilder() {
|
||||
return new MysqlSqlBuilder();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,100 @@
|
||||
package ai.chat2db.plugin.mysql.builder;
|
||||
|
||||
import ai.chat2db.plugin.mysql.type.MysqlColumnTypeEnum;
|
||||
import ai.chat2db.plugin.mysql.type.MysqlIndexTypeEnum;
|
||||
import ai.chat2db.spi.SqlBuilder;
|
||||
import ai.chat2db.spi.model.Table;
|
||||
import ai.chat2db.spi.model.TableColumn;
|
||||
import ai.chat2db.spi.model.TableIndex;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
||||
public class MysqlSqlBuilder implements SqlBuilder {
|
||||
@Override
|
||||
public String buildCreateTableSql(Table table) {
|
||||
StringBuilder script = new StringBuilder();
|
||||
script.append("CREATE TABLE ");
|
||||
script.append("`").append(table.getName()).append("`").append(" (").append("\n");
|
||||
|
||||
// append column
|
||||
for (TableColumn column : table.getColumnList()) {
|
||||
MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(column.getColumnType());
|
||||
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
|
||||
}
|
||||
|
||||
// append primary key and index
|
||||
for (TableIndex tableIndex : table.getIndexList()) {
|
||||
MysqlIndexTypeEnum mysqlIndexTypeEnum = MysqlIndexTypeEnum.getByType(tableIndex.getType());
|
||||
script.append("\t").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
|
||||
}
|
||||
|
||||
script = new StringBuilder(script.substring(0, script.length() - 2));
|
||||
script.append("\n)");
|
||||
|
||||
|
||||
if (StringUtils.isNotBlank(table.getEngine())) {
|
||||
script.append(" ENGINE=").append(table.getEngine());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(table.getCharset())) {
|
||||
script.append(" DEFAULT CHARACTER SET=").append(table.getCharset());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(table.getCollate())) {
|
||||
script.append(" COLLATE=").append(table.getCollate());
|
||||
}
|
||||
|
||||
if (table.getIncrementValue() != null) {
|
||||
script.append(" AUTO_INCREMENT=").append(table.getIncrementValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(table.getComment())) {
|
||||
script.append(" COMMENT='").append(table.getComment()).append("'");
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(table.getPartition())) {
|
||||
script.append(" \n").append(table.getPartition());
|
||||
}
|
||||
script.append(";");
|
||||
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String buildModifyTaleSql(Table oldTable, Table newTable) {
|
||||
StringBuilder script = new StringBuilder();
|
||||
script.append("ALTER TABLE ").append("`").append(oldTable.getName()).append("`").append("\n");
|
||||
if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) {
|
||||
script.append("\t").append("RENAME TO ").append("`").append(newTable.getName()).append("`").append(",\n");
|
||||
}
|
||||
if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) {
|
||||
script.append("\t").append("COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n");
|
||||
}
|
||||
if (oldTable.getIncrementValue() != newTable.getIncrementValue()) {
|
||||
script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n");
|
||||
}
|
||||
|
||||
// append modify column
|
||||
for (TableColumn tableColumn : newTable.getColumnList()) {
|
||||
if (StringUtils.isNotBlank(tableColumn.getEditStatus())) {
|
||||
MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(tableColumn.getColumnType());
|
||||
script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n");
|
||||
}
|
||||
}
|
||||
|
||||
// append modify index
|
||||
for (TableIndex tableIndex : newTable.getIndexList()) {
|
||||
if (StringUtils.isNotBlank(tableIndex.getEditStatus())) {
|
||||
MysqlIndexTypeEnum mysqlIndexTypeEnum = MysqlIndexTypeEnum.getByType(tableIndex.getType());
|
||||
script.append("\t").append(mysqlIndexTypeEnum.buildModifyIndex(tableIndex)).append(",\n");
|
||||
}
|
||||
}
|
||||
|
||||
script = new StringBuilder(script.substring(0, script.length() - 2));
|
||||
script.append(";");
|
||||
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
package ai.chat2db.plugin.mysql.type;
|
||||
|
||||
import ai.chat2db.spi.ColumnBuilder;
|
||||
import ai.chat2db.spi.enums.EditStatus;
|
||||
import ai.chat2db.spi.model.ColumnType;
|
||||
import ai.chat2db.spi.model.TableColumn;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public enum MysqlColumnTypeEnum implements ColumnBuilder {
|
||||
@ -114,6 +114,10 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
|
||||
|
||||
private ColumnType columnType;
|
||||
|
||||
public static MysqlColumnTypeEnum getByType(String dataType) {
|
||||
return COLUMN_TYPE_MAP.get(dataType.toUpperCase());
|
||||
}
|
||||
|
||||
public ColumnType getColumnType() {
|
||||
return columnType;
|
||||
}
|
||||
@ -133,17 +137,104 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
|
||||
|
||||
|
||||
@Override
|
||||
public String generateColumnSql(TableColumn column) {
|
||||
public String buildCreateColumnSql(TableColumn column) {
|
||||
MysqlColumnTypeEnum type = COLUMN_TYPE_MAP.get(column.getColumnType().toUpperCase());
|
||||
if (type == null) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder script = new StringBuilder();
|
||||
|
||||
script.append("`").append(column.getName()).append("`").append(" ");
|
||||
|
||||
script.append(buildDataType(column, type)).append(" ");
|
||||
|
||||
script.append(buildNullable(column,type)).append(" ");
|
||||
|
||||
return null;
|
||||
script.append(buildDefaultValue(column,type)).append(" ");
|
||||
|
||||
script.append(buildExt(column,type)).append(" ");
|
||||
|
||||
script.append(buildAutoIncrement(column,type)).append(" ");
|
||||
|
||||
script.append(buildComment(column,type)).append(" ");
|
||||
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String buildModifyColumn(TableColumn tableColumn) {
|
||||
|
||||
if (EditStatus.DELETE.name().equals(tableColumn.getEditStatus())) {
|
||||
return StringUtils.join("DROP COLUMN `", tableColumn.getName() + "`");
|
||||
}
|
||||
if (EditStatus.ADD.name().equals(tableColumn.getEditStatus())) {
|
||||
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 "";
|
||||
}
|
||||
if (column.getAutoIncrement() != null && column.getAutoIncrement()) {
|
||||
return "AUTO_INCREMENT";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private String buildComment(TableColumn column, MysqlColumnTypeEnum type) {
|
||||
if(!type.columnType.isSupportComments() || StringUtils.isEmpty(column.getComment())){
|
||||
return "";
|
||||
}
|
||||
return StringUtils.join("COMMENT '",column.getComment(),"'");
|
||||
}
|
||||
|
||||
private String buildExt(TableColumn column, MysqlColumnTypeEnum type) {
|
||||
if(!type.columnType.isSupportExtent() || StringUtils.isEmpty(column.getExtent())){
|
||||
return "";
|
||||
}
|
||||
return column.getComment();
|
||||
}
|
||||
|
||||
private String buildDefaultValue(TableColumn column, MysqlColumnTypeEnum type) {
|
||||
if(!type.getColumnType().isSupportDefaultValue() || StringUtils.isEmpty(column.getDefaultValue())){
|
||||
return "";
|
||||
}
|
||||
if(Arrays.asList(CHAR,VARCHAR,BINARY,VARBINARY, SET,ENUM).contains(type)){
|
||||
return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'");
|
||||
}
|
||||
|
||||
if(Arrays.asList(DATE,TIME,YEAR).contains(type)){
|
||||
return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'");
|
||||
}
|
||||
|
||||
if(Arrays.asList(DATETIME,TIMESTAMP).contains(type)){
|
||||
if("CURRENT_TIMESTAMP".equalsIgnoreCase(column.getDefaultValue())){
|
||||
return StringUtils.join("DEFAULT ",column.getDefaultValue());
|
||||
}
|
||||
return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'");
|
||||
}
|
||||
|
||||
return StringUtils.join("DEFAULT ",column.getDefaultValue());
|
||||
}
|
||||
|
||||
private String buildNullable(TableColumn column,MysqlColumnTypeEnum type) {
|
||||
if(!type.getColumnType().isSupportNullable()){
|
||||
return "";
|
||||
}
|
||||
if (1==column.getNullable()) {
|
||||
return "NULL";
|
||||
} else {
|
||||
return "NOT NULL";
|
||||
}
|
||||
}
|
||||
|
||||
private String buildDataType(TableColumn column, MysqlColumnTypeEnum type) {
|
||||
@ -190,6 +281,9 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
|
||||
}
|
||||
|
||||
if(Arrays.asList(SET,ENUM).contains(type)){
|
||||
if(!StringUtils.isEmpty( column.getDefaultValue())){
|
||||
return StringUtils.join(columnType,"(",column.getDefaultValue(),")");
|
||||
}
|
||||
//List<String> enumList = column.
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,109 @@
|
||||
package ai.chat2db.plugin.mysql.type;
|
||||
|
||||
import ai.chat2db.spi.enums.EditStatus;
|
||||
import ai.chat2db.spi.model.TableIndex;
|
||||
import ai.chat2db.spi.model.TableIndexColumn;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public enum MysqlIndexTypeEnum {
|
||||
|
||||
PRIMARY_KEY("Primary", "PRIMARY KEY"),
|
||||
|
||||
NORMAL("Normal", "KEY"),
|
||||
|
||||
UNIQUE("Unique", "UNIQUE KEY"),
|
||||
|
||||
FULLTEXT("Fulltext", "FULLTEXT KEY"),
|
||||
|
||||
SPATIAL("Spatial", "SPATIAL KEY");
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
public String getKeyword() {
|
||||
return keyword;
|
||||
}
|
||||
|
||||
private String keyword;
|
||||
|
||||
MysqlIndexTypeEnum(String name, String keyword) {
|
||||
this.name = name;
|
||||
this.keyword = keyword;
|
||||
}
|
||||
|
||||
|
||||
public static MysqlIndexTypeEnum getByType(String type) {
|
||||
for (MysqlIndexTypeEnum value : MysqlIndexTypeEnum.values()) {
|
||||
if (value.name.equals(type)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String buildIndexScript(TableIndex tableIndex) {
|
||||
StringBuilder script = new StringBuilder();
|
||||
|
||||
script.append(keyword).append(" ");
|
||||
|
||||
script.append(buildIndexName(tableIndex)).append(" ");
|
||||
|
||||
script.append(buildIndexColumn(tableIndex)).append(" ");
|
||||
|
||||
script.append(buildIndexComment(tableIndex)).append(" ");
|
||||
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
private String buildIndexComment(TableIndex tableIndex) {
|
||||
if(StringUtils.isBlank(tableIndex.getComment())){
|
||||
return "";
|
||||
}else {
|
||||
return StringUtils.join("COMMENT '",tableIndex.getComment(),"'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String buildIndexColumn(TableIndex tableIndex) {
|
||||
StringBuilder script = new StringBuilder();
|
||||
script.append("(");
|
||||
for (TableIndexColumn column : tableIndex.getColumnList()) {
|
||||
script.append("`").append(column.getColumnName()).append("`").append(",");
|
||||
}
|
||||
script.deleteCharAt(script.length() - 1);
|
||||
script.append(")");
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
private String buildIndexName(TableIndex tableIndex) {
|
||||
if(this.equals(PRIMARY_KEY)){
|
||||
return "";
|
||||
}else {
|
||||
return "`"+tableIndex.getName()+"`";
|
||||
}
|
||||
}
|
||||
|
||||
public String buildModifyIndex(TableIndex tableIndex) {
|
||||
if (EditStatus.DELETE.name().equals(tableIndex.getEditStatus())) {
|
||||
return buildDropIndex(tableIndex);
|
||||
}
|
||||
if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) {
|
||||
return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex));
|
||||
}
|
||||
if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) {
|
||||
return StringUtils.join("MODIFY ", buildIndexScript(tableIndex));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private String buildDropIndex(TableIndex tableIndex) {
|
||||
if (MysqlIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) {
|
||||
return StringUtils.join("DROP PRIMARY KEY");
|
||||
}
|
||||
return StringUtils.join("DROP KEY `", tableIndex.getName());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user