13
0
forked from github/dataease

feat: 参数管理

This commit is contained in:
taojinlong 2022-06-16 22:41:09 +08:00
parent 4b5788b63c
commit 3293f3a870
7 changed files with 67 additions and 60 deletions
backend/src/main/java/io/dataease/service
frontend/src/views
panel/filter/filterMain
system/datasource

View File

@ -451,9 +451,7 @@ public class ChartViewService {
} }
} else if (StringUtils.equalsIgnoreCase(table.getType(), DatasetType.SQL.name())) { } else if (StringUtils.equalsIgnoreCase(table.getType(), DatasetType.SQL.name())) {
String sql = dataTableInfoDTO.getSql(); String sql = dataTableInfoDTO.getSql();
if (CollectionUtils.isNotEmpty(requestList.getFilter())) { sql = handleVariable(sql, requestList);
sql = handleVariable(sql, requestList);
}
if (StringUtils.equalsIgnoreCase("text", view.getType()) || StringUtils.equalsIgnoreCase("gauge", view.getType()) || StringUtils.equalsIgnoreCase("liquid", view.getType())) { if (StringUtils.equalsIgnoreCase("text", view.getType()) || StringUtils.equalsIgnoreCase("gauge", view.getType()) || StringUtils.equalsIgnoreCase("liquid", view.getType())) {
datasourceRequest.setQuery(qp.getSQLSummaryAsTmp(sql, yAxis, fieldCustomFilter, extFilterList, view)); datasourceRequest.setQuery(qp.getSQLSummaryAsTmp(sql, yAxis, fieldCustomFilter, extFilterList, view));
} else if (StringUtils.containsIgnoreCase(view.getType(), "stack")) { } else if (StringUtils.containsIgnoreCase(view.getType(), "stack")) {
@ -824,10 +822,7 @@ public class ChartViewService {
} }
} else if (StringUtils.equalsIgnoreCase(table.getType(), DatasetType.SQL.name())) { } else if (StringUtils.equalsIgnoreCase(table.getType(), DatasetType.SQL.name())) {
String sql = dataTableInfoDTO.getSql(); String sql = dataTableInfoDTO.getSql();
if (CollectionUtils.isNotEmpty(requestList.getFilter())) { sql = handleVariable(sql, requestList);
sql = handleVariable(sql, requestList);
}
if (StringUtils.equalsIgnoreCase("text", view.getType()) || StringUtils.equalsIgnoreCase("gauge", view.getType()) || StringUtils.equalsIgnoreCase("liquid", view.getType())) { if (StringUtils.equalsIgnoreCase("text", view.getType()) || StringUtils.equalsIgnoreCase("gauge", view.getType()) || StringUtils.equalsIgnoreCase("liquid", view.getType())) {
datasourceRequest.setQuery(qp.getSQLSummaryAsTmp(sql, yAxis, fieldCustomFilter, extFilterList, view)); datasourceRequest.setQuery(qp.getSQLSummaryAsTmp(sql, yAxis, fieldCustomFilter, extFilterList, view));
} else if (StringUtils.containsIgnoreCase(view.getType(), "stack")) { } else if (StringUtils.containsIgnoreCase(view.getType(), "stack")) {
@ -839,7 +834,6 @@ public class ChartViewService {
} else { } else {
datasourceRequest.setQuery(qp.getSQLAsTmp(sql, xAxis, yAxis, fieldCustomFilter, extFilterList, view)); datasourceRequest.setQuery(qp.getSQLAsTmp(sql, xAxis, yAxis, fieldCustomFilter, extFilterList, view));
} }
System.out.println(datasourceRequest.getQuery());
} else if (StringUtils.equalsIgnoreCase(table.getType(), DatasetType.CUSTOM.name())) { } else if (StringUtils.equalsIgnoreCase(table.getType(), DatasetType.CUSTOM.name())) {
DataTableInfoDTO dt = gson.fromJson(table.getInfo(), DataTableInfoDTO.class); DataTableInfoDTO dt = gson.fromJson(table.getInfo(), DataTableInfoDTO.class);
List<DataSetTableUnionDTO> list = dataSetTableUnionService.listByTableId(dt.getList().get(0).getTableId()); List<DataSetTableUnionDTO> list = dataSetTableUnionService.listByTableId(dt.getList().get(0).getTableId());
@ -1498,22 +1492,24 @@ public class ChartViewService {
chartViewMapper.updateByPrimaryKeySelective(chartView); chartViewMapper.updateByPrimaryKeySelective(chartView);
} }
private String handleVariable(String sql, ChartExtRequest requestList){ private String handleVariable(String sql, ChartExtRequest requestList)throws Exception{
for (ChartExtFilterRequest chartExtFilterRequest : requestList.getFilter()) { if(requestList !=null &&CollectionUtils.isNotEmpty(requestList.getFilter()) ){
chartExtFilterRequest.getParameters(); for (ChartExtFilterRequest chartExtFilterRequest : requestList.getFilter()) {
if(CollectionUtils.isEmpty(chartExtFilterRequest.getValue())){ if(CollectionUtils.isEmpty(chartExtFilterRequest.getValue())){
continue; continue;
}
if(chartExtFilterRequest.getValue().size() > 1){
for (String parameter : chartExtFilterRequest.getParameters()) {
sql = sql.replace("${" + parameter + "}", String.join(",", chartExtFilterRequest.getValue()));
} }
}else { if(chartExtFilterRequest.getValue().size() > 1){
for (String parameter : chartExtFilterRequest.getParameters()) { for (String parameter : chartExtFilterRequest.getParameters()) {
sql = sql.replace("${" + parameter + "}", chartExtFilterRequest.getValue().get(0)); sql = sql.replace("${" + parameter + "}", String.join(",", chartExtFilterRequest.getValue()));
}
}else {
for (String parameter : chartExtFilterRequest.getParameters()) {
sql = sql.replace("${" + parameter + "}", chartExtFilterRequest.getValue().get(0));
}
} }
} }
} }
sql = dataSetTableService.removeVariables(sql);
return sql; return sql;
} }
} }

