Permalink
Browse files

Add if statement

  • Loading branch information...
1 parent 29ab8fd commit 35ed6f9dabd7e77a414bc7354ecb0cf7ba12d979 @ivan1986 ivan1986 committed Apr 9, 2012
Showing with 49 additions and 0 deletions.
  1. +40 −0 DQL/IfStatement.php
  2. +9 −0 Resources/doc/dql_functions.rst
View
@@ -0,0 +1,40 @@
+<?php
+
+namespace Shtumi\UsefulBundle\DQL;
+
+use Doctrine\ORM\Query\AST\Functions\FunctionNode;
+use Doctrine\ORM\Query\Parser;
+use Doctrine\ORM\Query\SqlWalker;
+use Doctrine\ORM\Query\Lexer;
+
+
+class IfStatement extends FunctionNode
+{
+ // (1)
+
+ public $f1 = null;
+ public $f2 = null;
+ public $f3 = null;
+
+ public function parse(Parser $parser)
+ {
+ $parser->match(Lexer::T_IDENTIFIER); // (2)
+
+ $parser->match(Lexer::T_OPEN_PARENTHESIS); // (3)
+ $this->f1 = $parser->SimpleConditionalExpression(); // (4)
+ $parser->match(Lexer::T_COMMA); // (5)
+ $this->f2 = $parser->ArithmeticPrimary(); // (6)
+ $parser->match(Lexer::T_COMMA); // (5)
+ $this->f3 = $parser->ArithmeticPrimary(); // (6)
+ $parser->match(Lexer::T_CLOSE_PARENTHESIS); // (3)
+ }
+
+ public function getSql(SqlWalker $sqlWalker)
+ {
+ return 'IF(' .
+ $this->f1->dispatch($sqlWalker) . ', ' .
+ $this->f2->dispatch($sqlWalker) . ', ' .
+ $this->f3->dispatch($sqlWalker) .
+ ')'; // (7)
+ }
+}
@@ -21,13 +21,22 @@ To use provided by ShtumiUsefulBundle DQL functions you should configure your do
datetime_functions:
datediff: Shtumi\UsefulBundle\DQL\DateDiff
numeric_functions:
+ if: Shtumi\UsefulBundle\DQL\IfStatement
ifnull: Shtumi\UsefulBundle\DQL\IfNull
round: Shtumi\UsefulBundle\DQL\Round
Usage
=====
+IF
+------
+
+::
+
+ $em->createQuery('SELECT IF(s.a>s.b, s.a, s.b)
+ FROM AcmeDemoBundle:Sale s')
+
IFNULL
------

0 comments on commit 35ed6f9

Please sign in to comment.