-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NFR] PHQL: CASE expressions #651
Comments
That would be great I was just looking for whis. Would it be ever implemented ? |
+1 |
That would be great for me too! |
+1 |
+1 |
+1 |
+1024 |
@vorbeer with Phalcon\Db\RawValue, maybe? http://docs.phalconphp.com/en/latest/api/Phalcon_Db_RawValue.html |
+1 @wandersonwhcr As reading the docs it seems to me that $raw = new \Phalcon\Db\RawValue('CASE WHEN TRUE THEN 1 ELSE 0 END AS raw');
$query = $this->modelsManager->createBuilder()->columns(['col1, col2', $raw])
->from(...
$sql = $query->getPhql(); //debug
$query->getQuery()->execute(); //this line throws the exception Debugging |
hi @tmihalik 😄 It's not a bug, I think. After 4 months learning Phalcon, I think it is a new feature to implement in PHQL parser, which don't recognize CASE statements inside column definitions. If I remember, PHQL recognize CASE as a function call, waiting to an open bracket in the next token. 💩 |
@tmihalik you can use IF: SELECT IF(TRUE,1, 0) AS raw FROM Store\Robots |
@phalcon +1 :) |
@phalcon It works for Mysql, but not with Postgresql. This problem is nicely solved in Kohana framework and it's very useful. http://kohanaframework.org/3.3/guide/database/query/builder#database-expressions Something like this would be great in Phalcon. What do you think? |
@tmihalik in postgresql I created a function called "if" with 3 parameters 💩 |
@tmihalik maybe you want to extend the dialect to support that function: |
@phalcon Thanks! Extending the getSqlExpression method of the dialect class I was able to create a new functionCall type expression named "expr()" to handle any custom statements I want. Actually it's just returns the first argument to the query as it is. // postgreSQL
$this->modelsManager->createBuilder()->columns(['col1, col2',
"expr('CASE WHEN TRUE THEN 1 ELSE 0 END') AS raw",
"expr('name || age') AS name_and_age"
])->from(... So this is a workaround, however I still think there should be some easy built-in - documented - way to handle this common problem. A class like ? |
@tmihalik Phalcon uses a high layer to build SQL called I think we can't check if some code is a RAW value at "parse time". I think we need to change the parser to understand the new expression. |
This is implemented in 2.0.3 |
Great news! CASE
WHEN boolean-expression THEN
statements
[ WHEN boolean-expression THEN
statements
... ]
[ ELSE
statements ]
END |
It still does not work in postgreSql |
@broklyngagah If so create issue |
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.
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
Reference Manuals:
Where it implemented?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: