mirror of
https://github.com/dataease/dataease.git
synced 2026-05-18 01:38:11 +08:00
fix: 【数据集】带系统变量的SQL节点,对in操作的支持有误
This commit is contained in:
@@ -29,6 +29,7 @@ import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
@@ -42,10 +43,112 @@ public class SqlparserUtils {
|
||||
public static final String regex = "\\$\\{(.*?)\\}";
|
||||
public static final String regex2 = "\\[(.*?)\\]";
|
||||
private static final String SubstitutedParams = "DATAEASE_PATAMS_BI";
|
||||
private static final String SysParamsSubstitutedParams = "DeSysParams_";
|
||||
private static final String SubstitutedSql = " 'DE-BI' = 'DE-BI' ";
|
||||
private static final String SubstitutedSqlVirtualData = " 1 > 2 ";
|
||||
private ExpressionDeParser expressionDeParser;
|
||||
private boolean removeSysParams;
|
||||
private UserFormVO userEntity;
|
||||
|
||||
public static String removeVariables(final String sql, String dsType) throws Exception {
|
||||
public String handleVariableDefaultValue(String sql, String sqlVariableDetails, boolean isEdit, boolean isFromDataSet, List<SqlVariableDetails> parameters, boolean isCross, Map<Long, DatasourceSchemaDTO> dsMap, PluginManageApi pluginManage, UserFormVO userEntity) {
|
||||
if (StringUtils.isEmpty(sql)) {
|
||||
DEException.throwException(Translator.get("i18n_sql_not_empty"));
|
||||
}
|
||||
this.userEntity = userEntity;
|
||||
try {
|
||||
this.removeSysParams = true;
|
||||
removeVariables(sql, "");
|
||||
} catch (Exception e) {
|
||||
DEException.throwException(e);
|
||||
}
|
||||
sql = sql.trim();
|
||||
if (sql.endsWith(";")) {
|
||||
sql = sql.substring(0, sql.length() - 1);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(sqlVariableDetails)) {
|
||||
TypeReference<List<SqlVariableDetails>> listTypeReference = new TypeReference<List<SqlVariableDetails>>() {
|
||||
};
|
||||
List<SqlVariableDetails> defaultsSqlVariableDetails = JsonUtil.parseList(sqlVariableDetails, listTypeReference);
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(sql);
|
||||
|
||||
while (matcher.find()) {
|
||||
SqlVariableDetails defaultsSqlVariableDetail = null;
|
||||
for (SqlVariableDetails sqlVariableDetail : defaultsSqlVariableDetails) {
|
||||
if (matcher.group().substring(2, matcher.group().length() - 1).equalsIgnoreCase(sqlVariableDetail.getVariableName())) {
|
||||
defaultsSqlVariableDetail = sqlVariableDetail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SqlVariableDetails filterParameter = null;
|
||||
if (ObjectUtils.isNotEmpty(parameters)) {
|
||||
for (SqlVariableDetails parameter : parameters) {
|
||||
if (parameter.getVariableName().equalsIgnoreCase(defaultsSqlVariableDetail.getVariableName())) {
|
||||
filterParameter = parameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (filterParameter != null) {
|
||||
sql = sql.replace(matcher.group(), transFilter(filterParameter, dsMap));
|
||||
} else {
|
||||
if (defaultsSqlVariableDetail != null && StringUtils.isNotEmpty(defaultsSqlVariableDetail.getDefaultValue())) {
|
||||
if (!isEdit && isFromDataSet && defaultsSqlVariableDetail.getDefaultValueScope().equals(SqlVariableDetails.DefaultValueScope.ALLSCOPE)) {
|
||||
sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue());
|
||||
}
|
||||
if (isEdit) {
|
||||
sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
DatasourceSchemaDTO ds = dsMap.entrySet().iterator().next().getValue();
|
||||
this.removeSysParams = false;
|
||||
sql = removeVariables(sql, "mysql");
|
||||
// replace keyword '`'
|
||||
if (!isCross) {
|
||||
Map.Entry<Long, DatasourceSchemaDTO> next = dsMap.entrySet().iterator().next();
|
||||
DatasourceSchemaDTO value = next.getValue();
|
||||
|
||||
String prefix = "";
|
||||
String suffix = "";
|
||||
if (Arrays.stream(DatasourceConfiguration.DatasourceType.values()).map(DatasourceConfiguration.DatasourceType::getType).toList().contains(value.getType())) {
|
||||
DatasourceConfiguration.DatasourceType datasourceType = DatasourceConfiguration.DatasourceType.valueOf(value.getType());
|
||||
prefix = datasourceType.getPrefix();
|
||||
suffix = datasourceType.getSuffix();
|
||||
} else {
|
||||
if (LicenseUtil.licenseValid()) {
|
||||
List<XpackPluginsDatasourceVO> xpackPluginsDatasourceVOS = pluginManage.queryPluginDs();
|
||||
List<XpackPluginsDatasourceVO> list = xpackPluginsDatasourceVOS.stream().filter(ele -> StringUtils.equals(ele.getType(), value.getType())).toList();
|
||||
if (ObjectUtils.isNotEmpty(list)) {
|
||||
XpackPluginsDatasourceVO first = list.getFirst();
|
||||
prefix = first.getPrefix();
|
||||
suffix = first.getSuffix();
|
||||
} else {
|
||||
DEException.throwException("当前数据源插件不存在");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("(`.*?`)");
|
||||
Matcher matcher = pattern.matcher(sql);
|
||||
while (matcher.find()) {
|
||||
String group = matcher.group();
|
||||
String info = group.substring(1, group.length() - 1);
|
||||
sql = sql.replaceAll(group, prefix + info + suffix);
|
||||
}
|
||||
}
|
||||
this.removeSysParams = true;
|
||||
sql = removeVariables(sql, "mysql");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
|
||||
private String removeVariables(final String sql, String dsType) throws Exception {
|
||||
String tmpSql = sql.replaceAll("(?m)^\\s*$[\n\r]{0,}", "");
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(tmpSql);
|
||||
@@ -54,21 +157,23 @@ public class SqlparserUtils {
|
||||
hasVariables = true;
|
||||
tmpSql = tmpSql.replace(matcher.group(), SubstitutedParams);
|
||||
}
|
||||
|
||||
pattern = Pattern.compile(regex2);
|
||||
matcher = pattern.matcher(tmpSql);
|
||||
while (matcher.find()) {
|
||||
hasVariables = true;
|
||||
tmpSql = tmpSql.replace(matcher.group(), SubstitutedParams);
|
||||
if (removeSysParams) {
|
||||
pattern = Pattern.compile(regex2);
|
||||
matcher = pattern.matcher(tmpSql);
|
||||
while (matcher.find()) {
|
||||
hasVariables = true;
|
||||
tmpSql = tmpSql.replace(matcher.group(), SubstitutedParams);
|
||||
}
|
||||
} else {
|
||||
pattern = Pattern.compile(regex2);
|
||||
matcher = pattern.matcher(tmpSql);
|
||||
while (matcher.find()) {
|
||||
hasVariables = true;
|
||||
tmpSql = tmpSql.replace(matcher.group(), SysParamsSubstitutedParams + matcher.group().substring(1, matcher.group().length() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasVariables && !tmpSql.contains(SubstitutedParams)) {
|
||||
return tmpSql;
|
||||
}
|
||||
|
||||
Statement statement = CCJSqlParserUtil.parse(tmpSql);
|
||||
Select select = (Select) statement;
|
||||
|
||||
if (CollectionUtils.isNotEmpty(select.getWithItemsList())) {
|
||||
for (Iterator<WithItem> iter = select.getWithItemsList().iterator(); iter.hasNext(); ) {
|
||||
WithItem withItem = iter.next();
|
||||
@@ -78,7 +183,6 @@ public class SqlparserUtils {
|
||||
}
|
||||
|
||||
if (select.getSelectBody() instanceof PlainSelect) {
|
||||
|
||||
return handlePlainSelect((PlainSelect) select.getSelectBody(), select, dsType);
|
||||
} else {
|
||||
StringBuilder result = new StringBuilder();
|
||||
@@ -93,7 +197,7 @@ public class SqlparserUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static String handlePlainSelect(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
|
||||
private String handlePlainSelect(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
|
||||
handleSelectItems(plainSelect, dsType);
|
||||
handleFromItems(plainSelect, dsType);
|
||||
handleJoins(plainSelect, dsType);
|
||||
@@ -101,7 +205,7 @@ public class SqlparserUtils {
|
||||
return handleWhere(plainSelect, statementSelect, dsType);
|
||||
}
|
||||
|
||||
private static void handleSelectItems(PlainSelect plainSelect, String dsType) throws Exception {
|
||||
private void handleSelectItems(PlainSelect plainSelect, String dsType) throws Exception {
|
||||
List<SelectItem<?>> selectItems = new ArrayList<>();
|
||||
for (SelectItem selectItem : plainSelect.getSelectItems()) {
|
||||
try {
|
||||
@@ -118,7 +222,7 @@ public class SqlparserUtils {
|
||||
plainSelect.setSelectItems(selectItems);
|
||||
}
|
||||
|
||||
private static void handleFromItems(PlainSelect plainSelect, String dsType) throws Exception {
|
||||
private void handleFromItems(PlainSelect plainSelect, String dsType) throws Exception {
|
||||
FromItem fromItem = plainSelect.getFromItem();
|
||||
if (fromItem instanceof ParenthesedSelect) {
|
||||
handleParenthesedSelect(fromItem, dsType);
|
||||
@@ -135,7 +239,7 @@ public class SqlparserUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleParenthesedSelect(FromItem fromItem, String dsType) throws Exception {
|
||||
private void handleParenthesedSelect(FromItem fromItem, String dsType) throws Exception {
|
||||
if (((ParenthesedSelect) fromItem).getSelect() instanceof SetOperationList) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
SetOperationList setOperationList = (SetOperationList) ((ParenthesedSelect) fromItem).getSelect().getSelectBody();
|
||||
@@ -162,7 +266,7 @@ public class SqlparserUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleJoins(PlainSelect plainSelect, String dsType) throws Exception {
|
||||
private void handleJoins(PlainSelect plainSelect, String dsType) throws Exception {
|
||||
List<Join> joins = plainSelect.getJoins();
|
||||
if (joins != null) {
|
||||
List<Join> joinsList = new ArrayList<>();
|
||||
@@ -201,7 +305,7 @@ public class SqlparserUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleHaving(PlainSelect plainSelect) throws Exception {
|
||||
private void handleHaving(PlainSelect plainSelect) throws Exception {
|
||||
Expression expr = plainSelect.getHaving();
|
||||
if (expr == null) {
|
||||
return;
|
||||
@@ -215,7 +319,7 @@ public class SqlparserUtils {
|
||||
if (binaryExpression != null) {
|
||||
boolean hasSubBinaryExpression = binaryExpression instanceof AndExpression || binaryExpression instanceof OrExpression;
|
||||
if (!hasSubBinaryExpression && !(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && (hasVariable(binaryExpression.getLeftExpression().toString()) || hasVariable(binaryExpression.getRightExpression().toString()))) {
|
||||
stringBuilder.append(SubstitutedSql);
|
||||
stringBuilder.append(handleSubstitutedSql(binaryExpression.toString()));
|
||||
} else {
|
||||
expr.accept(getExpressionDeParser(stringBuilder));
|
||||
}
|
||||
@@ -225,7 +329,7 @@ public class SqlparserUtils {
|
||||
plainSelect.setHaving(CCJSqlParserUtil.parseCondExpression(stringBuilder.toString()));
|
||||
}
|
||||
|
||||
private static String handleWhere(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
|
||||
private String handleWhere(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
|
||||
Expression expr = plainSelect.getWhere();
|
||||
if (expr == null) {
|
||||
return handleWith(plainSelect, statementSelect, dsType);
|
||||
@@ -239,11 +343,10 @@ public class SqlparserUtils {
|
||||
if (binaryExpression != null) {
|
||||
boolean hasSubBinaryExpression = binaryExpression instanceof AndExpression || binaryExpression instanceof OrExpression;
|
||||
if (!hasSubBinaryExpression && !(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && (hasVariable(binaryExpression.getLeftExpression().toString()) || hasVariable(binaryExpression.getRightExpression().toString()))) {
|
||||
stringBuilder.append(SubstitutedSql);
|
||||
stringBuilder.append(handleSubstitutedSql(binaryExpression.toString()));
|
||||
} else {
|
||||
expr.accept(getExpressionDeParser(stringBuilder));
|
||||
}
|
||||
|
||||
} else {
|
||||
expr.accept(getExpressionDeParser(stringBuilder));
|
||||
}
|
||||
@@ -251,7 +354,7 @@ public class SqlparserUtils {
|
||||
return handleWith(plainSelect, statementSelect, dsType);
|
||||
}
|
||||
|
||||
private static String handleWith(PlainSelect plainSelect, Select select, String dsType) throws Exception {
|
||||
private String handleWith(PlainSelect plainSelect, Select select, String dsType) throws Exception {
|
||||
if (select != null && CollectionUtils.isNotEmpty(select.getWithItemsList())) {
|
||||
for (Iterator<WithItem> iter = select.getWithItemsList().iterator(); iter.hasNext(); ) {
|
||||
WithItem withItem = iter.next();
|
||||
@@ -262,8 +365,7 @@ public class SqlparserUtils {
|
||||
return plainSelect.toString();
|
||||
}
|
||||
|
||||
private static ExpressionDeParser getExpressionDeParser(StringBuilder stringBuilder) {
|
||||
SelectDeParser selectDeParser = new SelectDeParser(stringBuilder);
|
||||
private ExpressionDeParser getExpressionDeParser(StringBuilder stringBuilder) {
|
||||
ExpressionDeParser expressionDeParser = new ExpressionDeParser(null, stringBuilder) {
|
||||
@Override
|
||||
public void visit(Parenthesis parenthesis) {
|
||||
@@ -285,7 +387,7 @@ public class SqlparserUtils {
|
||||
@Override
|
||||
public void visit(Between between) {
|
||||
if (hasVariable(between.getBetweenExpressionStart().toString()) || hasVariable(between.getBetweenExpressionEnd().toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(between.toString()));
|
||||
} else {
|
||||
getBuffer().append(between.getLeftExpression()).append(" BETWEEN ").append(between.getBetweenExpressionStart()).append(" AND ").append(between.getBetweenExpressionEnd());
|
||||
}
|
||||
@@ -294,7 +396,7 @@ public class SqlparserUtils {
|
||||
@Override
|
||||
public void visit(MinorThan minorThan) {
|
||||
if (hasVariable(minorThan.getLeftExpression().toString()) || hasVariable(minorThan.getRightExpression().toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(minorThan.toString()));
|
||||
return;
|
||||
}
|
||||
getBuffer().append(minorThan.getLeftExpression());
|
||||
@@ -305,7 +407,7 @@ public class SqlparserUtils {
|
||||
@Override
|
||||
public void visit(MinorThanEquals minorThan) {
|
||||
if (hasVariable(minorThan.getLeftExpression().toString()) || hasVariable(minorThan.getRightExpression().toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(minorThan.toString()));
|
||||
return;
|
||||
}
|
||||
getBuffer().append(minorThan.getLeftExpression());
|
||||
@@ -316,7 +418,7 @@ public class SqlparserUtils {
|
||||
@Override
|
||||
public void visit(GreaterThanEquals minorThan) {
|
||||
if (hasVariable(minorThan.getLeftExpression().toString()) || hasVariable(minorThan.getRightExpression().toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(minorThan.toString()));
|
||||
return;
|
||||
}
|
||||
getBuffer().append(minorThan.getLeftExpression());
|
||||
@@ -327,7 +429,7 @@ public class SqlparserUtils {
|
||||
@Override
|
||||
public void visit(GreaterThan greaterThan) {
|
||||
if (hasVariable(greaterThan.getLeftExpression().toString()) || hasVariable(greaterThan.getRightExpression().toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(greaterThan.toString()));
|
||||
return;
|
||||
}
|
||||
getBuffer().append(greaterThan.getLeftExpression());
|
||||
@@ -349,7 +451,7 @@ public class SqlparserUtils {
|
||||
@Override
|
||||
public void visit(LikeExpression likeExpression) {
|
||||
if (hasVariable(likeExpression.toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(likeExpression.toString()));
|
||||
return;
|
||||
}
|
||||
visitBinaryExpression(likeExpression, (likeExpression.isNot() ? " NOT" : "") + (likeExpression.isCaseInsensitive() ? " ILIKE " : " LIKE "));
|
||||
@@ -361,11 +463,10 @@ public class SqlparserUtils {
|
||||
@Override
|
||||
public void visit(InExpression inExpression) {
|
||||
if (inExpression.getRightExpression() != null && hasVariable(inExpression.getRightExpression().toString()) && !(inExpression.getRightExpression() instanceof ParenthesedSelect)) {
|
||||
stringBuilder.append(SubstitutedSql);
|
||||
stringBuilder.append(handleSubstitutedSqlForIn(inExpression.toString()));
|
||||
return;
|
||||
}
|
||||
inExpression.getLeftExpression().accept(this);
|
||||
|
||||
if (inExpression.isNot()) {
|
||||
getBuffer().append(" " + " NOT IN " + " ");
|
||||
} else {
|
||||
@@ -420,6 +521,7 @@ public class SqlparserUtils {
|
||||
BinaryExpression leftBinaryExpression = (BinaryExpression) parenthesis.getExpression();
|
||||
hasSubBinaryExpression = leftBinaryExpression instanceof AndExpression || leftBinaryExpression instanceof OrExpression;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (expr.getLeftExpression() instanceof BinaryExpression) {
|
||||
@@ -430,15 +532,12 @@ public class SqlparserUtils {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if ((expr.getLeftExpression() instanceof BinaryExpression || expr.getLeftExpression() instanceof Parenthesis) && !hasSubBinaryExpression && hasVariable(expr.getLeftExpression().toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(expr.getLeftExpression().toString()));
|
||||
} else {
|
||||
expr.getLeftExpression().accept(this);
|
||||
}
|
||||
|
||||
getBuffer().append(" " + operator + " ");
|
||||
|
||||
hasSubBinaryExpression = false;
|
||||
if (expr.getRightExpression() instanceof Parenthesis) {
|
||||
Parenthesis parenthesis = (Parenthesis) expr.getRightExpression();
|
||||
@@ -450,25 +549,27 @@ public class SqlparserUtils {
|
||||
BinaryExpression rightBinaryExpression = (BinaryExpression) expr.getRightExpression();
|
||||
hasSubBinaryExpression = rightBinaryExpression instanceof AndExpression || rightBinaryExpression instanceof OrExpression;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if ((expr.getRightExpression() instanceof Parenthesis || expr.getRightExpression() instanceof BinaryExpression || expr.getRightExpression() instanceof Function) && !hasSubBinaryExpression && hasVariable(expr.getRightExpression().toString())) {
|
||||
getBuffer().append(SubstitutedSql);
|
||||
getBuffer().append(handleSubstitutedSql(expr.getRightExpression().toString()));
|
||||
} else {
|
||||
expr.getRightExpression().accept(this);
|
||||
}
|
||||
}
|
||||
};
|
||||
this.expressionDeParser = expressionDeParser;
|
||||
return expressionDeParser;
|
||||
}
|
||||
|
||||
private static boolean hasVariable(String sql) {
|
||||
return sql.contains(SubstitutedParams);
|
||||
private boolean hasVariable(String sql) {
|
||||
return sql.contains(SubstitutedParams) || (!removeSysParams && sql.contains(SysParamsSubstitutedParams));
|
||||
}
|
||||
|
||||
|
||||
private static void getDependencies(SqlNode sqlNode, Boolean fromOrJoin) {
|
||||
private void getDependencies(SqlNode sqlNode, Boolean fromOrJoin) {
|
||||
if (sqlNode == null) {
|
||||
return;
|
||||
}
|
||||
@@ -506,7 +607,7 @@ public class SqlparserUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static SqlShuttle getSqlShuttle() {
|
||||
private SqlShuttle getSqlShuttle() {
|
||||
return new SqlShuttle() {
|
||||
|
||||
@Override
|
||||
@@ -527,125 +628,7 @@ public class SqlparserUtils {
|
||||
};
|
||||
}
|
||||
|
||||
public static String handleVariableDefaultValue(String sql, String sqlVariableDetails, boolean isEdit, boolean isFromDataSet, List<SqlVariableDetails> parameters, boolean isCross, Map<Long, DatasourceSchemaDTO> dsMap, PluginManageApi pluginManage, UserFormVO userEntity) {
|
||||
if (StringUtils.isEmpty(sql)) {
|
||||
DEException.throwException(Translator.get("i18n_sql_not_empty"));
|
||||
}
|
||||
try {
|
||||
removeVariables(sql, "");
|
||||
} catch (Exception e) {
|
||||
DEException.throwException(e);
|
||||
}
|
||||
|
||||
sql = sql.trim();
|
||||
if (sql.endsWith(";")) {
|
||||
sql = sql.substring(0, sql.length() - 1);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(sqlVariableDetails)) {
|
||||
TypeReference<List<SqlVariableDetails>> listTypeReference = new TypeReference<List<SqlVariableDetails>>() {
|
||||
};
|
||||
List<SqlVariableDetails> defaultsSqlVariableDetails = JsonUtil.parseList(sqlVariableDetails, listTypeReference);
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(sql);
|
||||
|
||||
while (matcher.find()) {
|
||||
SqlVariableDetails defaultsSqlVariableDetail = null;
|
||||
for (SqlVariableDetails sqlVariableDetail : defaultsSqlVariableDetails) {
|
||||
if (matcher.group().substring(2, matcher.group().length() - 1).equalsIgnoreCase(sqlVariableDetail.getVariableName())) {
|
||||
defaultsSqlVariableDetail = sqlVariableDetail;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SqlVariableDetails filterParameter = null;
|
||||
if (ObjectUtils.isNotEmpty(parameters)) {
|
||||
for (SqlVariableDetails parameter : parameters) {
|
||||
if (parameter.getVariableName().equalsIgnoreCase(defaultsSqlVariableDetail.getVariableName())) {
|
||||
filterParameter = parameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (filterParameter != null) {
|
||||
sql = sql.replace(matcher.group(), transFilter(filterParameter, dsMap));
|
||||
} else {
|
||||
if (defaultsSqlVariableDetail != null && StringUtils.isNotEmpty(defaultsSqlVariableDetail.getDefaultValue())) {
|
||||
if (!isEdit && isFromDataSet && defaultsSqlVariableDetail.getDefaultValueScope().equals(SqlVariableDetails.DefaultValueScope.ALLSCOPE)) {
|
||||
sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue());
|
||||
}
|
||||
if (isEdit) {
|
||||
sql = sql.replace(matcher.group(), defaultsSqlVariableDetail.getDefaultValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sql = sql.replace("[sysParams.userId]", userEntity.getAccount());
|
||||
sql = sql.replace("[sysParams.userEmail]", userEntity.getEmail());
|
||||
sql = sql.replace("[sysParams.userName]", userEntity.getName());
|
||||
for (SysVariableValueItem variable : userEntity.getVariables()) {
|
||||
String value = null;
|
||||
if (!variable.isValid()) {
|
||||
continue;
|
||||
}
|
||||
if (variable.getSysVariableDto().getType().equalsIgnoreCase("text")) {
|
||||
for (SysVariableValueDto sysVariableValueDto : variable.getValueList()) {
|
||||
if (sysVariableValueDto.getId().equals(variable.getVariableValueId())) {
|
||||
value = sysVariableValueDto.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
value = variable.getVariableValue();
|
||||
}
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
sql = sql.replace("[" + variable.getVariableId() + "]", value);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
DatasourceSchemaDTO ds = dsMap.entrySet().iterator().next().getValue();
|
||||
sql = removeVariables(sql, ds.getType());
|
||||
|
||||
// replace keyword '`'
|
||||
if (!isCross) {
|
||||
Map.Entry<Long, DatasourceSchemaDTO> next = dsMap.entrySet().iterator().next();
|
||||
DatasourceSchemaDTO value = next.getValue();
|
||||
|
||||
String prefix = "";
|
||||
String suffix = "";
|
||||
if (Arrays.stream(DatasourceConfiguration.DatasourceType.values()).map(DatasourceConfiguration.DatasourceType::getType).toList().contains(value.getType())) {
|
||||
DatasourceConfiguration.DatasourceType datasourceType = DatasourceConfiguration.DatasourceType.valueOf(value.getType());
|
||||
prefix = datasourceType.getPrefix();
|
||||
suffix = datasourceType.getSuffix();
|
||||
} else {
|
||||
if (LicenseUtil.licenseValid()) {
|
||||
List<XpackPluginsDatasourceVO> xpackPluginsDatasourceVOS = pluginManage.queryPluginDs();
|
||||
List<XpackPluginsDatasourceVO> list = xpackPluginsDatasourceVOS.stream().filter(ele -> StringUtils.equals(ele.getType(), value.getType())).toList();
|
||||
if (ObjectUtils.isNotEmpty(list)) {
|
||||
XpackPluginsDatasourceVO first = list.getFirst();
|
||||
prefix = first.getPrefix();
|
||||
suffix = first.getSuffix();
|
||||
} else {
|
||||
DEException.throwException("当前数据源插件不存在");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("(`.*?`)");
|
||||
Matcher matcher = pattern.matcher(sql);
|
||||
while (matcher.find()) {
|
||||
String group = matcher.group();
|
||||
String info = group.substring(1, group.length() - 1);
|
||||
sql = sql.replaceAll(group, prefix + info + suffix);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
private static String transFilter(SqlVariableDetails sqlVariableDetails, Map<Long, DatasourceSchemaDTO> dsMap) {
|
||||
private String transFilter(SqlVariableDetails sqlVariableDetails, Map<Long, DatasourceSchemaDTO> dsMap) {
|
||||
if (sqlVariableDetails.getOperator().equals("in")) {
|
||||
if (StringUtils.equalsIgnoreCase(dsMap.entrySet().iterator().next().getValue().getType(), DatasourceConfiguration.DatasourceType.sqlServer.getType())
|
||||
&& sqlVariableDetails.getDeType() == 0) {
|
||||
@@ -675,5 +658,64 @@ public class SqlparserUtils {
|
||||
} else {
|
||||
return (String) sqlVariableDetails.getValue().get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String handleSubstitutedSql(String sql) {
|
||||
if (sql.contains(SysParamsSubstitutedParams)) {
|
||||
sql = sql.replace(SysParamsSubstitutedParams + "sysParams.userId", userEntity.getAccount());
|
||||
sql = sql.replace(SysParamsSubstitutedParams + "sysParams.userEmail", userEntity.getEmail());
|
||||
sql = sql.replace(SysParamsSubstitutedParams + "sysParams.userName", userEntity.getName());
|
||||
for (SysVariableValueItem variable : userEntity.getVariables()) {
|
||||
String value = null;
|
||||
if (!variable.isValid()) {
|
||||
continue;
|
||||
}
|
||||
if (variable.getSysVariableDto().getType().equalsIgnoreCase("text")) {
|
||||
for (SysVariableValueDto sysVariableValueDto : variable.getValueList()) {
|
||||
if (sysVariableValueDto.getId().equals(variable.getVariableValueId())) {
|
||||
value = sysVariableValueDto.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
value = variable.getVariableValue();
|
||||
}
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
sql = sql.replace(SysParamsSubstitutedParams + variable.getVariableId(), value);
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
} else {
|
||||
return SubstitutedSql;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String handleSubstitutedSqlForIn(String sql) {
|
||||
if (sql.contains(SysParamsSubstitutedParams)) {
|
||||
for (SysVariableValueItem variable : userEntity.getVariables()) {
|
||||
List<String> values = new ArrayList<>();
|
||||
if (!variable.isValid()) {
|
||||
continue;
|
||||
}
|
||||
if (variable.getSysVariableDto().getType().equalsIgnoreCase("text")) {
|
||||
for (SysVariableValueDto sysVariableValueDto : variable.getValueList()) {
|
||||
if (sysVariableValueDto.getSysVariableId().equals(variable.getVariableId())) {
|
||||
values.add(sysVariableValueDto.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(values)) {
|
||||
sql = sql.replace(SysParamsSubstitutedParams + variable.getVariableId(), "'" + String.join("','", values) + "'");
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
} else {
|
||||
return SubstitutedSql;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ public class DatasetDataManage {
|
||||
// parser sql params and replace default value
|
||||
String s = new String(Base64.getDecoder().decode(tableInfoDTO.getSql()));
|
||||
UserFormVO userEntity = getRowPermissionsApi().getUserById(AuthUtils.getUser().getUserId());
|
||||
String originSql = SqlparserUtils.handleVariableDefaultValue(s, datasetTableDTO.getSqlVariableDetails(), false, false, null, false, datasourceRequest.getDsList(), pluginManage, userEntity);
|
||||
String originSql = new SqlparserUtils().handleVariableDefaultValue(s, datasetTableDTO.getSqlVariableDetails(), false, false, null, false, datasourceRequest.getDsList(), pluginManage, userEntity);
|
||||
originSql = provider.replaceComment(originSql);
|
||||
// add sql table schema
|
||||
|
||||
@@ -418,7 +418,7 @@ public class DatasetDataManage {
|
||||
|
||||
String s = new String(Base64.getDecoder().decode(dto.getSql()));
|
||||
UserFormVO userEntity = getRowPermissionsApi().getUserById(AuthUtils.getUser().getUserId());
|
||||
String originSql = SqlparserUtils.handleVariableDefaultValue(datasetSQLManage.subPrefixSuffixChar(s), dto.getSqlVariableDetails(), true, true, null, false, dsMap, pluginManage, userEntity);
|
||||
String originSql = new SqlparserUtils().handleVariableDefaultValue(datasetSQLManage.subPrefixSuffixChar(s), dto.getSqlVariableDetails(), true, true, null, false, dsMap, pluginManage, userEntity);
|
||||
originSql = provider.replaceComment(originSql);
|
||||
|
||||
// sql 作为临时表,外层加上limit
|
||||
|
||||
@@ -471,7 +471,7 @@ public class DatasetSQLManage {
|
||||
// parser sql params and replace default value
|
||||
String s = new String(Base64.getDecoder().decode(infoDTO.getSql()));
|
||||
UserFormVO userEntity = getRowPermissionsApi().getUserById(AuthUtils.getUser().getUserId());
|
||||
String sql = SqlparserUtils.handleVariableDefaultValue(s, currentDs.getSqlVariableDetails(), false, isFromDataSet, parameters, isCross, dsMap, pluginManage, userEntity);
|
||||
String sql = new SqlparserUtils().handleVariableDefaultValue(s, currentDs.getSqlVariableDetails(), false, isFromDataSet, parameters, isCross, dsMap, pluginManage, userEntity);
|
||||
sql = provider.replaceComment(sql);
|
||||
// add table schema
|
||||
if (isCross) {
|
||||
|
||||
@@ -227,10 +227,8 @@ const handleSearchVariableApi = async () => {
|
||||
onMounted(async () => {
|
||||
dsChange(sqlNode.value.datasourceId)
|
||||
await handleSearchVariableApi()
|
||||
codeCom.value = myCm.value.codeComInit(
|
||||
setNameIdTrans('id', 'name', Base64.decode(sqlNode.value.sql)),
|
||||
true
|
||||
)
|
||||
sql = Base64.decode(sqlNode.value.sql)
|
||||
codeCom.value = myCm.value.codeComInit(setNameIdTrans('id', 'name', sql), true)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
@@ -382,6 +380,7 @@ const close = () => {
|
||||
|
||||
const handleClose = () => {
|
||||
let sqlNew = codeCom.value.state.doc.toString()
|
||||
sqlNew = setNameIdTrans('name', 'id', sqlNew)
|
||||
if (changeFlag || sql !== sqlNew || !sqlNew.trim()) {
|
||||
ElMessageBox.confirm(t('chart.tips'), {
|
||||
confirmButtonType: 'primary',
|
||||
@@ -405,7 +404,7 @@ const getSQLPreview = () => {
|
||||
parseVariable()
|
||||
dataPreviewLoading.value = true
|
||||
getPreviewSql({
|
||||
sql: Base64.encode(codeCom.value.state.doc.toString()),
|
||||
sql: Base64.encode((sql = setNameIdTrans('name', 'id', codeCom.value.state.doc.toString()))),
|
||||
datasourceId: sqlNode.value.datasourceId,
|
||||
sqlVariableDetails: JSON.stringify(state.variables)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user