|
6 | 6 |
|
7 | 7 | use Yiisoft\Db\Constant\DataType; |
8 | 8 | use Yiisoft\Db\Constant\PseudoType; |
| 9 | +use Yiisoft\Db\Expression\Statement\When; |
9 | 10 | use Yiisoft\Db\Expression\Value\ArrayExpression; |
10 | | -use Yiisoft\Db\Expression\Statement\CaseExpression; |
| 11 | +use Yiisoft\Db\Expression\Statement\CaseX; |
11 | 12 | use Yiisoft\Db\Expression\Expression; |
12 | 13 | use Yiisoft\Db\Expression\Function\ArrayMerge; |
13 | 14 | use Yiisoft\Db\Expression\Value\Param; |
| 15 | +use Yiisoft\Db\Expression\Value\Value; |
14 | 16 | use Yiisoft\Db\Pgsql\Column\ColumnBuilder; |
15 | 17 | use Yiisoft\Db\Pgsql\Column\IntegerColumn; |
16 | 18 | use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; |
@@ -519,56 +521,68 @@ public static function prepareValue(): array |
519 | 521 | return $values; |
520 | 522 | } |
521 | 523 |
|
522 | | - public static function caseExpressionBuilder(): array |
| 524 | + public static function caseXBuilder(): array |
523 | 525 | { |
524 | | - $data = parent::caseExpressionBuilder(); |
| 526 | + $data = parent::caseXBuilder(); |
525 | 527 |
|
526 | 528 | $db = self::getDb(); |
527 | 529 | $serverVersion = $db->getServerInfo()->getVersion(); |
528 | 530 | $db->close(); |
529 | 531 |
|
530 | 532 | if (version_compare($serverVersion, '10', '<')) { |
531 | 533 | $data['without case expression'] = [ |
532 | | - (new CaseExpression()) |
533 | | - ->addWhen(['=', 'column_name', 1], $paramA = new Param('a', DataType::STRING)) |
534 | | - ->addWhen( |
| 534 | + new CaseX( |
| 535 | + when1: new When(['=', 'column_name', 1], new Value('a')), |
| 536 | + when2: new When( |
535 | 537 | '"column_name" = 2', |
536 | 538 | $db->select(new Expression( |
537 | 539 | ':pv2::text', |
538 | | - [':pv2' => $paramB = new Param('b', DataType::STRING)], |
| 540 | + [':pv2' => $param = new Param('b', DataType::STRING)], |
539 | 541 | )), |
540 | 542 | ), |
| 543 | + ), |
541 | 544 | 'CASE WHEN "column_name" = 1 THEN :qp0 WHEN "column_name" = 2 THEN (SELECT :pv2::text) END', |
542 | | - [':qp0' => $paramA, ':pv2' => $paramB], |
| 545 | + [ |
| 546 | + ':qp0' => new Param('a', DataType::STRING), |
| 547 | + ':pv2' => $param, |
| 548 | + ], |
543 | 549 | 'b', |
544 | 550 | ]; |
545 | 551 | } |
546 | 552 |
|
547 | 553 | return [ |
548 | 554 | ...$data, |
549 | 555 | 'without case and type hint' => [ |
550 | | - (new CaseExpression())->caseType('int') |
551 | | - ->addWhen(true, "'a'"), |
| 556 | + new CaseX( |
| 557 | + valueType: 'int', |
| 558 | + when: new When(true, "'a'"), |
| 559 | + ), |
552 | 560 | "CASE WHEN TRUE THEN 'a' END", |
553 | 561 | [], |
554 | 562 | 'a', |
555 | 563 | ], |
556 | 564 | 'with case and type hint' => [ |
557 | | - (new CaseExpression('1 + 1', 'int')) |
558 | | - ->addWhen(1, "'a'") |
559 | | - ->else("'b'"), |
| 565 | + new CaseX( |
| 566 | + '1 + 1', |
| 567 | + 'int', |
| 568 | + new When(1, "'a'"), |
| 569 | + "'b'", |
| 570 | + ), |
560 | 571 | "CASE (1 + 1)::int WHEN (1)::int THEN 'a' ELSE 'b' END", |
561 | 572 | [], |
562 | 573 | 'b', |
563 | 574 | ], |
564 | 575 | 'with case and type hint with column' => [ |
565 | | - (new CaseExpression('1 + 1', new IntegerColumn())) |
566 | | - ->addWhen(1, $paramA = new Param('a', DataType::STRING)) |
567 | | - ->else($paramB = new Param('b', DataType::STRING)), |
| 576 | + new CaseX( |
| 577 | + '1 + 1', |
| 578 | + new IntegerColumn(), |
| 579 | + new When(1, new Value('a')), |
| 580 | + $param = new Param('b', DataType::STRING), |
| 581 | + ), |
568 | 582 | 'CASE (1 + 1)::integer WHEN (1)::integer THEN :qp0 ELSE :qp1 END', |
569 | 583 | [ |
570 | | - ':qp0' => $paramA, |
571 | | - ':qp1' => $paramB, |
| 584 | + ':qp0' => new Param('a', DataType::STRING), |
| 585 | + ':qp1' => $param, |
572 | 586 | ], |
573 | 587 | 'b', |
574 | 588 | ], |
|
0 commit comments