-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optional second parameter (fmt) for Oracle TRUNC function + test. (#301)
Oracle TRUNC function fmt parameter is optional
- Loading branch information
1 parent
1d02c58
commit 469f653
Showing
2 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,33 +3,53 @@ | |
namespace DoctrineExtensions\Query\Oracle; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\AST\Node; | ||
use Doctrine\ORM\Query\Lexer; | ||
|
||
/** | ||
* @author Mohammad ZeinEddin <[email protected]> | ||
*/ | ||
class Trunc extends FunctionNode | ||
{ | ||
/** | ||
* @var Node | ||
*/ | ||
private $date; | ||
|
||
/** | ||
* @var Node | ||
*/ | ||
private $fmt; | ||
|
||
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) | ||
{ | ||
return sprintf( | ||
if ($this->fmt) { | ||
return sprintf( | ||
'TRUNC(%s, %s)', | ||
$sqlWalker->walkArithmeticPrimary($this->date), | ||
$sqlWalker->walkArithmeticPrimary($this->fmt) | ||
); | ||
} | ||
|
||
return sprintf( | ||
'TRUNC(%s)', | ||
$sqlWalker->walkArithmeticPrimary($this->date) | ||
); | ||
} | ||
|
||
public function parse(\Doctrine\ORM\Query\Parser $parser) | ||
{ | ||
$lexer = $parser->getLexer(); | ||
|
||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
$this->date = $parser->ArithmeticExpression(); | ||
$parser->match(Lexer::T_COMMA); | ||
$this->fmt = $parser->StringExpression(); | ||
|
||
if ($lexer->isNextToken(Lexer::T_COMMA)) { | ||
$parser->match(Lexer::T_COMMA); | ||
$this->fmt = $parser->StringExpression(); | ||
} | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Query\Oracle; | ||
|
||
use DoctrineExtensions\Tests\Query\OracleTestCase; | ||
|
||
/** | ||
* @author Alexey Kalinin <[email protected]> | ||
*/ | ||
class TruncTest extends OracleTestCase | ||
{ | ||
public function testFullQuery() | ||
{ | ||
$dql = 'SELECT TRUNC(d.created, \'YYYY\') FROM DoctrineExtensions\\Tests\\Entities\\Date d'; | ||
$q = $this->entityManager->createQuery($dql); | ||
|
||
$sql = 'SELECT TRUNC(d0_.created, \'YYYY\') AS sclr_0 FROM Date d0_'; | ||
$this->assertEquals($sql, $q->getSql()); | ||
} | ||
|
||
public function testShortQuery() | ||
{ | ||
$dql = 'SELECT TRUNC(d.created) FROM DoctrineExtensions\\Tests\\Entities\\Date d'; | ||
$q = $this->entityManager->createQuery($dql); | ||
|
||
$sql = 'SELECT TRUNC(d0_.created) AS sclr_0 FROM Date d0_'; | ||
$this->assertEquals($sql, $q->getSql()); | ||
} | ||
} |