feat: SQL数据集参数支持在预览时带入参数默认值

This commit is contained in:
taojinlong 2023-03-01 18:12:53 +08:00
parent 80c4cedb7f
commit 6ee4cff97e
9 changed files with 70 additions and 15 deletions

View File

@ -71,6 +71,8 @@ import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
@ -1795,8 +1797,7 @@ public class ChartViewService {
}
private String handleVariable(String sql, ChartExtRequest requestList, QueryProvider qp, DataSetTableDTO table, Datasource ds) throws Exception {
List<SqlVariableDetails> sqlVariables = new Gson().fromJson(table.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {
}.getType());
List<SqlVariableDetails> sqlVariables = new Gson().fromJson(table.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {}.getType());
if (requestList != null && CollectionUtils.isNotEmpty(requestList.getFilter())) {
for (ChartExtFilterRequest chartExtFilterRequest : requestList.getFilter()) {
if (CollectionUtils.isEmpty(chartExtFilterRequest.getValue())) {
@ -1845,7 +1846,7 @@ public class ChartViewService {
}
}
}
sql = dataSetTableService.removeVariables(sql, ds.getType());
sql = dataSetTableService.handleVariableDefaultValue(sql, table.getSqlVariableDetails(), ds.getType());
return sql;
}

View File

@ -123,7 +123,6 @@ public class ViewPluginBaseServiceImpl implements ViewPluginBaseService {
case SQL:
String sql = dataTableInfoDTO.isBase64Encryption() ? new String(java.util.Base64.getDecoder().decode(dataTableInfoDTO.getSql())) : dataTableInfoDTO.getSql();
tableName = dataSetTableService.handleVariableDefaultValue(sql, null, pluginViewSet.getDsType());
tableName = "(" + sqlFix(tableName) + ")";
break;
case CUSTOM:

View File

@ -144,7 +144,7 @@ public class DataSetTableService {
private static boolean isUpdatingDatasetTableStatus = false;
private static final String lastUpdateTime = "${__last_update_time__}";
private static final String currentUpdateTime = "${__current_update_time__}";
private static final String regex = "\\$\\{(.*?)\\}";
public static final String regex = "\\$\\{(.*?)\\}";
private static final String SubstitutedParams = "DATAEASE_PATAMS_BI";
private static final String SubstitutedSql = " 'BI' = 'BI' ";
@ -701,7 +701,7 @@ public class DataSetTableService {
datasourceRequest.setDatasource(ds);
DataTableInfoDTO dataTableInfo = new Gson().fromJson(datasetTable.getInfo(), DataTableInfoDTO.class);
String sql = dataTableInfo.isBase64Encryption() ? new String(java.util.Base64.getDecoder().decode(dataTableInfo.getSql())) : dataTableInfo.getSql();
sql = handleVariableDefaultValue(sql, null, ds.getType());
sql = handleVariableDefaultValue(sql, datasetTable.getSqlVariableDetails(), ds.getType());
QueryProvider qp = ProviderFactory.getQueryProvider(ds.getType());
datasourceRequest.setQuery(
qp.createQuerySQLWithPage(sql, fields, page, pageSize, realSize, false, null, rowPermissionsTree));
@ -1058,15 +1058,15 @@ public class DataSetTableService {
Matcher matcher = pattern.matcher(sql);
while (matcher.find()) {
SqlVariableDetails defaultsSqlVariableDetail = null;
List<SqlVariableDetails> defaultsSqlVariableDetails = new Gson().fromJson(sqlVariableDetails, new TypeToken<List<SqlVariableDetails>>() {
}.getType());
List<SqlVariableDetails> defaultsSqlVariableDetails = new Gson().fromJson(sqlVariableDetails, new TypeToken<List<SqlVariableDetails>>() {}.getType());
for (SqlVariableDetails sqlVariableDetail : defaultsSqlVariableDetails) {
if (matcher.group().substring(2, matcher.group().length() - 1).equalsIgnoreCase(sqlVariableDetail.getVariableName())) {
defaultsSqlVariableDetail = sqlVariableDetail;
break;
}
}
if (defaultsSqlVariableDetail != null && StringUtils.isNotEmpty(defaultsSqlVariableDetail.getDefaultValue())) {
if (defaultsSqlVariableDetail != null && defaultsSqlVariableDetail.getDefaultValueScope() != null &&
defaultsSqlVariableDetail.getDefaultValueScope().equals(SqlVariableDetails.DefaultValueScope.ALLSCOPE) && StringUtils.isNotEmpty(defaultsSqlVariableDetail.getDefaultValue())) {
sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue());
}
}

View File

@ -70,6 +70,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

View File

@ -171,7 +171,7 @@ public class DirectFieldService implements DataSetFieldService {
if (dataTableInfoDTO.isBase64Encryption()) {
sql = new String(java.util.Base64.getDecoder().decode(sql));
}
sql = dataSetTableService.removeVariables(sql, ds.getType());
sql = dataSetTableService.handleVariableDefaultValue(sql, datasetTable.getSqlVariableDetails(), ds.getType());
datasourceRequest.setQuery(qp.createQuerySQLAsTmp(sql, permissionFields, !needSort, customFilter, rowPermissionsTree, deSortFields));
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), DatasetType.CUSTOM.toString())) {
DataTableInfoDTO dt = new Gson().fromJson(datasetTable.getInfo(), DataTableInfoDTO.class);

View File

@ -1519,6 +1519,8 @@ export default {
p_center: 'Center'
},
dataset: {
scope_edit: 'Effective only when editing',
scope_all: 'Global effective',
spend_time: 'Spend',
sql: 'SQL',
sql_result: 'Result',

View File

@ -1513,6 +1513,8 @@ export default {
p_center: '居中'
},
dataset: {
scope_edit: '僅編輯時生效',
scope_all: '全域生效',
spend_time: '耗時',
sql: 'SQL 語句',
sql_result: '運行結果',

View File

@ -1512,6 +1512,8 @@ export default {
p_center: '居中'
},
dataset: {
scope_edit: '仅编辑时生效',
scope_all: '全局生效',
spend_time: '耗时',
sql: 'SQL 语句',
sql_result: '运行结果',

View File

@ -478,8 +478,21 @@
v-model="scope.row.defaultValue"
size="small"
type="text"
:placeholder="$t('fu.search_bar.please_input')"
:placeholder="$t('fu.search_bar.please_input')">
<el-select
slot="prepend"
v-model="scope.row.defaultValueScope"
style="width: 100px"
size="small"
>
<el-option
v-for="item in defaultValueScopeList"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-input>
<el-input
v-if="
scope.row.type[0] === 'LONG' ||
@ -489,7 +502,21 @@
size="small"
:placeholder="$t('fu.search_bar.please_input')"
type="number"
>
<el-select
slot="prepend"
v-model="scope.row.defaultValueScope"
style="width: 100px"
size="small"
>
<el-option
v-for="item in defaultValueScopeList"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-input>
<el-date-picker
v-if="scope.row.type[0] === 'DATETIME-YEAR'"
@ -498,7 +525,21 @@
size="small"
value-format="yyyy"
:placeholder="$t('dataset.select_year')"
>
<el-select
slot="prepend"
v-model="scope.row.defaultValueScope"
style="width: 100px"
size="small"
>
<el-option
v-for="item in defaultValueScopeList"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-date-picker>
<el-date-picker
v-if="scope.row.type[0] === 'DATETIME-YEAR-MONTH'"
@ -653,6 +694,9 @@ export default {
dialogTitle: '',
variables: [],
variablesTmp: [],
defaultValueScopeList: [
{ label: this.$t('dataset.scope_edit'), value: 'EDIT' },
{ label: this.$t('dataset.scope_all'), value: 'ALLSCOPE' }],
fieldOptions: [
{ label: this.$t('dataset.text'), value: 'TEXT' },
{ label: this.$t('dataset.value'), value: 'LONG' },
@ -1038,6 +1082,9 @@ export default {
for (let i = 0; i < this.variables.length; i++) {
if (this.variables[i].variableName === name) {
obj = this.variables[i]
if(!obj.hasOwnProperty("defaultValueScope")){
obj.defaultValueScope = 'EDIT'
}
}
}
if (obj === undefined) {
@ -1047,7 +1094,8 @@ export default {
type: [],
required: false,
defaultValue: '',
details: ''
details: '',
defaultValueScope: 'EDIT'
}
obj.type.push('TEXT')
}