Skip to content

Commit 992030b

Browse files
committed
feat(oracle): NVL2 function
1 parent 2e24c3d commit 992030b

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ MySQL, Oracle, PostgreSQL and SQLite.
1414
| DB | Functions |
1515
|:--:|:---------:|
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, JSON_CONTAINS, JSON_DEPTH, JSON_LENGTH, 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, WEEKOFYEAR, YEAR, YEARMONTH, YEARWEEK` |
17-
| Oracle | `CEIL, DAY, FLOOR, HOUR, LISTAGG, MINUTE, MONTH, NVL, SECOND, TO_CHAR, TO_DATE, TRUNC, YEAR` |
17+
| Oracle | `CEIL, DAY, FLOOR, HOUR, LISTAGG, MINUTE, MONTH, NVL, NVL2, 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` |
2020

@@ -39,7 +39,7 @@ If you want to run phpunit:
3939
composer run test
4040
```
4141

42-
If you want to run php-cs-fixer:
42+
If you want to run PHP_CodeSniffer and PHPStan:
4343

4444
```sh
4545
composer run lint

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
}
3939
},
4040
"scripts": {
41+
"lint": [
42+
"vendor/bin/phpcs -q --report=diff",
43+
"vendor/bin/phpstan analyse src tests -v"
44+
],
4145
"test": "phpunit --colors=always"
4246
}
4347
}

config/oracle.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ doctrine:
1313

1414
string_functions:
1515
nvl: DoctrineExtensions\Query\Oracle\Nvl
16+
nvl2: DoctrineExtensions\Query\Oracle\Nvl2
1617
listagg: DoctrineExtensions\Query\Oracle\Listagg
1718
to_date: DoctrineExtensions\Query\Oracle\ToDate
1819

src/Query/Oracle/Nvl2.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Query\Oracle;
4+
5+
use Doctrine\ORM\Query\AST\ArithmeticExpression;
6+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
7+
use Doctrine\ORM\Query\Parser;
8+
use Doctrine\ORM\Query\SqlWalker;
9+
use Doctrine\ORM\Query\TokenType;
10+
11+
use function sprintf;
12+
13+
/** @author https://github.com/nxtpge */
14+
class Nvl2 extends FunctionNode
15+
{
16+
private ArithmeticExpression $expr1;
17+
18+
private ArithmeticExpression $expr2;
19+
20+
private ArithmeticExpression $expr3;
21+
22+
public function getSql(SqlWalker $sqlWalker): string
23+
{
24+
return sprintf(
25+
'NVL2(%s, %s, %s)',
26+
$sqlWalker->walkArithmeticExpression($this->expr1),
27+
$sqlWalker->walkArithmeticExpression($this->expr2),
28+
$sqlWalker->walkArithmeticExpression($this->expr3),
29+
);
30+
}
31+
32+
public function parse(Parser $parser): void
33+
{
34+
$parser->match(TokenType::T_IDENTIFIER);
35+
$parser->match(TokenType::T_OPEN_PARENTHESIS);
36+
$this->expr1 = $parser->ArithmeticExpression();
37+
$parser->match(TokenType::T_COMMA);
38+
$this->expr2 = $parser->ArithmeticExpression();
39+
$parser->match(TokenType::T_COMMA);
40+
$this->expr3 = $parser->ArithmeticExpression();
41+
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
42+
}
43+
}

tests/Query/Oracle/Nvl2Test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace DoctrineExtensions\Tests\Query\Oracle;
4+
5+
use DoctrineExtensions\Tests\Query\OracleTestCase;
6+
7+
/** @author https://github.com/nxtpge */
8+
class Nvl2Test extends OracleTestCase
9+
{
10+
public function testNvl2(): void
11+
{
12+
$this->assertDqlProducesSql(
13+
'SELECT NVL2(p.name, \'expr1\', \'expr2\') FROM DoctrineExtensions\Tests\Entities\Product p',
14+
'SELECT NVL2(p0_.name, \'expr1\', \'expr2\') AS sclr_0 FROM Product p0_'
15+
);
16+
}
17+
}

0 commit comments

Comments
 (0)