fix: 修复 sql 参数替换问题

This commit is contained in:
tjlygdx
2026-06-18 12:29:05 +08:00
parent 3a6fa094a1
commit 4f49a1c9e3
2 changed files with 30 additions and 20 deletions

View File

@@ -76,8 +76,7 @@ public class TableInfoHandler extends DefaultChartHandler {
}
boolean crossDs = ((DatasetGroupInfoDTO) formatResult.getContext().get("dataset")).getIsCross();
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setIsCross(crossDs);
datasourceRequest.setDsList(dsMap);
fillDatasourceRequest(datasourceRequest, crossDs, dsMap, sqlMap);
var xAxis = formatResult.getAxisMap().get(ChartAxis.xAxis);
var allFields = (List<ChartViewFieldDTO>) filterResult.getContext().get("allFields");
PageInfo pageInfo = new PageInfo();
@@ -151,7 +150,7 @@ public class TableInfoHandler extends DefaultChartHandler {
var assistFields = getAssistFields(dynamicAssistFields, yAxis, xAxis);
if (CollectionUtils.isNotEmpty(assistFields)) {
var req = new DatasourceRequest();
req.setDsList(dsMap);
fillDatasourceRequest(req, crossDs, dsMap, sqlMap);
List<ChartSeniorAssistDTO> assists = dynamicAssistFields.stream().filter(ele -> !StringUtils.equalsIgnoreCase(ele.getSummary(), "last_item")).toList();
if (ObjectUtils.isNotEmpty(assists)) {

View File

@@ -4,6 +4,7 @@ import io.dataease.api.dataset.union.*;
import io.dataease.api.permissions.auth.dto.BusiPerCheckDTO;
import io.dataease.api.permissions.dataset.api.RowPermissionsApi;
import io.dataease.api.permissions.user.vo.UserFormVO;
import io.dataease.commons.utils.SqlVariableHandleResult;
import io.dataease.commons.utils.SqlparserUtils;
import io.dataease.constant.AuthEnum;
import io.dataease.constant.BusiResourceEnum;
@@ -20,10 +21,7 @@ import io.dataease.datasource.manage.EngineManage;
import io.dataease.engine.constant.ExtFieldConstant;
import io.dataease.exception.DEException;
import io.dataease.extensions.datasource.api.PluginManageApi;
import io.dataease.extensions.datasource.dto.DatasetTableDTO;
import io.dataease.extensions.datasource.dto.DatasetTableFieldDTO;
import io.dataease.extensions.datasource.dto.DatasourceSchemaDTO;
import io.dataease.extensions.datasource.dto.DsTypeDTO;
import io.dataease.extensions.datasource.dto.*;
import io.dataease.extensions.datasource.factory.ProviderFactory;
import io.dataease.extensions.datasource.model.SQLObj;
import io.dataease.extensions.datasource.provider.Provider;
@@ -122,6 +120,10 @@ public class DatasetSQLManage {
}
public Map<String, Object> getUnionSQLForEdit(DatasetGroupInfoDTO dataTableInfoDTO, ChartExtRequest chartExtRequest) throws Exception {
return getUnionSQLForEdit(dataTableInfoDTO, chartExtRequest, null, chartExtRequest == null);
}
public Map<String, Object> getUnionSQLForEdit(DatasetGroupInfoDTO dataTableInfoDTO, ChartExtRequest chartExtRequest, CoreDatasource coreDatasource, boolean isFromDataSet) throws Exception {
Map<Long, DatasourceSchemaDTO> dsMap = new LinkedHashMap<>();
// 获取所有字段,将dataeaseName赋值给currentField中的dataeaseName
List<DatasetTableFieldDTO> allFields = dataTableInfoDTO.getAllFields();
@@ -130,6 +132,7 @@ public class DatasetSQLManage {
Map<String, String[]> checkedInfo = new LinkedHashMap<>();
List<UnionParamDTO> unionList = new ArrayList<>();
List<DatasetTableFieldDTO> checkedFields = new ArrayList<>();
List<TableFieldWithValue> tableFieldWithValues = new ArrayList<>();
String sql = "";
if (ObjectUtils.isEmpty(union)) {
return null;
@@ -146,9 +149,9 @@ public class DatasetSQLManage {
if (dsMap.containsKey(datasetTable.getDatasourceId())) {
schema = dsMap.get(datasetTable.getDatasourceId()).getSchemaAlias();
} else {
schema = putObj2Map(dsMap, datasetTable, isCross);
schema = putObj2Map(dsMap, datasetTable, isCross, coreDatasource);
}
SQLObj table = getUnionTable(datasetTable, tableInfo, schema, i, filterParameters(chartExtRequest, currentDs.getId()), chartExtRequest == null, isCross, dsMap);
SQLObj table = getUnionTable(datasetTable, tableInfo, schema, i, filterParameters(chartExtRequest, currentDs.getId()), isFromDataSet, isCross, dsMap, tableFieldWithValues);
if (i == 0) {
tableName = table;
}
@@ -198,7 +201,7 @@ public class DatasetSQLManage {
checkedFields.addAll(fields);
// 获取child的fields和union
if (!CollectionUtils.isEmpty(unionDTO.getChildrenDs())) {
getUnionForEdit(datasetTable, table, unionDTO.getChildrenDs(), checkedInfo, unionList, checkedFields, dsMap, chartExtRequest, isCross, allFields);
getUnionForEdit(datasetTable, table, unionDTO.getChildrenDs(), checkedInfo, unionList, checkedFields, dsMap, chartExtRequest, isCross, allFields, tableFieldWithValues);
}
}
// build sql
@@ -307,7 +310,7 @@ public class DatasetSQLManage {
// 校验系统设置是否允许跨源,如果禁用跨源,则校验dsMap是否是同一数据源
String key = "basic.disableCrossDs";
String val = sysParameterManage.singleVal(key);
if (StringUtils.isEmpty(val) || StringUtils.equalsIgnoreCase(val,"true")) {
if (StringUtils.isEmpty(val) || StringUtils.equalsIgnoreCase(val, "true")) {
if (dsMap.size() > 1) {
DEException.throwException(Translator.get("i18n_sys_disable_cross_ds"));
}
@@ -318,6 +321,7 @@ public class DatasetSQLManage {
map.put("join", unionList);
map.put("dsMap", dsMap);
map.put("isFullJoin", isFullJoin);
map.put("tableFieldWithValues", tableFieldWithValues);
return map;
}
@@ -326,7 +330,8 @@ public class DatasetSQLManage {
List<UnionDTO> childrenDs, Map<String, String[]> checkedInfo,
List<UnionParamDTO> unionList, List<DatasetTableFieldDTO> checkedFields,
Map<Long, DatasourceSchemaDTO> dsMap, ChartExtRequest chartExtRequest,
boolean isCross, List<DatasetTableFieldDTO> allFields) throws Exception {
boolean isCross, List<DatasetTableFieldDTO> allFields,
List<TableFieldWithValue> tableFieldWithValues) throws Exception {
for (int i = 0; i < childrenDs.size(); i++) {
int index = unionList.size() + 1;
@@ -340,7 +345,7 @@ public class DatasetSQLManage {
} else {
schema = putObj2Map(dsMap, datasetTable, isCross);
}
SQLObj table = getUnionTable(datasetTable, tableInfo, schema, index, filterParameters(chartExtRequest, datasetTable.getId()), chartExtRequest == null, isCross, dsMap);
SQLObj table = getUnionTable(datasetTable, tableInfo, schema, index, filterParameters(chartExtRequest, datasetTable.getId()), chartExtRequest == null, isCross, dsMap, tableFieldWithValues);
List<DatasetTableFieldDTO> fields = unionDTO.getCurrentDsFields();
fields = fields.stream().filter(DatasetTableFieldDTO::getChecked).collect(Collectors.toList());
@@ -390,7 +395,7 @@ public class DatasetSQLManage {
unionToParent.setCurrentSQLObj(table);
unionList.add(unionToParent);
if (!CollectionUtils.isEmpty(unionDTO.getChildrenDs())) {
getUnionForEdit(datasetTable, table, unionDTO.getChildrenDs(), checkedInfo, unionList, checkedFields, dsMap, chartExtRequest, isCross, allFields);
getUnionForEdit(datasetTable, table, unionDTO.getChildrenDs(), checkedInfo, unionList, checkedFields, dsMap, chartExtRequest, isCross, allFields, tableFieldWithValues);
}
}
}
@@ -489,7 +494,7 @@ public class DatasetSQLManage {
return getRowPermissionsApi().getUserById(V3UserUtil.getUid());
}
private SQLObj getUnionTable(DatasetTableDTO currentDs, DatasetTableInfoDTO infoDTO, String tableSchema, int index, List<SqlVariableDetails> parameters, boolean isFromDataSet, boolean isCross, Map<Long, DatasourceSchemaDTO> dsMap) {
private SQLObj getUnionTable(DatasetTableDTO currentDs, DatasetTableInfoDTO infoDTO, String tableSchema, int index, List<SqlVariableDetails> parameters, boolean isFromDataSet, boolean isCross, Map<Long, DatasourceSchemaDTO> dsMap, List<TableFieldWithValue> tableFieldWithValues) {
SQLObj tableObj;
String tableAlias = String.format(SQLConstants.TABLE_ALIAS_PREFIX, index);
if (StringUtils.equalsIgnoreCase(currentDs.getType(), DatasetTableTypeConstants.DATASET_TABLE_DB)) {
@@ -498,8 +503,10 @@ public class DatasetSQLManage {
Provider provider = ProviderFactory.getProvider(dsMap.entrySet().iterator().next().getValue().getType());
// parser sql params and replace default value
String s = new String(Base64.getDecoder().decode(infoDTO.getSql()));
String sql = new SqlparserUtils().handleVariableDefaultValue(s, currentDs.getSqlVariableDetails(), false, isFromDataSet, parameters, isCross, dsMap, pluginManage, getUserEntity());
sql = provider.replaceComment(sql);
s = provider.replaceComment(s);
SqlVariableHandleResult sqlResult = new SqlparserUtils().handleVariableDefaultValueWithPreparedParams(s, currentDs.getSqlVariableDetails(), false, isFromDataSet, parameters, isCross, dsMap, pluginManage, getUserEntity());
String sql = sqlResult.getSql();
tableFieldWithValues.addAll(sqlResult.getTableFieldWithValues());
// add table schema
if (isCross) {
sql = SqlUtils.addSchema(sql, tableSchema);
@@ -513,6 +520,10 @@ public class DatasetSQLManage {
}
public String putObj2Map(Map<Long, DatasourceSchemaDTO> dsMap, DatasetTableDTO ds, boolean isCross) {
return putObj2Map(dsMap, ds, isCross, null);
}
public String putObj2Map(Map<Long, DatasourceSchemaDTO> dsMap, DatasetTableDTO ds, boolean isCross, CoreDatasource coreDatasource) {
// 通过datasource id校验数据源权限
BusiPerCheckDTO dto = new BusiPerCheckDTO(ds.getDatasourceId(), BusiResourceEnum.DATASOURCE, AuthEnum.READ);
boolean checked = corePermissionManage.checkAuth(dto);
@@ -523,7 +534,7 @@ public class DatasetSQLManage {
String schemaAlias;
if (StringUtils.equalsIgnoreCase(ds.getType(), DatasetTableType.DB) || StringUtils.equalsIgnoreCase(ds.getType(), DatasetTableType.SQL)) {
CoreDatasource coreDatasource = dataSourceManage.getCoreDatasource(ds.getDatasourceId());
coreDatasource = dataSourceManage.getCoreDatasource(ds.getDatasourceId());
if (coreDatasource == null) {
DEException.throwException(Translator.get("i18n_dataset_ds_error") + ",ID:" + ds.getDatasourceId());
}
@@ -545,7 +556,7 @@ public class DatasetSQLManage {
dsMap.put(coreDatasource.getId(), datasourceSchemaDTO);
}
} else if (StringUtils.equalsIgnoreCase(ds.getType(), DatasetTableType.Es)) {
CoreDatasource coreDatasource = dataSourceManage.getCoreDatasource(ds.getDatasourceId());
coreDatasource = dataSourceManage.getCoreDatasource(ds.getDatasourceId());
schemaAlias = String.format(SQLConstants.SCHEMA, coreDatasource.getId());
if (!dsMap.containsKey(coreDatasource.getId())) {
DatasourceSchemaDTO datasourceSchemaDTO = new DatasourceSchemaDTO();
@@ -554,7 +565,7 @@ public class DatasetSQLManage {
dsMap.put(coreDatasource.getId(), datasourceSchemaDTO);
}
} else {
CoreDatasource coreDatasource = engineManage.getDeEngine();
coreDatasource = engineManage.getDeEngine();
Map map = JsonUtil.parseObject(coreDatasource.getConfiguration(), Map.class);
if (!isCross && ObjectUtils.isNotEmpty(map.get("schema"))) {
schemaAlias = (String) map.get("schema");