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;
}
if(!SqliteIndexTypeEnum.PRIMARY_KEY.getName().equals( tableIndex.getType())) {
SqliteIndexTypeEnum sqliteIndexTypeEnum = SqliteIndexTypeEnum.getByType(tableIndex.getType());
script.append("\t").append("").append(sqliteIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
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,6 +59,9 @@ 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(" ");
@ -66,8 +69,15 @@ public enum SqliteIndexTypeEnum {
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("CONSTRAINT ").append(buildIndexName(tableIndex)).append(" ").append(keyword).append(" ").append(buildIndexColumn(tableIndex));
return script.toString();
}
@ -95,7 +105,7 @@ public enum SqliteIndexTypeEnum {
private String buildIndexName(TableIndex tableIndex) {
if (this.equals(PRIMARY_KEY)) {
return "";
return tableIndex.getTableName()+"_pk";
} else {
return "\"" + tableIndex.getName() + "\"";
}
@ -120,6 +130,7 @@ public enum SqliteIndexTypeEnum {
}
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());
}

View File

@ -241,6 +241,9 @@ public class TableServiceImpl implements TableService {
keyIndex.setSchemaName(newTable.getSchemaName());
keyIndex.setDatabaseName(newTable.getDatabaseName());
keyIndex.setEditStatus(status);
if(!EditStatus.ADD.name().equals(status)){
keyIndex.setOldName(keyIndex.getName());
}
indexes.add(keyIndex);
}
List<TableIndexColumn> tableIndexColumns = keyIndex.getColumnList();