feat: support export specified tables
This commit is contained in:
parent
061b6d7f02
commit
38ba1f7e7d
|
@ -74,7 +74,10 @@ public class DocumentController {
|
|||
public ResponseEntity<StreamingResponseBody> getDocumentFiles(@PathVariable Integer projectId,
|
||||
@RequestParam(required = false)
|
||||
Long version,
|
||||
@RequestParam DocumentFileType fileType) {
|
||||
@RequestParam(required = false)
|
||||
List<Integer> tableIds,
|
||||
@RequestParam
|
||||
DocumentFileType fileType) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
String projectName = projectService.getOne(projectId).getName();
|
||||
String fileName = projectName + "." + fileType.getFileExtension();
|
||||
|
@ -84,7 +87,7 @@ public class DocumentController {
|
|||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.body(out -> documentService.export(projectId, version, fileType, out));
|
||||
.body(out -> documentService.export(projectId, version, tableIds, fileType, out));
|
||||
}
|
||||
|
||||
@GetMapping(Routes.Document.EXPORT_TYPES)
|
||||
|
|
|
@ -4,7 +4,6 @@ import com.databasir.common.SystemException;
|
|||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.databasir.core.domain.document.data.TableDocumentResponse;
|
||||
import com.databasir.core.domain.document.generator.DocumentFileGenerator;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sourceforge.plantuml.FileFormatOption;
|
||||
import net.sourceforge.plantuml.SourceStringReader;
|
||||
|
@ -34,13 +33,24 @@ public abstract class BasePlantUmlFileGenerator implements DocumentFileGenerator
|
|||
|
||||
protected abstract FileFormatOption fileFormatOption();
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class ErDsl {
|
||||
|
||||
private final DocumentFileGenerateContext context;
|
||||
|
||||
private Set<String> foreignKeyRelations = new HashSet<>(16);
|
||||
|
||||
private Set<String> tables = new HashSet<>(16);
|
||||
|
||||
public ErDsl(DocumentFileGenerateContext context) {
|
||||
this.context = context;
|
||||
Set<String> tables = context.getDatabaseDocument()
|
||||
.getTables()
|
||||
.stream()
|
||||
.map(TableDocumentResponse::getName)
|
||||
.collect(Collectors.toSet());
|
||||
this.tables = tables;
|
||||
}
|
||||
|
||||
public String toDsl() {
|
||||
DatabaseDocumentResponse databaseDocument = context.getDatabaseDocument();
|
||||
StringBuilder dslBuilder = new StringBuilder(1024);
|
||||
|
@ -115,19 +125,22 @@ public abstract class BasePlantUmlFileGenerator implements DocumentFileGenerator
|
|||
dslBuilder.append("}");
|
||||
dslBuilder.append(LINE);
|
||||
|
||||
table.getForeignKeys().forEach(fk -> {
|
||||
String fkTableName = fk.getFkTableName();
|
||||
String fkColumnName = fk.getFkColumnName();
|
||||
String pkTableName = fk.getPkTableName();
|
||||
String pkColumnName = fk.getPkColumnName();
|
||||
StringBuilder relationBuilder = new StringBuilder();
|
||||
relationBuilder.append(fkTableName).append("::").append(fkColumnName)
|
||||
.append(" --> ")
|
||||
.append(pkTableName).append("::").append(pkColumnName)
|
||||
.append(" : ")
|
||||
.append(Objects.requireNonNullElse(fk.getFkName(), ""));
|
||||
foreignKeyRelations.add(relationBuilder.toString());
|
||||
});
|
||||
table.getForeignKeys()
|
||||
.stream()
|
||||
.filter(fk -> tables.contains(fk.getFkTableName()) && tables.contains(fk.getPkTableName()))
|
||||
.forEach(fk -> {
|
||||
String fkTableName = fk.getFkTableName();
|
||||
String fkColumnName = fk.getFkColumnName();
|
||||
String pkTableName = fk.getPkTableName();
|
||||
String pkColumnName = fk.getPkColumnName();
|
||||
StringBuilder relationBuilder = new StringBuilder();
|
||||
relationBuilder.append(fkTableName).append("::").append(fkColumnName)
|
||||
.append(" --> ")
|
||||
.append(pkTableName).append("::").append(pkColumnName)
|
||||
.append(" : ")
|
||||
.append(Objects.requireNonNullElse(fk.getFkName(), ""));
|
||||
foreignKeyRelations.add(relationBuilder.toString());
|
||||
});
|
||||
return dslBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.databasir.core.domain.document.data.*;
|
|||
import com.databasir.core.domain.document.data.TableDocumentResponse.ForeignKeyDocumentResponse;
|
||||
import com.databasir.core.domain.document.event.DocumentUpdated;
|
||||
import com.databasir.core.domain.document.generator.DocumentFileGenerator;
|
||||
import com.databasir.core.domain.document.generator.DocumentFileGenerator.DocumentFileGenerateContext;
|
||||
import com.databasir.core.domain.document.generator.DocumentFileType;
|
||||
import com.databasir.core.infrastructure.connection.DatabaseConnectionService;
|
||||
import com.databasir.core.infrastructure.converter.JsonConverter;
|
||||
|
@ -526,19 +527,34 @@ public class DocumentService {
|
|||
|
||||
public void export(Integer projectId,
|
||||
Long version,
|
||||
List<Integer> tableIds,
|
||||
DocumentFileType type,
|
||||
OutputStream out) {
|
||||
getOneByProjectId(projectId, version)
|
||||
.ifPresent(doc -> {
|
||||
var context = DocumentFileGenerator.DocumentFileGenerateContext.builder()
|
||||
.documentFileType(type)
|
||||
.databaseDocument(doc)
|
||||
.build();
|
||||
documentFileGenerators.stream()
|
||||
.filter(g -> g.support(type))
|
||||
.findFirst()
|
||||
.ifPresent(generator -> generator.generate(context, out));
|
||||
});
|
||||
DatabaseDocumentResponse doc;
|
||||
if (tableIds == null || CollectionUtils.isEmpty(tableIds)) {
|
||||
doc = getOneByProjectId(projectId, version)
|
||||
.orElseThrow(DomainErrors.DOCUMENT_VERSION_IS_INVALID::exception);
|
||||
} else {
|
||||
DatabaseDocumentPojo databaseDoc;
|
||||
if (version == null) {
|
||||
databaseDoc = databaseDocumentDao.selectNotArchivedByProjectId(projectId)
|
||||
.orElseThrow(DomainErrors.DOCUMENT_VERSION_IS_INVALID::exception);
|
||||
} else {
|
||||
databaseDoc = databaseDocumentDao.selectOptionalByProjectIdAndVersion(projectId, version)
|
||||
.orElseThrow(DomainErrors.DOCUMENT_VERSION_IS_INVALID::exception);
|
||||
}
|
||||
Integer databaseDocId = databaseDoc.getId();
|
||||
List<TableDocumentResponse> tableDocs = getTableDetails(projectId, databaseDocId, tableIds);
|
||||
doc = documentResponseConverter.of(databaseDoc, tableDocs);
|
||||
}
|
||||
var context = DocumentFileGenerateContext.builder()
|
||||
.documentFileType(type)
|
||||
.databaseDocument(doc)
|
||||
.build();
|
||||
documentFileGenerators.stream()
|
||||
.filter(g -> g.support(type))
|
||||
.findFirst()
|
||||
.ifPresent(generator -> generator.generate(context, out));
|
||||
}
|
||||
|
||||
public List<TableResponse> getTableAndColumns(Integer projectId, Long version) {
|
||||
|
|
|
@ -43,6 +43,14 @@ public class DatabaseDocumentDao extends BaseDao<DatabaseDocumentPojo> {
|
|||
.fetchOptionalInto(DatabaseDocumentPojo.class);
|
||||
}
|
||||
|
||||
public Optional<Integer> selectIdByProjectIdAndVersion(Integer projectId,
|
||||
Long version) {
|
||||
return getDslContext()
|
||||
.select(DATABASE_DOCUMENT.ID).from(DATABASE_DOCUMENT)
|
||||
.where(DATABASE_DOCUMENT.PROJECT_ID.eq(projectId).and(DATABASE_DOCUMENT.VERSION.eq(version)))
|
||||
.fetchOptionalInto(Integer.class);
|
||||
}
|
||||
|
||||
public void update(DatabaseDocumentPojo toPojo) {
|
||||
DatabaseDocumentRecord record = getDslContext().newRecord(DATABASE_DOCUMENT, toPojo);
|
||||
record.changed(DATABASE_DOCUMENT.ID, false);
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 7bac0c7f123c89a1e70c82d43f2c7c7d061dd943
|
||||
Subproject commit 05b6e6761238bcda003a787cb66c51a002c810c7
|
Loading…
Reference in New Issue