Skip to content

Commit

Permalink
Issues 1974 (#1975)
Browse files Browse the repository at this point in the history
* [fix][common]Support 'BETWEEN AND' query condition parameter parsing `CURRENT`. #1972
  • Loading branch information
zehuiHuang authored Dec 25, 2024
1 parent 493a803 commit 6738aba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<>("<", "<"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@
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;
Expand Down Expand Up @@ -183,6 +174,8 @@ private static void removeExpressionWithConstant(Expression expression,
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);
Expand Down Expand Up @@ -226,6 +219,17 @@ private static void handleLikeExpression(LikeExpression likeExpression,
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());
Expand All @@ -245,6 +249,12 @@ private static void updateLikeExpression(LikeExpression original,
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)) {
Expand Down Expand Up @@ -373,6 +383,10 @@ private static Expression dealComparisonOperatorFilter(Expression expression,
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ void testRemoveWhereCondition() {
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
Expand Down

0 comments on commit 6738aba

Please sign in to comment.