Skip to content

Commit a253cc1

Browse files
authored
Adapt for changes in db (#449)
1 parent 0372646 commit a253cc1

15 files changed

+172
-192
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
for type casting performance. Related with yiisoft/db#752 (@Tigrov)
99
- Chg #348: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov)
1010
- Enh #349: Add method chaining for column classes (@Tigrov)
11-
- New #350: Add array overlaps and JSON overlaps condition builders (@Tigrov)
11+
- New #350, #449: Add array overlaps and JSON overlaps condition builders (@Tigrov)
1212
- Enh #353: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
1313
- Enh #354: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov)
1414
- Enh #356: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
@@ -31,7 +31,7 @@
3131
- Enh #382: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov)
3232
- New #384: Add `IndexMethod` class (@Tigrov)
3333
- Bug #387: Explicitly mark nullable parameters (@vjik)
34-
- Enh #386: Refactor array, structured and JSON expression builders (@Tigrov)
34+
- Enh #386, #449: Refactor array, structured and JSON expression builders (@Tigrov)
3535
- Chg #388: Change supported PHP versions to `8.1 - 8.4` (@Tigrov)
3636
- Enh #388: Minor refactoring (@Tigrov)
3737
- Chg #390: Remove `yiisoft/json` dependency (@Tigrov)

src/Builder/ArrayOverlapsBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Yiisoft\Db\Pgsql\Builder;
66

7-
use Yiisoft\Db\Expression\Value\ArrayExpression;
7+
use Yiisoft\Db\Expression\Value\ArrayValue;
88
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
99
use Yiisoft\Db\Expression\ExpressionInterface;
10-
use Yiisoft\Db\Expression\Value\JsonExpression;
10+
use Yiisoft\Db\Expression\Value\JsonValue;
1111
use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlaps;
1212
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
1313

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

4040
if (!$values instanceof ExpressionInterface) {
41-
$values = new ArrayExpression($values);
42-
} elseif ($values instanceof JsonExpression) {
41+
$values = new ArrayValue($values);
42+
} elseif ($values instanceof JsonValue) {
4343
/** @psalm-suppress MixedArgument */
44-
$values = new ArrayExpression($values->getValue());
44+
$values = new ArrayValue($values->value);
4545
}
4646

4747
$values = $this->queryBuilder->buildExpression($values, $params);

src/Builder/ArrayExpressionBuilder.php renamed to src/Builder/ArrayValueBuilder.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Yiisoft\Db\Constant\ColumnType;
88
use Yiisoft\Db\Constant\DataType;
99
use Yiisoft\Db\Constant\GettypeResult;
10-
use Yiisoft\Db\Expression\Value\ArrayExpression;
11-
use Yiisoft\Db\Expression\Value\Builder\AbstractArrayExpressionBuilder;
10+
use Yiisoft\Db\Expression\Value\ArrayValue;
11+
use Yiisoft\Db\Expression\Value\Builder\AbstractArrayValueBuilder;
1212
use Yiisoft\Db\Expression\ExpressionInterface;
1313
use Yiisoft\Db\Expression\Value\Param;
1414
use Yiisoft\Db\Pgsql\Data\LazyArray;
@@ -25,11 +25,11 @@
2525
use function str_repeat;
2626

2727
/**
28-
* Builds expressions for {@see ArrayExpression} for PostgreSQL Server.
28+
* Builds expressions for {@see ArrayValue} for PostgreSQL Server.
2929
*/
30-
final class ArrayExpressionBuilder extends AbstractArrayExpressionBuilder
30+
final class ArrayValueBuilder extends AbstractArrayValueBuilder
3131
{
32-
protected function buildStringValue(string $value, ArrayExpression $expression, array &$params): string
32+
protected function buildStringValue(string $value, ArrayValue $expression, array &$params): string
3333
{
3434
$param = new Param($value, DataType::STRING);
3535

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

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

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

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

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

132132
if ($type === null || $type instanceof AbstractArrayColumn) {
133133
return $type;

src/Builder/JsonExpressionBuilder.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/Builder/JsonOverlapsBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Yiisoft\Db\Pgsql\Builder;
66

7-
use Yiisoft\Db\Expression\Value\ArrayExpression;
7+
use Yiisoft\Db\Expression\Value\ArrayValue;
88
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
99
use Yiisoft\Db\Expression\ExpressionInterface;
10-
use Yiisoft\Db\Expression\Value\JsonExpression;
10+
use Yiisoft\Db\Expression\Value\JsonValue;
1111
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlaps;
1212
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
1313

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

40-
if ($values instanceof JsonExpression) {
40+
if ($values instanceof JsonValue) {
4141
/** @psalm-suppress MixedArgument */
42-
$values = new ArrayExpression($values->getValue());
42+
$values = new ArrayValue($values->value);
4343
} elseif (!$values instanceof ExpressionInterface) {
44-
$values = new ArrayExpression($values);
44+
$values = new ArrayValue($values);
4545
}
4646

4747
$values = $this->queryBuilder->buildExpression($values, $params);

src/Builder/JsonValueBuilder.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Pgsql\Builder;
6+
7+
use Yiisoft\Db\Expression\Value\ArrayValue;
8+
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
9+
use Yiisoft\Db\Expression\ExpressionInterface;
10+
use Yiisoft\Db\Expression\Value\JsonValue;
11+
use Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder as BaseJsonValueBuilder;
12+
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
13+
14+
/**
15+
* Builds expressions for {@see `Yiisoft\Db\Expression\Value\JsonValue`} for PostgreSQL Server.
16+
*
17+
* @implements ExpressionBuilderInterface<JsonValue>
18+
*/
19+
final class JsonValueBuilder implements ExpressionBuilderInterface
20+
{
21+
private BaseJsonValueBuilder $baseValueBuilder;
22+
23+
public function __construct(QueryBuilderInterface $queryBuilder)
24+
{
25+
$this->baseValueBuilder = new BaseJsonValueBuilder($queryBuilder);
26+
}
27+
28+
/**
29+
* The Method builds the raw SQL from the $expression that won't be additionally escaped or quoted.
30+
*
31+
* @param JsonValue $expression The expression to build.
32+
* @param array $params The binding parameters.
33+
*
34+
* @return string The raw SQL that won't be additionally escaped or quoted.
35+
*/
36+
public function build(ExpressionInterface $expression, array &$params = []): string
37+
{
38+
$statement = $this->baseValueBuilder->build($expression, $params);
39+
40+
if ($expression->value instanceof ArrayValue) {
41+
$statement = 'array_to_json(' . $statement . ')';
42+
}
43+
44+
return $statement . $this->getTypeHint($expression);
45+
}
46+
47+
/**
48+
* @return string The typecast expression based on {@see JsonValue::type}.
49+
*/
50+
private function getTypeHint(JsonValue $expression): string
51+
{
52+
$type = $expression->type;
53+
54+
if ($type === null) {
55+
return '';
56+
}
57+
58+
return '::' . $type;
59+
}
60+
}

src/Builder/StructuredExpressionBuilder.php renamed to src/Builder/StructuredValueBuilder.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
namespace Yiisoft\Db\Pgsql\Builder;
66

77
use Yiisoft\Db\Constant\DataType;
8-
use Yiisoft\Db\Exception\Exception;
9-
use InvalidArgumentException;
10-
use Yiisoft\Db\Exception\InvalidConfigException;
11-
use Yiisoft\Db\Exception\NotSupportedException;
12-
use Yiisoft\Db\Expression\Value\Builder\AbstractStructuredExpressionBuilder;
8+
use Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder;
139
use Yiisoft\Db\Expression\Value\Param;
14-
use Yiisoft\Db\Expression\Value\StructuredExpression;
10+
use Yiisoft\Db\Expression\Value\StructuredValue;
1511
use Yiisoft\Db\Pgsql\Data\StructuredLazyArray;
1612
use Yiisoft\Db\Query\QueryInterface;
1713
use Yiisoft\Db\Schema\Column\AbstractStructuredColumn;
@@ -20,25 +16,25 @@
2016
use function implode;
2117

2218
/**
23-
* Builds expressions for {@see StructuredExpression} for PostgreSQL Server.
19+
* Builds expressions for {@see StructuredValue} for PostgreSQL Server.
2420
*/
25-
final class StructuredExpressionBuilder extends AbstractStructuredExpressionBuilder
21+
final class StructuredValueBuilder extends AbstractStructuredValueBuilder
2622
{
27-
protected function buildStringValue(string $value, StructuredExpression $expression, array &$params): string
23+
protected function buildStringValue(string $value, StructuredValue $expression, array &$params): string
2824
{
2925
$param = new Param($value, DataType::STRING);
3026

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

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

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

41-
protected function buildValue(array|object $value, StructuredExpression $expression, array &$params): string
37+
protected function buildValue(array|object $value, StructuredValue $expression, array &$params): string
4238
{
4339
$value = $this->prepareValues($value, $expression);
4440
/** @psalm-var string[] $placeholders */
@@ -60,17 +56,12 @@ protected function getLazyArrayValue(LazyArrayInterface $value): array|string
6056
* Builds a placeholder array out of $expression value.
6157
*
6258
* @param array $value The expression value.
63-
* @param StructuredExpression $expression The structured expression.
59+
* @param StructuredValue $expression The structured expression.
6460
* @param array $params The binding parameters.
65-
*
66-
* @throws Exception
67-
* @throws InvalidArgumentException
68-
* @throws InvalidConfigException
69-
* @throws NotSupportedException
7061
*/
71-
private function buildPlaceholders(array $value, StructuredExpression $expression, array &$params): array
62+
private function buildPlaceholders(array $value, StructuredValue $expression, array &$params): array
7263
{
73-
$type = $expression->getType();
64+
$type = $expression->type;
7465
$queryBuilder = $this->queryBuilder;
7566
$columns = $type instanceof AbstractStructuredColumn && $queryBuilder->isTypecastingEnabled()
7667
? $type->getColumns()
@@ -93,9 +84,9 @@ private function buildPlaceholders(array $value, StructuredExpression $expressio
9384
/**
9485
* Returns the type hint expression based on type.
9586
*/
96-
private function getTypeHint(StructuredExpression $expression): string
87+
private function getTypeHint(StructuredValue $expression): string
9788
{
98-
$type = $expression->getType();
89+
$type = $expression->type;
9990

10091
if ($type instanceof AbstractStructuredColumn) {
10192
$type = $type->getDbType();

src/DQLQueryBuilder.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
namespace Yiisoft\Db\Pgsql;
66

7-
use Yiisoft\Db\Expression\Value\ArrayExpression;
7+
use Yiisoft\Db\Expression\Value\ArrayValue;
88
use Yiisoft\Db\Expression\Statement\CaseX;
99
use Yiisoft\Db\Expression\Function\ArrayMerge;
10-
use Yiisoft\Db\Expression\Value\JsonExpression;
11-
use Yiisoft\Db\Expression\Value\StructuredExpression;
12-
use Yiisoft\Db\Pgsql\Builder\ArrayExpressionBuilder;
10+
use Yiisoft\Db\Expression\Value\JsonValue;
11+
use Yiisoft\Db\Expression\Value\StructuredValue;
12+
use Yiisoft\Db\Pgsql\Builder\ArrayValueBuilder;
1313
use Yiisoft\Db\Pgsql\Builder\ArrayMergeBuilder;
1414
use Yiisoft\Db\Pgsql\Builder\ArrayOverlapsBuilder;
1515
use Yiisoft\Db\Pgsql\Builder\CaseXBuilder;
1616
use Yiisoft\Db\Pgsql\Builder\JsonOverlapsBuilder;
1717
use Yiisoft\Db\Pgsql\Builder\LikeBuilder;
18-
use Yiisoft\Db\Pgsql\Builder\StructuredExpressionBuilder;
19-
use Yiisoft\Db\Pgsql\Builder\JsonExpressionBuilder;
18+
use Yiisoft\Db\Pgsql\Builder\StructuredValueBuilder;
19+
use Yiisoft\Db\Pgsql\Builder\JsonValueBuilder;
2020
use Yiisoft\Db\QueryBuilder\AbstractDQLQueryBuilder;
2121
use Yiisoft\Db\QueryBuilder\Condition\Like;
2222
use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlaps;
@@ -32,11 +32,11 @@ protected function defaultExpressionBuilders(): array
3232
{
3333
return [
3434
...parent::defaultExpressionBuilders(),
35-
ArrayExpression::class => ArrayExpressionBuilder::class,
35+
ArrayValue::class => ArrayValueBuilder::class,
3636
ArrayOverlaps::class => ArrayOverlapsBuilder::class,
37-
JsonExpression::class => JsonExpressionBuilder::class,
37+
JsonValue::class => JsonValueBuilder::class,
3838
JsonOverlaps::class => JsonOverlapsBuilder::class,
39-
StructuredExpression::class => StructuredExpressionBuilder::class,
39+
StructuredValue::class => StructuredValueBuilder::class,
4040
Like::class => LikeBuilder::class,
4141
NotLike::class => LikeBuilder::class,
4242
CaseX::class => CaseXBuilder::class,

0 commit comments

Comments
 (0)