Skip to content

Commit

Permalink
Force typing for Postgresql::date_trunc so it can be used in a cond…
Browse files Browse the repository at this point in the history
…ition (#415)
  • Loading branch information
florentdestremau authored Mar 9, 2024
1 parent 281f165 commit 40e117a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Query/Postgresql/DateTrunc.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function parse(Parser $parser): void
public function getSql(SqlWalker $sqlWalker): string
{
return sprintf(
'DATE_TRUNC(%s, %s)',
'DATE_TRUNC(%s::text, %s::timestamp)',
$this->fieldText->dispatch($sqlWalker),
$this->fieldTimestamp->dispatch($sqlWalker)
);
Expand Down
16 changes: 15 additions & 1 deletion tests/Query/Postgresql/DateTruncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace DoctrineExtensions\Tests\Query\Postgresql;

use DateTime;
use Doctrine\ORM\QueryBuilder;
use DoctrineExtensions\Tests\Entities\Date;
use DoctrineExtensions\Tests\Query\PostgresqlTestCase;

class DateTruncTest extends PostgresqlTestCase
Expand All @@ -14,7 +16,19 @@ public function testDateTrunc(): void
->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_";
$expected = "SELECT DATE_TRUNC('YEAR'::text, d0_.created::timestamp) AS sclr_0 FROM Date d0_";

$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL());
}

public function testDateTruncCondition(): void
{
$queryBuilder = $this->entityManager->getRepository(Date::class)
->createQueryBuilder('dt')
->where("date_trunc('YEAR', dt.created) = date_trunc('YEAR', :date)")
->setParameter('date', new DateTime('2010-01-01'));

$expected = "SELECT d0_.id AS id_0, d0_.created AS created_1 FROM Date d0_ WHERE DATE_TRUNC('YEAR'::text, d0_.created::timestamp) = DATE_TRUNC('YEAR'::text, ?::timestamp)";

$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL());
}
Expand Down

0 comments on commit 40e117a

Please sign in to comment.