Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
for type casting performance. Related with yiisoft/db#752 (@Tigrov)
- Chg #348: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov)
- Enh #349: Add method chaining for column classes (@Tigrov)
- New #350: Add array overlaps and JSON overlaps condition builders (@Tigrov)
- New #350, #449: Add array overlaps and JSON overlaps condition builders (@Tigrov)
- Enh #353: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
- Enh #354: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov)
- Enh #356: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
Expand All @@ -31,7 +31,7 @@
- Enh #382: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov)
- New #384: Add `IndexMethod` class (@Tigrov)
- Bug #387: Explicitly mark nullable parameters (@vjik)
- Enh #386: Refactor array, structured and JSON expression builders (@Tigrov)
- Enh #386, #449: Refactor array, structured and JSON expression builders (@Tigrov)
- Chg #388: Change supported PHP versions to `8.1 - 8.4` (@Tigrov)
- Enh #388: Minor refactoring (@Tigrov)
- Chg #390: Remove `yiisoft/json` dependency (@Tigrov)
Expand Down
10 changes: 5 additions & 5 deletions src/Builder/ArrayOverlapsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Yiisoft\Db\Pgsql\Builder;

use Yiisoft\Db\Expression\Value\ArrayExpression;
use Yiisoft\Db\Expression\Value\ArrayValue;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\Value\JsonExpression;
use Yiisoft\Db\Expression\Value\JsonValue;
use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlaps;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;

Expand Down Expand Up @@ -38,10 +38,10 @@ public function build(ExpressionInterface $expression, array &$params = []): str
$values = $expression->values;

if (!$values instanceof ExpressionInterface) {
$values = new ArrayExpression($values);
} elseif ($values instanceof JsonExpression) {
$values = new ArrayValue($values);
} elseif ($values instanceof JsonValue) {
/** @psalm-suppress MixedArgument */
$values = new ArrayExpression($values->getValue());
$values = new ArrayValue($values->value);
}

