Skip to content

Commit 51d9339

Browse files
barryostevelacey
authored andcommitted
Add MySQL IS_IPV4_MAPPED/COMPAT functions
1 parent cf83460 commit 51d9339

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-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, 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, FROM_UNIXTIME, GREATEST, GROUP_CONCAT, HEX, HOUR, IFELSE, IFNULL, INET_ATON, INET_NTOA, INET6_ATON, INET6_NTOA, INSTR, IS_IPV4, IS_IPV6, LAST_DAY, LEAST, LOG, LOG10, LOG2, LPAD, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, 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, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, YEAR, YEARMONTH, YEARWEEK` |
16+
| MySQL | `ACOS, 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, 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, LAST_DAY, LEAST, LOG, LOG10, LOG2, LPAD, MATCH, MD5, MINUTE, MONTH, MONTHNAME, NOW, NULLIF, 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, UNHEX, UNIX_TIMESTAMP, UTC_TIMESTAMP, UUID_SHORT, VARIANCE, WEEK, WEEKDAY, YEAR, YEARMONTH, YEARWEEK` |
1717
| Oracle | `DAY, LISTAGG, MONTH, NVL, TO_CHAR, TO_DATE, TRUNC, YEAR` |
1818
| Sqlite | `DATE, MINUTE, HOUR, DAY, WEEK, WEEKDAY, MONTH, YEAR, STRFTIME, DATE_FORMAT*, CASE WHEN THEN ELSE END, IFNULL, REPLACE, ROUND` |
1919
| PostgreSQL | `TO_DATE, TO_CHAR, AT_TIME_ZONE, COUNT_FILTER, STRING_AGG` |

config/mysql.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ doctrine:
9292
inet6_ntoa: DoctrineExtensions\Query\Mysql\Inet6Ntoa
9393
instr: DoctrineExtensions\Query\Mysql\Instr
9494
is_ipv4: DoctrineExtensions\Query\Mysql\IsIpv4
95+
is_ipv4_compat: DoctrineExtensions\Query\Mysql\IsIpv4Compat
96+
is_ipv4_mapped: DoctrineExtensions\Query\Mysql\IsIpv4Mapped
9597
is_ipv6: DoctrineExtensions\Query\Mysql\IsIpv6
9698
least: DoctrineExtensions\Query\Mysql\Least
9799
lpad: DoctrineExtensions\Query\Mysql\Lpad

src/Query/Mysql/IsIpv4Compat.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Query\Mysql;
4+
5+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
6+
use Doctrine\ORM\Query\AST\Node;
7+
use Doctrine\ORM\Query\Lexer;
8+
use Doctrine\ORM\Query\Parser;
9+
use Doctrine\ORM\Query\QueryException;
10+
use Doctrine\ORM\Query\SqlWalker;
11+
12+
class IsIpv4Compat extends FunctionNode
13+
{
14+
public $valueExpression = null;
15+
16+
/**
17+
* @param Parser $parser
18+
*
19+
* @throws QueryException
20+
*/
21+
public function parse(Parser $parser)
22+
{
23+
$parser->match(Lexer::T_IDENTIFIER);
24+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
25+
$this->valueExpression = $parser->StringPrimary();
26+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
27+
}
28+
29+
/**
30+
* @param SqlWalker $sqlWalker
31+
*
32+
* @return string
33+
*/
34+
public function getSql(SqlWalker $sqlWalker)
35+
{
36+
return 'IS_IPV4_COMPAT('
37+
. (
38+
$this->valueExpression instanceof Node
39+
? $this->valueExpression->dispatch($sqlWalker)
40+
: "'" . $this->valueExpression . "'"
41+
)
42+
.')';
43+
}
44+
}

src/Query/Mysql/IsIpv4Mapped.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Query\Mysql;
4+
5+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
6+
use Doctrine\ORM\Query\AST\Node;
7+
use Doctrine\ORM\Query\Lexer;
8+
use Doctrine\ORM\Query\Parser;
9+
use Doctrine\ORM\Query\QueryException;
10+
use Doctrine\ORM\Query\SqlWalker;
11+
12+
class IsIpv4Mapped extends FunctionNode
13+
{
14+
public $valueExpression = null;
15+
16+
/**
17+
* @param Parser $parser
18+
*
19+
* @throws QueryException
20+
*/
21+
public function parse(Parser $parser)
22+
{
23+
$parser->match(Lexer::T_IDENTIFIER);
24+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
25+
$this->valueExpression = $parser->StringPrimary();
26+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
27+
}
28+
29+
/**
30+
* @param SqlWalker $sqlWalker
31+
*
32+
* @return string
33+
*/
34+
public function getSql(SqlWalker $sqlWalker)
35+
{
36+
return 'IS_IPV4_MAPPED('
37+
. (
38+
$this->valueExpression instanceof Node
39+
? $this->valueExpression->dispatch($sqlWalker)
40+
: "'" . $this->valueExpression . "'"
41+
)
42+
.')';
43+
}
44+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Tests\Query\Mysql;
4+
5+
use DoctrineExtensions\Tests\Query\MysqlTestCase;
6+
7+
class IsIpv4CompatTest extends MysqlTestCase
8+
{
9+
public function testIsIpv4Compat()
10+
{
11+
$this->assertDqlProducesSql(
12+
"SELECT IS_IPV4_COMPAT(INET6_ATON('::192.0.2.1')) FROM DoctrineExtensions\Tests\Entities\Blank b",
13+
"SELECT IS_IPV4_COMPAT(INET6_ATON('::192.0.2.1')) AS sclr_0 FROM Blank b0_"
14+
);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Tests\Query\Mysql;
4+
5+
use DoctrineExtensions\Tests\Query\MysqlTestCase;
6+
7+
class IsIpv4MappedTest extends MysqlTestCase
8+
{
9+
public function testIsIpv4Mapped()
10+
{
11+
$this->assertDqlProducesSql(
12+
"SELECT IS_IPV4_MAPPED(INET6_ATON('::ffff:192.0.2.1')) FROM DoctrineExtensions\Tests\Entities\Blank b",
13+
"SELECT IS_IPV4_MAPPED(INET6_ATON('::ffff:192.0.2.1')) AS sclr_0 FROM Blank b0_"
14+
);
15+
}
16+
}

0 commit comments

Comments
 (0)