(improvement)(Headless) Fixed duplicate fields error (#1870)

This commit is contained in:
jipeli
2024-10-31 22:46:46 +08:00
committed by GitHub
parent 7773c82eea
commit 0cb1faaf53
6 changed files with 96 additions and 36 deletions

View File

@@ -20,17 +20,7 @@ import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.GroupByElement;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.ParenthesedFromItem;
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.select.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.util.CollectionUtils;
@@ -623,6 +613,63 @@ public class SqlReplaceHelper {
return selectStatement.toString();
}
public static void replaceSqlByPositions(Select select) {
if (select instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) select;
if (plainSelect.getSelectItems() != null) {
Map<String, Integer> columnMap = new HashMap<>();
for (int i = 0; i < plainSelect.getSelectItems().size(); i++) {
SelectItem selectItem = plainSelect.getSelectItems().get(i);
if (selectItem.getAlias() != null) {
columnMap.put(selectItem.getAlias().getName(), i + 1);
} else if (selectItem.getExpression() instanceof Column) {
Column column = (Column) selectItem.getExpression();
columnMap.put(column.getColumnName(), i + 1);
}
}
if (plainSelect.getGroupBy() != null) {
ExpressionList groupByExpressionList =
plainSelect.getGroupBy().getGroupByExpressionList();
List<Expression> groupByExpressions = groupByExpressionList.getExpressions();
for (Expression expression : groupByExpressions) {
if (expression instanceof Column) {
Column column = (Column) expression;
if (columnMap.containsKey(column.getColumnName())) {
column.setColumnName(
String.valueOf(columnMap.get(column.getColumnName())));
}
}
}
}
if (plainSelect.getOrderByElements() != null) {
for (OrderByElement orderByElement : plainSelect.getOrderByElements()) {
if (orderByElement.getExpression() instanceof Column) {
Column column = (Column) orderByElement.getExpression();
if (columnMap.containsKey(column.getColumnName())) {
orderByElement.setExpression(
new LongValue(columnMap.get(column.getColumnName())));
}
}
}
}
}
}
if (select instanceof ParenthesedSelect) {
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) select;
replaceSqlByPositions(parenthesedSelect.getSelect());
}
if (select instanceof SetOperationList) {
SetOperationList setOperationList = (SetOperationList) select;
setOperationList.getSelects().forEach(s -> replaceSqlByPositions(s));
}
}
public static String replaceSqlByPositions(String sql) {
Select selectStatement = SqlSelectHelper.getSelect(sql);
replaceSqlByPositions(selectStatement);
return selectStatement.toString();
}
private static void replacePlainSelectByExpr(PlainSelect plainSelect,
Map<String, String> replace) {
QueryExpressionReplaceVisitor expressionReplaceVisitor =