feat: add batch query table api

This commit is contained in:
vran 2022-03-09 00:10:27 +08:00
parent 035107c731
commit 6e75a78017
7 changed files with 79 additions and 38 deletions

View File

@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import static org.springframework.data.domain.Sort.Direction.DESC;
@ -86,10 +87,11 @@ public class DocumentController {
return JsonData.ok(documentService.getSimpleOneByProjectId(projectId, version));
}
@GetMapping(Routes.Document.GET_TABLE_DETAIL)
public JsonData<DatabaseDocumentResponse.TableDocumentResponse> getTableDocument(@PathVariable Integer projectId,
@PathVariable Integer tableId) {
return JsonData.ok(documentService.getTableDetails(projectId, tableId));
@PostMapping(Routes.Document.GET_TABLE_DETAIL)
public JsonData<List<DatabaseDocumentResponse.TableDocumentResponse>> getTableDocument(@PathVariable Integer projectId,
@PathVariable Integer documentId,
@RequestBody List<Integer> tableIds) {
return JsonData.ok(documentService.getTableDetails(projectId, documentId, tableIds));
}
}

View File

@ -80,7 +80,7 @@ public interface Routes {
String GET_SIMPLE_ONE = BASE + "/projects/{projectId}/documents/simple";
String GET_TABLE_DETAIL = BASE + "/projects/{projectId}/table_documents/{tableId}";
String GET_TABLE_DETAIL = BASE + "/projects/{projectId}/documents/{documentId}/table_documents";
String EXPORT = BASE + "/projects/{projectId}/document_files";
}

View File

@ -21,6 +21,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.sql.Connection;
import java.util.*;
@ -180,30 +181,36 @@ public class DocumentService {
.orElseGet(Page::empty);
}
public Optional<DatabaseDocumentResponse.TableDocumentResponse> getTableDetails(Integer projectId,
Integer tableId) {
public List<DatabaseDocumentResponse.TableDocumentResponse> getTableDetails(Integer projectId,
Integer databaseDocumentId,
List<Integer> tableIds) {
// maybe deleted
if (!projectDao.existsById(projectId)) {
return Optional.empty();
if (CollectionUtils.isEmpty(tableIds) || !projectDao.existsById(projectId)) {
return Collections.emptyList();
}
return tableDocumentDao.selectOptionalById(tableId)
var tables =
tableDocumentDao.selectByDatabaseDocumentIdAndIdIn(databaseDocumentId, tableIds);
var columns =
tableColumnDocumentDao.selectByDatabaseDocumentIdAndTableIdIn(databaseDocumentId, tableIds);
var indexes =
tableIndexDocumentDao.selectByDatabaseDocumentIdAndIdIn(databaseDocumentId, tableIds);
var triggers =
tableTriggerDocumentDao.selectByDatabaseDocumentIdAndIdIn(databaseDocumentId, tableIds);
Map<Integer, List<TableColumnDocumentPojo>> columnsGroupByTableMetaId = columns.stream()
.collect(Collectors.groupingBy(TableColumnDocumentPojo::getTableDocumentId));
Map<Integer, List<TableIndexDocumentPojo>> indexesGroupByTableMetaId = indexes.stream()
.collect(Collectors.groupingBy(TableIndexDocumentPojo::getTableDocumentId));
Map<Integer, List<TableTriggerDocumentPojo>> triggersGroupByTableMetaId = triggers.stream()
.collect(Collectors.groupingBy(TableTriggerDocumentPojo::getTableDocumentId));
return tables.stream()
.map(table -> {
Integer documentId = table.getDatabaseDocumentId();
var columns = tableColumnDocumentDao.selectByDatabaseDocumentId(documentId);
var indexes = tableIndexDocumentDao.selectByDatabaseMetaId(documentId);
var triggers = tableTriggerDocumentDao.selectByDatabaseDocumentId(documentId);
Map<Integer, List<TableColumnDocumentPojo>> columnsGroupByTableMetaId = columns.stream()
.collect(Collectors.groupingBy(TableColumnDocumentPojo::getTableDocumentId));
Map<Integer, List<TableIndexDocumentPojo>> indexesGroupByTableMetaId = indexes.stream()
.collect(Collectors.groupingBy(TableIndexDocumentPojo::getTableDocumentId));
Map<Integer, List<TableTriggerDocumentPojo>> triggersGroupByTableMetaId = triggers.stream()
.collect(Collectors.groupingBy(TableTriggerDocumentPojo::getTableDocumentId));
Integer tableId = table.getId();
var subColumns = columnsGroupByTableMetaId.getOrDefault(tableId, Collections.emptyList());
var subIndexes = indexesGroupByTableMetaId.getOrDefault(tableId, Collections.emptyList());
var subTriggers = triggersGroupByTableMetaId.getOrDefault(tableId, Collections.emptyList());
return documentResponseConverter.of(table, subColumns, subIndexes, subTriggers);
});
})
.collect(Collectors.toList());
}
public Optional<String> toMarkdown(Integer projectId, Long version) {

View File

@ -6,6 +6,7 @@ import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collections;
import java.util.List;
import static com.databasir.dao.Tables.TABLE_COLUMN_DOCUMENT;
@ -28,10 +29,15 @@ public class TableColumnDocumentDao extends BaseDao<TableColumnDocumentPojo> {
.fetchInto(TableColumnDocumentPojo.class);
}
public void deleteByDatabaseDocumentId(Integer schemaDocumentId) {
getDslContext()
.deleteFrom(TABLE_COLUMN_DOCUMENT)
.where(TABLE_COLUMN_DOCUMENT.DATABASE_DOCUMENT_ID.eq(schemaDocumentId))
.execute();
public List<TableColumnDocumentPojo> selectByDatabaseDocumentIdAndTableIdIn(Integer schemaDocumentId,
List<Integer> tableIdIn) {
if (tableIdIn == null || tableIdIn.isEmpty()) {
return Collections.emptyList();
}
return getDslContext()
.select(TABLE_COLUMN_DOCUMENT.fields()).from(TABLE_COLUMN_DOCUMENT)
.where(TABLE_COLUMN_DOCUMENT.DATABASE_DOCUMENT_ID.eq(schemaDocumentId)
.and(TABLE_COLUMN_DOCUMENT.TABLE_DOCUMENT_ID.in(tableIdIn)))
.fetchInto(TableColumnDocumentPojo.class);
}
}

View File

@ -6,6 +6,7 @@ import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collections;
import java.util.List;
import static com.databasir.dao.Tables.TABLE_DOCUMENT;
@ -33,4 +34,16 @@ public class TableDocumentDao extends BaseDao<TableDocumentPojo> {
.deleteFrom(TABLE_DOCUMENT).where(TABLE_DOCUMENT.DATABASE_DOCUMENT_ID.eq(schemaDocumentId))
.execute();
}
public List<TableDocumentPojo> selectByDatabaseDocumentIdAndIdIn(Integer databaseDocumentId,
List<Integer> idList) {
if (idList == null || idList.isEmpty()) {
return Collections.emptyList();
}
return getDslContext()
.selectFrom(TABLE_DOCUMENT)
.where(TABLE_DOCUMENT.DATABASE_DOCUMENT_ID.eq(databaseDocumentId)
.and(TABLE_DOCUMENT.ID.in(idList)))
.fetchInto(TableDocumentPojo.class);
}
}