View File

@ -7,6 +7,7 @@ import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import io.dataease.auth.annotation.DeCleaner; import io.dataease.auth.annotation.DeCleaner;
import io.dataease.auth.api.dto.CurrentUserDto; import io.dataease.auth.api.dto.CurrentUserDto;
import io.dataease.controller.request.chart.ChartExtRequest;
import io.dataease.dto.SysLogDTO; import io.dataease.dto.SysLogDTO;
import io.dataease.ext.ExtDataSetGroupMapper; import io.dataease.ext.ExtDataSetGroupMapper;
import io.dataease.ext.ExtDataSetTableMapper; import io.dataease.ext.ExtDataSetTableMapper;
@ -31,6 +32,7 @@ import io.dataease.plugins.common.constants.DatasetType;
import io.dataease.plugins.common.constants.DatasourceTypes; import io.dataease.plugins.common.constants.DatasourceTypes;
import io.dataease.plugins.common.dto.chart.ChartFieldCustomFilterDTO; import io.dataease.plugins.common.dto.chart.ChartFieldCustomFilterDTO;
import io.dataease.plugins.common.dto.datasource.TableField; import io.dataease.plugins.common.dto.datasource.TableField;
import io.dataease.plugins.common.request.chart.ChartExtFilterRequest;
import io.dataease.plugins.common.request.datasource.DatasourceRequest; import io.dataease.plugins.common.request.datasource.DatasourceRequest;
import io.dataease.plugins.datasource.provider.Provider; import io.dataease.plugins.datasource.provider.Provider;
import io.dataease.plugins.datasource.query.QueryProvider; import io.dataease.plugins.datasource.query.QueryProvider;
@ -47,6 +49,7 @@ import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression; import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression; import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement; import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.statement.select.PlainSelect;
@ -922,65 +925,61 @@ public class DataSetTableService {
List<SqlVariableDetails> sqlVariableDetails = new ArrayList<>(); List<SqlVariableDetails> sqlVariableDetails = new ArrayList<>();
datasetTables.forEach(datasetTable -> { datasetTables.forEach(datasetTable -> {
if (StringUtils.isNotEmpty(datasetTable.getSqlVariableDetails())) { if (StringUtils.isNotEmpty(datasetTable.getSqlVariableDetails())) {
sqlVariableDetails.addAll(new Gson().fromJson(datasetTable.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() { sqlVariableDetails.addAll(new Gson().fromJson(datasetTable.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {}.getType()));
}.getType()));
} }
}); });
return sqlVariableDetails; return sqlVariableDetails;
} }
public String handleVariableDefaultValue(String sql, String sqlVariableDetails){
public String handleVariableDefaultValue(String sql, String sqlVariableDetails) {
if (StringUtils.isEmpty(sql)) { if (StringUtils.isEmpty(sql)) {
DataEaseException.throwException(Translator.get("i18n_sql_not_empty")); DataEaseException.throwException(Translator.get("i18n_sql_not_empty"));
} }
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql); Matcher matcher = pattern.matcher(sql);
while (matcher.find()) { while (matcher.find()) {
boolean customValue = false; SqlVariableDetails defaultsSqlVariableDetail = null;
// if (CollectionUtils.isNotEmpty(customSqlVariableDetails)) { List<SqlVariableDetails> defaultsSqlVariableDetails = new Gson().fromJson(sqlVariableDetails, new TypeToken<List<SqlVariableDetails>>() {}.getType());
// for (SqlVariableDetails customSqlVariableDetail : customSqlVariableDetails) { for (SqlVariableDetails sqlVariableDetail : defaultsSqlVariableDetails) {
// if (matcher.group().substring(2, matcher.group().length() - 1).equalsIgnoreCase(customSqlVariableDetail.getVariableName())) { if (matcher.group().substring(2, matcher.group().length() - 1).equalsIgnoreCase(sqlVariableDetail.getVariableName())) {
// sql = sql.replace(matcher.group(), customSqlVariableDetail.getDefaultValue()); defaultsSqlVariableDetail = sqlVariableDetail;
// customValue = true; break;
// break;
// }
// }
// }
if (!customValue) {
SqlVariableDetails defaultsSqlVariableDetail = null;
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.isEmpty(defaultsSqlVariableDetail.getDefaultValue())) {
throw new RuntimeException(matcher.group().substring(2, matcher.group().length() - 1) + "没有默认值!");
} }
}
if (defaultsSqlVariableDetail != null && StringUtils.isNotEmpty(defaultsSqlVariableDetail.getDefaultValue())) {
sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue()); sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue());
} }
} }
try {
sql = removeVariables(sql);
}catch (Exception e){
e.printStackTrace();
}
return sql; return sql;
} }
private String removeVariables(String sql) throws Exception { public String removeVariables(String sql) throws Exception {
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql); Matcher matcher = pattern.matcher(sql);
while (matcher.find()) { while (matcher.find()) {
sql = sql.replace(matcher.group(), SubstitutedParams); sql = sql.replace(matcher.group(), SubstitutedParams);
} }
CCJSqlParserUtil.parse(sql, parser -> parser.withSquareBracketQuotation(true)); CCJSqlParserUtil.parse(sql, parser -> parser.withSquareBracketQuotation(true));
Statement statement = CCJSqlParserUtil.parse(sql); Statement statement = CCJSqlParserUtil.parse(sql);
Select select = (Select) statement; Select select = (Select) statement;
Expression expr = ((PlainSelect) select.getSelectBody()).getWhere(); PlainSelect plainSelect = ((PlainSelect) select.getSelectBody());
Expression expr = plainSelect.getWhere();
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
expr.accept(getExpressionDeParser(stringBuilder)); BinaryExpression binaryExpression = (BinaryExpression)expr;
return stringBuilder.toString();
if(!(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getRightExpression() instanceof BinaryExpression) && hasVarible(binaryExpression.toString())){
stringBuilder.append(SubstitutedSql);
}else {
expr.accept(getExpressionDeParser(stringBuilder));
}
plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(stringBuilder.toString()));
return plainSelect.toString();
} }
public Map<String, Object> getSQLPreview(DataSetTableRequest dataSetTableRequest) throws Exception { public Map<String, Object> getSQLPreview(DataSetTableRequest dataSetTableRequest) throws Exception {
@ -2510,6 +2509,21 @@ public class DataSetTableService {
} }
} }
@Override
public void visit(LikeExpression likeExpression) {
if (hasVarible(likeExpression.toString())) {
getBuffer().append(SubstitutedSql);
return;
}
visitBinaryExpression(likeExpression, (likeExpression.isNot() ? " NOT" : "") + (likeExpression.isCaseInsensitive() ? " ILIKE " : " LIKE "));
Expression escape = likeExpression.getEscape();
if (escape != null) {
getBuffer().append(" ESCAPE ");
likeExpression.getEscape().accept(this);
}
}
@Override @Override
public void visit(InExpression inExpression) { public void visit(InExpression inExpression) {
inExpression.getLeftExpression().accept(this); inExpression.getLeftExpression().accept(this);
@ -2536,7 +2550,7 @@ public class DataSetTableService {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
Expression in = ((PlainSelect) subSelect.getSelectBody()).getWhere(); Expression in = ((PlainSelect) subSelect.getSelectBody()).getWhere();
if (in instanceof BinaryExpression && hasVarible(in.toString())) { if (in instanceof BinaryExpression && hasVarible(in.toString())) {
stringBuilder.append(SubstitutedSql); stringBuilder.append(SubstitutedParams);
} else { } else {
in.accept(getExpressionDeParser(stringBuilder)); in.accept(getExpressionDeParser(stringBuilder));
} }
@ -2550,9 +2564,6 @@ public class DataSetTableService {
} }
} }
private boolean hasVarible(String sql) {
return sql.contains(SubstitutedParams);
}
private void visitBinaryExpr(BinaryExpression expr, String operator) { private void visitBinaryExpr(BinaryExpression expr, String operator) {
boolean hasBinaryExpression = false; boolean hasBinaryExpression = false;
@ -2588,4 +2599,7 @@ public class DataSetTableService {
}; };
return expressionDeParser; return expressionDeParser;
} }
static private boolean hasVarible(String sql) {
return sql.contains(SubstitutedParams);
}
} }

