mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
(improvement)(chat) dsl supports revision (#200)
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import net.sf.jsqlparser.expression.DoubleValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
|
||||
@@ -19,6 +22,7 @@ import net.sf.jsqlparser.schema.Column;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@Slf4j
|
||||
public class FieldlValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
ParseVisitorHelper parseVisitorHelper = new ParseVisitorHelper();
|
||||
@@ -55,7 +59,7 @@ public class FieldlValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
public <T extends Expression> void replaceComparisonExpression(T expression) {
|
||||
Expression leftExpression = ((ComparisonOperator) expression).getLeftExpression();
|
||||
Expression rightExpression = ((ComparisonOperator) expression).getRightExpression();
|
||||
if (!(leftExpression instanceof Column)) {
|
||||
if (!(leftExpression instanceof Column || leftExpression instanceof Function)) {
|
||||
return;
|
||||
}
|
||||
if (CollectionUtils.isEmpty(filedNameToValueMap)) {
|
||||
@@ -64,18 +68,30 @@ public class FieldlValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
if (Objects.isNull(rightExpression) || Objects.isNull(leftExpression)) {
|
||||
return;
|
||||
}
|
||||
Column leftColumnName = (Column) leftExpression;
|
||||
|
||||
String columnName = leftColumnName.getColumnName();
|
||||
String columnName = "";
|
||||
if (leftExpression instanceof Column) {
|
||||
Column leftColumnName = (Column) leftExpression;
|
||||
columnName = leftColumnName.getColumnName();
|
||||
}
|
||||
if (leftExpression instanceof Function) {
|
||||
Function function = (Function) leftExpression;
|
||||
columnName = ((Column) function.getParameters().getExpressions().get(0)).getColumnName();
|
||||
}
|
||||
if (StringUtils.isEmpty(columnName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> valueMap = filedNameToValueMap.get(columnName);
|
||||
Map<String, String> valueMap = new HashMap<>();
|
||||
for (String key : filedNameToValueMap.keySet()) {
|
||||
if (columnName.contains(key)) {
|
||||
valueMap = filedNameToValueMap.get(key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
//filedNameToValueMap.get(columnName);
|
||||
if (Objects.isNull(valueMap) || valueMap.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (rightExpression instanceof LongValue) {
|
||||
LongValue rightStringValue = (LongValue) rightExpression;
|
||||
String replaceValue = getReplaceValue(valueMap, String.valueOf(rightStringValue.getValue()));
|
||||
|
||||
@@ -154,6 +154,19 @@ public class SqlParserSelectHelper {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<FilterExpression> getHavingExpressions(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<FilterExpression> result = new HashSet<>();
|
||||
Expression having = plainSelect.getHaving();
|
||||
if (Objects.nonNull(having)) {
|
||||
having.accept(new FieldAndValueAcquireVisitor(result));
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
public static List<String> getOrderByFields(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
|
||||
@@ -59,6 +59,21 @@ public class SqlParserUpdateHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceHavingValue(String sql, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
Expression having = plainSelect.getHaving();
|
||||
FieldlValueReplaceVisitor visitor = new FieldlValueReplaceVisitor(false, filedNameToValueMap);
|
||||
if (Objects.nonNull(having)) {
|
||||
having.accept(visitor);
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceFieldNameByValue(String sql, Map<String, Set<String>> fieldValueToFieldNames) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
Reference in New Issue
Block a user