|
9 | 9 | use Yiisoft\Db\Exception\InvalidArgumentException; |
10 | 10 | use Yiisoft\Db\Exception\InvalidConfigException; |
11 | 11 | use Yiisoft\Db\Exception\NotSupportedException; |
| 12 | +use Yiisoft\Db\Expression\Expression; |
12 | 13 | use Yiisoft\Db\Expression\ExpressionInterface; |
13 | 14 | use Yiisoft\Db\Expression\JsonExpression; |
14 | 15 | use Yiisoft\Db\Query\Query; |
15 | 16 | use Yiisoft\Db\Query\QueryInterface; |
| 17 | +use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition; |
16 | 18 | use Yiisoft\Db\Schema\SchemaInterface; |
17 | 19 | use Yiisoft\Db\Sqlite\Column; |
18 | 20 | use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; |
@@ -776,4 +778,61 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e |
776 | 778 | { |
777 | 779 | parent::testSelectScalar($columns, $expected); |
778 | 780 | } |
| 781 | + |
| 782 | + public function testJsonOverlapsConditionBuilder(): void |
| 783 | + { |
| 784 | + $db = $this->getConnection(); |
| 785 | + $qb = $db->getQueryBuilder(); |
| 786 | + |
| 787 | + $params = []; |
| 788 | + $sql = $qb->buildExpression(new JsonOverlapsCondition('column', [1, 2, 3]), $params); |
| 789 | + |
| 790 | + $this->assertSame( |
| 791 | + 'EXISTS(SELECT value FROM json_each(`column`) INTERSECT SELECT value FROM json_each(:qp0))=1', |
| 792 | + $sql |
| 793 | + ); |
| 794 | + $this->assertSame([':qp0' => '[1,2,3]'], $params); |
| 795 | + |
| 796 | + // Test column as Expression |
| 797 | + $params = []; |
| 798 | + $sql = $qb->buildExpression(new JsonOverlapsCondition(new Expression('column'), [1, 2, 3]), $params); |
| 799 | + |
| 800 | + $this->assertSame( |
| 801 | + 'EXISTS(SELECT value FROM json_each(column) INTERSECT SELECT value FROM json_each(:qp0))=1', |
| 802 | + $sql |
| 803 | + ); |
| 804 | + $this->assertSame([':qp0' => '[1,2,3]'], $params); |
| 805 | + |
| 806 | + $db->close(); |
| 807 | + } |
| 808 | + |
| 809 | + /** @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\QueryBuilderProvider::overlapsCondition */ |
| 810 | + public function testJsonOverlapsCondition(iterable|ExpressionInterface $values, int $expectedCount): void |
| 811 | + { |
| 812 | + $db = $this->getConnection(true); |
| 813 | + |
| 814 | + $count = (new Query($db)) |
| 815 | + ->from('json_type') |
| 816 | + ->where(new JsonOverlapsCondition('json_col', $values)) |
| 817 | + ->count(); |
| 818 | + |
| 819 | + $this->assertSame($expectedCount, $count); |
| 820 | + |
| 821 | + $db->close(); |
| 822 | + } |
| 823 | + |
| 824 | + /** @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\QueryBuilderProvider::overlapsCondition */ |
| 825 | + public function testJsonOverlapsConditionOperator(iterable|ExpressionInterface $values, int $expectedCount): void |
| 826 | + { |
| 827 | + $db = $this->getConnection(true); |
| 828 | + |
| 829 | + $count = (new Query($db)) |
| 830 | + ->from('json_type') |
| 831 | + ->where(['json overlaps', 'json_col', $values]) |
| 832 | + ->count(); |
| 833 | + |
| 834 | + $this->assertSame($expectedCount, $count); |
| 835 | + |
| 836 | + $db->close(); |
| 837 | + } |
779 | 838 | } |
0 commit comments