mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 12:07:42 +00:00
(improvement)(Headless)headless supports with (#1113)
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -18,6 +19,7 @@ import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.schema.Table;
|
||||
import net.sf.jsqlparser.statement.select.GroupByElement;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
@@ -41,11 +43,9 @@ public class SqlAddHelper {
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement;
|
||||
fields.stream().filter(Objects::nonNull).forEach(field -> {
|
||||
//SelectExpressionItem selectExpressionItem = new SelectExpressionItem(new Column(field));
|
||||
SelectItem<Column> selectExpressionItem = new SelectItem(new Column(field));
|
||||
plainSelect.addSelectItems(selectExpressionItem);
|
||||
});
|
||||
@@ -56,7 +56,6 @@ public class SqlAddHelper {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
fields.stream().forEach(field -> {
|
||||
//SelectExpressionItem selectExpressionItem = new SelectExpressionItem(new Column(field));
|
||||
SelectItem<Column> selectExpressionItem = new SelectItem(new Column(field));
|
||||
subPlainSelect.addSelectItems(selectExpressionItem);
|
||||
});
|
||||
@@ -71,7 +70,6 @@ public class SqlAddHelper {
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
@@ -98,7 +96,6 @@ public class SqlAddHelper {
|
||||
boolean existFunction = false;
|
||||
for (Expression expression : expressionList) {
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
//SelectExpressionItem expressionItem = (SelectExpressionItem) selectItem;
|
||||
if (selectItem.getExpression() instanceof Function) {
|
||||
Function expressionFunction = (Function) selectItem.getExpression();
|
||||
if (expression.toString().equalsIgnoreCase(expressionFunction.toString())) {
|
||||
@@ -108,7 +105,6 @@ public class SqlAddHelper {
|
||||
}
|
||||
}
|
||||
if (!existFunction) {
|
||||
//SelectExpressionItem sumExpressionItem = new SelectExpressionItem(expression);
|
||||
SelectItem sumExpressionItem = new SelectItem(expression);
|
||||
selectItems.add(sumExpressionItem);
|
||||
}
|
||||
@@ -122,7 +118,6 @@ public class SqlAddHelper {
|
||||
return sql;
|
||||
}
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -145,7 +140,6 @@ public class SqlAddHelper {
|
||||
|
||||
public static String addWhere(String sql, Expression expression) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -158,30 +152,51 @@ public class SqlAddHelper {
|
||||
dateWhere = true;
|
||||
}
|
||||
}
|
||||
List<PlainSelect> plainSelectList = SqlSelectHelper.getWithItem(selectStatement);
|
||||
if (!CollectionUtils.isEmpty(plainSelectList) && dateWhere) {
|
||||
List<String> withNameList = SqlSelectHelper.getWithName(sql);
|
||||
for (int i = 0; i < plainSelectList.size(); i++) {
|
||||
if (plainSelectList.get(i).getFromItem() instanceof Table) {
|
||||
Table table = (Table) plainSelectList.get(i).getFromItem();
|
||||
if (withNameList.contains(table.getName())) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Set<String> result = new HashSet<>();
|
||||
List<PlainSelect> subPlainSelectList = new ArrayList<>();
|
||||
subPlainSelectList.add(plainSelectList.get(i));
|
||||
SqlSelectHelper.getWhereFields(subPlainSelectList, result);
|
||||
if (TimeDimensionEnum.containsZhTimeDimension(new ArrayList<>(result))) {
|
||||
continue;
|
||||
}
|
||||
Expression subWhere = plainSelectList.get(i).getWhere();
|
||||
addWhere(plainSelectList.get(i), subWhere, expression);
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect && dateWhere) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
|
||||
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
|
||||
Expression subWhere = subPlainSelect.getWhere();
|
||||
if (subWhere == null) {
|
||||
subPlainSelect.setWhere(expression);
|
||||
} else {
|
||||
subPlainSelect.setWhere(new AndExpression(subWhere, expression));
|
||||
}
|
||||
addWhere(subPlainSelect, subWhere, expression);
|
||||
return selectStatement.toString();
|
||||
}
|
||||
Expression where = plainSelect.getWhere();
|
||||
|
||||
addWhere(plainSelect, where, expression);
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static void addWhere(PlainSelect plainSelect, Expression where, Expression expression) {
|
||||
if (where == null) {
|
||||
plainSelect.setWhere(expression);
|
||||
} else {
|
||||
plainSelect.setWhere(new AndExpression(where, expression));
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String addWhere(String sql, List<Expression> expressionList) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -206,7 +221,6 @@ public class SqlAddHelper {
|
||||
|
||||
public static String addAggregateToField(String sql, Map<String, String> fieldNameToAggregate) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -228,7 +242,6 @@ public class SqlAddHelper {
|
||||
return sql;
|
||||
}
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -345,7 +358,6 @@ public class SqlAddHelper {
|
||||
|
||||
public static String addHaving(String sql, Set<String> fieldNames) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -374,7 +386,6 @@ public class SqlAddHelper {
|
||||
|
||||
public static String addHaving(String sql, List<Expression> expressionList) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -399,7 +410,6 @@ public class SqlAddHelper {
|
||||
|
||||
public static String addParenthesisToWhere(String sql) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
|
||||
@@ -50,12 +50,10 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String replaceSelectFields(String sql, Map<String, String> fieldNameMap) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
((PlainSelect) selectStatement).getSelectItems().stream().forEach(o -> {
|
||||
//SelectExpressionItem selectExpressionItem = (SelectExpressionItem) o;
|
||||
SelectItem selectExpressionItem = (SelectItem) o;
|
||||
String alias = "";
|
||||
if (selectExpressionItem.getExpression() instanceof Function) {
|
||||
@@ -123,7 +121,6 @@ public class SqlReplaceHelper {
|
||||
public static String replaceValue(String sql, Map<String, Map<String, String>> filedNameToValueMap,
|
||||
boolean exactReplace) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
@@ -142,7 +139,6 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String replaceFieldNameByValue(String sql, Map<String, Set<String>> fieldValueToFieldNames) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
@@ -165,8 +161,8 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String replaceFields(String sql, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
System.out.println(selectStatement.getSelectBody());
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
List<PlainSelect> plainSelectList = SqlSelectHelper.getWithItem(selectStatement);
|
||||
//plainSelectList.add(selectStatement.getPlainSelect());
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
plainSelectList.add((PlainSelect) selectStatement);
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
@@ -234,11 +230,14 @@ public class SqlReplaceHelper {
|
||||
List<Join> joins = plainSelect.getJoins();
|
||||
if (!CollectionUtils.isEmpty(joins)) {
|
||||
for (Join join : joins) {
|
||||
join.getOnExpression().accept(visitor);
|
||||
if (!CollectionUtils.isEmpty(join.getOnExpressions())) {
|
||||
join.getOnExpressions().stream().forEach(onExpression -> {
|
||||
onExpression.accept(visitor);
|
||||
});
|
||||
}
|
||||
if (!(join.getRightItem() instanceof ParenthesedSelect)) {
|
||||
continue;
|
||||
}
|
||||
// SelectBody subSelectBody = ((SubSelect) join.getRightItem()).getSelectBody();
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
plainSelectList.add((PlainSelect) join.getRightItem());
|
||||
List<PlainSelect> subPlainSelects = SqlSelectHelper.getPlainSelects(plainSelectList);
|
||||
@@ -270,7 +269,6 @@ public class SqlReplaceHelper {
|
||||
public static String replaceFunction(String sql, Map<String, String> functionMap,
|
||||
Map<String, UnaryOperator> functionCall) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
@@ -312,7 +310,6 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String replaceFunction(String sql) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
@@ -393,7 +390,27 @@ public class SqlReplaceHelper {
|
||||
return sql;
|
||||
}
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
List<PlainSelect> plainSelectList = SqlSelectHelper.getWithItem(selectStatement);
|
||||
if (!CollectionUtils.isEmpty(plainSelectList)) {
|
||||
List<String> withNameList = SqlSelectHelper.getWithName(sql);
|
||||
plainSelectList.stream().forEach(plainSelect -> {
|
||||
if (plainSelect.getFromItem() instanceof Table) {
|
||||
Table table = (Table) plainSelect.getFromItem();
|
||||
if (!withNameList.contains(table.getName())) {
|
||||
replaceSingleTable(plainSelect, tableName);
|
||||
}
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
|
||||
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
|
||||
Table table = (Table) subPlainSelect.getFromItem();
|
||||
if (!withNameList.contains(table.getName())) {
|
||||
replaceSingleTable(subPlainSelect, tableName);
|
||||
}
|
||||
}
|
||||
});
|
||||
return selectStatement.toString();
|
||||
}
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement;
|
||||
replaceSingleTable(plainSelect, tableName);
|
||||
@@ -437,7 +454,6 @@ public class SqlReplaceHelper {
|
||||
if (!CollectionUtils.isEmpty(joins)) {
|
||||
for (Join join : joins) {
|
||||
if (join.getRightItem() instanceof ParenthesedFromItem) {
|
||||
//SelectBody subSelectBody = ((SubSelect) join.getRightItem()).getSelectBody();
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
plainSelectList.add((PlainSelect) join.getRightItem());
|
||||
List<PlainSelect> subPlainSelects = SqlSelectHelper.getPlainSelects(plainSelectList);
|
||||
@@ -455,7 +471,6 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String replaceAlias(String sql) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
@@ -473,7 +488,6 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String replaceHavingValue(String sql, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
@@ -586,7 +600,6 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String replaceSqlByExpression(String sql, Map<String, String> replace) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
plainSelectList.add((PlainSelect) selectStatement);
|
||||
@@ -634,26 +647,39 @@ public class SqlReplaceHelper {
|
||||
|
||||
public static String dealAliasToOrderBy(String querySql) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(querySql);
|
||||
PlainSelect plainSelect = selectStatement.getPlainSelect();
|
||||
List<SelectItem<?>> selectItemList = plainSelect.getSelectItems();
|
||||
List<OrderByElement> orderByElementList = plainSelect.getOrderByElements();
|
||||
if (CollectionUtils.isEmpty(orderByElementList)) {
|
||||
return querySql;
|
||||
}
|
||||
Map<String, Expression> map = new HashMap<>();
|
||||
for (int i = 0; i < selectItemList.size(); i++) {
|
||||
if (!Objects.isNull(selectItemList.get(i).getAlias())) {
|
||||
map.put(selectItemList.get(i).getAlias().getName(), selectItemList.get(i).getExpression());
|
||||
selectItemList.get(i).setAlias(null);
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
plainSelectList.add((PlainSelect) selectStatement);
|
||||
} 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
for (OrderByElement orderByElement : orderByElementList) {
|
||||
if (map.containsKey(orderByElement.getExpression().toString())) {
|
||||
orderByElement.setExpression(map.get(orderByElement.getExpression().toString()));
|
||||
for (PlainSelect plainSelect : plainSelectList) {
|
||||
List<SelectItem<?>> selectItemList = plainSelect.getSelectItems();
|
||||
List<OrderByElement> orderByElementList = plainSelect.getOrderByElements();
|
||||
if (CollectionUtils.isEmpty(orderByElementList)) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Expression> map = new HashMap<>();
|
||||
for (int i = 0; i < selectItemList.size(); i++) {
|
||||
if (!Objects.isNull(selectItemList.get(i).getAlias())) {
|
||||
map.put(selectItemList.get(i).getAlias().getName(), selectItemList.get(i).getExpression());
|
||||
selectItemList.get(i).setAlias(null);
|
||||
}
|
||||
}
|
||||
for (OrderByElement orderByElement : orderByElementList) {
|
||||
if (map.containsKey(orderByElement.getExpression().toString())) {
|
||||
orderByElement.setExpression(map.get(orderByElement.getExpression().toString()));
|
||||
}
|
||||
}
|
||||
plainSelect.setOrderByElements(orderByElementList);
|
||||
}
|
||||
plainSelect.setOrderByElements(orderByElementList);
|
||||
return plainSelect.toString();
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ public class SqlSelectHelper {
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
private static void getWhereFields(List<PlainSelect> plainSelectList, Set<String> result) {
|
||||
public static void getWhereFields(List<PlainSelect> plainSelectList, Set<String> result) {
|
||||
plainSelectList.stream().forEach(plainSelect -> {
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (Objects.nonNull(where)) {
|
||||
@@ -121,9 +121,9 @@ public class SqlSelectHelper {
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
plainSelectList.addAll(getWithItem(selectStatement));
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement;
|
||||
getSubPlainSelect(plainSelect, plainSelectList);
|
||||
@@ -139,6 +139,20 @@ public class SqlSelectHelper {
|
||||
return plainSelectList;
|
||||
}
|
||||
|
||||
public static Boolean hasSubSelect(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return false;
|
||||
}
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement;
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void getSubPlainSelect(Select select, List<PlainSelect> plainSelectList) {
|
||||
if (select instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = (PlainSelect) select;
|
||||
@@ -453,7 +467,6 @@ public class SqlSelectHelper {
|
||||
|
||||
public static boolean hasGroupBy(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return false;
|
||||
@@ -470,7 +483,6 @@ public class SqlSelectHelper {
|
||||
|
||||
public static boolean hasDistinct(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return false;
|
||||
@@ -531,12 +543,89 @@ public class SqlSelectHelper {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static Boolean hasWith(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return false;
|
||||
}
|
||||
List<WithItem> withItemList = selectStatement.getWithItemsList();
|
||||
if (!CollectionUtils.isEmpty(withItemList)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<PlainSelect> getWithItem(Select selectStatement) {
|
||||
if (selectStatement == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
List<WithItem> withItemList = selectStatement.getWithItemsList();
|
||||
if (!CollectionUtils.isEmpty(withItemList)) {
|
||||
for (int i = 0; i < withItemList.size(); i++) {
|
||||
WithItem withItem = withItemList.get(i);
|
||||
Select withSelect = withItem.getSelect();
|
||||
if (withSelect instanceof PlainSelect) {
|
||||
PlainSelect withPlainSelect = (PlainSelect) withSelect;
|
||||
plainSelectList.add(withPlainSelect);
|
||||
if (withPlainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) withPlainSelect.getFromItem();
|
||||
plainSelectList.add(parenthesedSelect.getPlainSelect());
|
||||
}
|
||||
}
|
||||
if (withSelect instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) withSelect;
|
||||
PlainSelect withPlainSelect = parenthesedSelect.getPlainSelect();
|
||||
plainSelectList.add(withPlainSelect);
|
||||
}
|
||||
}
|
||||
}
|
||||
return plainSelectList;
|
||||
}
|
||||
|
||||
public static List<String> getWithName(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<String> withNameList = new ArrayList<>();
|
||||
List<WithItem> withItemList = selectStatement.getWithItemsList();
|
||||
if (!CollectionUtils.isEmpty(withItemList)) {
|
||||
for (int i = 0; i < withItemList.size(); i++) {
|
||||
WithItem withItem = withItemList.get(i);
|
||||
withNameList.add(withItem.getAlias().getName());
|
||||
}
|
||||
}
|
||||
return withNameList;
|
||||
}
|
||||
|
||||
public static Map<String, WithItem> getWith(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
Map<String, WithItem> withMap = new HashMap<>();
|
||||
List<WithItem> withItemList = selectStatement.getWithItemsList();
|
||||
if (!CollectionUtils.isEmpty(withItemList)) {
|
||||
for (int i = 0; i < withItemList.size(); i++) {
|
||||
WithItem withItem = withItemList.get(i);
|
||||
withMap.put(withItem.getAlias().getName(), withItem);
|
||||
}
|
||||
}
|
||||
return withMap;
|
||||
}
|
||||
|
||||
public static Table getTable(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
//SelectBody selectBody = selectStatement.getSelectBody();
|
||||
List<PlainSelect> plainSelectList = getWithItem(selectStatement);
|
||||
if (!CollectionUtils.isEmpty(plainSelectList)) {
|
||||
Table table = getTable(plainSelectList.get(0).toString());
|
||||
return table;
|
||||
}
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement;
|
||||
if (plainSelect.getFromItem() instanceof Table) {
|
||||
@@ -544,7 +633,6 @@ public class SqlSelectHelper {
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
|
||||
//ParenthesedFromItem subSelect = (ParenthesedFromItem) plainSelect.getFromItem();
|
||||
PlainSelect subSelect = ((ParenthesedSelect) plainSelect.getFromItem()).getPlainSelect();
|
||||
return getTable(subSelect.getSelectBody().toString());
|
||||
}
|
||||
@@ -577,7 +665,6 @@ public class SqlSelectHelper {
|
||||
columns.add(((Column) expression).getColumnName());
|
||||
}
|
||||
if (expression instanceof Function) {
|
||||
//List<Expression> expressionList = ((Function) expression).getParameters().getExpressions();
|
||||
ExpressionList<?> expressionList = ((Function) expression).getParameters();
|
||||
for (Expression expr : expressionList) {
|
||||
getColumnFromExpr(expr, columns);
|
||||
@@ -624,13 +711,26 @@ public class SqlSelectHelper {
|
||||
|
||||
public static Boolean hasLimit(String querySql) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(querySql);
|
||||
PlainSelect plainSelect = selectStatement.getPlainSelect();
|
||||
Limit limit = plainSelect.getLimit();
|
||||
if (Objects.nonNull(limit)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = selectStatement.getPlainSelect();
|
||||
Limit limit = plainSelect.getLimit();
|
||||
return Objects.nonNull(limit);
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
Boolean result = true;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
for (Select select : setOperationList.getSelects()) {
|
||||
PlainSelect subPlainSelect = select.getPlainSelect();
|
||||
Limit limit = subPlainSelect.getLimit();
|
||||
if (Objects.isNull(limit)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Map<String, Set<String>> getFieldsWithSubQuery(String sql) {
|
||||
|
||||
Reference in New Issue
Block a user