Skip to content

Commit f19e560

Browse files
authored
Add JSON_DEPTH for MySQL (#355)
1 parent 8141836 commit f19e560

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ MySQL, Oracle, PostgreSQL and SQLite.
1313

1414
| DB | Functions |
1515
|:--:|:---------:|
16-
| 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` |
16+
| 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, JSON_DEPTH, 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` |
1919
| 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` |

config/mysql.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ doctrine:
5454
degrees: DoctrineExtensions\Query\Mysql\Degrees
5555
exp: DoctrineExtensions\Query\Mysql\Exp
5656
floor: DoctrineExtensions\Query\Mysql\Floor
57+
json_depth: DoctrineExtensions\Query\Mysql\JsonDepth
5758
log: DoctrineExtensions\Query\Mysql\Log
5859
log10: DoctrineExtensions\Query\Mysql\Log10
5960
log2: DoctrineExtensions\Query\Mysql\Log2

src/Query/Mysql/JsonDepth.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Query\Mysql;
4+
5+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
6+
use Doctrine\ORM\Query\Lexer;
7+
use Doctrine\ORM\Query\Parser;
8+
use Doctrine\ORM\Query\SqlWalker;
9+
10+
class JsonDepth extends FunctionNode
11+
{
12+
protected $target;
13+
14+
public function parse(Parser $parser)
15+
{
16+
$parser->match(Lexer::T_IDENTIFIER);
17+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
18+
19+
$this->target = $parser->StringPrimary();
20+
21+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
22+
}
23+
24+
public function getSql(SqlWalker $sqlWalker)
25+
{
26+
return sprintf('JSON_DEPTH(%s)', $sqlWalker->walkStringPrimary($this->target));
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Tests\Query\Mysql;
4+
5+
use DoctrineExtensions\Tests\Query\MysqlTestCase;
6+
7+
class JsonDepthTest extends MysqlTestCase
8+
{
9+
public function testJsonDepthWithTargetExplicit(): void
10+
{
11+
$this->assertDqlProducesSql(
12+
"SELECT JSON_DEPTH('{}') from DoctrineExtensions\Tests\Entities\Blank b",
13+
"SELECT JSON_DEPTH('{}') AS sclr_0 FROM Blank b0_"
14+
);
15+
}
16+
17+
public function testJsonDepthWithTargetParam(): void
18+
{
19+
$this->assertDqlProducesSql(
20+
"SELECT JSON_DEPTH(:param) from DoctrineExtensions\Tests\Entities\Blank b",
21+
'SELECT JSON_DEPTH(?) AS sclr_0 FROM Blank b0_'
22+
);
23+
}
24+
}

0 commit comments

Comments
 (0)