Skip to content

Commit 4df1391

Browse files
authored
Extending CaseExpression, covering #1458 (#1459)
* Add unit tests for Case expressions. * More tests for CaseExpression. * Switch expression becomes an Expression instead of a Condition. It allows complex expressions in the switch, similarly to what is allowed in when clauses.
1 parent 3695e04 commit 4df1391

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4393,7 +4393,7 @@ Expression CaseWhenExpression() #CaseWhenExpression:
43934393
}
43944394
{
43954395
<K_CASE> { caseCounter++; }
4396-
[ switchExp=Condition() ]
4396+
[ switchExp=Expression() ]
43974397
( clause=WhenThenSearchCondition() { whenClauses.add(clause); } )+
43984398
[<K_ELSE> (LOOKAHEAD( ["("] CaseWhenExpression() [")"] ( <K_WHEN> | <K_ELSE> | <K_END> ) ) ["("] elseExp=CaseWhenExpression() [")" { ((CaseExpression) elseExp).setUsingBrackets(true); } ]
43994399
| elseExp=Expression()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2022 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.expression;
11+
12+
import net.sf.jsqlparser.JSQLParserException;
13+
import net.sf.jsqlparser.test.TestUtils;
14+
import org.junit.jupiter.api.Test;
15+
16+
/**
17+
* @author Mathieu Goeminne
18+
*/
19+
public class CaseExpressionTest {
20+
@Test
21+
public void testSimpleCase() throws JSQLParserException {
22+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true WHEN true THEN 1 ELSE 2 END", true);
23+
}
24+
25+
@Test
26+
public void testCaseBinaryAndWhen() throws JSQLParserException {
27+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true WHEN true & false THEN 1 ELSE 2 END", true);
28+
}
29+
30+
@Test
31+
public void testCaseBinaryOrWhen() throws JSQLParserException {
32+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true WHEN true | false THEN 1 ELSE 2 END", true);
33+
}
34+
35+
@Test
36+
public void testCaseExclamationWhen() throws JSQLParserException {
37+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true WHEN !true THEN 1 ELSE 2 END", true);
38+
}
39+
40+
@Test
41+
public void testCaseNotWhen() throws JSQLParserException {
42+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true WHEN NOT true THEN 1 ELSE 2 END", true);
43+
}
44+
45+
@Test
46+
public void testCaseAndWhen() throws JSQLParserException {
47+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true WHEN true AND false THEN 1 ELSE 2 END", true);
48+
}
49+
50+
@Test
51+
public void testCaseOrWhen() throws JSQLParserException {
52+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true WHEN true OR false THEN 1 ELSE 2 END", true);
53+
}
54+
55+
@Test
56+
public void testCaseExclamationSwitch() throws JSQLParserException {
57+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE !true WHEN true THEN 1 ELSE 2 END", true);
58+
}
59+
60+
@Test
61+
public void testCaseNotSwitch() throws JSQLParserException {
62+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE NOT true WHEN true THEN 1 ELSE 2 END", true);
63+
}
64+
65+
@Test
66+
public void testCaseBinaryAndSwitch() throws JSQLParserException {
67+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true & false WHEN true THEN 1 ELSE 2 END", true);
68+
}
69+
70+
@Test
71+
public void testCaseBinaryOrSwitch() throws JSQLParserException {
72+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true | false WHEN true THEN 1 ELSE 2 END", true);
73+
}
74+
75+
@Test
76+
public void testCaseAndSwitch() throws JSQLParserException {
77+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true AND false WHEN true THEN 1 ELSE 2 END", true);
78+
}
79+
80+
@Test
81+
public void testCaseOrSwitch() throws JSQLParserException {
82+
TestUtils.assertExpressionCanBeParsedAndDeparsed("CASE true OR false WHEN true THEN 1 ELSE 2 END", true);
83+
}
84+
}

0 commit comments

Comments
 (0)