Skip to content

Commit 469f653

Browse files
nitsostevelacey
authored andcommitted
Optional second parameter (fmt) for Oracle TRUNC function + test. (#301)
Oracle TRUNC function fmt parameter is optional
1 parent 1d02c58 commit 469f653

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/Query/Oracle/Trunc.php

100644100755
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,53 @@
33
namespace DoctrineExtensions\Query\Oracle;
44

55
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
6+
use Doctrine\ORM\Query\AST\Node;
67
use Doctrine\ORM\Query\Lexer;
78

89
/**
910
* @author Mohammad ZeinEddin <[email protected]>
1011
*/
1112
class Trunc extends FunctionNode
1213
{
14+
/**
15+
* @var Node
16+
*/
1317
private $date;
1418

19+
/**
20+
* @var Node
21+
*/
1522
private $fmt;
1623

1724
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
1825
{
19-
return sprintf(
26+
if ($this->fmt) {
27+
return sprintf(
2028
'TRUNC(%s, %s)',
2129
$sqlWalker->walkArithmeticPrimary($this->date),
2230
$sqlWalker->walkArithmeticPrimary($this->fmt)
31+
);
32+
}
33+
34+
return sprintf(
35+
'TRUNC(%s)',
36+
$sqlWalker->walkArithmeticPrimary($this->date)
2337
);
2438
}
2539

2640
public function parse(\Doctrine\ORM\Query\Parser $parser)
2741
{
42+
$lexer = $parser->getLexer();
43+
2844
$parser->match(Lexer::T_IDENTIFIER);
2945
$parser->match(Lexer::T_OPEN_PARENTHESIS);
3046
$this->date = $parser->ArithmeticExpression();
31-
$parser->match(Lexer::T_COMMA);
32-
$this->fmt = $parser->StringExpression();
47+
48+
if ($lexer->isNextToken(Lexer::T_COMMA)) {
49+
$parser->match(Lexer::T_COMMA);
50+
$this->fmt = $parser->StringExpression();
51+
}
52+
3353
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
3454
}
3555
}

tests/Query/Oracle/TruncTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Query\Oracle;
4+
5+
use DoctrineExtensions\Tests\Query\OracleTestCase;
6+
7+
/**
8+
* @author Alexey Kalinin <[email protected]>
9+
*/
10+
class TruncTest extends OracleTestCase
11+
{
12+
public function testFullQuery()
13+
{
14+
$dql = 'SELECT TRUNC(d.created, \'YYYY\') FROM DoctrineExtensions\\Tests\\Entities\\Date d';
15+
$q = $this->entityManager->createQuery($dql);
16+
17+
$sql = 'SELECT TRUNC(d0_.created, \'YYYY\') AS sclr_0 FROM Date d0_';
18+
$this->assertEquals($sql, $q->getSql());
19+
}
20+
21+
public function testShortQuery()
22+
{
23+
$dql = 'SELECT TRUNC(d.created) FROM DoctrineExtensions\\Tests\\Entities\\Date d';
24+
$q = $this->entityManager->createQuery($dql);
25+
26+
$sql = 'SELECT TRUNC(d0_.created) AS sclr_0 FROM Date d0_';
27+
$this->assertEquals($sql, $q->getSql());
28+
}
29+
}

0 commit comments

Comments
 (0)