support table primary key

This commit is contained in:
SwallowGG
2023-11-06 15:47:42 +08:00
parent 9b3adb18ee
commit b9d8929fc0
3 changed files with 44 additions and 26 deletions

View File

@ -25,21 +25,25 @@ public class SqliteBuilder extends DefaultSqlBuilder implements SqlBuilder {
SqliteColumnTypeEnum typeEnum = SqliteColumnTypeEnum.getByType(column.getColumnType());
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
}
for (TableIndex tableIndex : table.getIndexList()) {
if(SqliteIndexTypeEnum.PRIMARY_KEY.getName().equals( tableIndex.getType())) {
SqliteIndexTypeEnum sqliteIndexTypeEnum = SqliteIndexTypeEnum.getByType(tableIndex.getType());
script.append("\t").append(sqliteIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
}
}
script = new StringBuilder(script.substring(0, script.length() - 2));
script.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");
if(!SqliteIndexTypeEnum.PRIMARY_KEY.getName().equals( tableIndex.getType())) {
SqliteIndexTypeEnum sqliteIndexTypeEnum = SqliteIndexTypeEnum.getByType(tableIndex.getType());
script.append("\n").append("CREATE ").append(sqliteIndexTypeEnum.buildIndexScript(tableIndex)).append(";\n");
}
}
script = new StringBuilder(script.substring(0, script.length() - 2));
script.append("\n)");
script.append(";");
return script.toString();
}

View File

@ -59,23 +59,33 @@ public enum SqliteIndexTypeEnum {
}
public String buildIndexScript(TableIndex tableIndex) {
if (this.equals(PRIMARY_KEY)) {
return buildPrimaryKeyScript(tableIndex);
} else {
StringBuilder script = new StringBuilder();
script.append(keyword).append(" ");
script.append(buildIndexName(tableIndex)).append(" ON ").append(tableIndex.getTableName()).append(" ");
script.append(buildIndexColumn(tableIndex)).append(" ");
return script.toString();
}
}
private String buildPrimaryKeyScript(TableIndex tableIndex) {
StringBuilder script = new StringBuilder();
script.append(keyword).append(" ");
script.append(buildIndexName(tableIndex)).append(" ON ").append(tableIndex.getTableName()).append(" ");
script.append(buildIndexColumn(tableIndex)).append(" ");
script.append("CONSTRAINT ").append(buildIndexName(tableIndex)).append(" ").append(keyword).append(" ").append(buildIndexColumn(tableIndex));
return script.toString();
}
private String buildIndexComment(TableIndex tableIndex) {
if(StringUtils.isBlank(tableIndex.getComment())){
if (StringUtils.isBlank(tableIndex.getComment())) {
return "";
}else {
return StringUtils.join("COMMENT '",tableIndex.getComment(),"'");
} else {
return StringUtils.join("COMMENT '", tableIndex.getComment(), "'");
}
}
@ -84,7 +94,7 @@ public enum SqliteIndexTypeEnum {
StringBuilder script = new StringBuilder();
script.append("(");
for (TableIndexColumn column : tableIndex.getColumnList()) {
if(StringUtils.isNotBlank(column.getColumnName())) {
if (StringUtils.isNotBlank(column.getColumnName())) {
script.append("\"").append(column.getColumnName()).append("\"").append(",");
}
}
@ -94,10 +104,10 @@ public enum SqliteIndexTypeEnum {
}
private String buildIndexName(TableIndex tableIndex) {
if(this.equals(PRIMARY_KEY)){
return "";
}else {
return "\""+tableIndex.getName()+"\"";
if (this.equals(PRIMARY_KEY)) {
return tableIndex.getTableName()+"_pk";
} else {
return "\"" + tableIndex.getName() + "\"";
}
}
@ -106,7 +116,7 @@ public enum SqliteIndexTypeEnum {
return buildDropIndex(tableIndex);
}
if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) {
return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex));
return StringUtils.join(buildDropIndex(tableIndex), ",\n", "ADD ", buildIndexScript(tableIndex));
}
if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) {
return StringUtils.join("CREATE ", buildIndexScript(tableIndex));
@ -118,8 +128,9 @@ public enum SqliteIndexTypeEnum {
if (SqliteIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) {
return StringUtils.join("DROP PRIMARY KEY");
}
return StringUtils.join("DROP INDEX \"", tableIndex.getOldName(),"\"");
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());
}