Merge pull request #209 from vran-dev/issue-208

feature: support export specified tables
This commit is contained in:
vran 2022-05-26 09:40:59 +08:00 committed by GitHub
commit 1f18009125
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 84 additions and 44 deletions

View File

@ -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)

View File

@ -1 +1 @@
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>databasir</title><script defer="defer" type="module" src="/js/chunk-vendors.661f96f0.js"></script><script defer="defer" type="module" src="/js/app.98502fba.js"></script><link href="/css/chunk-vendors.113af7af.css" rel="stylesheet"><link href="/css/app.ee11d130.css" rel="stylesheet"><script defer="defer" src="/js/chunk-vendors-legacy.fc4c9525.js" nomodule></script><script defer="defer" src="/js/app-legacy.25171435.js" nomodule></script></head><body><noscript><strong>We're sorry but databasir doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>databasir</title><script defer="defer" type="module" src="/js/chunk-vendors.661f96f0.js"></script><script defer="defer" type="module" src="/js/app.7bd4174c.js"></script><link href="/css/chunk-vendors.113af7af.css" rel="stylesheet"><link href="/css/app.ee11d130.css" rel="stylesheet"><script defer="defer" src="/js/chunk-vendors-legacy.fc4c9525.js" nomodule></script><script defer="defer" src="/js/app-legacy.9cbae090.js" nomodule></script></head><body><noscript><strong>We're sorry but databasir doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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,7 +125,10 @@ public abstract class BasePlantUmlFileGenerator implements DocumentFileGenerator
dslBuilder.append("}");
dslBuilder.append(LINE);
table.getForeignKeys().forEach(fk -> {
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();

View File

@ -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,11 +527,27 @@ 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()
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();
@ -538,7 +555,6 @@ public class DocumentService {
.filter(g -> g.support(type))
.findFirst()
.ifPresent(generator -> generator.generate(context, out));
});
}
public List<TableResponse> getTableAndColumns(Integer projectId, Long version) {

View File

@ -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