mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-28 10:12:39 +08:00
Support table schema edit
This commit is contained in:
@ -6,12 +6,17 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import ai.chat2db.plugin.sqlite.type.SqliteCollationEnum;
|
||||
import ai.chat2db.plugin.sqlite.type.SqliteColumnTypeEnum;
|
||||
import ai.chat2db.plugin.sqlite.type.SqliteIndexTypeEnum;
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.jdbc.DefaultMetaService;
|
||||
import ai.chat2db.spi.model.Database;
|
||||
import ai.chat2db.spi.model.Schema;
|
||||
import ai.chat2db.spi.model.TableMeta;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class SqliteMetaData extends DefaultMetaService implements MetaData {
|
||||
@Override
|
||||
@ -38,8 +43,19 @@ public class SqliteMetaData extends DefaultMetaService implements MetaData {
|
||||
return Lists.newArrayList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableMeta getTableMeta(String databaseName, String schemaName, String tableName) {
|
||||
return TableMeta.builder()
|
||||
.columnTypes(SqliteColumnTypeEnum.getTypes())
|
||||
.charsets(null)
|
||||
.collations(SqliteCollationEnum.getCollations())
|
||||
.indexTypes(SqliteIndexTypeEnum.getIndexTypes())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getMetaDataName(String... names) {
|
||||
return Arrays.stream(names).collect(Collectors.joining("."));
|
||||
return Arrays.stream(names).filter(name -> StringUtils.isBlank(name)).map(name -> "\"" + name + "\"").collect(Collectors.joining("."));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,106 @@
|
||||
package ai.chat2db.plugin.sqlite.builder;
|
||||
|
||||
import ai.chat2db.plugin.sqlite.type.SqliteColumnTypeEnum;
|
||||
import ai.chat2db.plugin.sqlite.type.SqliteIndexTypeEnum;
|
||||
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 SqliteBuilder 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()) {
|
||||
if(StringUtils.isBlank(column.getName())|| StringUtils.isBlank(column.getColumnType())){
|
||||
continue;
|
||||
}
|
||||
SqliteColumnTypeEnum typeEnum = SqliteColumnTypeEnum.getByType(column.getColumnType());
|
||||
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
|
||||
}
|
||||
|
||||
// append primary key and index
|
||||
for (TableIndex tableIndex : table.getIndexList()) {
|
||||
if(StringUtils.isBlank(tableIndex.getName())|| StringUtils.isBlank(tableIndex.getType())){
|
||||
continue;
|
||||
}
|
||||
SqliteIndexTypeEnum sqliteIndexTypeEnum = SqliteIndexTypeEnum.getByType(tableIndex.getType());
|
||||
script.append("\t").append("").append(sqliteIndexTypeEnum.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()) && StringUtils.isNotBlank(tableColumn.getColumnType())&& StringUtils.isNotBlank(tableColumn.getName())){
|
||||
SqliteColumnTypeEnum typeEnum = SqliteColumnTypeEnum.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()) && StringUtils.isNotBlank(tableIndex.getType())) {
|
||||
SqliteIndexTypeEnum sqliteIndexTypeEnum = SqliteIndexTypeEnum.getByType(tableIndex.getType());
|
||||
script.append("\t").append(sqliteIndexTypeEnum.buildModifyIndex(tableIndex)).append(",\n");
|
||||
}
|
||||
}
|
||||
|
||||
script = new StringBuilder(script.substring(0, script.length() - 2));
|
||||
script.append(";");
|
||||
|
||||
return script.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
//package ai.chat2db.plugin.sqlite.type;
|
||||
//
|
||||
//import ai.chat2db.spi.model.Charset;
|
||||
//
|
||||
//import java.util.Arrays;
|
||||
//import java.util.List;
|
||||
//
|
||||
//public enum SqliteCharsetEnum {
|
||||
//
|
||||
// UTF8("utf8", "utf8_general_ci"),
|
||||
// BIG5("big5", "big5_chinese_ci"),
|
||||
// DEC8("dec8", "dec8_swedish_ci"),
|
||||
// CP850("cp850", "cp850_general_ci"),
|
||||
// HP8("hp8", "hp8_english_ci"),
|
||||
// KOI8R("koi8r", "koi8r_general_ci"),
|
||||
// LATIN1("latin1", "latin1_swedish_ci"),
|
||||
// LATIN2("latin2", "latin2_general_ci"),
|
||||
// SWE7("swe7", "swe7_swedish_ci"),
|
||||
// ASCII("ascii", "ascii_general_ci"),
|
||||
// UJIS("ujis", "ujis_japanese_ci"),
|
||||
// SJIS("sjis", "sjis_japanese_ci"),
|
||||
// HEBREW("hebrew", "hebrew_general_ci"),
|
||||
// TIS620("tis620", "tis620_thai_ci"),
|
||||
// EUCKR("euckr", "euckr_korean_ci"),
|
||||
// KOI8U("koi8u", "koi8u_general_ci"),
|
||||
// GB2312("gb2312", "gb2312_chinese_ci"),
|
||||
// GREEK("greek", "greek_general_ci"),
|
||||
// CP1250("cp1250", "cp1250_general_ci"),
|
||||
// GBK("gbk", "gbk_chinese_ci"),
|
||||
// LATIN5("latin5", "latin5_turkish_ci"),
|
||||
// ARMSCII8("armscii8", "armscii8_general_ci"),
|
||||
// UCS2("ucs2", "ucs2_general_ci"),
|
||||
// CP866("cp866", "cp866_general_ci"),
|
||||
// KEYBCS2("keybcs2", "keybcs2_general_ci"),
|
||||
// MACCE("macce", "macce_general_ci"),
|
||||
// MACROMAN("macroman", "macroman_general_ci"),
|
||||
// CP852("cp852", "cp852_general_ci"),
|
||||
// LATIN7("latin7", "latin7_general_ci"),
|
||||
// UTF8MB4("utf8mb4", "utf8mb4_general_ci"),
|
||||
// CP1251("cp1251", "cp1251_general_ci"),
|
||||
// UTF16("utf16", "utf16_general_ci"),
|
||||
// UTF16LE("utf16le", "utf16le_general_ci"),
|
||||
// CP1256("cp1256", "cp1256_general_ci"),
|
||||
// CP1257("cp1257", "cp1257_general_ci"),
|
||||
// UTF32("utf32", "utf32_general_ci"),
|
||||
// BINARY("binary", "binary"),
|
||||
// GEOSTD8("geostd8", "geostd8_general_ci"),
|
||||
// CP932("cp932", "cp932_japanese_ci"),
|
||||
// EUCJPMS("eucjpms", "eucjpms_japanese_ci"),
|
||||
// GB18030("gb18030", "gb18030_chinese_ci");
|
||||
// private Charset charset;
|
||||
//
|
||||
// SqliteCharsetEnum(String charsetName, String defaultCollationName) {
|
||||
// this.charset = new Charset(charsetName, defaultCollationName);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public Charset getCharset() {
|
||||
// return charset;
|
||||
// }
|
||||
//
|
||||
// public static List<Charset> getCharsets() {
|
||||
// return Arrays.stream(SqliteCharsetEnum.values()).map(SqliteCharsetEnum::getCharset).collect(java.util.stream.Collectors.toList());
|
||||
// }
|
||||
//
|
||||
//}
|
@ -0,0 +1,31 @@
|
||||
package ai.chat2db.plugin.sqlite.type;
|
||||
|
||||
import ai.chat2db.spi.model.Collation;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public enum SqliteCollationEnum {
|
||||
|
||||
BINARY("BINARY"),
|
||||
|
||||
NOCASE("NOCASE"),
|
||||
|
||||
RTRIM("RTRIM"),
|
||||
;
|
||||
private Collation collation;
|
||||
|
||||
SqliteCollationEnum(String collationName) {
|
||||
this.collation = new Collation(collationName);
|
||||
}
|
||||
|
||||
public Collation getCollation() {
|
||||
return collation;
|
||||
}
|
||||
|
||||
|
||||
public static List<Collation> getCollations() {
|
||||
return Arrays.asList(SqliteCollationEnum.values()).stream().map(SqliteCollationEnum::getCollation).collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,187 @@
|
||||
package ai.chat2db.plugin.sqlite.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 SqliteColumnTypeEnum implements ColumnBuilder {
|
||||
|
||||
|
||||
INTEGER("INTEGER", true, false, true, false, false, true, true, false, false, false),
|
||||
|
||||
REAL("REAL", true, false, true, false, false, true, true, false, false, false),
|
||||
|
||||
BLOB("BLOB", true, false, true, false, false, true, true, false, false, false),
|
||||
|
||||
|
||||
TEXT("TEXT", true, false, true, false, false, true, true, false, false, false),
|
||||
|
||||
;
|
||||
private ColumnType columnType;
|
||||
|
||||
public static SqliteColumnTypeEnum getByType(String dataType) {
|
||||
return COLUMN_TYPE_MAP.get(dataType.toUpperCase());
|
||||
}
|
||||
|
||||
public ColumnType getColumnType() {
|
||||
return columnType;
|
||||
}
|
||||
|
||||
|
||||
SqliteColumnTypeEnum(String dataTypeName, boolean supportLength, boolean supportScale, boolean supportNullable, boolean supportAutoIncrement, boolean supportCharset, boolean supportCollation, boolean supportComments, boolean supportDefaultValue, boolean supportExtent, boolean supportValue) {
|
||||
this.columnType = new ColumnType(dataTypeName, supportLength, supportScale, supportNullable, supportAutoIncrement, supportCharset, supportCollation, supportComments, supportDefaultValue, supportExtent, supportValue, false);
|
||||
}
|
||||
|
||||
private static Map<String, SqliteColumnTypeEnum> COLUMN_TYPE_MAP = Maps.newHashMap();
|
||||
|
||||
static {
|
||||
for (SqliteColumnTypeEnum value : SqliteColumnTypeEnum.values()) {
|
||||
COLUMN_TYPE_MAP.put(value.getColumnType().getTypeName(), value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String buildCreateColumnSql(TableColumn column) {
|
||||
SqliteColumnTypeEnum 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(buildCharset(column, type)).append(" ");
|
||||
|
||||
script.append(buildCollation(column, type)).append(" ");
|
||||
|
||||
script.append(buildNullable(column, type)).append(" ");
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
private String buildCharset(TableColumn column, SqliteColumnTypeEnum type) {
|
||||
if (!type.getColumnType().isSupportCharset() || StringUtils.isEmpty(column.getCharSetName())) {
|
||||
return "";
|
||||
}
|
||||
return StringUtils.join("CHARACTER SET ", column.getCharSetName());
|
||||
}
|
||||
|
||||
private String buildCollation(TableColumn column, SqliteColumnTypeEnum type) {
|
||||
if (!type.getColumnType().isSupportCollation() || StringUtils.isEmpty(column.getCollationName())) {
|
||||
return "";
|
||||
}
|
||||
return StringUtils.join("COLLATE ", column.getCollationName());
|
||||
}
|
||||
|
||||
@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, SqliteColumnTypeEnum type) {
|
||||
if (!type.getColumnType().isSupportAutoIncrement()) {
|
||||
return "";
|
||||
}
|
||||
if (column.getAutoIncrement() != null && column.getAutoIncrement()) {
|
||||
return "AUTO_INCREMENT";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private String buildComment(TableColumn column, SqliteColumnTypeEnum type) {
|
||||
if (!type.columnType.isSupportComments() || StringUtils.isEmpty(column.getComment())) {
|
||||
return "";
|
||||
}
|
||||
return StringUtils.join("COMMENT '", column.getComment(), "'");
|
||||
}
|
||||
|
||||
private String buildExt(TableColumn column, SqliteColumnTypeEnum type) {
|
||||
if (!type.columnType.isSupportExtent() || StringUtils.isEmpty(column.getExtent())) {
|
||||
return "";
|
||||
}
|
||||
return column.getComment();
|
||||
}
|
||||
|
||||
private String buildDefaultValue(TableColumn column, SqliteColumnTypeEnum type) {
|
||||
if (!type.getColumnType().isSupportDefaultValue() || StringUtils.isEmpty(column.getDefaultValue())) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if ("EMPTY_STRING".equalsIgnoreCase(column.getDefaultValue().trim())) {
|
||||
return StringUtils.join("DEFAULT ''");
|
||||
}
|
||||
|
||||
if ("NULL".equalsIgnoreCase(column.getDefaultValue().trim())) {
|
||||
return StringUtils.join("DEFAULT NULL");
|
||||
}
|
||||
|
||||
return StringUtils.join("DEFAULT ", column.getDefaultValue());
|
||||
}
|
||||
|
||||
private String buildNullable(TableColumn column, SqliteColumnTypeEnum type) {
|
||||
if (!type.getColumnType().isSupportNullable()) {
|
||||
return "";
|
||||
}
|
||||
if (column.getNullable() != null && 1 == column.getNullable()) {
|
||||
return "NULL";
|
||||
} else {
|
||||
return "NOT NULL";
|
||||
}
|
||||
}
|
||||
|
||||
private String buildDataType(TableColumn column, SqliteColumnTypeEnum type) {
|
||||
String columnType = type.columnType.getTypeName();
|
||||
|
||||
if (column.getColumnSize() == null || column.getDecimalDigits() == null) {
|
||||
return columnType;
|
||||
}
|
||||
if (column.getColumnSize() != null && column.getDecimalDigits() == null) {
|
||||
return StringUtils.join(columnType, "(", column.getColumnSize() + ")");
|
||||
}
|
||||
if (column.getColumnSize() != null && column.getDecimalDigits() != null) {
|
||||
return StringUtils.join(columnType, "(", column.getColumnSize() + "," + column.getDecimalDigits() + ")");
|
||||
}
|
||||
return columnType;
|
||||
}
|
||||
|
||||
|
||||
public static List<ColumnType> getTypes() {
|
||||
return Arrays.stream(SqliteColumnTypeEnum.values()).map(columnTypeEnum ->
|
||||
columnTypeEnum.getColumnType()
|
||||
).toList();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
package ai.chat2db.plugin.sqlite.type;
|
||||
|
||||
import ai.chat2db.spi.enums.EditStatus;
|
||||
import ai.chat2db.spi.model.Collation;
|
||||
import ai.chat2db.spi.model.IndexType;
|
||||
import ai.chat2db.spi.model.TableIndex;
|
||||
import ai.chat2db.spi.model.TableIndexColumn;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public enum SqliteIndexTypeEnum {
|
||||
|
||||
PRIMARY_KEY("Primary", "PRIMARY KEY"),
|
||||
|
||||
NORMAL("Normal", "INDEX"),
|
||||
|
||||
UNIQUE("Unique", "UNIQUE INDEX");
|
||||
|
||||
|
||||
public IndexType getIndexType() {
|
||||
return indexType;
|
||||
}
|
||||
|
||||
public void setIndexType(IndexType indexType) {
|
||||
this.indexType = indexType;
|
||||
}
|
||||
|
||||
private IndexType indexType;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
public String getKeyword() {
|
||||
return keyword;
|
||||
}
|
||||
|
||||
private String keyword;
|
||||
|
||||
SqliteIndexTypeEnum(String name, String keyword) {
|
||||
this.name = name;
|
||||
this.keyword = keyword;
|
||||
this.indexType = new IndexType(name);
|
||||
}
|
||||
|
||||
|
||||
public static SqliteIndexTypeEnum getByType(String type) {
|
||||
for (SqliteIndexTypeEnum value : SqliteIndexTypeEnum.values()) {
|
||||
if (value.name.equalsIgnoreCase(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()) {
|
||||
if(StringUtils.isNotBlank(column.getColumnName())) {
|
||||
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.MODIFY.name().equals(tableIndex.getEditStatus())) {
|
||||
return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex));
|
||||
}
|
||||
if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) {
|
||||
return StringUtils.join("ADD ", buildIndexScript(tableIndex));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private String buildDropIndex(TableIndex tableIndex) {
|
||||
if (SqliteIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) {
|
||||
return StringUtils.join("DROP PRIMARY KEY");
|
||||
}
|
||||
return StringUtils.join("DROP INDEX `", tableIndex.getOldName(),"`");
|
||||
}
|
||||
public static List<IndexType> getIndexTypes() {
|
||||
return Arrays.asList(SqliteIndexTypeEnum.values()).stream().map(SqliteIndexTypeEnum::getIndexType).collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user