|
8 | 8 | use Yiisoft\Db\Constant\DataType;
|
9 | 9 | use Yiisoft\Db\Constant\PseudoType;
|
10 | 10 | use Yiisoft\Db\Expression\ArrayExpression;
|
| 11 | +use Yiisoft\Db\Expression\CaseExpression; |
11 | 12 | use Yiisoft\Db\Expression\Expression;
|
12 | 13 | use Yiisoft\Db\Pgsql\Column\ColumnBuilder;
|
| 14 | +use Yiisoft\Db\Pgsql\Column\IntegerColumn; |
13 | 15 | use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
|
14 | 16 | use Yiisoft\Db\Query\Query;
|
15 | 17 |
|
@@ -511,4 +513,60 @@ public static function prepareValue(): array
|
511 | 513 |
|
512 | 514 | return $values;
|
513 | 515 | }
|
| 516 | + |
| 517 | + public static function caseExpressionBuilder(): array |
| 518 | + { |
| 519 | + $data = parent::caseExpressionBuilder(); |
| 520 | + |
| 521 | + $db = self::getDb(); |
| 522 | + $serverVersion = $db->getServerInfo()->getVersion(); |
| 523 | + $db->close(); |
| 524 | + |
| 525 | + if (version_compare($serverVersion, '10', '<')) { |
| 526 | + $data['without case expression'] = [ |
| 527 | + (new CaseExpression()) |
| 528 | + ->addWhen(['=', 'column_name', 1], $paramA = new Param('a', DataType::STRING)) |
| 529 | + ->addWhen( |
| 530 | + '"column_name" = 2', |
| 531 | + $db->select(new Expression( |
| 532 | + ':pv2::text', |
| 533 | + [':pv2' => $paramB = new Param('b', DataType::STRING)], |
| 534 | + )), |
| 535 | + ), |
| 536 | + 'CASE WHEN "column_name" = :qp0 THEN :qp1 WHEN "column_name" = 2 THEN (SELECT :pv2::text) END', |
| 537 | + [':qp0' => 1, ':qp1' => $paramA, ':pv2' => $paramB], |
| 538 | + 'b', |
| 539 | + ]; |
| 540 | + } |
| 541 | + |
| 542 | + return [ |
| 543 | + ...$data, |
| 544 | + 'without case and type hint' => [ |
| 545 | + (new CaseExpression())->caseType('int') |
| 546 | + ->addWhen(true, "'a'"), |
| 547 | + "CASE WHEN TRUE THEN 'a' END", |
| 548 | + [], |
| 549 | + 'a', |
| 550 | + ], |
| 551 | + 'with case and type hint' => [ |
| 552 | + (new CaseExpression('1 + 1', 'int')) |
| 553 | + ->addWhen(1, "'a'") |
| 554 | + ->else("'b'"), |
| 555 | + "CASE (1 + 1)::int WHEN (1)::int THEN 'a' ELSE 'b' END", |
| 556 | + [], |
| 557 | + 'b', |
| 558 | + ], |
| 559 | + 'with case and type hint with column' => [ |
| 560 | + (new CaseExpression('1 + 1', new IntegerColumn())) |
| 561 | + ->addWhen(1, $paramA = new Param('a', DataType::STRING)) |
| 562 | + ->else($paramB = new Param('b', DataType::STRING)), |
| 563 | + 'CASE (1 + 1)::integer WHEN (1)::integer THEN :qp0 ELSE :qp1 END', |
| 564 | + [ |
| 565 | + ':qp0' => $paramA, |
| 566 | + ':qp1' => $paramB, |
| 567 | + ], |
| 568 | + 'b', |
| 569 | + ], |
| 570 | + ]; |
| 571 | + } |
514 | 572 | }
|
0 commit comments