File tree Expand file tree Collapse file tree 2 files changed +52
-3
lines changed Expand file tree Collapse file tree 2 files changed +52
-3
lines changed Original file line number Diff line number Diff line change 33namespace DoctrineExtensions \Query \Oracle ;
44
55use Doctrine \ORM \Query \AST \Functions \FunctionNode ;
6+ use Doctrine \ORM \Query \AST \Node ;
67use Doctrine \ORM \Query \Lexer ;
78
89/**
910 * @author Mohammad ZeinEddin <[email protected] > 1011 */
1112class 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments