diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GetTablesTool.java b/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GetTablesTool.java index 072825e..c8dba34 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GetTablesTool.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/impls/GetTablesTool.java @@ -58,7 +58,8 @@ public class GetTablesTool implements DataAgentTool { description = """ Get synced semantic-layer tables and ordinary views, including name, object type, domain, \ - description, data granularity (what one row represents) and enabled relations.\ + description, data granularity (what one row represents), business key columns and enabled relations. \ + Business keys are administrator declarations, not database-enforced or data-validated uniqueness guarantees.\ """) public ToolResultBo getTables( @ToolParam( @@ -114,6 +115,12 @@ private TablePromptResponse filterTable( rel.targetTableName(), rel.targetColumnNames())) .toList(); - return table.toBuilder().relations(relations).build(); + return table.toBuilder() + .relations(relations) + .businessKeyColumns( + scope.canColumns(table.name(), table.businessKeyColumns()) + ? table.businessKeyColumns() + : List.of()) + .build(); } } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/convertor/PromptConverter.java b/data-agent-backend/src/main/java/io/github/malonetalk/convertor/PromptConverter.java index 1f9ae16..10cded5 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/convertor/PromptConverter.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/convertor/PromptConverter.java @@ -46,7 +46,9 @@ public static ColumnPromptResponse mapColumnPrompt(ColumnInfo column) { } public static TablePromptResponse mapTablePrompt( - TableInfo table, List resolvedRelations) { + TableInfo table, + List resolvedRelations, + List businessKeyColumns) { if (!SemanticAvailabilityHelper.isTableAvailable(table)) { return null; } @@ -56,6 +58,7 @@ public static TablePromptResponse mapTablePrompt( .domain(SemanticUtils.normalizeDomain(table.getDomain())) .description(SemanticUtils.trimToNull(table.getTableDescription())) .dataGranularity(SemanticUtils.trimToNull(table.getDataGranularity())) + .businessKeyColumns(businessKeyColumns) .relations(resolvedRelations) .build(); } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/convertor/SemanticConverter.java b/data-agent-backend/src/main/java/io/github/malonetalk/convertor/SemanticConverter.java index dabb5e0..c32ad54 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/convertor/SemanticConverter.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/convertor/SemanticConverter.java @@ -31,6 +31,7 @@ import io.github.malonetalk.service.semantic.relation.LogicalTableRelationHelper; import io.github.malonetalk.utils.SemanticUtils; import java.util.List; +import java.util.Set; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -40,7 +41,8 @@ public class SemanticConverter { private final LogicalTableRelationHelper logicalTableRelationHelper; - public TableSemanticResponse toResponse(TableInfo tableInfo) { + public TableSemanticResponse toResponse( + TableInfo tableInfo, Set physicalColumnNames, boolean canReadKey) { Boolean isVisible = tableInfo.getIsVisible(); boolean hasPhysicalTable = SemanticAvailabilityHelper.hasPhysicalTable(tableInfo); return TableSemanticResponse.builder() @@ -52,6 +54,13 @@ public TableSemanticResponse toResponse(TableInfo tableInfo) { SemanticUtils.trimToNull(tableInfo.getPhysicalTableDescription())) .tableDescription(SemanticUtils.trimToNull(tableInfo.getTableDescription())) .dataGranularity(SemanticUtils.trimToNull(tableInfo.getDataGranularity())) + .businessKeyColumns(canReadKey ? tableInfo.getBusinessKeyColumns() : null) + .businessKeyInvalidReason( + canReadKey + ? SemanticAvailabilityHelper.businessKeyInvalidReason( + tableInfo, physicalColumnNames) + .orElse(null) + : null) .isVisible(isVisible) .hasPhysicalTable(hasPhysicalTable) .invalidReason(SemanticAvailabilityHelper.tableInvalidReason(tableInfo)) diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/dto/prompt/TablePromptResponse.java b/data-agent-backend/src/main/java/io/github/malonetalk/dto/prompt/TablePromptResponse.java index 31e995b..aff7578 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/dto/prompt/TablePromptResponse.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/dto/prompt/TablePromptResponse.java @@ -28,4 +28,5 @@ public record TablePromptResponse( String domain, String description, String dataGranularity, + List businessKeyColumns, List relations) {} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticResponse.java b/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticResponse.java index e3690c3..83d0b6f 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticResponse.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticResponse.java @@ -18,6 +18,7 @@ package io.github.malonetalk.dto.semantic; import java.time.LocalDateTime; +import java.util.List; import lombok.Builder; @Builder @@ -29,6 +30,8 @@ public record TableSemanticResponse( String physicalTableDescription, String tableDescription, String dataGranularity, + List businessKeyColumns, + String businessKeyInvalidReason, Boolean isVisible, Boolean hasPhysicalTable, String invalidReason, diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticUpdateRequest.java b/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticUpdateRequest.java index 0d645fa..9d5dde9 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticUpdateRequest.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/dto/semantic/TableSemanticUpdateRequest.java @@ -19,11 +19,15 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import java.util.List; +import lombok.Builder; +@Builder public record TableSemanticUpdateRequest( @NotNull(message = "datasourceId 不能为空") Integer datasourceId, @NotBlank(message = "tableName 不能为空") String tableName, String domain, String tableDescription, String dataGranularity, + List<@NotBlank(message = "业务唯一键字段不能为空") String> businessKeyColumns, @NotNull(message = "isVisible 不能为空") Boolean isVisible) {} diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/entity/TableInfo.java b/data-agent-backend/src/main/java/io/github/malonetalk/entity/TableInfo.java index aed8d5e..c72fad0 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/entity/TableInfo.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/entity/TableInfo.java @@ -18,6 +18,8 @@ package io.github.malonetalk.entity; import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; import lombok.Data; @Data @@ -29,10 +31,15 @@ public class TableInfo { private String physicalTableDescription; private String tableDescription; private String dataGranularity; + private List businessKeyColumns; private String domain; private Integer datasourceId; private Boolean isVisible; private Boolean physicalStatus; private LocalDateTime createTime; private LocalDateTime updateTime; + + public List getBusinessKeyColumns() { + return Optional.ofNullable(businessKeyColumns).orElseGet(List::of); + } } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticAvailabilityHelper.java b/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticAvailabilityHelper.java index cebcfbb..f313d2e 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticAvailabilityHelper.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticAvailabilityHelper.java @@ -21,7 +21,10 @@ import io.github.malonetalk.entity.TableInfo; import io.github.malonetalk.service.semantic.enums.ColumnInvalidReasonEnum; import io.github.malonetalk.service.semantic.enums.TableInvalidReasonEnum; +import io.github.malonetalk.utils.SemanticUtils; import java.util.Objects; +import java.util.Optional; +import java.util.Set; public final class SemanticAvailabilityHelper { @@ -71,4 +74,19 @@ public static String unavailableMessage(String fieldName, String objectName, Str String fallbackReason = reason == null || reason.isBlank() ? "unavailable" : reason; return fieldName + " " + objectName + " is unavailable: " + fallbackReason; } + + public static Optional businessKeyInvalidReason( + TableInfo table, Set physicalColumnNames) { + if (table.getBusinessKeyColumns().isEmpty()) { + return Optional.empty(); + } + if (!hasPhysicalTable(table)) { + return Optional.of("数据库对象缺失,业务唯一键已失效"); + } + boolean missingColumn = + table.getBusinessKeyColumns().stream() + .map(name -> SemanticUtils.normalizeObjectName(name, "业务唯一键字段不能为空")) + .anyMatch(name -> !physicalColumnNames.contains(name)); + return missingColumn ? Optional.of("业务唯一键字段缺失,请重新选择") : Optional.empty(); + } } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticMergeService.java b/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticMergeService.java index 5ce4523..40bf022 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticMergeService.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/SemanticMergeService.java @@ -100,7 +100,13 @@ public List listVisibleTablesByDomains( relationsBySourceId.getOrDefault( table.getId(), List.of()), tablesById, - availableColumnsByTableId))) + availableColumnsByTableId), + hasUnavailableColumn( + availableColumnsByTableId, + table.getId(), + table.getBusinessKeyColumns()) + ? List.of() + : table.getBusinessKeyColumns())) .filter(Objects::nonNull) .toList(); } diff --git a/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/table/TableSemanticServiceImpl.java b/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/table/TableSemanticServiceImpl.java index c5549c8..8cc55d2 100644 --- a/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/table/TableSemanticServiceImpl.java +++ b/data-agent-backend/src/main/java/io/github/malonetalk/service/semantic/table/TableSemanticServiceImpl.java @@ -25,13 +25,18 @@ import io.github.malonetalk.dto.semantic.TableSemanticPageQuery; import io.github.malonetalk.dto.semantic.TableSemanticResponse; import io.github.malonetalk.dto.semantic.TableSemanticUpdateRequest; +import io.github.malonetalk.entity.ColumnInfo; import io.github.malonetalk.entity.TableInfo; import io.github.malonetalk.exception.BusinessException; import io.github.malonetalk.exception.ErrorCode; +import io.github.malonetalk.mapper.ColumnSemanticInfoMapper; import io.github.malonetalk.mapper.TableInfoMapper; +import io.github.malonetalk.model.bo.DataPermissionScopeBo; import io.github.malonetalk.model.holder.UserContextHolder; import io.github.malonetalk.service.DataPermissionService; import io.github.malonetalk.service.DatasourceService; +import io.github.malonetalk.service.DatasourceWriteLockService; +import io.github.malonetalk.service.semantic.SemanticAvailabilityHelper; import io.github.malonetalk.service.semantic.SemanticMergeService; import io.github.malonetalk.utils.SemanticUtils; import java.time.LocalDateTime; @@ -39,6 +44,8 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -53,10 +60,13 @@ public class TableSemanticServiceImpl implements TableSemanticService { private final DataPermissionService dataPermissions; private final UserContextHolder holder; private final TableInfoMapper tableInfoMapper; + private final ColumnSemanticInfoMapper columnSemanticInfoMapper; + private final DatasourceWriteLockService datasourceLocks; private final SemanticMergeService semanticMergeService; private final SemanticConverter semanticConverter; @Override + @Transactional(readOnly = true) public PageResponse getTablePage(TableSemanticPageQuery query) { SemanticUtils.requireDatasourceId(query.datasourceId()); int pageNumber = PageResponse.resolvePage(query.page()); @@ -65,6 +75,7 @@ public PageResponse getTablePage(TableSemanticPageQuery q return PageResponse.empty(pageNumber, pageSize); } boolean sortDescending = SemanticUtils.isDescendingSort(query.sortOrder()); + var user = holder.checkAndGet(); IPage page = tableInfoMapper.selectPageByDatasourceId( new Page<>(pageNumber, pageSize), @@ -75,9 +86,51 @@ public PageResponse getTablePage(TableSemanticPageQuery q SemanticUtils.trimToNull(query.keyword()), query.sortOrder()), sortDescending, - dataPermissions.roleScope(holder.checkAndGet()).orElse(null)); + dataPermissions.roleScope(user).orElse(null)); + if (page.getRecords().isEmpty()) { + return PageResponse.of(List.of(), page.getTotal(), pageNumber, pageSize); + } + var scope = dataPermissions.loadGrantedScope(user, query.datasourceId()); + Set tableIds = + page.getRecords().stream() + .filter(table -> !table.getBusinessKeyColumns().isEmpty()) + .filter(SemanticAvailabilityHelper::hasPhysicalTable) + .filter( + table -> + scope.canColumns( + table.getTableName(), + table.getBusinessKeyColumns())) + .map(TableInfo::getId) + .collect(Collectors.toSet()); + List columns = + tableIds.isEmpty() + ? List.of() + : columnSemanticInfoMapper.selectByDatasourceIdAndTableIds( + query.datasourceId(), tableIds); + Map> physicalColumns = + columns.stream() + .filter(SemanticAvailabilityHelper::hasPhysicalColumn) + .collect( + Collectors.groupingBy( + ColumnInfo::getTableId, + Collectors.mapping( + column -> + SemanticUtils.normalizeObjectName( + column.getColumnName(), + "Missing columnName."), + Collectors.toSet()))); List responses = - page.getRecords().stream().map(semanticConverter::toResponse).toList(); + page.getRecords().stream() + .map( + table -> + semanticConverter.toResponse( + table, + physicalColumns.getOrDefault( + table.getId(), Set.of()), + scope.canColumns( + table.getTableName(), + table.getBusinessKeyColumns()))) + .toList(); return PageResponse.of(responses, page.getTotal(), pageNumber, pageSize); } @@ -129,14 +182,16 @@ public List listMergedTablesByDomains( } @Override + @Transactional public void updateTableSemantic(TableSemanticUpdateRequest request) { - requireDatasource(request.datasourceId()); + SemanticUtils.requireDatasourceId(request.datasourceId()); + datasourceLocks.lock(request.datasourceId()); String tableName = SemanticUtils.requireTrimmed( request.tableName(), "Missing tableName for table semantic update."); String normalizedTableName = tableName.toLowerCase(Locale.ROOT); - dataPermissions.requireTable( - holder.checkAndGet(), request.datasourceId(), normalizedTableName); + var scope = dataPermissions.loadScope(holder.checkAndGet(), request.datasourceId()); + scope.requireTable(normalizedTableName); TableInfo existing = tableInfoMapper.selectByDatasourceIdAndTableName( request.datasourceId(), normalizedTableName); @@ -149,11 +204,15 @@ public void updateTableSemantic(TableSemanticUpdateRequest request) { tableInfo.setDomain(SemanticUtils.normalizeDomain(request.domain())); tableInfo.setIsVisible(request.isVisible()); tableInfo.setPhysicalStatus(Boolean.FALSE); + Optional.ofNullable(request.businessKeyColumns()) + .ifPresent(columns -> updateBusinessKey(tableInfo, columns, scope)); tableInfo.setCreateTime(LocalDateTime.now()); tableInfo.setUpdateTime(LocalDateTime.now()); tableInfoMapper.insert(tableInfo); return; } + Optional.ofNullable(request.businessKeyColumns()) + .ifPresent(columns -> updateBusinessKey(existing, columns, scope)); existing.setTableDescription(SemanticUtils.trimToNull(request.tableDescription())); existing.setDataGranularity(SemanticUtils.trimToNull(request.dataGranularity())); existing.setDomain(SemanticUtils.normalizeDomain(request.domain())); @@ -162,27 +221,78 @@ public void updateTableSemantic(TableSemanticUpdateRequest request) { tableInfoMapper.updateSemanticFields(existing); } + private void updateBusinessKey( + TableInfo table, List requestedColumns, DataPermissionScopeBo scope) { + if (!scope.canColumns(table.getTableName(), table.getBusinessKeyColumns())) { + throw BusinessException.of(ErrorCode.FORBIDDEN, "无权修改包含未授权字段的业务唯一键"); + } + List normalized = + requestedColumns.stream() + .map(name -> SemanticUtils.normalizeObjectName(name, "业务唯一键字段不能为空")) + .toList(); + if (new LinkedHashSet<>(normalized).size() != normalized.size()) { + throw BusinessException.of(ErrorCode.BAD_REQUEST, "业务唯一键字段不能重复"); + } + if (!scope.canColumns(table.getTableName(), normalized)) { + throw BusinessException.of(ErrorCode.FORBIDDEN, "业务唯一键包含未授权字段"); + } + // 未修改时保留已失效的配置,允许独立修改描述、粒度等语义信息。 + if (normalized.equals( + table.getBusinessKeyColumns().stream() + .map(name -> SemanticUtils.normalizeObjectName(name, "业务唯一键字段不能为空")) + .toList())) { + return; + } + if (normalized.isEmpty()) { + table.setBusinessKeyColumns(List.of()); + return; + } + if (!SemanticAvailabilityHelper.hasPhysicalTable(table)) { + throw BusinessException.of(ErrorCode.BAD_REQUEST, "请先同步数据库对象后再配置业务唯一键"); + } + Map physicalColumns = + columnSemanticInfoMapper + .selectByDatasourceIdAndTableName( + table.getDatasourceId(), table.getTableName()) + .stream() + .filter(SemanticAvailabilityHelper::hasPhysicalColumn) + .collect( + Collectors.toMap( + column -> + SemanticUtils.normalizeObjectName( + column.getColumnName(), + "Missing columnName."), + ColumnInfo::getColumnName)); + if (!physicalColumns.keySet().containsAll(normalized)) { + throw BusinessException.of(ErrorCode.BAD_REQUEST, "业务唯一键只能使用当前已同步且存在的字段"); + } + table.setBusinessKeyColumns(normalized.stream().map(physicalColumns::get).toList()); + } + @Override @Transactional public void resetTableSemantic(Integer datasourceId, String tableName) { - requireDatasource(datasourceId); + SemanticUtils.requireDatasourceId(datasourceId); + datasourceLocks.lock(datasourceId); String normalizedTableName = SemanticUtils.requireTrimmed( tableName, "Missing tableName for table semantic reset."); - dataPermissions.requireTable(holder.checkAndGet(), datasourceId, normalizedTableName); + var scope = dataPermissions.loadScope(holder.checkAndGet(), datasourceId); + scope.requireTable(normalizedTableName); TableInfo existing = tableInfoMapper.selectByDatasourceIdAndTableName(datasourceId, normalizedTableName); if (existing == null) { throw BusinessException.of( ErrorCode.RESOURCE_NOT_FOUND, "Table semantic metadata does not exist."); } - resetTableRecords(datasourceId, List.of(existing)); + resetTableRecords(datasourceId, List.of(existing), scope); } @Override @Transactional public int resetTableSemantics(Integer datasourceId, List tableNames) { - requireDatasource(datasourceId); + SemanticUtils.requireDatasourceId(datasourceId); + datasourceLocks.lock(datasourceId); if (tableNames == null || tableNames.isEmpty()) { return 0; } @@ -210,14 +320,18 @@ public int resetTableSemantics(Integer datasourceId, List tableNames) { + datasourceId + "."); } - return resetTableRecords(datasourceId, matchedTables); + return resetTableRecords(datasourceId, matchedTables, scope); } - private int resetTableRecords(Integer datasourceId, List tables) { + private int resetTableRecords( + Integer datasourceId, List tables, DataPermissionScopeBo scope) { List resetIds = new ArrayList<>(); List deleteIds = new ArrayList<>(); // 仅删除明确标记为物理缺失的表;其他记录保留并重置语义字段。 for (TableInfo table : tables) { + if (!scope.canColumns(table.getTableName(), table.getBusinessKeyColumns())) { + throw BusinessException.of(ErrorCode.FORBIDDEN, "无权重置包含未授权字段的业务唯一键"); + } (Boolean.FALSE.equals(table.getPhysicalStatus()) ? deleteIds : resetIds) .add(table.getId()); } @@ -230,12 +344,4 @@ private int resetTableRecords(Integer datasourceId, List tables) { } return affected; } - - private void requireDatasource(Integer datasourceId) { - SemanticUtils.requireDatasourceId(datasourceId); - if (datasourceService.findById(datasourceId).isEmpty()) { - throw BusinessException.of( - ErrorCode.RESOURCE_NOT_FOUND, "Datasource does not exist: " + datasourceId); - } - } } diff --git a/data-agent-backend/src/main/resources/mapper/TableInfoMapper.xml b/data-agent-backend/src/main/resources/mapper/TableInfoMapper.xml index c8852ac..d8ee8c7 100644 --- a/data-agent-backend/src/main/resources/mapper/TableInfoMapper.xml +++ b/data-agent-backend/src/main/resources/mapper/TableInfoMapper.xml @@ -9,6 +9,8 @@ + @@ -69,12 +71,13 @@ INSERT INTO table_info ( - table_name, object_type, physical_table_description, table_description, data_granularity, domain, + table_name, object_type, physical_table_description, table_description, data_granularity, business_key_columns_json, domain, datasource_id, is_visible, physical_status, create_time, update_time ) VALUES ( #{tableName}, #{objectType}, #{physicalTableDescription}, #{tableDescription}, #{dataGranularity}, + #{businessKeyColumns,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler}, #{domain}, #{datasourceId}, COALESCE(#{isVisible}, 1), COALESCE(#{physicalStatus}, 1), COALESCE(#{createTime}, NOW()), COALESCE(#{updateTime}, NOW()) @@ -107,6 +110,7 @@ table_description = #{tableDescription}, data_granularity = #{dataGranularity}, + business_key_columns_json = #{businessKeyColumns,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler}, domain = #{domain}, is_visible = #{isVisible}, update_time = #{updateTime}, @@ -172,6 +176,7 @@ UPDATE table_info SET table_description = physical_table_description, data_granularity = NULL, + business_key_columns_json = NULL, domain = 'default', is_visible = 1, update_time = NOW() diff --git a/data-agent-backend/src/test/java/io/github/malonetalk/service/semantic/BusinessKeyColumnNameTest.java b/data-agent-backend/src/test/java/io/github/malonetalk/service/semantic/BusinessKeyColumnNameTest.java new file mode 100644 index 0000000..eb850d8 --- /dev/null +++ b/data-agent-backend/src/test/java/io/github/malonetalk/service/semantic/BusinessKeyColumnNameTest.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2026 github.com/MaloneTalk + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * limitations under the License. + */ +package io.github.malonetalk.service.semantic; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import io.github.malonetalk.convertor.SemanticConverter; +import io.github.malonetalk.dto.semantic.TableSemanticPageQuery; +import io.github.malonetalk.dto.semantic.TableSemanticUpdateRequest; +import io.github.malonetalk.entity.ColumnInfo; +import io.github.malonetalk.entity.TableInfo; +import io.github.malonetalk.mapper.ColumnSemanticInfoMapper; +import io.github.malonetalk.mapper.TableInfoMapper; +import io.github.malonetalk.model.bo.DataPermissionScopeBo; +import io.github.malonetalk.model.bo.UserContextBo; +import io.github.malonetalk.model.holder.UserContextHolder; +import io.github.malonetalk.model.po.DatasourcePo; +import io.github.malonetalk.service.DataPermissionService; +import io.github.malonetalk.service.DatasourceService; +import io.github.malonetalk.service.DatasourceWriteLockService; +import io.github.malonetalk.service.semantic.relation.LogicalTableRelationHelper; +import io.github.malonetalk.service.semantic.table.TableSemanticServiceImpl; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class BusinessKeyColumnNameTest { + private static final UserContextBo USER = UserContextBo.builder().userId(1).build(); + private static final DataPermissionScopeBo SCOPE = + new DataPermissionScopeBo(true, Map.of(), Map.of()); + + @Mock private DatasourceService datasourceService; + @Mock private DataPermissionService dataPermissions; + @Mock private UserContextHolder holder; + @Mock private TableInfoMapper tableInfoMapper; + @Mock private ColumnSemanticInfoMapper columnMapper; + @Mock private DatasourceWriteLockService datasourceLocks; + @Mock private SemanticMergeService semanticMergeService; + + @Spy + private SemanticConverter converter = + new SemanticConverter(mock(LogicalTableRelationHelper.class)); + + @InjectMocks private TableSemanticServiceImpl service; + + @BeforeEach + void setUser() { + when(holder.checkAndGet()).thenReturn(USER); + } + + @ParameterizedTest + @ValueSource(strings = {"a\"b", "a`b"}) + void savesAndDistinguishesColumnNamesContainingQuotes(String quotedName) { + TableInfo table = table(); + when(dataPermissions.loadScope(USER, 7)).thenReturn(SCOPE); + when(tableInfoMapper.selectByDatasourceIdAndTableName(7, "orders")).thenReturn(table); + when(columnMapper.selectByDatasourceIdAndTableName(7, "orders")) + .thenReturn(List.of(column("ab"), column(quotedName))); + var request = + TableSemanticUpdateRequest.builder() + .datasourceId(7) + .tableName("orders") + .isVisible(true); + + service.updateTableSemantic(request.businessKeyColumns(List.of("ab")).build()); + assertEquals(List.of("ab"), table.getBusinessKeyColumns()); + + service.updateTableSemantic(request.businessKeyColumns(List.of(quotedName)).build()); + assertEquals(List.of(quotedName), table.getBusinessKeyColumns()); + + service.updateTableSemantic( + request.businessKeyColumns( + List.of(" AB ", " " + quotedName.toUpperCase(Locale.ROOT) + " ")) + .build()); + assertEquals(List.of("ab", quotedName), table.getBusinessKeyColumns()); + verify(tableInfoMapper, times(3)).updateSemanticFields(table); + } + + @ParameterizedTest + @ValueSource(strings = {"a\"b", "a`b"}) + void detectsMissingQuotedColumnsAndAcceptsDistinctPresentColumns(String quotedName) { + TableInfo table = table(); + table.setBusinessKeyColumns(List.of(quotedName)); + when(datasourceService.findById(7)).thenReturn(Optional.of(new DatasourcePo())); + when(dataPermissions.loadGrantedScope(USER, 7)).thenReturn(SCOPE); + when(tableInfoMapper.selectPageByDatasourceId(any(), any(), anyBoolean(), any())) + .thenReturn(new Page(1, 20, 1).setRecords(List.of(table))); + when(columnMapper.selectByDatasourceIdAndTableIds(7, Set.of(1))) + .thenReturn(List.of(column("ab")), List.of(column("ab"), column(quotedName))); + var query = new TableSemanticPageQuery(7, 1, 20, null, null); + + assertNotNull(service.getTablePage(query).items().get(0).businessKeyInvalidReason()); + + table.setBusinessKeyColumns(List.of("ab", quotedName)); + var response = service.getTablePage(query).items().get(0); + assertEquals(List.of("ab", quotedName), response.businessKeyColumns()); + assertNull(response.businessKeyInvalidReason()); + } + + private TableInfo table() { + TableInfo table = new TableInfo(); + table.setId(1); + table.setDatasourceId(7); + table.setTableName("orders"); + table.setIsVisible(true); + table.setPhysicalStatus(true); + return table; + } + + private ColumnInfo column(String name) { + ColumnInfo column = new ColumnInfo(); + column.setTableId(1); + column.setColumnName(name); + column.setPhysicalStatus(true); + return column; + } +} diff --git a/data-agent-frontend/src/api/semantic.ts b/data-agent-frontend/src/api/semantic.ts index 301fa89..1466bb6 100644 --- a/data-agent-frontend/src/api/semantic.ts +++ b/data-agent-frontend/src/api/semantic.ts @@ -31,6 +31,8 @@ export interface TableSemanticResponse { domain: string | null; tableDescription: string | null; dataGranularity: string | null; + businessKeyColumns: string[] | null; + businessKeyInvalidReason: string | null; isVisible: boolean; hasPhysicalTable: boolean; invalidReason: string | null; @@ -147,6 +149,7 @@ export interface TableSemanticUpdateRequest { domain?: string; tableDescription?: string; dataGranularity?: string; + businessKeyColumns?: string[]; isVisible: boolean; } diff --git a/data-agent-frontend/src/views/semantic/components/TableSemanticEditDialog.vue b/data-agent-frontend/src/views/semantic/components/TableSemanticEditDialog.vue index fd7a48e..9b13757 100644 --- a/data-agent-frontend/src/views/semantic/components/TableSemanticEditDialog.vue +++ b/data-agent-frontend/src/views/semantic/components/TableSemanticEditDialog.vue @@ -16,19 +16,25 @@ --> + + diff --git a/data-agent-frontend/src/views/semantic/components/TableSemanticManage.vue b/data-agent-frontend/src/views/semantic/components/TableSemanticManage.vue index a419133..3f84888 100644 --- a/data-agent-frontend/src/views/semantic/components/TableSemanticManage.vue +++ b/data-agent-frontend/src/views/semantic/components/TableSemanticManage.vue @@ -86,7 +86,7 @@ if (selectedDatasourceId === null) return; try { await ElMessageBox.confirm( - `确认重置表 ${row.tableName} 的语义信息吗?若该表当前已标记为物理缺失,还会删除该表及其列的语义记录和所有关联到该表的逻辑关系;重新同步不会恢复这些关系。`, + `确认重置表 ${row.tableName} 的语义信息(含业务唯一键)吗?若该表当前已标记为物理缺失,还会删除该表及其列的语义记录和所有关联到该表的逻辑关系;重新同步不会恢复这些关系。`, '确认重置', { type: 'warning', @@ -170,6 +170,7 @@ 表语义 说明表或普通视图在业务上代表什么、属于哪个领域、是否对 AI 可见,以及它和其他对象的逻辑关系。数据粒度用于说明一行代表什么。 + 业务唯一键用于声明哪些字段共同唯一标识一行。 @@ -242,6 +243,14 @@ {{ row.dataGranularity || '-' }} + + +