View File

@ -140,7 +140,7 @@ public class DirectFieldService implements DataSetFieldService {
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), DatasetType.SQL.toString())) { } else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), DatasetType.SQL.toString())) {
String sql = dataTableInfoDTO.getSql(); String sql = dataTableInfoDTO.getSql();
if (rowAndColumnMgm) { if (rowAndColumnMgm) {
sql = ""; sql = dataSetTableService.removeVariables(sql);
} }
datasourceRequest.setQuery(qp.createQuerySQLAsTmp(sql, permissionFields, true, customFilter, deSortFields)); datasourceRequest.setQuery(qp.createQuerySQLAsTmp(sql, permissionFields, true, customFilter, deSortFields));
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), DatasetType.CUSTOM.toString())) { } else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), DatasetType.CUSTOM.toString())) {

View File

@ -60,7 +60,7 @@
/> />
</el-popover> </el-popover>
</span> </span>
<span style="padding-left: 10px;"> <span v-show="element.component === 'de-select'" style="padding-left: 10px;">
<el-checkbox v-model="attrs.enableParameters" @change="enableParametersChange"><span> <el-checkbox v-model="attrs.enableParameters" @change="enableParametersChange"><span>
{{ $t('panel.binding_parameters') }} </span> </el-checkbox> {{ $t('panel.binding_parameters') }} </span> </el-checkbox>

View File

@ -207,7 +207,6 @@ export default {
case "WWW_FORM": case "WWW_FORM":
this.setContentType("application/x-www-form-urlencoded"); this.setContentType("application/x-www-form-urlencoded");
break; break;
// todo from data
case "BINARY": case "BINARY":
this.setContentType("application/octet-stream"); this.setContentType("application/octet-stream");
break; break;

View File

@ -157,7 +157,6 @@
this.items.push(new KeyValue({enable: true})); this.items.push(new KeyValue({enable: true}));
} }
this.$emit('change', this.items); this.$emit('change', this.items);
// TODO key
}, },
isDisable: function (index) { isDisable: function (index) {
return this.items.length - 1 === index; return this.items.length - 1 === index;

View File

@ -164,7 +164,6 @@ export default {
})); }));
} }
this.$emit('change', this.parameters); this.$emit('change', this.parameters);
// TODO key
}, },
isDisable: function (index) { isDisable: function (index) {
return this.parameters.length - 1 == index; return this.parameters.length - 1 == index;