HHH-1689 - Support subqueries in HQL as CASE statement alternatives;
SQM-30 - test
(cherry picked from commit 57401b9aa8
)
This commit is contained in:
parent
7633d2fce5
commit
fa7a675490
|
@ -611,10 +611,36 @@ arithmeticExpr
|
|||
;
|
||||
|
||||
caseExpr
|
||||
: #(CASE { inCase = true; } (#(WHEN logicalExpr expr))+ (#(ELSE expr))?) { inCase = false; }
|
||||
| #(CASE2 { inCase = true; } expr (#(WHEN expr expr))+ (#(ELSE expr))?) { inCase = false; }
|
||||
: simpleCaseExpression
|
||||
| searchedCaseExpression
|
||||
;
|
||||
|
||||
expressionOrSubQuery
|
||||
: expr
|
||||
| query
|
||||
;
|
||||
|
||||
simpleCaseExpression
|
||||
: #(CASE2 {inCase=true;} expressionOrSubQuery (simpleCaseWhenClause)+ (elseClause)?) {inCase=false;}
|
||||
;
|
||||
|
||||
simpleCaseWhenClause
|
||||
: #(WHEN expressionOrSubQuery expressionOrSubQuery)
|
||||
;
|
||||
|
||||
elseClause
|
||||
: #(ELSE expressionOrSubQuery)
|
||||
;
|
||||
|
||||
searchedCaseExpression
|
||||
: #(CASE {inCase = true;} (searchedCaseWhenClause)+ (elseClause)?) {inCase = false;}
|
||||
;
|
||||
|
||||
searchedCaseWhenClause
|
||||
: #(WHEN logicalExpr expressionOrSubQuery)
|
||||
;
|
||||
|
||||
|
||||
//TODO: I don't think we need this anymore .. how is it different to
|
||||
// maxelements, etc, which are handled by functionCall
|
||||
collectionFunction
|
||||
|
|
|
@ -639,23 +639,34 @@ unaryExpression
|
|||
| quantifiedExpression
|
||||
| atom
|
||||
;
|
||||
|
||||
|
||||
caseExpression
|
||||
: CASE^ (whenClause)+ (elseClause)? END!
|
||||
| CASE^ { #CASE.setType(CASE2); } unaryExpression (altWhenClause)+ (elseClause)? END!
|
||||
// NOTE : the unaryExpression rule contains the subQuery rule
|
||||
: simpleCaseStatement
|
||||
| searchedCaseStatement
|
||||
;
|
||||
|
||||
whenClause
|
||||
: (WHEN^ logicalExpression THEN! unaryExpression)
|
||||
|
||||
simpleCaseStatement
|
||||
: CASE^ unaryExpression (simpleCaseWhenClause)+ (elseClause)? END! {
|
||||
#simpleCaseStatement.setType(CASE2);
|
||||
}
|
||||
;
|
||||
|
||||
altWhenClause
|
||||
|
||||
simpleCaseWhenClause
|
||||
: (WHEN^ unaryExpression THEN! unaryExpression)
|
||||
;
|
||||
|
||||
|
||||
elseClause
|
||||
: (ELSE^ unaryExpression)
|
||||
;
|
||||
|
||||
searchedCaseStatement
|
||||
: CASE^ (searchedCaseWhenClause)+ (elseClause)? END!
|
||||
;
|
||||
|
||||
searchedCaseWhenClause
|
||||
: (WHEN^ logicalExpression THEN! unaryExpression)
|
||||
;
|
||||
|
||||
quantifiedExpression
|
||||
: ( SOME^ | EXISTS^ | ALL^ | ANY^ )
|
||||
|
|
|
@ -17,6 +17,7 @@ import javax.persistence.Table;
|
|||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
@ -155,4 +156,64 @@ public class SubQueryTest extends BaseCoreFunctionalTestCase {
|
|||
s.close();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-1689, SQM-30" )
|
||||
public void testSubQueryAsSearchedCaseResultExpression() {
|
||||
final String query = "SELECT CASE WHEN l.id IS NOT NULL THEN (SELECT COUNT(r.id) FROM Root r) ELSE 0 END FROM Leaf l";
|
||||
// simple syntax check
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( query ).list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-1689, SQM-30" )
|
||||
public void testSubQueryAsSearchedCaseExpression() {
|
||||
final String query = "SELECT CASE WHEN (SELECT COUNT(r.id) FROM Root r) > 1 THEN 1 ELSE 0 END FROM Leaf l";
|
||||
// simple syntax check
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( query ).list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-1689, SQM-30" )
|
||||
public void testSubQueryAsCaseElseResultExpression() {
|
||||
final String query = "SELECT CASE WHEN l.id > 1 THEN 1 ELSE (SELECT COUNT(r.id) FROM Root r) END FROM Leaf l";
|
||||
// simple syntax check
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( query ).list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-1689, SQM-30" )
|
||||
public void testSubQueryAsSimpleCaseTestExpression() {
|
||||
final String query = "SELECT CASE (SELECT COUNT(r.id) FROM Root r) WHEN 1 THEN 1 ELSE 0 END FROM Leaf l";
|
||||
// simple syntax check
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( query ).list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-1689, SQM-30" )
|
||||
public void testSubQueryAsSimpleCaseWhenExpression() {
|
||||
final String query = "SELECT CASE l.id WHEN (SELECT COUNT(r.id) FROM Root r) THEN 1 ELSE 0 END FROM Leaf l";
|
||||
// simple syntax check
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( query ).list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue