-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json overlaps condition builder (#350)
- Loading branch information
Showing
9 changed files
with
229 additions
and
46 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
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_text($column::jsonb)) && $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