Issues 1974 (#1975)

* [fix][common]Support 'BETWEEN AND' query condition parameter parsing `CURRENT`. #1972
This commit is contained in:
zehuiHuang
2024-12-25 19:33:40 +08:00
committed by GitHub
parent 493a8035cd
commit 6738aba19e
3 changed files with 33 additions and 10 deletions

View File

@@ -26,6 +26,7 @@ public class JsqlConstants {
public static final String EQUAL_CONSTANT = " 1 = 1 ";
public static final String IN_CONSTANT = " 1 in (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 Map<String, String> rightMap = Stream.of(
new AbstractMap.SimpleEntry<>("<=", "<="), new AbstractMap.SimpleEntry<>("<", "<"),

View File

@@ -8,16 +8,7 @@ import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
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.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.expression.operators.relational.*;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.AllColumns;
@@ -183,6 +174,8 @@ public class SqlRemoveHelper {
handleInExpression((InExpression) expression, removeFieldNames);
} else if (expression instanceof LikeExpression) {
handleLikeExpression((LikeExpression) expression, removeFieldNames);
} else if (expression instanceof Between) {
handleBetweenExpression((Between) expression, removeFieldNames);
}
} catch (JSQLParserException e) {
log.error("JSQLParserException", e);
@@ -226,6 +219,17 @@ public class SqlRemoveHelper {
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,
ComparisonOperator constantExpression) {
original.setLeftExpression(constantExpression.getLeftExpression());
@@ -245,6 +249,12 @@ public class SqlRemoveHelper {
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) {
Select selectStatement = SqlSelectHelper.getSelect(sql);
if (!(selectStatement instanceof PlainSelect)) {
@@ -373,6 +383,10 @@ public class SqlRemoveHelper {
LikeExpression likeExpression = (LikeExpression) expression;
Expression leftExpression = likeExpression.getLeftExpression();
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;
}

View File

@@ -157,6 +157,14 @@ class SqlRemoveHelperTest {
replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames);
Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE (datediff('day', 发布日期, '2023-08-09') <= 1) "
+ "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