cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] PHQL: CASE expressions

Open igorgolovanov opened this issue 13 years ago • 21 comments

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.

igorgolovanov avatar May 20 '13 11:05 igorgolovanov

That would be great I was just looking for whis. Would it be ever implemented ?

brzuchal avatar Sep 06 '13 15:09 brzuchal

+1

joeyew avatar Nov 11 '13 09:11 joeyew

That would be great for me too! +1

joebak avatar Nov 11 '13 09:11 joebak

+1

wandersonwhcr avatar Jun 14 '14 13:06 wandersonwhcr

+1 Is there any way to force this statement not be parsed and passed into SQL as is?

vorbeer avatar Jun 19 '14 09:06 vorbeer

+1

ddenysov avatar Jun 19 '14 13:06 ddenysov

+1024

phoenixgao avatar Aug 23 '14 16:08 phoenixgao

@vorbeer with Phalcon\Db\RawValue, maybe?

http://docs.phalconphp.com/en/latest/api/Phalcon_Db_RawValue.html

wandersonwhcr avatar Aug 23 '14 16:08 wandersonwhcr

+1

@wandersonwhcr As reading the docs it seems to me that Phalcon\Db\RawValue stands exactly for situations like this but it throws Phalcon\Mvc\Model\Exception when I try to execute a query with CASE WHEN statement. :(

$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 $sql shows a well formatted query, there's no problem here. I can't understand why. Is it a bug?

tmihalik avatar Jan 08 '15 13:01 tmihalik

hi @tmihalik :smile:

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. :hankey:

wandersonwhcr avatar Jan 08 '15 14:01 wandersonwhcr

@tmihalik you can use IF:

SELECT IF(TRUE,1, 0) AS raw FROM Store\Robots

ghost avatar Jan 08 '15 14:01 ghost

@phalcon +1 :)

wandersonwhcr avatar Jan 08 '15 15:01 wandersonwhcr

@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 avatar Jan 08 '15 19:01 tmihalik

@tmihalik in postgresql I created a function called "if" with 3 parameters :hankey:

wandersonwhcr avatar Jan 08 '15 19:01 wandersonwhcr

@tmihalik maybe you want to extend the dialect to support that function:

  • https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Db#dialectmysqlextended
  • https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Db/Dialect/MysqlExtended.php

ghost avatar Jan 08 '15 19:01 ghost

@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 Phalcon\Db\RawValue working really as easy as "hey, put that string to the query as it is, I now what I'm doing". It could be Phalcon\Db\RawExpression

?

tmihalik avatar Jan 09 '15 12:01 tmihalik

@tmihalik Phalcon uses a high layer to build SQL called Phalcon\Db. It outputs an SQL language called PHQL (Phalcon Query Language), interpreted and parsed internally, to the target database.

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.

wandersonwhcr avatar Jan 09 '15 15:01 wandersonwhcr

This is implemented in 2.0.3

andresgutierrez avatar Jun 05 '15 15:06 andresgutierrez

@andresgutierrez

Great news! Thank you for implementing this. However Searched CASE syntax still throws syntax error. (first post)

CASE
    WHEN boolean-expression THEN
        statements
    [ WHEN boolean-expression THEN
        statements
    ... ]
    [ ELSE
        statements ]
END

tmihalik avatar Jun 11 '15 12:06 tmihalik

@andresgutierrez

It still does not work in postgreSql

broklyngagah avatar Dec 19 '15 21:12 broklyngagah