-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array and json overlaps condition builders
- Loading branch information
Showing
8 changed files
with
220 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Pgsql\Builder; | ||
|
||
use Yiisoft\Db\Exception\Exception; | ||
use Yiisoft\Db\Exception\InvalidArgumentException; | ||
use Yiisoft\Db\Exception\InvalidConfigException; | ||
use Yiisoft\Db\Exception\NotSupportedException; | ||
use Yiisoft\Db\Expression\ArrayExpression; | ||
use Yiisoft\Db\Expression\ExpressionInterface; | ||
use Yiisoft\Db\Expression\JsonExpression; | ||
use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlapsCondition; | ||
use Yiisoft\Db\QueryBuilder\Condition\Builder\AbstractOverlapsConditionBuilder; | ||
|
||
/** | ||
* Builds expressions for {@see ArrayOverlapsCondition} for PostgreSQL Server. | ||
*/ | ||
final class ArrayOverlapsConditionBuilder extends AbstractOverlapsConditionBuilder | ||
{ | ||
/** | ||
* Build SQL for {@see ArrayOverlapsCondition}. | ||
* | ||
* @param ArrayOverlapsCondition $expression the {@see ArrayOverlapsCondition} to be built. | ||
* | ||
* @throws Exception | ||
* @throws InvalidArgumentException | ||
* @throws InvalidConfigException | ||
* @throws NotSupportedException | ||
*/ | ||
public function build(ExpressionInterface $expression, array &$params = []): string | ||
{ | ||
$column = $this->prepareColumn($expression->getColumn()); | ||
$values = $expression->getValues(); | ||
|
||
if ($values instanceof JsonExpression) { | ||
$values = new ArrayExpression($values->getValue()); | ||
} elseif (!$values instanceof ExpressionInterface) { | ||
$values = new ArrayExpression($values); | ||
} | ||
|
||
$values = $this->queryBuilder->buildExpression($values, $params); | ||
|
||
return "$column::text[] && $values::text[]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Pgsql\Builder; | ||
|
||
use Yiisoft\Db\Exception\Exception; | ||
use Yiisoft\Db\Exception\InvalidArgumentException; | ||
use Yiisoft\Db\Exception\InvalidConfigException; | ||
use Yiisoft\Db\Exception\NotSupportedException; | ||
use Yiisoft\Db\Expression\ArrayExpression; | ||
use Yiisoft\Db\Expression\ExpressionInterface; | ||
use Yiisoft\Db\Expression\JsonExpression; | ||
use Yiisoft\Db\QueryBuilder\Condition\Builder\AbstractOverlapsConditionBuilder; | ||
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition; | ||
|
||
/** | ||
* Builds expressions for {@see JsonOverlapsCondition} for PostgreSQL Server. | ||
*/ | ||
final class JsonOverlapsConditionBuilder extends AbstractOverlapsConditionBuilder | ||
{ | ||
/** | ||
* Build SQL for {@see JsonOverlapsCondition}. | ||
* | ||
* @param JsonOverlapsCondition $expression the {@see JsonOverlapsCondition} to be built. | ||
* | ||
* @throws Exception | ||
* @throws InvalidArgumentException | ||
* @throws InvalidConfigException | ||
* @throws NotSupportedException | ||
*/ | ||
public function build(ExpressionInterface $expression, array &$params = []): string | ||
{ | ||
$column = $this->prepareColumn($expression->getColumn()); | ||
$values = $expression->getValues(); | ||
|
||
if ($values instanceof JsonExpression) { | ||
$values = new ArrayExpression($values->getValue()); | ||
} elseif (!$values instanceof ExpressionInterface) { | ||
$values = new ArrayExpression($values); | ||
} | ||
|
||
$values = $this->queryBuilder->buildExpression($values, $params); | ||
|
||
return "ARRAY(SELECT jsonb_array_elements($column::jsonb))::text[] && $values::text[]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters