mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-14 13:47:09 +00:00
Issues 1974 (#1975)
* [fix][common]Support 'BETWEEN AND' query condition parameter parsing `CURRENT`. #1972
This commit is contained in:
@@ -26,6 +26,7 @@ public class JsqlConstants {
|
|||||||
public static final String EQUAL_CONSTANT = " 1 = 1 ";
|
public static final String EQUAL_CONSTANT = " 1 = 1 ";
|
||||||
public static final String IN_CONSTANT = " 1 in (1) ";
|
public static final String IN_CONSTANT = " 1 in (1) ";
|
||||||
public static final String LIKE_CONSTANT = "1 like 1";
|
public static final String LIKE_CONSTANT = "1 like 1";
|
||||||
|
public static final String BETWEEN_AND_CONSTANT = "1 between 2 and 3";
|
||||||
public static final String IN = "IN";
|
public static final String IN = "IN";
|
||||||
public static final Map<String, String> rightMap = Stream.of(
|
public static final Map<String, String> rightMap = Stream.of(
|
||||||
new AbstractMap.SimpleEntry<>("<=", "<="), new AbstractMap.SimpleEntry<>("<", "<"),
|
new AbstractMap.SimpleEntry<>("<=", "<="), new AbstractMap.SimpleEntry<>("<", "<"),
|
||||||
|
|||||||
@@ -8,16 +8,7 @@ import net.sf.jsqlparser.expression.LongValue;
|
|||||||
import net.sf.jsqlparser.expression.Parenthesis;
|
import net.sf.jsqlparser.expression.Parenthesis;
|
||||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||||
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
||||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
import net.sf.jsqlparser.expression.operators.relational.*;
|
||||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
|
||||||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
|
||||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
|
||||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
|
|
||||||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
|
||||||
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.NotEqualsTo;
|
|
||||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||||
import net.sf.jsqlparser.schema.Column;
|
import net.sf.jsqlparser.schema.Column;
|
||||||
import net.sf.jsqlparser.statement.select.AllColumns;
|
import net.sf.jsqlparser.statement.select.AllColumns;
|
||||||
@@ -183,6 +174,8 @@ public class SqlRemoveHelper {
|
|||||||
handleInExpression((InExpression) expression, removeFieldNames);
|
handleInExpression((InExpression) expression, removeFieldNames);
|
||||||
} else if (expression instanceof LikeExpression) {
|
} else if (expression instanceof LikeExpression) {
|
||||||
handleLikeExpression((LikeExpression) expression, removeFieldNames);
|
handleLikeExpression((LikeExpression) expression, removeFieldNames);
|
||||||
|
} else if (expression instanceof Between) {
|
||||||
|
handleBetweenExpression((Between) expression, removeFieldNames);
|
||||||
}
|
}
|
||||||
} catch (JSQLParserException e) {
|
} catch (JSQLParserException e) {
|
||||||
log.error("JSQLParserException", e);
|
log.error("JSQLParserException", e);
|
||||||
@@ -226,6 +219,17 @@ public class SqlRemoveHelper {
|
|||||||
updateLikeExpression(likeExpression, constantExpression);
|
updateLikeExpression(likeExpression, constantExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void handleBetweenExpression(Between between,
|
||||||
|
Set<String> removeFieldNames) throws JSQLParserException {
|
||||||
|
String columnName = SqlSelectHelper.getColumnName(between.getLeftExpression());
|
||||||
|
if (!removeFieldNames.contains(columnName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Between constantExpression =
|
||||||
|
(Between) CCJSqlParserUtil.parseCondExpression(JsqlConstants.BETWEEN_AND_CONSTANT);
|
||||||
|
updateBetweenExpression(between, constantExpression);
|
||||||
|
}
|
||||||
|
|
||||||
private static void updateComparisonOperator(ComparisonOperator original,
|
private static void updateComparisonOperator(ComparisonOperator original,
|
||||||
ComparisonOperator constantExpression) {
|
ComparisonOperator constantExpression) {
|
||||||
original.setLeftExpression(constantExpression.getLeftExpression());
|
original.setLeftExpression(constantExpression.getLeftExpression());
|
||||||
@@ -245,6 +249,12 @@ public class SqlRemoveHelper {
|
|||||||
original.setRightExpression(constantExpression.getRightExpression());
|
original.setRightExpression(constantExpression.getRightExpression());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void updateBetweenExpression(Between between, Between constantExpression) {
|
||||||
|
between.setBetweenExpressionEnd(constantExpression.getBetweenExpressionEnd());
|
||||||
|
between.setBetweenExpressionStart(constantExpression.getBetweenExpressionStart());
|
||||||
|
between.setLeftExpression(constantExpression.getLeftExpression());
|
||||||
|
}
|
||||||
|
|
||||||
public static String removeHavingCondition(String sql, Set<String> removeFieldNames) {
|
public static String removeHavingCondition(String sql, Set<String> removeFieldNames) {
|
||||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||||
if (!(selectStatement instanceof PlainSelect)) {
|
if (!(selectStatement instanceof PlainSelect)) {
|
||||||
@@ -373,6 +383,10 @@ public class SqlRemoveHelper {
|
|||||||
LikeExpression likeExpression = (LikeExpression) expression;
|
LikeExpression likeExpression = (LikeExpression) expression;
|
||||||
Expression leftExpression = likeExpression.getLeftExpression();
|
Expression leftExpression = likeExpression.getLeftExpression();
|
||||||
return recursionBase(leftExpression, expression, sqlEditEnum);
|
return recursionBase(leftExpression, expression, sqlEditEnum);
|
||||||
|
} else if (expression instanceof Between) {
|
||||||
|
Between between = (Between) expression;
|
||||||
|
Expression leftExpression = between.getLeftExpression();
|
||||||
|
return recursionBase(leftExpression, expression, sqlEditEnum);
|
||||||
}
|
}
|
||||||
return expression;
|
return expression;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -157,6 +157,14 @@ class SqlRemoveHelperTest {
|
|||||||
replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames);
|
replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames);
|
||||||
Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE (datediff('day', 发布日期, '2023-08-09') <= 1) "
|
Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE (datediff('day', 发布日期, '2023-08-09') <= 1) "
|
||||||
+ "AND 数据日期 = '2023-08-09' ORDER BY 播放量 DESC LIMIT 11", replaceSql);
|
+ "AND 数据日期 = '2023-08-09' ORDER BY 播放量 DESC LIMIT 11", replaceSql);
|
||||||
|
|
||||||
|
sql = "select 歌曲名 from 歌曲库 where datediff('day', 发布日期, '2023-08-09') <= 1 "
|
||||||
|
+ "and 歌曲名 between '2023-08-09' and '2024-08-09' and 数据日期 = '2023-08-09' and 歌曲发布时 = '2023-08-01'"
|
||||||
|
+ " order by 播放量 desc limit 11";
|
||||||
|
replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames);
|
||||||
|
Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE datediff('day', 发布日期, '2023-08-09') <= 1 "
|
||||||
|
+ "AND 数据日期 = '2023-08-09' AND 歌曲发布时 = '2023-08-01' "
|
||||||
|
+ "ORDER BY 播放量 DESC LIMIT 11", replaceSql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user