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
|
caseExpr
|
||||||
: #(CASE { inCase = true; } (#(WHEN logicalExpr expr))+ (#(ELSE expr))?) { inCase = false; }
|
: simpleCaseExpression
|
||||||
| #(CASE2 { inCase = true; } expr (#(WHEN expr expr))+ (#(ELSE expr))?) { inCase = false; }
|
| 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
|
//TODO: I don't think we need this anymore .. how is it different to
|
||||||
// maxelements, etc, which are handled by functionCall
|
// maxelements, etc, which are handled by functionCall
|
||||||
collectionFunction
|
collectionFunction
|
||||||
|
|
|
@ -641,15 +641,18 @@ unaryExpression
|
||||||
;
|
;
|
||||||
|
|
||||||
caseExpression
|
caseExpression
|
||||||
: CASE^ (whenClause)+ (elseClause)? END!
|
// NOTE : the unaryExpression rule contains the subQuery rule
|
||||||
| CASE^ { #CASE.setType(CASE2); } unaryExpression (altWhenClause)+ (elseClause)? END!
|
: simpleCaseStatement
|
||||||
|
| searchedCaseStatement
|
||||||
;
|
;
|
||||||
|
|
||||||
whenClause
|
simpleCaseStatement
|
||||||
: (WHEN^ logicalExpression THEN! unaryExpression)
|
: CASE^ unaryExpression (simpleCaseWhenClause)+ (elseClause)? END! {
|
||||||
|
#simpleCaseStatement.setType(CASE2);
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
altWhenClause
|
simpleCaseWhenClause
|
||||||
: (WHEN^ unaryExpression THEN! unaryExpression)
|
: (WHEN^ unaryExpression THEN! unaryExpression)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -657,6 +660,14 @@ elseClause
|
||||||
: (ELSE^ unaryExpression)
|
: (ELSE^ unaryExpression)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
searchedCaseStatement
|
||||||
|
: CASE^ (searchedCaseWhenClause)+ (elseClause)? END!
|
||||||
|
;
|
||||||
|
|
||||||
|
searchedCaseWhenClause
|
||||||
|
: (WHEN^ logicalExpression THEN! unaryExpression)
|
||||||
|
;
|
||||||
|
|
||||||
quantifiedExpression
|
quantifiedExpression
|
||||||
: ( SOME^ | EXISTS^ | ALL^ | ANY^ )
|
: ( SOME^ | EXISTS^ | ALL^ | ANY^ )
|
||||||
( identifier | collectionExpr | (OPEN! ( subQuery ) CLOSE!) )
|
( identifier | collectionExpr | (OPEN! ( subQuery ) CLOSE!) )
|
||||||
|
|
|
@ -17,6 +17,7 @@ import javax.persistence.Table;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
|
||||||
|
import org.hibernate.testing.FailureExpected;
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -155,4 +156,64 @@ public class SubQueryTest extends BaseCoreFunctionalTestCase {
|
||||||
s.close();
|
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