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

View File

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

View File

@ -7,6 +7,7 @@ import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import io.dataease.auth.annotation.DeCleaner;
import io.dataease.auth.api.dto.CurrentUserDto;
import io.dataease.controller.request.chart.ChartExtRequest;
import io.dataease.dto.SysLogDTO;
import io.dataease.ext.ExtDataSetGroupMapper;
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.dto.chart.ChartFieldCustomFilterDTO;
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.datasource.provider.Provider;
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.relational.ExpressionList;
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.statement.Statement;
import net.sf.jsqlparser.statement.select.PlainSelect;
@ -922,65 +925,61 @@ public class DataSetTableService {
List<SqlVariableDetails> sqlVariableDetails = new ArrayList<>();
datasetTables.forEach(datasetTable -> {
if (StringUtils.isNotEmpty(datasetTable.getSqlVariableDetails())) {
sqlVariableDetails.addAll(new Gson().fromJson(datasetTable.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {
}.getType()));
sqlVariableDetails.addAll(new Gson().fromJson(datasetTable.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {}.getType()));
}
});
return sqlVariableDetails;
}
public String handleVariableDefaultValue(String sql, String sqlVariableDetails) {
public String handleVariableDefaultValue(String sql, String sqlVariableDetails){
if (StringUtils.isEmpty(sql)) {
DataEaseException.throwException(Translator.get("i18n_sql_not_empty"));
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql);
while (matcher.find()) {
boolean customValue = false;
// if (CollectionUtils.isNotEmpty(customSqlVariableDetails)) {
// for (SqlVariableDetails customSqlVariableDetail : customSqlVariableDetails) {
// if (matcher.group().substring(2, matcher.group().length() - 1).equalsIgnoreCase(customSqlVariableDetail.getVariableName())) {
// sql = sql.replace(matcher.group(), customSqlVariableDetail.getDefaultValue());
// customValue = true;
// 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) + "没有默认值!");
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.isNotEmpty(defaultsSqlVariableDetail.getDefaultValue())) {
sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue());
}
}
try {
sql = removeVariables(sql);
}catch (Exception e){
e.printStackTrace();
}
return sql;
}
private String removeVariables(String sql) throws Exception {
public String removeVariables(String sql) throws Exception {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql);
while (matcher.find()) {
sql = sql.replace(matcher.group(), SubstitutedParams);
}
CCJSqlParserUtil.parse(sql, parser -> parser.withSquareBracketQuotation(true));
Statement statement = CCJSqlParserUtil.parse(sql);
Select select = (Select) statement;
Expression expr = ((PlainSelect) select.getSelectBody()).getWhere();
PlainSelect plainSelect = ((PlainSelect) select.getSelectBody());
Expression expr = plainSelect.getWhere();
StringBuilder stringBuilder = new StringBuilder();
expr.accept(getExpressionDeParser(stringBuilder));
return stringBuilder.toString();
BinaryExpression binaryExpression = (BinaryExpression)expr;
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 {
@ -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
public void visit(InExpression inExpression) {
inExpression.getLeftExpression().accept(this);
@ -2536,7 +2550,7 @@ public class DataSetTableService {
StringBuilder stringBuilder = new StringBuilder();
Expression in = ((PlainSelect) subSelect.getSelectBody()).getWhere();
if (in instanceof BinaryExpression && hasVarible(in.toString())) {
stringBuilder.append(SubstitutedSql);
stringBuilder.append(SubstitutedParams);
} else {
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) {
boolean hasBinaryExpression = false;
@ -2588,4 +2599,7 @@ public class DataSetTableService {
};
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())) {
String sql = dataTableInfoDTO.getSql();
if (rowAndColumnMgm) {
sql = "";
sql = dataSetTableService.removeVariables(sql);
}
datasourceRequest.setQuery(qp.createQuerySQLAsTmp(sql, permissionFields, true, customFilter, deSortFields));
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), DatasetType.CUSTOM.toString())) {

View File

@ -60,7 +60,7 @@
/>
</el-popover>
</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>
{{ $t('panel.binding_parameters') }} </span> </el-checkbox>

View File

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

View File

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

View File

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