Skip to content

Commit

Permalink
Add array and json overlaps conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jun 29, 2024
1 parent 0da26ac commit 32515f0
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/QueryBuilder/AbstractDQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ protected function defaultConditionClasses(): array
'OR NOT LIKE' => Condition\LikeCondition::class,
'EXISTS' => Condition\ExistsCondition::class,
'NOT EXISTS' => Condition\ExistsCondition::class,
'ARRAY OVERLAPS' => Condition\ArrayOverlapsCondition::class,
'JSON OVERLAPS' => Condition\JsonOverlapsCondition::class,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\QueryBuilder\Condition\Interface\OverlapConditionInterface;
use Yiisoft\Db\QueryBuilder\Condition\Interface\OverlapsConditionInterface;

use function is_iterable;
use function is_string;

/**
* Condition that's represented `OVERLAP` operator is used to check if a value is between two values.
* The base class for classes representing the array and JSON overlaps conditions.
*/
abstract class AbstractOverlapCondition implements OverlapConditionInterface
abstract class AbstractOverlapsCondition implements OverlapsConditionInterface
{
public function __construct(
private string|ExpressionInterface $column,
Expand Down
12 changes: 0 additions & 12 deletions src/QueryBuilder/Condition/ArrayOverlapCondition.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/QueryBuilder/Condition/ArrayOverlapsCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\QueryBuilder\Condition;

/**
* Condition that represents `ARRAY OVERLAPS` operator is used to check if a column of array type overlaps another array.
*/
final class ArrayOverlapsCondition extends AbstractOverlapsCondition
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\QueryBuilder\Condition\Builder;

use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;

/**
* The base class for classes building SQL expressions for array and JSON overlaps conditions.
*/
abstract class AbstractOverlapsConditionBuilder implements ExpressionBuilderInterface
{
public function __construct(protected QueryBuilderInterface $queryBuilder)
{
}

protected function prepareColumn(ExpressionInterface|string $column): string
{
if ($column instanceof ExpressionInterface) {
return $this->queryBuilder->buildExpression($column);
}

return $this->queryBuilder->quoter()->quoteColumnName($column);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Yiisoft\Db\Expression\ExpressionInterface;

interface OverlapConditionInterface extends ConditionInterface
/**
* Represents array and JSON overlaps conditions.
*/
interface OverlapsConditionInterface extends ConditionInterface
{
/**
* @return ExpressionInterface|string The column name. If it's an array, a composite `IN` condition will be
Expand Down
12 changes: 0 additions & 12 deletions src/QueryBuilder/Condition/JsonOverlapCondition.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/QueryBuilder/Condition/JsonOverlapsCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\QueryBuilder\Condition;

/**
* Condition that represents `JSON OVERLAPS` operator and is used to check if a column of json type overlaps an array.
*/
final class JsonOverlapsCondition extends AbstractOverlapsCondition
{
}
23 changes: 23 additions & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Yiisoft\Db\Tests\Provider;

use ArrayIterator;
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\QueryBuilder\Condition\BetweenColumnsCondition;
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
Expand Down Expand Up @@ -1538,4 +1540,25 @@ public static function columnTypes(): array
[new Column('string(100)')],
];
}

public static function overlapsCondition(): array
{
return [
[[], 0],
[[0], 0],
[[1], 1],
[[4], 1],
[[3], 2],
[[0, 1], 1],
[[1, 2], 1],
[[1, 4], 2],
[[0, 1, 2, 3, 4, 5, 6], 2],
[[6, 7, 8, 9], 0],
[new ArrayIterator([0, 1, 2, 7]), 1],
'null' => [[null], 1],
'expression' => [new Expression("'[0,1,2,7]'"), 1],
'json expression' => [new JsonExpression([0,1,2,7]), 1],
'query expression' => [(new Query(static::getDb()))->select(new JsonExpression([0,1,2,7])), 1],
];
}
}

0 comments on commit 32515f0

Please sign in to comment.