forked from beberlei/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add date_trunc function for postgres (beberlei#350)
- Loading branch information
1 parent
8b09d6c
commit 5f16b30
Showing
4 changed files
with
58 additions
and
2 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
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
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,34 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Postgresql; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
|
||
class DateTrunc extends FunctionNode | ||
{ | ||
public $fieldText = null; | ||
|
||
public $fieldTimestamp = null; | ||
|
||
public function parse(Parser $parser) | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
$this->fieldText = $parser->ArithmeticPrimary(); | ||
$parser->match(Lexer::T_COMMA); | ||
$this->fieldTimestamp = $parser->ArithmeticPrimary(); | ||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(SqlWalker $sqlWalker) | ||
{ | ||
return sprintf( | ||
'DATE_TRUNC(%s, %s)', | ||
$this->fieldText->dispatch($sqlWalker), | ||
$this->fieldTimestamp->dispatch($sqlWalker) | ||
); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Postgresql; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use DoctrineExtensions\Tests\Query\PostgresqlTestCase; | ||
|
||
class DateTruncTest extends PostgresqlTestCase | ||
{ | ||
public function testDateTrunc() | ||
{ | ||
$queryBuilder = new QueryBuilder($this->entityManager); | ||
$queryBuilder | ||
->select("date_trunc('YEAR', dt.created)") | ||
->from('DoctrineExtensions\Tests\Entities\Date', 'dt'); | ||
|
||
$expected = "SELECT DATE_TRUNC('YEAR', d0_.created) AS sclr_0 FROM Date d0_"; | ||
|
||
$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL()); | ||
} | ||
} |