Merge pull request #11363 from dataease/pr@dev-v2@fix_schema

fix(数据集): 修复自建schema无法读取数据的问题 #11202
This commit is contained in:
Junjun 2024-08-05 15:31:16 +08:00 committed by GitHub
commit 06d107f3bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 7 deletions

View File

@ -95,15 +95,15 @@ public class DatasetSQLManage {
if (ObjectUtils.isEmpty(union)) {
return null;
}
Set<Long> allDs = getAllDs(union);
boolean isCross = allDs.size() > 1;
DatasetTableDTO currentDs = union.get(0).getCurrentDs();
// get datasource and schema,put map
String tableSchema = putObj2Map(dsMap, currentDs);
String tableSchema = putObj2Map(dsMap, currentDs, isCross);
// get table
DatasetTableInfoDTO infoDTO = JsonUtil.parseObject(currentDs.getInfo(), DatasetTableInfoDTO.class);
Set<Long> allDs = getAllDs(union);
boolean isCross = allDs.size() > 1;
SQLObj tableName = getUnionTable(currentDs, infoDTO, tableSchema, 0, filterParameters(chartExtRequest, currentDs.getId()), chartExtRequest == null, isCross, dsMap);
@ -116,7 +116,7 @@ public class DatasetSQLManage {
if (dsMap.containsKey(datasetTable.getDatasourceId())) {
schema = dsMap.get(datasetTable.getDatasourceId()).getSchemaAlias();
} else {
schema = putObj2Map(dsMap, datasetTable);
schema = putObj2Map(dsMap, datasetTable, isCross);
}
SQLObj table = getUnionTable(datasetTable, tableInfo, schema, i, filterParameters(chartExtRequest, currentDs.getId()), chartExtRequest == null, isCross, dsMap);
@ -287,7 +287,7 @@ public class DatasetSQLManage {
if (dsMap.containsKey(datasetTable.getDatasourceId())) {
schema = dsMap.get(datasetTable.getDatasourceId()).getSchemaAlias();
} else {
schema = putObj2Map(dsMap, datasetTable);
schema = putObj2Map(dsMap, datasetTable, isCross);
}
SQLObj table = getUnionTable(datasetTable, tableInfo, schema, index, filterParameters(chartExtRequest, datasetTable.getId()), chartExtRequest == null, isCross, dsMap);
@ -444,7 +444,7 @@ public class DatasetSQLManage {
return tableObj;
}
private String putObj2Map(Map<Long, DatasourceSchemaDTO> dsMap, DatasetTableDTO ds) throws Exception {
private String putObj2Map(Map<Long, DatasourceSchemaDTO> dsMap, DatasetTableDTO ds, boolean isCross) throws Exception {
// 通过datasource id校验数据源权限
BusiPerCheckDTO dto = new BusiPerCheckDTO();
dto.setId(ds.getDatasourceId());
@ -464,7 +464,14 @@ public class DatasetSQLManage {
if (StringUtils.equalsIgnoreCase("excel", coreDatasource.getType()) || StringUtils.equalsIgnoreCase("api", coreDatasource.getType())) {
coreDatasource = engineManage.getDeEngine();
}
schemaAlias = String.format(SQLConstants.SCHEMA, coreDatasource.getId());
Map map = JsonUtil.parseObject(coreDatasource.getConfiguration(), Map.class);
if (!isCross && ObjectUtils.isNotEmpty(map.get("schema"))) {
schemaAlias = (String) map.get("schema");
} else {
schemaAlias = String.format(SQLConstants.SCHEMA, coreDatasource.getId());
}
if (!dsMap.containsKey(coreDatasource.getId())) {
DatasourceSchemaDTO datasourceSchemaDTO = new DatasourceSchemaDTO();
BeanUtils.copyBean(datasourceSchemaDTO, coreDatasource);

View File

@ -9,6 +9,7 @@ import io.dataease.extensions.datasource.dto.DatasourceSchemaDTO;
import io.dataease.extensions.datasource.model.SQLObj;
import io.dataease.extensions.datasource.vo.DatasourceConfiguration;
import io.dataease.i18n.Translator;
import io.dataease.utils.JsonUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
@ -251,6 +252,10 @@ public class Utils {
public static String replaceSchemaAlias(String sql, Map<Long, DatasourceSchemaDTO> dsMap) {
DatasourceSchemaDTO value = dsMap.entrySet().iterator().next().getValue();
Map map = JsonUtil.parseObject(value.getConfiguration(), Map.class);
if (ObjectUtils.isNotEmpty(map.get("schema"))) {
return sql;
}
return sql.replaceAll(SqlPlaceholderConstants.KEYWORD_PREFIX_REGEX + value.getSchemaAlias() + SqlPlaceholderConstants.KEYWORD_SUFFIX_REGEX + "\\.", "");
}