(improvement)(Headless) corrector supports subselect sql (#1006)

* [improvement] corrector support subselect sql

---------

Co-authored-by: zuopengge
This commit is contained in:
mainmain
2024-05-17 10:01:49 +08:00
committed by GitHub
parent 2411cb3fdc
commit 7949efea75
6 changed files with 32 additions and 5 deletions

View File

@@ -206,6 +206,12 @@ public class SqlReplaceHelper {
replaceAsName(fieldNameMap, selectItem);
}
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, subPlainSelect);
}
//3. replace oder by fields
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
if (!CollectionUtils.isEmpty(orderByElements)) {

View File

@@ -122,19 +122,28 @@ public class SqlSelectHelper {
List<PlainSelect> plainSelectList = new ArrayList<>();
if (selectStatement instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectStatement;
plainSelectList.add(plainSelect);
getSubPlainSelect(plainSelect, plainSelectList);
} else if (selectStatement instanceof SetOperationList) {
SetOperationList setOperationList = (SetOperationList) selectStatement;
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
setOperationList.getSelects().forEach(subSelectBody -> {
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
plainSelectList.add(subPlainSelect);
getSubPlainSelect(subPlainSelect, plainSelectList);
});
}
}
return plainSelectList;
}
public static void getSubPlainSelect(PlainSelect plainSelect, List<PlainSelect> plainSelectList) {
plainSelectList.add(plainSelect);
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
getSubPlainSelect(subPlainSelect, plainSelectList);
}
}
public static Select getSelect(String sql) {
Statement statement = null;
try {
@@ -275,6 +284,14 @@ public class SqlSelectHelper {
if (Objects.nonNull(where)) {
where.accept(new FieldAndValueAcquireVisitor(result));
}
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
Expression subWhere = subPlainSelect.getWhere();
if (Objects.nonNull(subWhere)) {
subWhere.accept(new FieldAndValueAcquireVisitor(result));
}
}
}
return new ArrayList<>(result);
}