fix: SQL变量误报错 #6624

This commit is contained in:
taojinlong 2023-11-29 12:59:43 +08:00
parent 02d678a9b6
commit 2062cfd4e6

View File

@ -1115,7 +1115,22 @@ public class DataSetTableService {
}
}
private String handlePlainSelect(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
private void handleSelectItems(PlainSelect plainSelect, String dsType) throws Exception{
List<SelectItem> selectItems = new ArrayList<>();
for (SelectItem selectItem : plainSelect.getSelectItems()) {
SelectExpressionItem selectExpressionItem = (SelectExpressionItem) selectItem;
if (selectExpressionItem.getExpression() instanceof SubSelect) {
SubSelect subSelect = (SubSelect) selectExpressionItem.getExpression();
Select select = (Select) CCJSqlParserUtil.parse (removeVariables(subSelect.getSelectBody().toString(), dsType));
subSelect.setSelectBody(select.getSelectBody());
((SelectExpressionItem) selectItem).setExpression(subSelect);
}
selectItems.add(selectItem);
}
plainSelect.setSelectItems(selectItems);
}
private void handleFromItems(PlainSelect plainSelect, String dsType) throws Exception{
FromItem fromItem = plainSelect.getFromItem();
if (fromItem instanceof SubSelect) {
SelectBody selectBody = ((SubSelect) fromItem).getSelectBody();
@ -1134,6 +1149,8 @@ public class DataSetTableService {
}
plainSelect.setFromItem(subSelect);
}
}
private void handleJoins(PlainSelect plainSelect, String dsType) throws Exception{
List<Join> joins = plainSelect.getJoins();
if (joins != null) {
List<Join> joinsList = new ArrayList<>();
@ -1159,27 +1176,29 @@ public class DataSetTableService {
}
plainSelect.setJoins(joinsList);
}
Expression expr = plainSelect.getWhere();
if (expr == null) {
return handleWith(plainSelect, statementSelect, dsType);
}
StringBuilder stringBuilder = new StringBuilder();
BinaryExpression binaryExpression = null;
try {
binaryExpression = (BinaryExpression) expr;
} catch (Exception e) {
}
if (binaryExpression != null) {
if (!(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && hasVariable(binaryExpression.getRightExpression().toString())) {
stringBuilder.append(SubstitutedSql);
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(stringBuilder.toString()));
return handleWith(plainSelect, statementSelect, dsType);
}
private String handleWhere(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception{
Expression expr = plainSelect.getWhere();
if (expr == null) {
return handleWith(plainSelect, statementSelect, dsType);
}
StringBuilder stringBuilder = new StringBuilder();
BinaryExpression binaryExpression = null;
try {
binaryExpression = (BinaryExpression) expr;
} catch (Exception e) {
}
if (binaryExpression != null) {
if (!(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && hasVariable(binaryExpression.getRightExpression().toString())) {
stringBuilder.append(SubstitutedSql);
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
} else {
expr.accept(getExpressionDeParser(stringBuilder));
}
plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(stringBuilder.toString()));
return handleWith(plainSelect, statementSelect, dsType);
}
private String handleWith(PlainSelect plainSelect, Select select, String dsType) throws Exception {
@ -1200,6 +1219,15 @@ public class DataSetTableService {
return builder.toString();
}
private String handlePlainSelect(PlainSelect plainSelect, Select statementSelect, String dsType) throws Exception {
handleSelectItems(plainSelect, dsType);
handleFromItems(plainSelect, dsType);
handleJoins(plainSelect, dsType);
return handleWhere(plainSelect, statementSelect, dsType);
}
public Map<String, Object> getDBPreview(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
if (ds == null) {