|
10 | 10 | use Cycle\Database\Injection\Expression; |
11 | 11 | use Cycle\Database\Injection\Fragment; |
12 | 12 | use Cycle\Database\Injection\Parameter; |
| 13 | +use Cycle\Database\Injection\SubQuery; |
13 | 14 | use Cycle\Database\Query\SelectQuery; |
14 | 15 | use Cycle\Database\Tests\Functional\Driver\Common\BaseTest; |
15 | 16 | use Spiral\Pagination\PaginableInterface; |
@@ -2639,4 +2640,98 @@ public function testOrWhereNotWithArrayAnd(): void |
2639 | 2640 | $select, |
2640 | 2641 | ); |
2641 | 2642 | } |
| 2643 | + |
| 2644 | + public function testSelectFromSubQuery(): void |
| 2645 | + { |
| 2646 | + $innerSelect = $this->database |
| 2647 | + ->select() |
| 2648 | + ->from(['users']) |
| 2649 | + ->where(['name' => 'John Doe']); |
| 2650 | + $injection = new SubQuery($innerSelect, 'u'); |
| 2651 | + |
| 2652 | + $outerSelect = $this->database |
| 2653 | + ->select() |
| 2654 | + ->from($injection) |
| 2655 | + ->where('u.id', '>', 10); |
| 2656 | + |
| 2657 | + $this->assertSameQuery( |
| 2658 | + <<<SQL |
| 2659 | + SELECT * |
| 2660 | + FROM (SELECT * FROM {users} WHERE {name} = ?) AS {u} |
| 2661 | + WHERE {u}.{id} > ? |
| 2662 | + SQL, |
| 2663 | + $outerSelect, |
| 2664 | + ); |
| 2665 | + |
| 2666 | + $this->assertSameParameters( |
| 2667 | + [ |
| 2668 | + 'John Doe', |
| 2669 | + 10, |
| 2670 | + ], |
| 2671 | + $outerSelect, |
| 2672 | + ); |
| 2673 | + } |
| 2674 | + |
| 2675 | + public function testSelectFromTwoSubQuery(): void |
| 2676 | + { |
| 2677 | + $innerSelect1 = $this->database |
| 2678 | + ->select() |
| 2679 | + ->from(['users']) |
| 2680 | + ->where(['name' => 'John Doe']); |
| 2681 | + $injection1 = new SubQuery($innerSelect1, 'u'); |
| 2682 | + |
| 2683 | + $innerSelect2 = $this->database |
| 2684 | + ->select() |
| 2685 | + ->from(['apartments']) |
| 2686 | + ->where(['dom' => 12]); |
| 2687 | + $injection2 = new SubQuery($innerSelect2, 'a'); |
| 2688 | + |
| 2689 | + |
| 2690 | + $outerSelect = $this->database |
| 2691 | + ->select() |
| 2692 | + ->from($injection1, $injection2); |
| 2693 | + |
| 2694 | + $this->assertSameQuery( |
| 2695 | + <<<SQL |
| 2696 | + SELECT * |
| 2697 | + FROM |
| 2698 | + (SELECT * FROM {users} WHERE {name} = ?) AS {u}, |
| 2699 | + (SELECT * FROM {apartments} WHERE {dom} = ?) AS {a} |
| 2700 | + SQL, |
| 2701 | + $outerSelect, |
| 2702 | + ); |
| 2703 | + |
| 2704 | + $this->assertSameParameters( |
| 2705 | + [ |
| 2706 | + 'John Doe', |
| 2707 | + 12, |
| 2708 | + ], |
| 2709 | + $outerSelect, |
| 2710 | + ); |
| 2711 | + } |
| 2712 | + |
| 2713 | + public function testSelectSelectSubQuery(): void |
| 2714 | + { |
| 2715 | + $innerSelect = $this->database |
| 2716 | + ->select() |
| 2717 | + ->from(['users']) |
| 2718 | + ->where(['name' => 'John Doe']); |
| 2719 | + $injection = new SubQuery($innerSelect, 'u'); |
| 2720 | + |
| 2721 | + $outerSelect = $this->database |
| 2722 | + ->select(['*', $injection]) |
| 2723 | + ->from(['apartments']); |
| 2724 | + |
| 2725 | + $this->assertSameQuery( |
| 2726 | + 'SELECT *, (SELECT * FROM {users} WHERE {name} = ?) AS {u} FROM {apartments}', |
| 2727 | + $outerSelect, |
| 2728 | + ); |
| 2729 | + |
| 2730 | + $this->assertSameParameters( |
| 2731 | + [ |
| 2732 | + 'John Doe', |
| 2733 | + ], |
| 2734 | + $outerSelect, |
| 2735 | + ); |
| 2736 | + } |
2642 | 2737 | } |
0 commit comments