View File

@ -6,6 +6,7 @@ import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collections;
import java.util.List;
import static com.databasir.dao.Tables.TABLE_INDEX_DOCUMENT;
@ -21,16 +22,22 @@ public class TableIndexDocumentDao extends BaseDao<TableIndexDocumentPojo> {
super(TABLE_INDEX_DOCUMENT, TableIndexDocumentPojo.class);
}
public List<TableIndexDocumentPojo> selectByDatabaseMetaId(Integer schemaMetaId) {
public List<TableIndexDocumentPojo> selectByDatabaseMetaId(Integer documentId) {
return getDslContext()
.select(TABLE_INDEX_DOCUMENT.fields()).from(TABLE_INDEX_DOCUMENT)
.where(TABLE_INDEX_DOCUMENT.DATABASE_DOCUMENT_ID.eq(schemaMetaId))
.where(TABLE_INDEX_DOCUMENT.DATABASE_DOCUMENT_ID.eq(documentId))
.fetchInto(TableIndexDocumentPojo.class);
}
public void deleteByDatabaseMetaId(Integer schemaMetaId) {
getDslContext()
.deleteFrom(TABLE_INDEX_DOCUMENT).where(TABLE_INDEX_DOCUMENT.DATABASE_DOCUMENT_ID.eq(schemaMetaId))
.execute();
public List<TableIndexDocumentPojo> selectByDatabaseDocumentIdAndIdIn(Integer documentId,
List<Integer> tableIdIn) {
if (tableIdIn == null || tableIdIn.isEmpty()) {
return Collections.emptyList();
}
return getDslContext()
.select(TABLE_INDEX_DOCUMENT.fields()).from(TABLE_INDEX_DOCUMENT)
.where(TABLE_INDEX_DOCUMENT.DATABASE_DOCUMENT_ID.eq(documentId)
.and(TABLE_INDEX_DOCUMENT.TABLE_DOCUMENT_ID.in(tableIdIn)))
.fetchInto(TableIndexDocumentPojo.class);
}
}

View File

@ -6,6 +6,7 @@ import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collections;
import java.util.List;
import static com.databasir.dao.Tables.TABLE_TRIGGER_DOCUMENT;
@ -28,10 +29,15 @@ public class TableTriggerDocumentDao extends BaseDao<TableTriggerDocumentPojo> {
.fetchInto(TableTriggerDocumentPojo.class);
}
public void deleteByDatabaseDocumentId(Integer schemaDocumentId) {
getDslContext()
.deleteFrom(TABLE_TRIGGER_DOCUMENT)
.where(TABLE_TRIGGER_DOCUMENT.DATABASE_DOCUMENT_ID.eq(schemaDocumentId))
.execute();
public List<TableTriggerDocumentPojo> selectByDatabaseDocumentIdAndIdIn(Integer documentId,
List<Integer> tableIdIn) {
if (tableIdIn == null || tableIdIn.isEmpty()) {
return Collections.emptyList();
}
return getDslContext()
.select(TABLE_TRIGGER_DOCUMENT.fields()).from(TABLE_TRIGGER_DOCUMENT)
.where(TABLE_TRIGGER_DOCUMENT.DATABASE_DOCUMENT_ID.eq(documentId)
.and(TABLE_TRIGGER_DOCUMENT.TABLE_DOCUMENT_ID.in(tableIdIn)))
.fetchInto(TableTriggerDocumentPojo.class);
}
}