Skip to content

Commit

Permalink
Add date_trunc function for postgres (beberlei#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamontovdmitriy authored Jun 30, 2020
1 parent 8b09d6c commit 5f16b30
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ MySQL, Oracle, PostgreSQL and SQLite.
| MySQL | `ACOS, ADDTIME, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BINARY, BIT_COUNT, BIT_XOR, CAST, CEIL, CHAR_LENGTH, COLLATE, CONCAT_WS, CONVERT_TZ, COS, COT, COUNTIF, CRC32, DATE, DATE_FORMAT, DATEADD, DATEDIFF, DATESUB, DAY, DAYNAME, DAYOFWEEK, DAYOFYEAR, DEGREES, DIV, EXP, EXTRACT, FIELD, FIND_IN_SET, FLOOR, FORMAT, FROM_BASE64, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, INET_ATON, INET_NTOA, INET6_ATON, INET6_NTOA, INSTR, IS_IPV4, IS_IPV4_COMPAT, IS_IPV4_MAPPED, IS_IPV6, LAG, LAST_DAY, LEAD, LEAST, LOG, LOG10, LOG2, LPAD, MAKEDATE, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, OVER, PERIOD_DIFF, PI, POWER, QUARTER, RADIANS, RAND, REGEXP, REPLACE, ROUND, RPAD, SECOND, SECTOTIME, SHA1, SHA2, SIN, SOUNDEX, STD, STDDEV, STRTODATE, STR_TO_DATE, SUBSTRING_INDEX, TAN, TIME, TIMEDIFF, TIMESTAMPADD, TIMESTAMPDIFF, TIMETOSEC, TRUNCATE, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, YEAR, YEARMONTH, YEARWEEK` |
| Oracle | `CEIL, DAY, FLOOR, HOUR, LISTAGG, MINUTE, MONTH, NVL, SECOND, TO_CHAR, TO_DATE, TRUNC, YEAR` |
| Sqlite | `CASE WHEN THEN ELSE END, DATE, DATE_FORMAT*, DAY, HOUR, IFNULL, JULIANDAY, MINUTE, MONTH, REPLACE, ROUND, SECOND, STRFTIME, WEEK, WEEKDAY, YEAR` |
| PostgreSQL | `AT_TIME_ZONE, COUNT_FILTER, DATE_PART, DAY, EXTRACT, GREATEST, HOUR, LEAST, MINUTE, MONTH, REGEXP_REPLACE, SECOND, STRING_AGG, TO_CHAR, TO_DATE, YEAR` |
| PostgreSQL | `AT_TIME_ZONE, COUNT_FILTER, DATE_PART, DATE_TRUNC, DAY, EXTRACT, GREATEST, HOUR, LEAST, MINUTE, MONTH, REGEXP_REPLACE, SECOND, STRING_AGG, TO_CHAR, TO_DATE, YEAR` |

> Note: Sqlite date functions are implemented as `strftime(format, value)`.
Sqlite only supports the [most common formats](https://www.sqlite.org/lang_datefunc.html),
Expand Down
3 changes: 2 additions & 1 deletion config/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ doctrine:
at_time_zone: DoctrineExtensions\Query\Postgresql\AtTimeZoneFunction
date_part: DoctrineExtensions\Query\Postgresql\DatePart
extract: DoctrineExtensions\Query\Postgresql\ExtractFunction

date_trunc: DoctrineExtensions\Query\Postgresql\DateTrunc

string_functions:
str_to_date: DoctrineExtensions\Query\Postgresql\StrToDate
count_filter: DoctrineExtensions\Query\Postgresql\CountFilterFunction
Expand Down
34 changes: 34 additions & 0 deletions src/Query/Postgresql/DateTrunc.php
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)
);
}
}
21 changes: 21 additions & 0 deletions tests/Query/Postgresql/DateTruncTest.php
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());
}
}

0 comments on commit 5f16b30

Please sign in to comment.