I ask how to make CASE work, or why it doesn't work(especially given that the same CASE expression works in pure SQL). I am NOT looking for alternate solutions.
I'm working with the Doctrine ORM(and its SQL-derivative query language - DQL) and trying to create a DQL query with a CASE expression in the WHERE clause, but Doctrine throws an error at me.
DQL:
SELECT p FROM Entity\Product p WHERE (CASE WHEN p.price < 20 THEN p.quantity >= 30 ELSE p.quantity > 5 END)
I receive the following error:
[Syntax Error] line 0, col 87: Error: Expected Doctrine\ORM\Query\Lexer::T_ELSE, got '>'
(around the >= 30
above)
I can't figure out what I'm doing wrong. I know DQL supports CASE expressions. I've found SO answers that use them. At the very least the stack trace has a parser call acknowledging that.
The same SQL statement:
SELECT p.* FROM product p WHERE (CASE WHEN p.price < 20 THEN p.quantity >= 30 ELSE p.quantity > 5 END)
works as it should.
With the help of a colleague, and based on the formal grammar of DQL, what I am doing wrong is trying to use an unsupported feature.
At present(as of Doctrine v2.6) DQL simply does not support the CASE expression evaluating to a conditional expression. It may ONLY evaluate to a ScalarExpression
as defined in the syntax which cannot be an instance of ConditionalExpression
.
ScalarExpression ::= SimpleArithmeticExpression | StringPrimary | DateTimePrimary | StateFieldPathExpression | BooleanPrimary | CaseExpression | InstanceOfExpression
CaseExpression ::= GeneralCaseExpression | SimpleCaseExpression | CoalesceExpression | NullifExpression
GeneralCaseExpression ::= "CASE" WhenClause {WhenClause}* "ELSE" ScalarExpression "END"
WhenClause ::= "WHEN" ConditionalExpression "THEN" ScalarExpression
SimpleCaseExpression ::= "CASE" CaseOperand SimpleWhenClause {SimpleWhenClause}* "ELSE" ScalarExpression "END"
CaseOperand ::= StateFieldPathExpression | TypeDiscriminator
SimpleWhenClause ::= "WHEN" ScalarExpression "THEN" ScalarExpression
CoalesceExpression ::= "COALESCE" "(" ScalarExpression {"," ScalarExpression}* ")"
NullifExpression ::= "NULLIF" "(" ScalarExpression "," ScalarExpression ")"