Skip to content

Commit f592c49

Browse files
Matthias Büsingstevelacey
authored andcommitted
Adding YearMonth function (#266)
Adding MySql YEARMONTH Function
1 parent a7957e5 commit f592c49

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ functions available in MySQL, Oracle, Sqlite and PostgreSQL.
1919

2020
| DB | Functions |
2121
|:--:|:---------:|
22-
| MySQL | `ACOS, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BINARY, BIT_COUNT, BIT_XOR, 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, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, LAST_DAY, LEAST, LOG, LOG10, LOG2, LPAD, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, 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, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, YEAR, YEARWEEK` |
22+
| MySQL | `ACOS, AES_DECRYPT, AES_ENCRYPT, ANY_VALUE, ASCII, ASIN, ATAN, ATAN2, BINARY, BIT_COUNT, BIT_XOR, 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, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, LAST_DAY, LEAST, LOG, LOG10, LOG2, LPAD, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, 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, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, YEAR, YEARMONTH, YEARWEEK` |
2323
| Oracle | `DAY, LISTAGG, MONTH, NVL, TO_CHAR, TO_DATE, TRUNC, YEAR` |
2424
| Sqlite | `DATE, MINUTE, HOUR, DAY, WEEK, WEEKDAY, MONTH, YEAR, STRFTIME, DATE_FORMAT*, CASE WHEN THEN ELSE END, IFNULL, REPLACE, ROUND` |
2525
| PostgreSQL | `TO_DATE, TO_CHAR, AT_TIME_ZONE, COUNT_FILTER, STRING_AGG` |

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
"dev-master": "1.0-dev"
3535
}
3636
}
37-
}
37+
}

config/mysql.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ doctrine:
3131
week: DoctrineExtensions\Query\Mysql\Week
3232
weekday: DoctrineExtensions\Query\Mysql\WeekDay
3333
year: DoctrineExtensions\Query\Mysql\Year
34+
yearmonth: DoctrineExtensions\Query\Mysql\YearMonth
3435
yearweek: DoctrineExtensions\Query\Mysql\YearWeek
3536
unix_timestamp: DoctrineExtensions\Query\Mysql\UnixTimestamp
3637
utc_timestamp: DoctrineExtensions\Query\Mysql\UtcTimestamp

src/Query/Mysql/YearMonth.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* DoctrineExtensions Mysql Function Pack
5+
*
6+
* LICENSE
7+
*
8+
* This source file is subject to the new BSD license that is bundled
9+
* with this package in the file LICENSE.txt.
10+
* If you did not receive a copy of the license and are unable to
11+
* obtain it through the world-wide-web, please send an email
12+
* to [email protected] so I can send you a copy immediately.
13+
*/
14+
15+
namespace DoctrineExtensions\Query\Mysql;
16+
17+
use Doctrine\ORM\Query\Lexer;
18+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
19+
20+
class YearMonth extends FunctionNode
21+
{
22+
public $date;
23+
24+
/**
25+
* @override
26+
*/
27+
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
28+
{
29+
return sprintf(
30+
'EXTRACT(YEAR_MONTH FROM %s)',
31+
$sqlWalker->walkArithmeticPrimary($this->date)
32+
);
33+
}
34+
35+
/**
36+
* @override
37+
*/
38+
public function parse(\Doctrine\ORM\Query\Parser $parser)
39+
{
40+
$parser->match(Lexer::T_IDENTIFIER);
41+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
42+
$this->date = $parser->ArithmeticPrimary();
43+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
44+
}
45+
}

0 commit comments

Comments
 (0)