$values = $this->queryBuilder->buildExpression($values, $params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Constant\GettypeResult;
use Yiisoft\Db\Expression\Value\ArrayExpression;
use Yiisoft\Db\Expression\Value\Builder\AbstractArrayExpressionBuilder;
use Yiisoft\Db\Expression\Value\ArrayValue;
use Yiisoft\Db\Expression\Value\Builder\AbstractArrayValueBuilder;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\Value\Param;
use Yiisoft\Db\Pgsql\Data\LazyArray;
Expand All @@ -25,11 +25,11 @@
use function str_repeat;

/**
* Builds expressions for {@see ArrayExpression} for PostgreSQL Server.
* Builds expressions for {@see ArrayValue} for PostgreSQL Server.
*/
final class ArrayExpressionBuilder extends AbstractArrayExpressionBuilder
final class ArrayValueBuilder extends AbstractArrayValueBuilder
{
protected function buildStringValue(string $value, ArrayExpression $expression, array &$params): string
protected function buildStringValue(string $value, ArrayValue $expression, array &$params): string
{
$param = new Param($value, DataType::STRING);

Expand All @@ -41,15 +41,15 @@ protected function buildStringValue(string $value, ArrayExpression $expression,
return $this->queryBuilder->bindParam($param, $params) . $typeHint;
}

protected function buildSubquery(QueryInterface $query, ArrayExpression $expression, array &$params): string
protected function buildSubquery(QueryInterface $query, ArrayValue $expression, array &$params): string
{
$column = $this->getColumn($expression);
$dbType = $this->getColumnDbType($column);

return $this->buildNestedSubquery($query, $dbType, $column?->getDimension() ?? 1, $params);
}

protected function buildValue(iterable $value, ArrayExpression $expression, array &$params): string
protected function buildValue(iterable $value, ArrayValue $expression, array &$params): string
{
$column = $this->getColumn($expression);
$dbType = $this->getColumnDbType($column);
Expand Down Expand Up @@ -125,9 +125,9 @@ private function buildNestedValue(iterable $value, string|null &$dbType, ColumnI
return $this->buildNestedArray($placeholders, $dbType, $dimension);
}

private function getColumn(ArrayExpression $expression): AbstractArrayColumn|null
private function getColumn(ArrayValue $expression): AbstractArrayColumn|null
{
$type = $expression->getType();
$type = $expression->type;

if ($type === null || $type instanceof AbstractArrayColumn) {
return $type;
Expand Down
71 changes: 0 additions & 71 deletions src/Builder/JsonExpressionBuilder.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Builder/JsonOverlapsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Yiisoft\Db\Pgsql\Builder;

use Yiisoft\Db\Expression\Value\ArrayExpression;
use Yiisoft\Db\Expression\Value\ArrayValue;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\Value\JsonExpression;
use Yiisoft\Db\Expression\Value\JsonValue;
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlaps;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;

Expand Down Expand Up @@ -37,11 +37,11 @@ public function build(ExpressionInterface $expression, array &$params = []): str
: $this->queryBuilder->getQuoter()->quoteColumnName($expression->column);
$values = $expression->values;

if ($values instanceof JsonExpression) {
if ($values instanceof JsonValue) {
/** @psalm-suppress MixedArgument */
$values = new ArrayExpression($values->getValue());
$values = new ArrayValue($values->value);
} elseif (!$values instanceof ExpressionInterface) {
$values = new ArrayExpression($values);
$values = new ArrayValue($values);
}

$values = $this->queryBuilder->buildExpression($values, $params);
Expand Down
60 changes: 60 additions & 0 deletions src/Builder/JsonValueBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Builder;

use Yiisoft\Db\Expression\Value\ArrayValue;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\Value\JsonValue;
use Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder as BaseJsonValueBuilder;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;

/**
* Builds expressions for {@see `Yiisoft\Db\Expression\Value\JsonValue`} for PostgreSQL Server.
*
* @implements ExpressionBuilderInterface<JsonValue>
*/
final class JsonValueBuilder implements ExpressionBuilderInterface
{
private BaseJsonValueBuilder $baseValueBuilder;

public function __construct(QueryBuilderInterface $queryBuilder)
{
$this->baseValueBuilder = new BaseJsonValueBuilder($queryBuilder);
}

/**
* The Method builds the raw SQL from the $expression that won't be additionally escaped or quoted.
*
* @param JsonValue $expression The expression to build.
* @param array $params The binding parameters.
*
* @return string The raw SQL that won't be additionally escaped or quoted.
*/
public function build(ExpressionInterface $expression, array &$params = []): string
{
$statement = $this->baseValueBuilder->build($expression, $params);

if ($expression->value instanceof ArrayValue) {
$statement = 'array_to_json(' . $statement . ')';
}

return $statement . $this->getTypeHint($expression);
}

/**
* @return string The typecast expression based on {@see JsonValue::type}.
*/
private function getTypeHint(JsonValue $expression): string
{
$type = $expression->type;

if ($type === null) {
return '';
}

return '::' . $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
namespace Yiisoft\Db\Pgsql\Builder;

use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Exception\Exception;
use InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Value\Builder\AbstractStructuredExpressionBuilder;
use Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder;
use Yiisoft\Db\Expression\Value\Param;
use Yiisoft\Db\Expression\Value\StructuredExpression;
use Yiisoft\Db\Expression\Value\StructuredValue;
use Yiisoft\Db\Pgsql\Data\StructuredLazyArray;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\Column\AbstractStructuredColumn;
Expand All @@ -20,25 +16,25 @@
use function implode;

/**
* Builds expressions for {@see StructuredExpression} for PostgreSQL Server.
* Builds expressions for {@see StructuredValue} for PostgreSQL Server.
*/
final class StructuredExpressionBuilder extends AbstractStructuredExpressionBuilder
final class StructuredValueBuilder extends AbstractStructuredValueBuilder
{
protected function buildStringValue(string $value, StructuredExpression $expression, array &$params): string
protected function buildStringValue(string $value, StructuredValue $expression, array &$params): string
{
$param = new Param($value, DataType::STRING);

return $this->queryBuilder->bindParam($param, $params) . $this->getTypeHint($expression);
}

protected function buildSubquery(QueryInterface $query, StructuredExpression $expression, array &$params): string
protected function buildSubquery(QueryInterface $query, StructuredValue $expression, array &$params): string
{
[$sql, $params] = $this->queryBuilder->build($query, $params);

return "($sql)" . $this->getTypeHint($expression);
}

protected function buildValue(array|object $value, StructuredExpression $expression, array &$params): string
protected function buildValue(array|object $value, StructuredValue $expression, array &$params): string
{
$value = $this->prepareValues($value, $expression);
/** @psalm-var string[] $placeholders */
Expand All @@ -60,17 +56,12 @@ protected function getLazyArrayValue(LazyArrayInterface $value): array|string
* Builds a placeholder array out of $expression value.
*
* @param array $value The expression value.
* @param StructuredExpression $expression The structured expression.
* @param StructuredValue $expression The structured expression.
* @param array $params The binding parameters.
*
* @throws Exception
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotSupportedException
*/
private function buildPlaceholders(array $value, StructuredExpression $expression, array &$params): array
private function buildPlaceholders(array $value, StructuredValue $expression, array &$params): array
{
$type = $expression->getType();
$type = $expression->type;
$queryBuilder = $this->queryBuilder;
$columns = $type instanceof AbstractStructuredColumn && $queryBuilder->isTypecastingEnabled()
? $type->getColumns()
Expand All @@ -93,9 +84,9 @@ private function buildPlaceholders(array $value, StructuredExpression $expressio
/**
* Returns the type hint expression based on type.
*/
private function getTypeHint(StructuredExpression $expression): string
private function getTypeHint(StructuredValue $expression): string
{
$type = $expression->getType();
$type = $expression->type;

if ($type instanceof AbstractStructuredColumn) {
$type = $type->getDbType();
Expand Down
18 changes: 9 additions & 9 deletions src/DQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace Yiisoft\Db\Pgsql;

use Yiisoft\Db\Expression\Value\ArrayExpression;
use Yiisoft\Db\Expression\Value\ArrayValue;
use Yiisoft\Db\Expression\Statement\CaseX;
use Yiisoft\Db\Expression\Function\ArrayMerge;
use Yiisoft\Db\Expression\Value\JsonExpression;
use Yiisoft\Db\Expression\Value\StructuredExpression;
use Yiisoft\Db\Pgsql\Builder\ArrayExpressionBuilder;
use Yiisoft\Db\Expression\Value\JsonValue;
use Yiisoft\Db\Expression\Value\StructuredValue;
use Yiisoft\Db\Pgsql\Builder\ArrayValueBuilder;
use Yiisoft\Db\Pgsql\Builder\ArrayMergeBuilder;
use Yiisoft\Db\Pgsql\Builder\ArrayOverlapsBuilder;
use Yiisoft\Db\Pgsql\Builder\CaseXBuilder;
use Yiisoft\Db\Pgsql\Builder\JsonOverlapsBuilder;
use Yiisoft\Db\Pgsql\Builder\LikeBuilder;
use Yiisoft\Db\Pgsql\Builder\StructuredExpressionBuilder;
use Yiisoft\Db\Pgsql\Builder\JsonExpressionBuilder;
use Yiisoft\Db\Pgsql\Builder\StructuredValueBuilder;
use Yiisoft\Db\Pgsql\Builder\JsonValueBuilder;
use Yiisoft\Db\QueryBuilder\AbstractDQLQueryBuilder;
use Yiisoft\Db\QueryBuilder\Condition\Like;
use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlaps;
Expand All @@ -32,11 +32,11 @@ protected function defaultExpressionBuilders(): array
{
return [
...parent::defaultExpressionBuilders(),
ArrayExpression::class => ArrayExpressionBuilder::class,
ArrayValue::class => ArrayValueBuilder::class,
ArrayOverlaps::class => ArrayOverlapsBuilder::class,
JsonExpression::class => JsonExpressionBuilder::class,
JsonValue::class => JsonValueBuilder::class,
JsonOverlaps::class => JsonOverlapsBuilder::class,
StructuredExpression::class => StructuredExpressionBuilder::class,
StructuredValue::class => StructuredValueBuilder::class,
Like::class => LikeBuilder::class,
NotLike::class => LikeBuilder::class,
CaseX::class => CaseXBuilder::class,
Expand Down
Loading