Skip to content

Commit 6624c39

Browse files
authored
Add DATE function for PostgreSQL (#353)
1 parent c9c303a commit 6624c39

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ MySQL, Oracle, PostgreSQL and SQLite.
1616
| 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` |
1717
| Oracle | `CEIL, DAY, FLOOR, HOUR, LISTAGG, MINUTE, MONTH, NVL, SECOND, TO_CHAR, TO_DATE, TRUNC, YEAR` |
1818
| Sqlite | `CASE WHEN THEN ELSE END, DATE, DATE_FORMAT*, DAY, HOUR, IFNULL, JULIANDAY, MINUTE, MONTH, REPLACE, ROUND, SECOND, STRFTIME, WEEK, WEEKDAY, YEAR` |
19-
| 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` |
19+
| PostgreSQL | `AT_TIME_ZONE, COUNT_FILTER, DATE, DATE_PART, DATE_TRUNC, DAY, EXTRACT, GREATEST, HOUR, LEAST, MINUTE, MONTH, REGEXP_REPLACE, SECOND, STRING_AGG, TO_CHAR, TO_DATE, YEAR` |
2020

2121
> Note: Sqlite date functions are implemented as `strftime(format, value)`.
2222
Sqlite only supports the [most common formats](https://www.sqlite.org/lang_datefunc.html),

config/postgres.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ doctrine:
1313
date_part: DoctrineExtensions\Query\Postgresql\DatePart
1414
extract: DoctrineExtensions\Query\Postgresql\ExtractFunction
1515
date_trunc: DoctrineExtensions\Query\Postgresql\DateTrunc
16+
date: DoctrineExtensions\Query\Postgresql\Date
1617

1718
string_functions:
1819
str_to_date: DoctrineExtensions\Query\Postgresql\StrToDate

src/Query/Postgresql/Date.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Query\Postgresql;
4+
5+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
6+
use Doctrine\ORM\Query\Lexer;
7+
8+
/**
9+
* @author Steve Lacey <[email protected]>
10+
*/
11+
class Date extends FunctionNode
12+
{
13+
public $date;
14+
15+
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
16+
{
17+
return 'DATE(' . $sqlWalker->walkArithmeticPrimary($this->date) . ')';
18+
}
19+
20+
public function parse(\Doctrine\ORM\Query\Parser $parser)
21+
{
22+
$parser->match(Lexer::T_IDENTIFIER);
23+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
24+
25+
$this->date = $parser->ArithmeticPrimary();
26+
27+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
28+
}
29+
}

tests/Query/Postgresql/DateTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ public function testStrToDate()
1414

1515
$this->assertEquals($sql, $q->getSql());
1616
}
17+
18+
public function testDateFunction()
19+
{
20+
$dql = "SELECT DATE(p.created) FROM DoctrineExtensions\Tests\Entities\Date p";
21+
$sql = 'SELECT DATE(d0_.created) AS sclr_0 FROM Date d0_';
22+
23+
$this->assertDqlProducesSql($dql, $sql);
24+
}
1725
}

0 commit comments

Comments
 (0)