Description
I think, we needs to provide a new built-in functionality into PHQL for working with CASE syntax.
There're two syntaxes: Simple CASE and Searched CASE.
Simple CASE syntax
The simple CASE function compares an expression to a set of simple expressions to determine the result.
CASE search-expression
WHEN expression [, expression [ ... ]] THEN
statements
[ WHEN expression [, expression [ ... ]] THEN
statements
... ]
[ ELSE
statements ]
END
The simple form of CASE provides conditional execution based on equality of operands.
The search-expression is evaluated (once) and successively compared to each expression in the WHEN clauses.
If a match is found, then the corresponding statements are executed, and then control passes to the next statement after END CASE.
If no match is found, the ELSE statements are executed; but if ELSE is not present, then must be returned a NULL value.
Searched CASE syntax
The searched CASE function evaluates a set of Boolean expressions to determine the result.
CASE
WHEN boolean-expression THEN
statements
[ WHEN boolean-expression THEN
statements
... ]
[ ELSE
statements ]
END
The searched form of CASE provides conditional execution based on truth of boolean expressions.
Each WHEN clause's boolean-expression is evaluated in turn, until one is found that yields true.
Then the corresponding statements are executed, and then control passes to the next statement after END CASE.
If no true result is found, the ELSE statements are executed; but if ELSE is not present, then must be returned a NULL value.
General Agreement
- if ELSE is not present, then must be returned a NULL value. Because different db have a different behavior in this case, i think that PHQL must always add "ELSE NULL" if else not presented in user query.
- CASE must be closed with END.
Reference Manuals:
- MySql: http://dev.mysql.com/doc/refman/5.0/en/case.html
- Postgresql: http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS
- Oracle: http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm
- Microsoft Sql Server: http://msdn.microsoft.com/en-us/library/ms173318.aspx
- Sqlite: http://www.sqlite.org/lang_expr.html
Where it implemented?
- Doctrine: http://docs.doctrine-project.org/de/latest/reference/dql-doctrine-query-language.html#case-expressions
- Hibernate: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html#d5e3099
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.