mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
(improvement)(chat) dsl supports revision (#254)
This commit is contained in:
@@ -12,13 +12,14 @@ import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
@@ -46,6 +47,14 @@ public class FieldAndValueAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
filterExpressions.add(filterExpression);
|
||||
}
|
||||
|
||||
public void visit(InExpression expr) {
|
||||
FilterExpression filterExpression = new FilterExpression();
|
||||
filterExpression.setFieldName(((Column) expr.getLeftExpression()).getColumnName());
|
||||
filterExpression.setOperator(JsqlConstants.IN);
|
||||
filterExpression.setFieldValue(expr.getRightItemsList());
|
||||
filterExpressions.add(filterExpression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MinorThan expr) {
|
||||
FilterExpression filterExpression = getFilterExpression(expr);
|
||||
@@ -139,4 +148,4 @@ public class FieldAndValueAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,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 || leftExpression instanceof Function)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -16,5 +16,6 @@ public class JsqlConstants {
|
||||
public static final String EQUAL_CONSTANT = " 1 = 1 ";
|
||||
|
||||
public static final String IN_CONSTANT = " 1 in (1) ";
|
||||
public static final String IN = "IN";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +113,31 @@ public class SqlParserAddHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String addWhere(String sql, List<Expression> expressionList) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
if (CollectionUtils.isEmpty(expressionList)) {
|
||||
return sql;
|
||||
}
|
||||
Expression expression = expressionList.get(0);
|
||||
for (int i = 1; i < expressionList.size(); i++) {
|
||||
expression = new AndExpression(expression, expressionList.get(i));
|
||||
}
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
Expression where = plainSelect.getWhere();
|
||||
|
||||
if (where == null) {
|
||||
plainSelect.setWhere(expression);
|
||||
} else {
|
||||
plainSelect.setWhere(new AndExpression(where, expression));
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String addAggregateToField(String sql, Map<String, String> fieldNameToAggregate) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
@@ -274,6 +299,31 @@ public class SqlParserAddHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String addHaving(String sql, List<Expression> expressionList) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
if (CollectionUtils.isEmpty(expressionList)) {
|
||||
return sql;
|
||||
}
|
||||
Expression expression = expressionList.get(0);
|
||||
for (int i = 1; i < expressionList.size(); i++) {
|
||||
expression = new AndExpression(expression, expressionList.get(i));
|
||||
}
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
Expression having = plainSelect.getHaving();
|
||||
|
||||
if (having == null) {
|
||||
plainSelect.setHaving(expression);
|
||||
} else {
|
||||
plainSelect.setHaving(new AndExpression(having, expression));
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String addParenthesisToWhere(String sql) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
@@ -7,8 +7,12 @@ import net.sf.jsqlparser.JSQLParserException;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.Parenthesis;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
@@ -60,17 +64,36 @@ public class SqlParserRemoveHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getConstant(Expression expression){
|
||||
String constant = JsqlConstants.EQUAL_CONSTANT;
|
||||
if (expression instanceof GreaterThanEquals) {
|
||||
constant = JsqlConstants.GREATER_THAN_EQUALS_CONSTANT;
|
||||
} else if (expression instanceof MinorThanEquals) {
|
||||
constant = JsqlConstants.MINOR_THAN_EQUALS_CONSTANT;
|
||||
} else if (expression instanceof GreaterThan) {
|
||||
constant = JsqlConstants.GREATER_THAN_CONSTANT;
|
||||
} else if (expression instanceof MinorThan) {
|
||||
constant = JsqlConstants.MINOR_THAN_CONSTANT;
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
private static void removeExpressionWithConstant(Expression expression, Set<String> removeFieldNames) {
|
||||
if (expression instanceof EqualsTo) {
|
||||
if (expression instanceof EqualsTo
|
||||
|| expression instanceof GreaterThanEquals
|
||||
|| expression instanceof GreaterThan
|
||||
|| expression instanceof MinorThanEquals
|
||||
|| expression instanceof MinorThan) {
|
||||
ComparisonOperator comparisonOperator = (ComparisonOperator) expression;
|
||||
String columnName = SqlParserSelectHelper.getColumnName(comparisonOperator.getLeftExpression(),
|
||||
comparisonOperator.getRightExpression());
|
||||
if (!removeFieldNames.contains(columnName)) {
|
||||
return;
|
||||
}
|
||||
String constant = getConstant(expression);
|
||||
try {
|
||||
ComparisonOperator constantExpression = (ComparisonOperator) CCJSqlParserUtil.parseCondExpression(
|
||||
JsqlConstants.EQUAL_CONSTANT);
|
||||
constant);
|
||||
comparisonOperator.setLeftExpression(constantExpression.getLeftExpression());
|
||||
comparisonOperator.setRightExpression(constantExpression.getRightExpression());
|
||||
comparisonOperator.setASTNode(constantExpression.getASTNode());
|
||||
@@ -97,6 +120,22 @@ public class SqlParserRemoveHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public static String removeHavingCondition(String sql, Set<String> removeFieldNames) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
selectBody.accept(new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
removeWhereCondition(plainSelect.getHaving(), removeFieldNames);
|
||||
}
|
||||
});
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String removeWhere(String sql, List<String> fields) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
@@ -208,6 +208,19 @@ public class SqlParserSelectHelper {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<FilterExpression> getWhereExpressions(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<FilterExpression> result = new HashSet<>();
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(new FieldAndValueAcquireVisitor(result));
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
public static List<FilterExpression> getHavingExpressions(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
@@ -317,6 +330,12 @@ public class SqlParserSelectHelper {
|
||||
if (leftExpression instanceof Column) {
|
||||
return ((Column) leftExpression).getColumnName();
|
||||
}
|
||||
if (leftExpression instanceof Function) {
|
||||
List<Expression> expressionList = ((Function) leftExpression).getParameters().getExpressions();
|
||||
if (!CollectionUtils.isEmpty(expressionList) && expressionList.get(0) instanceof Column) {
|
||||
return ((Column) expressionList.get(0)).getColumnName();
|
||||
}
|
||||
}
|
||||
if (rightExpression instanceof Column) {
|
||||
return ((Column) rightExpression).getColumnName();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,18 @@ import org.junit.jupiter.api.Test;
|
||||
*/
|
||||
class SqlParserRemoveHelperTest {
|
||||
|
||||
@Test
|
||||
void removeHavingCondition() {
|
||||
String sql = "select 歌曲名 from 歌曲库 where 歌手名 = '周杰伦' HAVING sum(播放量) > 20000";
|
||||
Set<String> removeFieldNames = new HashSet<>();
|
||||
removeFieldNames.add("播放量");
|
||||
String replaceSql = SqlParserRemoveHelper.removeHavingCondition(sql, removeFieldNames);
|
||||
Assert.assertEquals(
|
||||
"SELECT 歌曲名 FROM 歌曲库 WHERE 歌手名 = '周杰伦' HAVING 2 > 1",
|
||||
replaceSql);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeWhereCondition() {
|
||||
String sql = "select 歌曲名 from 歌曲库 where datediff('day', 发布日期, '2023-08-09') <= 1 "
|
||||
|
||||
Reference in New Issue
Block a user