From 241ee50aa736d3de33a5673f63e57e8c43042b65 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 1 Sep 2025 18:35:54 +0700 Subject: [PATCH 1/3] Rename `ArrayExpression`, `JsonExpression`, `StructuredExpression` --- docs/guide/en/schema/typecasting.md | 4 +- .../{ArrayExpression.php => ArrayValue.php} | 36 ++--------- ...lder.php => AbstractArrayValueBuilder.php} | 26 ++++---- ...php => AbstractStructuredValueBuilder.php} | 39 +++++------- ...ssionBuilder.php => ArrayValueBuilder.php} | 12 ++-- ...essionBuilder.php => JsonValueBuilder.php} | 23 ++----- ...Builder.php => StructuredValueBuilder.php} | 12 ++-- src/Expression/Value/JsonExpression.php | 57 ----------------- src/Expression/Value/JsonValue.php | 34 ++++++++++ ...uredExpression.php => StructuredValue.php} | 38 ++---------- src/QueryBuilder/AbstractDQLQueryBuilder.php | 18 +++--- src/QueryBuilder/AbstractQueryBuilder.php | 20 +++--- src/Schema/Column/AbstractArrayColumn.php | 4 +- src/Schema/Column/AbstractJsonColumn.php | 4 +- .../Column/AbstractStructuredColumn.php | 4 +- .../Function/MultiOperandFunctionTest.php | 6 +- ...yExpressionTest.php => ArrayValueTest.php} | 10 +-- ...lderTest.php => ArrayValueBuilderTest.php} | 20 +++--- ...ilderTest.php => JsonValueBuilderTest.php} | 22 +++---- .../Value/Builder/ParamBuilderTest.php | 2 +- ...est.php => StructuredValueBuilderTest.php} | 20 +++--- ...onExpressionTest.php => JsonValueTest.php} | 10 +-- ...essionTest.php => StructuredValueTest.php} | 10 +-- tests/Db/Schema/Column/ColumnTest.php | 8 +-- tests/Provider/ColumnProvider.php | 62 +++++++++---------- tests/Provider/CommandProvider.php | 6 +- tests/Provider/QueryBuilderProvider.php | 14 ++--- 27 files changed, 212 insertions(+), 309 deletions(-) rename src/Expression/Value/{ArrayExpression.php => ArrayValue.php} (60%) rename src/Expression/Value/Builder/{AbstractArrayExpressionBuilder.php => AbstractArrayValueBuilder.php} (79%) rename src/Expression/Value/Builder/{AbstractStructuredExpressionBuilder.php => AbstractStructuredValueBuilder.php} (80%) rename src/Expression/Value/Builder/{ArrayExpressionBuilder.php => ArrayValueBuilder.php} (73%) rename src/Expression/Value/Builder/{JsonExpressionBuilder.php => JsonValueBuilder.php} (79%) rename src/Expression/Value/Builder/{StructuredExpressionBuilder.php => StructuredValueBuilder.php} (78%) delete mode 100644 src/Expression/Value/JsonExpression.php create mode 100644 src/Expression/Value/JsonValue.php rename src/Expression/Value/{StructuredExpression.php => StructuredValue.php} (51%) rename tests/Db/Expression/Value/{ArrayExpressionTest.php => ArrayValueTest.php} (79%) rename tests/Db/Expression/Value/Builder/{ArrayExpressionBuilderTest.php => ArrayValueBuilderTest.php} (76%) rename tests/Db/Expression/Value/Builder/{JsonExpressionBuilderTest.php => JsonValueBuilderTest.php} (77%) rename tests/Db/Expression/Value/Builder/{StructuredExpressionBuilderTest.php => StructuredValueBuilderTest.php} (80%) rename tests/Db/Expression/Value/{JsonExpressionTest.php => JsonValueTest.php} (77%) rename tests/Db/Expression/Value/{StructuredExpressionTest.php => StructuredValueTest.php} (81%) diff --git a/docs/guide/en/schema/typecasting.md b/docs/guide/en/schema/typecasting.md index 79f5099ae..ba2f9a2f6 100644 --- a/docs/guide/en/schema/typecasting.md +++ b/docs/guide/en/schema/typecasting.md @@ -322,10 +322,10 @@ final class MyStructuredColumn extends AbstractStructuredColumn } if ($this->getDbType() === 'file_with_name' && $value instanceof FileWithName) { - return new StructuredExpression([$value->getPath(), $value->getName()], $this); + return new StructuredValue([$value->getPath(), $value->getName()], $this); } - return new StructuredExpression($value, $this); + return new StructuredValue($value, $this); } /** diff --git a/src/Expression/Value/ArrayExpression.php b/src/Expression/Value/ArrayValue.php similarity index 60% rename from src/Expression/Value/ArrayExpression.php rename to src/Expression/Value/ArrayValue.php index a014bfd99..802e0465a 100644 --- a/src/Expression/Value/ArrayExpression.php +++ b/src/Expression/Value/ArrayValue.php @@ -18,13 +18,13 @@ * Expressions of this type can be used in conditions as well: * * ```php - * $query->andWhere(['@>', 'items', new ArrayExpression([1, 2, 3], 'integer[]')]); + * $query->andWhere(['@>', 'items', new ArrayValue([1, 2, 3], 'integer[]')]); * ``` * * Which, depending on DBMS, will result in a well-prepared condition. For example, in PostgresSQL it will be compiled * to `WHERE "items" @> ARRAY[1, 2, 3]::integer[]`. */ -final class ArrayExpression implements ExpressionInterface +final class ArrayValue implements ExpressionInterface { /** * @param iterable|LazyArrayInterface|QueryInterface|string|null $value The array value which can be represented as @@ -48,36 +48,8 @@ final class ArrayExpression implements ExpressionInterface * {@see ColumnInterface}. */ public function __construct( - private readonly iterable|LazyArrayInterface|QueryInterface|string|null $value, - private readonly ColumnInterface|string|null $type = null, + public readonly iterable|LazyArrayInterface|QueryInterface|string|null $value, + public readonly ColumnInterface|string|null $type = null, ) { } - - /** - * The array column type which can be represented as - * - a native database column type; - * - an {@see ColumnType abstract} type; - * - an instance of {@see ColumnInterface}; - * - `null` if the type isn't explicitly specified. - * - * The column type is used to typecast array values before saving into the database and for adding type hint to - * the SQL statement. If the type isn't specified and DBMS can't guess it from the context, SQL error will be raised. - */ - public function getType(): ColumnInterface|string|null - { - return $this->type; - } - - /** - * The array value which can be represented as - * - an `array` of values; - * - an instance of {@see Traversable} or {@see LazyArrayInterface} that represents an array of values; - * - an instance of {@see QueryInterface} that represents an SQL sub-query; - * - a `string` retrieved value from the database that can be parsed into an array; - * - `null`. - */ - public function getValue(): iterable|LazyArrayInterface|QueryInterface|string|null - { - return $this->value; - } } diff --git a/src/Expression/Value/Builder/AbstractArrayExpressionBuilder.php b/src/Expression/Value/Builder/AbstractArrayValueBuilder.php similarity index 79% rename from src/Expression/Value/Builder/AbstractArrayExpressionBuilder.php rename to src/Expression/Value/Builder/AbstractArrayValueBuilder.php index b5a02e537..05298dfcc 100644 --- a/src/Expression/Value/Builder/AbstractArrayExpressionBuilder.php +++ b/src/Expression/Value/Builder/AbstractArrayValueBuilder.php @@ -5,7 +5,7 @@ namespace Yiisoft\Db\Expression\Value\Builder; use Yiisoft\Db\Expression\ExpressionBuilderInterface; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; @@ -14,48 +14,48 @@ use function is_string; /** - * Abstract expression builder for {@see ArrayExpression}. + * Abstract expression builder for {@see ArrayValue}. * - * @implements ExpressionBuilderInterface + * @implements ExpressionBuilderInterface */ -abstract class AbstractArrayExpressionBuilder implements ExpressionBuilderInterface +abstract class AbstractArrayValueBuilder implements ExpressionBuilderInterface { /** * Builds an SQL expression for a string value. * * @param string $value The valid SQL string representation of the array value. - * @param ArrayExpression $expression The array expression. + * @param ArrayValue $expression The array expression. * @param array $params The binding parameters. * * @return string The SQL expression representing the array value. */ - abstract protected function buildStringValue(string $value, ArrayExpression $expression, array &$params): string; + abstract protected function buildStringValue(string $value, ArrayValue $expression, array &$params): string; /** * Build an array expression from a sub-query object. * * @param QueryInterface $query The sub-query object. - * @param ArrayExpression $expression The array expression. + * @param ArrayValue $expression The array expression. * @param array $params The binding parameters. * * @return string The sub-query SQL expression representing an array. */ abstract protected function buildSubquery( QueryInterface $query, - ArrayExpression $expression, - array &$params + ArrayValue $expression, + array &$params ): string; /** * Builds a SQL expression for an array value. * * @param iterable $value The array value. - * @param ArrayExpression $expression The array expression. + * @param ArrayValue $expression The array expression. * @param array $params The binding parameters. * * @return string The SQL expression representing the array value. */ - abstract protected function buildValue(iterable $value, ArrayExpression $expression, array &$params): string; + abstract protected function buildValue(iterable $value, ArrayValue $expression, array &$params): string; /** * Returns the value of the lazy array as an array or a raw string depending on the implementation. @@ -73,14 +73,14 @@ public function __construct(protected readonly QueryBuilderInterface $queryBuild /** * The Method builds the raw SQL from the `$expression` that won't be additionally escaped or quoted. * - * @param ArrayExpression $expression The expression to build. + * @param ArrayValue $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 { - $value = $expression->getValue(); + $value = $expression->value; if ($value === null) { return 'NULL'; diff --git a/src/Expression/Value/Builder/AbstractStructuredExpressionBuilder.php b/src/Expression/Value/Builder/AbstractStructuredValueBuilder.php similarity index 80% rename from src/Expression/Value/Builder/AbstractStructuredExpressionBuilder.php rename to src/Expression/Value/Builder/AbstractStructuredValueBuilder.php index a6c01979f..aeb2d77af 100644 --- a/src/Expression/Value/Builder/AbstractStructuredExpressionBuilder.php +++ b/src/Expression/Value/Builder/AbstractStructuredValueBuilder.php @@ -10,7 +10,7 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\ExpressionBuilderInterface; use Yiisoft\Db\Expression\ExpressionInterface; -use Yiisoft\Db\Expression\Value\StructuredExpression; +use Yiisoft\Db\Expression\Value\StructuredValue; use Yiisoft\Db\Helper\DbArrayHelper; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; @@ -22,24 +22,24 @@ use function is_string; /** - * Abstract expression builder for {@see StructuredExpression}. + * Abstract expression builder for {@see StructuredValue}. * - * @implements ExpressionBuilderInterface + * @implements ExpressionBuilderInterface */ -abstract class AbstractStructuredExpressionBuilder implements ExpressionBuilderInterface +abstract class AbstractStructuredValueBuilder implements ExpressionBuilderInterface { /** * Builds an SQL expression for a string value. * * @param string $value The valid SQL string representation of the structured value. - * @param StructuredExpression $expression The structured expression. + * @param StructuredValue $expression The structured expression. * @param array $params The binding parameters. * * @return string The SQL expression representing the structured value. */ abstract protected function buildStringValue( string $value, - StructuredExpression $expression, + StructuredValue $expression, array &$params ): string; @@ -47,14 +47,14 @@ abstract protected function buildStringValue( * Build a structured expression from a sub-query object. * * @param QueryInterface $query The sub-query object. - * @param StructuredExpression $expression The structured expression. + * @param StructuredValue $expression The structured expression. * @param array $params The binding parameters. * * @return string The sub-query SQL expression representing a structured value. */ abstract protected function buildSubquery( QueryInterface $query, - StructuredExpression $expression, + StructuredValue $expression, array &$params ): string; @@ -62,15 +62,15 @@ abstract protected function buildSubquery( * Builds an SQL expression for a structured value. * * @param array|object $value The structured value. - * @param StructuredExpression $expression The structured expression. + * @param StructuredValue $expression The structured expression. * @param array $params The binding parameters. * * @return string The SQL expression representing the structured value. */ abstract protected function buildValue( - array|object $value, - StructuredExpression $expression, - array &$params + array|object $value, + StructuredValue $expression, + array &$params ): string; /** @@ -89,19 +89,14 @@ public function __construct(protected readonly QueryBuilderInterface $queryBuild /** * The method builds the raw SQL from the `$expression` that won't be additionally escaped or quoted. * - * @param StructuredExpression $expression The expression to build. + * @param StructuredValue $expression The expression to build. * @param array $params The binding parameters. * - * @throws Exception - * @throws InvalidArgumentException - * @throws InvalidConfigException - * @throws NotSupportedException - * * @return string The raw SQL that won't be additionally escaped or quoted. */ public function build(ExpressionInterface $expression, array &$params = []): string { - $value = $expression->getValue(); + $value = $expression->value; if ($value === null) { return 'NULL'; @@ -133,13 +128,13 @@ public function build(ExpressionInterface $expression, array &$params = []): str * If the structured type columns are not specified it will only convert the object to an array. * * @param array|object $value The structured type value. - * @param StructuredExpression $expression The structured expression. + * @param StructuredValue $expression The structured expression. */ - protected function prepareValues(array|object $value, StructuredExpression $expression): array + protected function prepareValues(array|object $value, StructuredValue $expression): array { $value = DbArrayHelper::toArray($value); - $type = $expression->getType(); + $type = $expression->type; $columns = $type instanceof AbstractStructuredColumn ? $type->getColumns() : []; if (empty($columns)) { diff --git a/src/Expression/Value/Builder/ArrayExpressionBuilder.php b/src/Expression/Value/Builder/ArrayValueBuilder.php similarity index 73% rename from src/Expression/Value/Builder/ArrayExpressionBuilder.php rename to src/Expression/Value/Builder/ArrayValueBuilder.php index 0bcc609f2..88f43aea6 100644 --- a/src/Expression/Value/Builder/ArrayExpressionBuilder.php +++ b/src/Expression/Value/Builder/ArrayValueBuilder.php @@ -6,7 +6,7 @@ use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Constant\DataType; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Schema\Data\LazyArray; @@ -20,25 +20,25 @@ use const JSON_THROW_ON_ERROR; /** - * Default expression builder for {@see ArrayExpression}. Builds an expression as a JSON. + * Default expression builder for {@see ArrayValue}. Builds an expression as a JSON. */ -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); return $this->queryBuilder->bindParam($param, $params); } - protected function buildSubquery(QueryInterface $query, ArrayExpression $expression, array &$params): string + protected function buildSubquery(QueryInterface $query, ArrayValue $expression, array &$params): string { [$sql, $params] = $this->queryBuilder->build($query, $params); return "($sql)"; } - protected function buildValue(iterable $value, ArrayExpression $expression, array &$params): string + protected function buildValue(iterable $value, ArrayValue $expression, array &$params): string { if (!is_array($value)) { $value = iterator_to_array($value, false); diff --git a/src/Expression/Value/Builder/JsonExpressionBuilder.php b/src/Expression/Value/Builder/JsonValueBuilder.php similarity index 79% rename from src/Expression/Value/Builder/JsonExpressionBuilder.php rename to src/Expression/Value/Builder/JsonValueBuilder.php index 85b0f306e..409cfc389 100644 --- a/src/Expression/Value/Builder/JsonExpressionBuilder.php +++ b/src/Expression/Value/Builder/JsonValueBuilder.php @@ -4,18 +4,13 @@ namespace Yiisoft\Db\Expression\Value\Builder; -use JsonException; use JsonSerializable; use Traversable; use Yiisoft\Db\Expression\ExpressionBuilderInterface; use Yiisoft\Db\Expression\Value\Param; 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\ExpressionInterface; -use Yiisoft\Db\Expression\Value\JsonExpression; +use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Schema\Data\LazyArray; @@ -30,11 +25,11 @@ use const JSON_THROW_ON_ERROR; /** - * Builds expressions for {@see JsonExpression}. + * Builds expressions for {@see JsonValue}. * - * @implements ExpressionBuilderInterface + * @implements ExpressionBuilderInterface */ -final class JsonExpressionBuilder implements ExpressionBuilderInterface +final class JsonValueBuilder implements ExpressionBuilderInterface { public function __construct(private readonly QueryBuilderInterface $queryBuilder) { @@ -43,20 +38,14 @@ public function __construct(private readonly QueryBuilderInterface $queryBuilder /** * The method builds the raw SQL from the `$expression` that won't be additionally escaped or quoted. * - * @param JsonExpression $expression The expression to build. + * @param JsonValue $expression The expression to build. * @param array $params The binding parameters. * - * @throws Exception - * @throws InvalidArgumentException - * @throws InvalidConfigException - * @throws JsonException - * @throws NotSupportedException - * * @return string The raw SQL that won't be additionally escaped or quoted. */ public function build(ExpressionInterface $expression, array &$params = []): string { - $value = $expression->getValue(); + $value = $expression->value; if ($value === null) { return 'NULL'; diff --git a/src/Expression/Value/Builder/StructuredExpressionBuilder.php b/src/Expression/Value/Builder/StructuredValueBuilder.php similarity index 78% rename from src/Expression/Value/Builder/StructuredExpressionBuilder.php rename to src/Expression/Value/Builder/StructuredValueBuilder.php index 2914232f9..99b8c1fd7 100644 --- a/src/Expression/Value/Builder/StructuredExpressionBuilder.php +++ b/src/Expression/Value/Builder/StructuredValueBuilder.php @@ -6,7 +6,7 @@ use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Constant\DataType; -use Yiisoft\Db\Expression\Value\StructuredExpression; +use Yiisoft\Db\Expression\Value\StructuredValue; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Schema\Data\LazyArray; @@ -19,25 +19,25 @@ use const JSON_THROW_ON_ERROR; /** - * Default expression builder for {@see StructuredExpression}. Builds an expression as a JSON. + * Default expression builder for {@see StructuredValue}. Builds an expression as a JSON. */ -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); } - 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)"; } - 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); $param = new Param(json_encode(array_values($value), JSON_THROW_ON_ERROR), DataType::STRING); diff --git a/src/Expression/Value/JsonExpression.php b/src/Expression/Value/JsonExpression.php deleted file mode 100644 index 398bb6336..000000000 --- a/src/Expression/Value/JsonExpression.php +++ /dev/null @@ -1,57 +0,0 @@ - 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}' - * ``` - */ -final class JsonExpression implements ExpressionInterface -{ - /** - * @param mixed $value The JSON content. It can be represented as - * - an `array` of values; - * - an instance which implements {@see Traversable} or {@see JsonSerializable} and represents an array of values; - * - an instance of {@see QueryInterface} that represents an SQL sub-query; - * - a valid JSON encoded array as a `string`, e.g. `'[1,2,3]'` or `'{"a":1,"b":2}'`; - * - any other value compatible with {@see \json_encode()} input requirements. - * @param string|null $type Type of database column, value should be cast to. Defaults to `null`, meaning no explicit - * casting will be performed. This property is used only for DBMSs that support different types of JSON. - * For example, PostgresSQL has `json` and `jsonb` types. - */ - public function __construct(private readonly mixed $value, private readonly string|null $type = null) - { - } - - /** - * The JSON content. It can be represented as - * - an `array` of values; - * - an instance which implements {@see Traversable} or {@see JsonSerializable} and represents an array of values; - * - an instance of {@see QueryInterface} that represents an SQL sub-query; - * - a valid JSON encoded array as a `string`, e.g. `[1,2,3]` or `'{"a":1,"b":2}'`; - * - any other value compatible with {@see \json_encode()} input requirements. - */ - public function getValue(): mixed - { - return $this->value; - } - - /** - * Type of JSON, expression should be cast to. Defaults to `null`, meaning no explicit casting will be performed. - * This property will be encountered only for DBMSs that support different types of JSON. - * For example, PostgresSQL has `json` and `jsonb` types. - */ - public function getType(): string|null - { - return $this->type; - } -} diff --git a/src/Expression/Value/JsonValue.php b/src/Expression/Value/JsonValue.php new file mode 100644 index 000000000..702a97ac3 --- /dev/null +++ b/src/Expression/Value/JsonValue.php @@ -0,0 +1,34 @@ + 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}' + * ``` + */ +final class JsonValue implements ExpressionInterface +{ + /** + * @param mixed $value The JSON content. It can be represented as + * - an `array` of values; + * - an instance which implements {@see Traversable} or {@see JsonSerializable} and represents an array of values; + * - an instance of {@see QueryInterface} that represents an SQL sub-query; + * - a valid JSON encoded array as a `string`, e.g. `'[1,2,3]'` or `'{"a":1,"b":2}'`; + * - any other value compatible with {@see \json_encode()} input requirements. + * @param string|null $type Type of database column, value should be cast to. Defaults to `null`, meaning no explicit + * casting will be performed. This property is used only for DBMSs that support different types of JSON. + * For example, PostgresSQL has `json` and `jsonb` types. + */ + public function __construct(public readonly mixed $value, public readonly string|null $type = null) + { + } +} diff --git a/src/Expression/Value/StructuredExpression.php b/src/Expression/Value/StructuredValue.php similarity index 51% rename from src/Expression/Value/StructuredExpression.php rename to src/Expression/Value/StructuredValue.php index 8261a6344..d65dce39f 100644 --- a/src/Expression/Value/StructuredExpression.php +++ b/src/Expression/Value/StructuredValue.php @@ -17,12 +17,12 @@ * For example: * * ```php - * new StructuredExpression(['price' => 10, 'currency_code' => 'USD']); + * new StructuredValue(['price' => 10, 'currency_code' => 'USD']); * ``` * * Will be encoded to `ROW(10, USD)` in PostgreSQL. */ -final class StructuredExpression implements ExpressionInterface +final class StructuredValue implements ExpressionInterface { /** * @param array|object|string|null $value The content of the structured type which can be represented as @@ -43,38 +43,8 @@ final class StructuredExpression implements ExpressionInterface * the SQL statement. If the type isn't specified and DBMS can't guess it from the context, SQL error will be raised. */ public function __construct( - private readonly array|object|string|null $value, - private readonly AbstractStructuredColumn|string|null $type = null, + public readonly array|object|string|null $value, + public readonly AbstractStructuredColumn|string|null $type = null, ) { } - - /** - * The structured column type which can be represented as - * - a native database column type suitable to store the {@see value}; - * - an instance of {@see AbstractStructuredColumn}; - * - `null` if the type isn't explicitly specified. - * - * The column type is used to typecast structured values before saving into the database and for adding type hint to - * the SQL statement. If the type isn't specified and DBMS can't guess it from the context, SQL error will be raised. - */ - public function getType(): AbstractStructuredColumn|string|null - { - return $this->type; - } - - /** - * The content of the structured type. It can be represented as - * - an associative `array` of column names and values; - * - an indexed `array` of column values in the order of structured type columns; - * - an {@see JsonSerializable} object that can be converted to an `array` using `jsonSerialize()`; - * - an `iterable` object that can be converted to an `array` using `iterator_to_array()`; - * - an `object` that can be converted to an `array` using `get_object_vars()`; - * - an {@see QueryInterface} object that represents a SQL sub-query; - * - a `string` retrieved value from the database that can be parsed into an array; - * - `null`. - */ - public function getValue(): array|object|string|null - { - return $this->value; - } } diff --git a/src/QueryBuilder/AbstractDQLQueryBuilder.php b/src/QueryBuilder/AbstractDQLQueryBuilder.php index 253964d43..196be34dc 100644 --- a/src/QueryBuilder/AbstractDQLQueryBuilder.php +++ b/src/QueryBuilder/AbstractDQLQueryBuilder.php @@ -10,8 +10,8 @@ use InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Expression\Value\ArrayExpression; -use Yiisoft\Db\Expression\Value\Builder\ArrayExpressionBuilder; +use Yiisoft\Db\Expression\Value\ArrayValue; +use Yiisoft\Db\Expression\Value\Builder\ArrayValueBuilder; use Yiisoft\Db\Expression\Value\ColumnName; use Yiisoft\Db\Expression\Value\Builder\ColumnNameBuilder; use Yiisoft\Db\Expression\Expression; @@ -28,12 +28,12 @@ use Yiisoft\Db\Expression\Function\Length; use Yiisoft\Db\Expression\Function\Longest; use Yiisoft\Db\Expression\Function\Shortest; -use Yiisoft\Db\Expression\Value\JsonExpression; -use Yiisoft\Db\Expression\Value\Builder\JsonExpressionBuilder; +use Yiisoft\Db\Expression\Value\JsonValue; +use Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder; use Yiisoft\Db\Expression\Statement\CaseX; use Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder; -use Yiisoft\Db\Expression\Value\StructuredExpression; -use Yiisoft\Db\Expression\Value\Builder\StructuredExpressionBuilder; +use Yiisoft\Db\Expression\Value\StructuredValue; +use Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder; use Yiisoft\Db\Expression\Value\Value; use Yiisoft\Db\Expression\Value\Builder\ValueBuilder; use Yiisoft\Db\Expression\Value\DateTimeValue; @@ -580,9 +580,9 @@ protected function defaultExpressionBuilders(): array Condition\All::class => Condition\Builder\AllBuilder::class, Condition\None::class => Condition\Builder\NoneBuilder::class, Simple::class => Condition\Builder\SimpleBuilder::class, - JsonExpression::class => JsonExpressionBuilder::class, - ArrayExpression::class => ArrayExpressionBuilder::class, - StructuredExpression::class => StructuredExpressionBuilder::class, + JsonValue::class => JsonValueBuilder::class, + ArrayValue::class => ArrayValueBuilder::class, + StructuredValue::class => StructuredValueBuilder::class, CaseX::class => CaseXBuilder::class, ColumnName::class => ColumnNameBuilder::class, Value::class => ValueBuilder::class, diff --git a/src/QueryBuilder/AbstractQueryBuilder.php b/src/QueryBuilder/AbstractQueryBuilder.php index e99351d83..313478d6c 100644 --- a/src/QueryBuilder/AbstractQueryBuilder.php +++ b/src/QueryBuilder/AbstractQueryBuilder.php @@ -17,9 +17,9 @@ use Yiisoft\Db\Connection\ServerInfoInterface; use Yiisoft\Db\Constant\GettypeResult; use InvalidArgumentException; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\ExpressionInterface; -use Yiisoft\Db\Expression\Value\JsonExpression; +use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\Condition\ConditionInterface; use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; @@ -271,7 +271,7 @@ public function buildValue(mixed $value, array &$params): string /** @psalm-suppress MixedArgument */ return match (gettype($value)) { GettypeResult::ARRAY => $this->buildExpression( - array_is_list($value) ? new ArrayExpression($value) : new JsonExpression($value), + array_is_list($value) ? new ArrayValue($value) : new JsonValue($value), $params, ), GettypeResult::BOOLEAN => $value ? static::TRUE_VALUE : static::FALSE_VALUE, @@ -286,9 +286,9 @@ public function buildValue(mixed $value, array &$params): string $value instanceof BackedEnum => is_string($value->value) ? $this->bindParam(new Param($value->value, DataType::STRING), $params) : (string) $value->value, - $value instanceof Iterator && $value->key() === 0 => $this->buildExpression(new ArrayExpression($value), $params), - $value instanceof Traversable => $this->buildExpression(new JsonExpression($value), $params), - $value instanceof JsonSerializable => $this->buildExpression(new JsonExpression($value), $params), + $value instanceof Iterator && $value->key() === 0 => $this->buildExpression(new ArrayValue($value), $params), + $value instanceof Traversable => $this->buildExpression(new JsonValue($value), $params), + $value instanceof JsonSerializable => $this->buildExpression(new JsonValue($value), $params), default => $this->bindParam($value, $params), }, GettypeResult::RESOURCE => $this->bindParam(new Param($value, DataType::LOB), $params), @@ -462,8 +462,8 @@ public function prepareValue(mixed $value): string GettypeResult::ARRAY => $this->replacePlaceholders( $this->buildExpression( array_is_list($value) - ? new ArrayExpression($value) - : new JsonExpression($value), + ? new ArrayValue($value) + : new JsonValue($value), $params ), array_map($this->prepareValue(...), $params), @@ -483,12 +483,12 @@ public function prepareValue(mixed $value): string ? $this->db->getQuoter()->quoteValue($value->value) : (string) $value->value, $value instanceof Iterator && $value->key() === 0 => $this->replacePlaceholders( - $this->buildExpression(new ArrayExpression($value), $params), + $this->buildExpression(new ArrayValue($value), $params), array_map($this->prepareValue(...), $params), ), $value instanceof Traversable, $value instanceof JsonSerializable => $this->replacePlaceholders( - $this->buildExpression(new JsonExpression($value), $params), + $this->buildExpression(new JsonValue($value), $params), array_map($this->prepareValue(...), $params), ), default => $this->db->getQuoter()->quoteValue((string) $value), diff --git a/src/Schema/Column/AbstractArrayColumn.php b/src/Schema/Column/AbstractArrayColumn.php index 5e6dbc8b6..7abb52019 100644 --- a/src/Schema/Column/AbstractArrayColumn.php +++ b/src/Schema/Column/AbstractArrayColumn.php @@ -5,7 +5,7 @@ namespace Yiisoft\Db\Schema\Column; use Yiisoft\Db\Constant\ColumnType; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\Schema\Data\LazyArrayInterface; @@ -80,6 +80,6 @@ public function dbTypecast(mixed $value): ExpressionInterface|null return $value; } - return new ArrayExpression($value, $this); + return new ArrayValue($value, $this); } } diff --git a/src/Schema/Column/AbstractJsonColumn.php b/src/Schema/Column/AbstractJsonColumn.php index 1c96d067e..09d662e05 100644 --- a/src/Schema/Column/AbstractJsonColumn.php +++ b/src/Schema/Column/AbstractJsonColumn.php @@ -6,7 +6,7 @@ use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Expression\ExpressionInterface; -use Yiisoft\Db\Expression\Value\JsonExpression; +use Yiisoft\Db\Expression\Value\JsonValue; /** * Represents an abstract JSON column. @@ -24,6 +24,6 @@ public function dbTypecast(mixed $value): ExpressionInterface|null return $value; } - return new JsonExpression($value, $this->getDbType()); + return new JsonValue($value, $this->getDbType()); } } diff --git a/src/Schema/Column/AbstractStructuredColumn.php b/src/Schema/Column/AbstractStructuredColumn.php index d9b0b34d6..b67913fc4 100644 --- a/src/Schema/Column/AbstractStructuredColumn.php +++ b/src/Schema/Column/AbstractStructuredColumn.php @@ -6,7 +6,7 @@ use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Expression\ExpressionInterface; -use Yiisoft\Db\Expression\Value\StructuredExpression; +use Yiisoft\Db\Expression\Value\StructuredValue; /** * Represents an abstract structured column. @@ -57,6 +57,6 @@ public function dbTypecast(mixed $value): ExpressionInterface|null return $value; } - return new StructuredExpression($value, $this); + return new StructuredValue($value, $this); } } diff --git a/tests/Db/Expression/Function/MultiOperandFunctionTest.php b/tests/Db/Expression/Function/MultiOperandFunctionTest.php index 689841fb7..65595246d 100644 --- a/tests/Db/Expression/Function/MultiOperandFunctionTest.php +++ b/tests/Db/Expression/Function/MultiOperandFunctionTest.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Db\Constant\DataType; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Function\ArrayMerge; use Yiisoft\Db\Expression\Function\Greatest; use Yiisoft\Db\Expression\Function\Least; @@ -29,9 +29,9 @@ public static function dataOperands(): array return [ ArrayMerge::class => [ArrayMerge::class, [ [[1, 2, 3]], - [new ArrayExpression([1, 2, 3])], + [new ArrayValue([1, 2, 3])], [$query], - [[1, 2, 3], '[1,2,3]', new ArrayExpression([1, 2, 3]), $query], + [[1, 2, 3], '[1,2,3]', new ArrayValue([1, 2, 3]), $query], ]], Greatest::class => [Greatest::class, [ [1], diff --git a/tests/Db/Expression/Value/ArrayExpressionTest.php b/tests/Db/Expression/Value/ArrayValueTest.php similarity index 79% rename from tests/Db/Expression/Value/ArrayExpressionTest.php rename to tests/Db/Expression/Value/ArrayValueTest.php index 5ef271398..b45ef1ee4 100644 --- a/tests/Db/Expression/Value/ArrayExpressionTest.php +++ b/tests/Db/Expression/Value/ArrayValueTest.php @@ -7,7 +7,7 @@ use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\Schema\Column\ArrayColumn; @@ -20,7 +20,7 @@ /** * @group db */ -final class ArrayExpressionTest extends TestCase +final class ArrayValueTest extends TestCase { use TestTrait; @@ -39,9 +39,9 @@ public function testConstruct( iterable|LazyArrayInterface|QueryInterface|string $value, ColumnInterface|string|null $type = null ): void { - $expression = new ArrayExpression($value, $type); + $arrayValue = new ArrayValue($value, $type); - $this->assertSame($value, $expression->getValue()); - $this->assertSame($type, $expression->getType()); + $this->assertSame($value, $arrayValue->value); + $this->assertSame($type, $arrayValue->type); } } diff --git a/tests/Db/Expression/Value/Builder/ArrayExpressionBuilderTest.php b/tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php similarity index 76% rename from tests/Db/Expression/Value/Builder/ArrayExpressionBuilderTest.php rename to tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php index 668343e6f..847d51f36 100644 --- a/tests/Db/Expression/Value/Builder/ArrayExpressionBuilderTest.php +++ b/tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Yiisoft\Db\Tests\Db\Expression\Builder; +namespace Yiisoft\Db\Tests\Db\Expression\Value\Builder; use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Constant\DataType; -use Yiisoft\Db\Expression\Value\ArrayExpression; -use Yiisoft\Db\Expression\Value\Builder\ArrayExpressionBuilder; +use Yiisoft\Db\Expression\Value\ArrayValue; +use Yiisoft\Db\Expression\Value\Builder\ArrayValueBuilder; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\LazyArray; use Yiisoft\Db\Schema\Data\LazyArrayInterface; @@ -20,7 +20,7 @@ /** * @group db */ -final class ArrayExpressionBuilderTest extends TestCase +final class ArrayValueBuilderTest extends TestCase { use TestTrait; @@ -44,8 +44,8 @@ public function testBuild(iterable|LazyArrayInterface|string $value, string $exp $qb = $db->getQueryBuilder(); $params = []; - $builder = new ArrayExpressionBuilder($qb); - $expression = new ArrayExpression($value); + $builder = new ArrayValueBuilder($qb); + $expression = new ArrayValue($value); $this->assertSame(':qp0', $builder->build($expression, $params)); $this->assertEquals([':qp0' => new Param($expected, DataType::STRING)], $params); @@ -57,8 +57,8 @@ public function testBuildNull(): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new ArrayExpressionBuilder($qb); - $expression = new ArrayExpression(null); + $builder = new ArrayValueBuilder($qb); + $expression = new ArrayValue(null); $this->assertSame('NULL', $builder->build($expression, $params)); $this->assertSame([], $params); @@ -70,8 +70,8 @@ public function testBuildQueryExpression(): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new ArrayExpressionBuilder($qb); - $expression = new ArrayExpression((new Query($db))->select('json_field')->from('json_table')); + $builder = new ArrayValueBuilder($qb); + $expression = new ArrayValue((new Query($db))->select('json_field')->from('json_table')); $this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params)); $this->assertSame([], $params); diff --git a/tests/Db/Expression/Value/Builder/JsonExpressionBuilderTest.php b/tests/Db/Expression/Value/Builder/JsonValueBuilderTest.php similarity index 77% rename from tests/Db/Expression/Value/Builder/JsonExpressionBuilderTest.php rename to tests/Db/Expression/Value/Builder/JsonValueBuilderTest.php index f3477abfc..84ec46dbe 100644 --- a/tests/Db/Expression/Value/Builder/JsonExpressionBuilderTest.php +++ b/tests/Db/Expression/Value/Builder/JsonValueBuilderTest.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Yiisoft\Db\Tests\Db\Expression\Builder; +namespace Yiisoft\Db\Tests\Db\Expression\Value\Builder; use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Constant\DataType; -use Yiisoft\Db\Expression\Value\JsonExpression; -use Yiisoft\Db\Expression\Value\Builder\JsonExpressionBuilder; +use Yiisoft\Db\Expression\Value\JsonValue; +use Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\LazyArray; use Yiisoft\Db\Schema\Data\JsonLazyArray; @@ -21,7 +21,7 @@ /** * @group db */ -final class JsonExpressionBuilderTest extends TestCase +final class JsonValueBuilderTest extends TestCase { use TestTrait; @@ -41,7 +41,7 @@ public static function buildProvider(): array [new LazyArray('[1,2,3]'), '[1,2,3]'], [new JsonLazyArray('[1,2,3]'), '[1,2,3]'], [new StructuredLazyArray('["5","USD"]'), '["5","USD"]'], - [new JsonExpression(['a' => 1, 'b' => 2, 'd' => ['e' => 3]]), '{"a":1,"b":2,"d":{"e":3}}'], + [new JsonValue(['a' => 1, 'b' => 2, 'd' => ['e' => 3]]), '{"a":1,"b":2,"d":{"e":3}}'], [['a' => 1, 'b' => null, 'c' => ['d' => 'e']], '{"a":1,"b":null,"c":{"d":"e"}}'], ['[1,2,3]', '[1,2,3]'], ['{"a":1,"b":null,"c":{"d":"e"}}', '{"a":1,"b":null,"c":{"d":"e"}}'], @@ -55,8 +55,8 @@ public function testBuild(mixed $value, string $expected): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new JsonExpressionBuilder($qb); - $expression = new JsonExpression($value); + $builder = new JsonValueBuilder($qb); + $expression = new JsonValue($value); $this->assertSame(':qp0', $builder->build($expression, $params)); $this->assertEquals([':qp0' => new Param($expected, DataType::STRING)], $params); @@ -68,8 +68,8 @@ public function testBuildNull(): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new JsonExpressionBuilder($qb); - $expression = new JsonExpression(null); + $builder = new JsonValueBuilder($qb); + $expression = new JsonValue(null); $this->assertSame('NULL', $builder->build($expression, $params)); $this->assertSame([], $params); @@ -81,8 +81,8 @@ public function testBuildQueryExpression(): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new JsonExpressionBuilder($qb); - $expression = new JsonExpression((new Query($db))->select('json_field')->from('json_table')); + $builder = new JsonValueBuilder($qb); + $expression = new JsonValue((new Query($db))->select('json_field')->from('json_table')); $this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params)); $this->assertSame([], $params); diff --git a/tests/Db/Expression/Value/Builder/ParamBuilderTest.php b/tests/Db/Expression/Value/Builder/ParamBuilderTest.php index cf2244ff9..51033ed1d 100644 --- a/tests/Db/Expression/Value/Builder/ParamBuilderTest.php +++ b/tests/Db/Expression/Value/Builder/ParamBuilderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Db\Tests\Db\Expression\Builder; +namespace Yiisoft\Db\Tests\Db\Expression\Value\Builder; use PHPUnit\Framework\TestCase; use Yiisoft\Db\Expression\Value\Builder\ParamBuilder; diff --git a/tests/Db/Expression/Value/Builder/StructuredExpressionBuilderTest.php b/tests/Db/Expression/Value/Builder/StructuredValueBuilderTest.php similarity index 80% rename from tests/Db/Expression/Value/Builder/StructuredExpressionBuilderTest.php rename to tests/Db/Expression/Value/Builder/StructuredValueBuilderTest.php index d110d3397..d7ebb4c40 100644 --- a/tests/Db/Expression/Value/Builder/StructuredExpressionBuilderTest.php +++ b/tests/Db/Expression/Value/Builder/StructuredValueBuilderTest.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Yiisoft\Db\Tests\Db\Expression\Builder; +namespace Yiisoft\Db\Tests\Db\Expression\Value\Builder; use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Constant\DataType; -use Yiisoft\Db\Expression\Value\StructuredExpression; -use Yiisoft\Db\Expression\Value\Builder\StructuredExpressionBuilder; +use Yiisoft\Db\Expression\Value\StructuredValue; +use Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; use Yiisoft\Db\Schema\Column\ColumnBuilder; @@ -21,7 +21,7 @@ /** * @group db */ -final class StructuredExpressionBuilderTest extends TestCase +final class StructuredValueBuilderTest extends TestCase { use TestTrait; @@ -54,8 +54,8 @@ public function testBuild(array|object|string $value, AbstractStructuredColumn|s $qb = $db->getQueryBuilder(); $params = []; - $builder = new StructuredExpressionBuilder($qb); - $expression = new StructuredExpression($value, $type); + $builder = new StructuredValueBuilder($qb); + $expression = new StructuredValue($value, $type); $this->assertSame(':qp0', $builder->build($expression, $params)); $this->assertEquals([':qp0' => new Param($expected, DataType::STRING)], $params); @@ -67,8 +67,8 @@ public function testBuildNull(): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new StructuredExpressionBuilder($qb); - $expression = new StructuredExpression(null); + $builder = new StructuredValueBuilder($qb); + $expression = new StructuredValue(null); $this->assertSame('NULL', $builder->build($expression, $params)); $this->assertSame([], $params); @@ -80,8 +80,8 @@ public function testBuildQueryExpression(): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new StructuredExpressionBuilder($qb); - $expression = new StructuredExpression((new Query($db))->select('json_field')->from('json_table')); + $builder = new StructuredValueBuilder($qb); + $expression = new StructuredValue((new Query($db))->select('json_field')->from('json_table')); $this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params)); $this->assertSame([], $params); diff --git a/tests/Db/Expression/Value/JsonExpressionTest.php b/tests/Db/Expression/Value/JsonValueTest.php similarity index 77% rename from tests/Db/Expression/Value/JsonExpressionTest.php rename to tests/Db/Expression/Value/JsonValueTest.php index b93ce6e85..4bfe6d475 100644 --- a/tests/Db/Expression/Value/JsonExpressionTest.php +++ b/tests/Db/Expression/Value/JsonValueTest.php @@ -7,7 +7,7 @@ use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Expression\Value\JsonExpression; +use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Data\JsonLazyArray; use Yiisoft\Db\Tests\Support\TestTrait; @@ -15,7 +15,7 @@ /** * @group db */ -final class JsonExpressionTest extends TestCase +final class JsonValueTest extends TestCase { use TestTrait; @@ -39,9 +39,9 @@ public function testConstruct( mixed $value, string|null $type = null ): void { - $expression = new JsonExpression($value, $type); + $expression = new JsonValue($value, $type); - $this->assertSame($value, $expression->getValue()); - $this->assertSame($type, $expression->getType()); + $this->assertSame($value, $expression->value); + $this->assertSame($type, $expression->type); } } diff --git a/tests/Db/Expression/Value/StructuredExpressionTest.php b/tests/Db/Expression/Value/StructuredValueTest.php similarity index 81% rename from tests/Db/Expression/Value/StructuredExpressionTest.php rename to tests/Db/Expression/Value/StructuredValueTest.php index 97adee58e..5dbf723cf 100644 --- a/tests/Db/Expression/Value/StructuredExpressionTest.php +++ b/tests/Db/Expression/Value/StructuredValueTest.php @@ -7,7 +7,7 @@ use ArrayIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Expression\Value\StructuredExpression; +use Yiisoft\Db\Expression\Value\StructuredValue; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; use Yiisoft\Db\Schema\Column\ColumnBuilder; @@ -17,7 +17,7 @@ /** * @group db */ -final class StructuredExpressionTest extends TestCase +final class StructuredValueTest extends TestCase { use TestTrait; @@ -43,9 +43,9 @@ public function testConstruct( array|object|string $value, AbstractStructuredColumn|string|null $type = null ): void { - $expression = new StructuredExpression($value, $type); + $expression = new StructuredValue($value, $type); - $this->assertSame($value, $expression->getValue()); - $this->assertSame($type, $expression->getType()); + $this->assertSame($value, $expression->value); + $this->assertSame($type, $expression->type); } } diff --git a/tests/Db/Schema/Column/ColumnTest.php b/tests/Db/Schema/Column/ColumnTest.php index 3272a2ab9..2f5ab148f 100644 --- a/tests/Db/Schema/Column/ColumnTest.php +++ b/tests/Db/Schema/Column/ColumnTest.php @@ -5,7 +5,7 @@ namespace Yiisoft\Db\Tests\Db\Schema\Column; use Yiisoft\Db\Constraint\ForeignKey; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Schema\Column\ArrayColumn; use Yiisoft\Db\Schema\Column\CollatableColumnInterface; use Yiisoft\Db\Schema\Column\ColumnBuilder; @@ -337,9 +337,9 @@ public function testArrayColumnDbTypecast(ColumnInterface $column, array $values $arrayCol->dimension($dimension); $dbValue = $arrayCol->dbTypecast($value); - $this->assertInstanceOf(ArrayExpression::class, $dbValue); - $this->assertSame($arrayCol, $dbValue->getType()); - $this->assertEquals($value, $dbValue->getValue()); + $this->assertInstanceOf(ArrayValue::class, $dbValue); + $this->assertSame($arrayCol, $dbValue->type); + $this->assertEquals($value, $dbValue->value); } } diff --git a/tests/Provider/ColumnProvider.php b/tests/Provider/ColumnProvider.php index 22052c38b..98a985df5 100644 --- a/tests/Provider/ColumnProvider.php +++ b/tests/Provider/ColumnProvider.php @@ -13,10 +13,10 @@ use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constraint\ForeignKey; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Expression; -use Yiisoft\Db\Expression\Value\JsonExpression; -use Yiisoft\Db\Expression\Value\StructuredExpression; +use Yiisoft\Db\Expression\Value\JsonValue; +use Yiisoft\Db\Expression\Value\StructuredValue; use Yiisoft\Db\Schema\Column\ArrayColumn; use Yiisoft\Db\Schema\Column\ArrayLazyColumn; use Yiisoft\Db\Schema\Column\BigIntColumn; @@ -436,16 +436,16 @@ public static function dbTypecastColumns(): array new JsonColumn(), [ [null, null], - [new JsonExpression(''), ''], - [new JsonExpression(1), 1], - [new JsonExpression(true), true], - [new JsonExpression(false), false], - [new JsonExpression('string'), 'string'], - [new JsonExpression([1, 2, 3]), [1, 2, 3]], - [new JsonExpression(['key' => 'value']), ['key' => 'value']], - [new JsonExpression(['a' => 1]), ['a' => 1]], - [new JsonExpression(new stdClass()), new stdClass()], - [$expression = new JsonExpression([1, 2, 3]), $expression], + [new JsonValue(''), ''], + [new JsonValue(1), 1], + [new JsonValue(true), true], + [new JsonValue(false), false], + [new JsonValue('string'), 'string'], + [new JsonValue([1, 2, 3]), [1, 2, 3]], + [new JsonValue(['key' => 'value']), ['key' => 'value']], + [new JsonValue(['a' => 1]), ['a' => 1]], + [new JsonValue(new stdClass()), new stdClass()], + [$expression = new JsonValue([1, 2, 3]), $expression], [$expression = new Expression('expression'), $expression], ], ], @@ -453,10 +453,10 @@ public static function dbTypecastColumns(): array $arrayCol = new ArrayColumn(), [ [null, null], - [new ArrayExpression([], $arrayCol), []], - [new ArrayExpression([1, 2, 3], $arrayCol), [1, 2, 3]], - [new ArrayExpression($iterator = new ArrayIterator([1, 2, 3]), $arrayCol), $iterator], - [new ArrayExpression('[1,2,3]', $arrayCol), '[1,2,3]'], + [new ArrayValue([], $arrayCol), []], + [new ArrayValue([1, 2, 3], $arrayCol), [1, 2, 3]], + [new ArrayValue($iterator = new ArrayIterator([1, 2, 3]), $arrayCol), $iterator], + [new ArrayValue('[1,2,3]', $arrayCol), '[1,2,3]'], [$expression = new Expression('expression'), $expression], ], ], @@ -464,10 +464,10 @@ public static function dbTypecastColumns(): array $structuredCol = new StructuredColumn(), [ [null, null], - [new StructuredExpression([], $structuredCol), []], - [new StructuredExpression(['value' => 1, 'currency_code' => 'USD'], $structuredCol), ['value' => 1, 'currency_code' => 'USD']], - [new StructuredExpression($iterator = new ArrayIterator(['value' => 1, 'currency_code' => 'USD']), $structuredCol), $iterator], - [new StructuredExpression('[1,"USD"]', $structuredCol), '[1,"USD"]'], + [new StructuredValue([], $structuredCol), []], + [new StructuredValue(['value' => 1, 'currency_code' => 'USD'], $structuredCol), ['value' => 1, 'currency_code' => 'USD']], + [new StructuredValue($iterator = new ArrayIterator(['value' => 1, 'currency_code' => 'USD']), $structuredCol), $iterator], + [new StructuredValue('[1,"USD"]', $structuredCol), '[1,"USD"]'], [$expression = new Expression('expression'), $expression], ], ], @@ -782,20 +782,20 @@ public static function dbTypecastArrayColumns() new JsonColumn(), [ [1, [ - new JsonExpression([1, 2, 3]), - new JsonExpression(['key' => 'value']), - new JsonExpression(['key' => 'value']), + new JsonValue([1, 2, 3]), + new JsonValue(['key' => 'value']), + new JsonValue(['key' => 'value']), null, - ], [[1, 2, 3], ['key' => 'value'], new JsonExpression(['key' => 'value']), null]], + ], [[1, 2, 3], ['key' => 'value'], new JsonValue(['key' => 'value']), null]], [2, [ [ - new JsonExpression([1, 2, 3]), - new JsonExpression(['key' => 'value']), - new JsonExpression(['key' => 'value']), + new JsonValue([1, 2, 3]), + new JsonValue(['key' => 'value']), + new JsonValue(['key' => 'value']), null, ], null, - ], [[[1, 2, 3], ['key' => 'value'], new JsonExpression(['key' => 'value']), null], null]], + ], [[[1, 2, 3], ['key' => 'value'], new JsonValue(['key' => 'value']), null], null]], ], ], ColumnType::STRUCTURED => [ @@ -804,7 +804,7 @@ public static function dbTypecastArrayColumns() [ 1, [ - new StructuredExpression(['value' => 10, 'currency' => 'USD'], 'structured_type'), + new StructuredValue(['value' => 10, 'currency' => 'USD'], 'structured_type'), null, ], [ @@ -815,7 +815,7 @@ public static function dbTypecastArrayColumns() [ 2, [[ - new StructuredExpression(['value' => 10, 'currency' => 'USD'], 'structured_type'), + new StructuredValue(['value' => 10, 'currency' => 'USD'], 'structured_type'), null, ]], [[ diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 85eb00ec0..3b3c79c75 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -13,7 +13,7 @@ use Yiisoft\Db\Constant\IndexType; use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Expression\Expression; -use Yiisoft\Db\Expression\Value\JsonExpression; +use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnBuilder; use Yiisoft\Db\Tests\Support\Stringable; @@ -309,7 +309,7 @@ public static function batchInsert(): array '{{%type}}', /** * This example is completely useless. This feature of batchInsert is intended to be used with complex - * expression objects, such as JsonExpression. + * expression objects, such as JsonValue. */ 'values' => [[new Expression(':exp1', [':exp1' => 42]), 1, 'test', false]], ['int_col', 'float_col', 'char_col', 'bool_col'], @@ -449,7 +449,7 @@ public function getIterator(): Traversable '{{%type}}', [ [1, 'a', 0.0, true, ['a' => 1, 'b' => true, 'c' => [1, 2, 3]]], - [2, 'b', -1.0, false, new JsonExpression(['d' => 'e', 'f' => false, 'g' => [4, 5, null]])], + [2, 'b', -1.0, false, new JsonValue(['d' => 'e', 'f' => false, 'g' => [4, 5, null]])], ], ['int_col', 'char_col', 'float_col', 'bool_col', 'json_col'], 'expected' => static::replaceQuotes( diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 4d660e77d..02d90523a 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -9,7 +9,7 @@ use DateTimeZone; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Statement\When; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Function\ArrayMerge; use Yiisoft\Db\Expression\Value\Param; use Yiisoft\Db\Constant\ColumnType; @@ -24,7 +24,7 @@ use Yiisoft\Db\Expression\Function\Least; use Yiisoft\Db\Expression\Function\Longest; use Yiisoft\Db\Expression\Function\Shortest; -use Yiisoft\Db\Expression\Value\JsonExpression; +use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Expression\Value\Value; use Yiisoft\Db\Expression\Value\DateTimeValue; use Yiisoft\Db\Query\Query; @@ -1162,7 +1162,7 @@ public static function insert(): array 'json expression' => [ 'json_type', [ - 'json_col' => new JsonExpression(['c' => 1, 'd' => 2]), + 'json_col' => new JsonValue(['c' => 1, 'd' => 2]), ], [], static::replaceQuotes( @@ -1621,8 +1621,8 @@ public static function overlapsCondition(): array [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], + 'json expression' => [new JsonValue([0, 1, 2, 7]), 1], + 'query expression' => [(new Query(static::getDb()))->select(new JsonValue([0, 1, 2, 7])), 1], ]; } @@ -2082,7 +2082,7 @@ public static function upsertWithMultiOperandFunctions(): array return [[ [ 'id' => 1, - 'array_col' => new ArrayExpression([1, 2, 3]), + 'array_col' => new ArrayValue([1, 2, 3]), 'greatest_col' => 10, 'least_col' => 10, 'longest_col' => 'longest', @@ -2090,7 +2090,7 @@ public static function upsertWithMultiOperandFunctions(): array ], [ 'id' => 1, - 'array_col' => new ArrayExpression([3, 4, 5]), + 'array_col' => new ArrayValue([3, 4, 5]), 'greatest_col' => 5, 'least_col' => 5, 'longest_col' => 'short', From 6447791b6ccb8a1ae3773c30d2b1a39c60dc7d14 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 1 Sep 2025 13:29:15 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- .../Value/Builder/AbstractArrayValueBuilder.php | 4 ++-- .../Value/Builder/AbstractStructuredValueBuilder.php | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Expression/Value/Builder/AbstractArrayValueBuilder.php b/src/Expression/Value/Builder/AbstractArrayValueBuilder.php index 05298dfcc..15b9038b3 100644 --- a/src/Expression/Value/Builder/AbstractArrayValueBuilder.php +++ b/src/Expression/Value/Builder/AbstractArrayValueBuilder.php @@ -42,8 +42,8 @@ abstract protected function buildStringValue(string $value, ArrayValue $expressi */ abstract protected function buildSubquery( QueryInterface $query, - ArrayValue $expression, - array &$params + ArrayValue $expression, + array &$params ): string; /** diff --git a/src/Expression/Value/Builder/AbstractStructuredValueBuilder.php b/src/Expression/Value/Builder/AbstractStructuredValueBuilder.php index aeb2d77af..bbf518795 100644 --- a/src/Expression/Value/Builder/AbstractStructuredValueBuilder.php +++ b/src/Expression/Value/Builder/AbstractStructuredValueBuilder.php @@ -4,10 +4,6 @@ namespace Yiisoft\Db\Expression\Value\Builder; -use Yiisoft\Db\Exception\Exception; -use InvalidArgumentException; -use Yiisoft\Db\Exception\InvalidConfigException; -use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\ExpressionBuilderInterface; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Expression\Value\StructuredValue; @@ -68,9 +64,9 @@ abstract protected function buildSubquery( * @return string The SQL expression representing the structured value. */ abstract protected function buildValue( - array|object $value, + array|object $value, StructuredValue $expression, - array &$params + array &$params ): string; /** From 329f1468739bc9f7ce1eb3053b676fb1ab0edf31 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 2 Sep 2025 08:23:34 +0700 Subject: [PATCH 3/3] Add lines to CHANGELOG.md and UPGRADE.md [skip ci] --- CHANGELOG.md | 2 ++ UPGRADE.md | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66ec80bad..08408ae4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -144,6 +144,8 @@ - Enh #1049: Refactor `AbstractDQLQueryBuilder::buildSelect()` to fix possible bugs (@Tigrov) - Enh #1051: Refactor `Quoter` class (@Tigrov) - Chg #1052: Rearrange expression namespaces (@Tigrov) +- Chg #1054: Rename `ArrayExpression` to `ArrayValue`, `JsonExpression` to `JsonValue`, + `StructuredExpression` to `StructuredValue` (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/UPGRADE.md b/UPGRADE.md index 5e65fbcb8..a8a67e5ce 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -272,4 +272,11 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace - Remove `BetweenColumns` condition; - Change `QueryBuilderInterface::getExpressionBuilder()` result type to `ExpressionBuilderInterface`; - Change `DQLQueryBuilderInterface::getExpressionBuilder()` result type to `ExpressionBuilderInterface`; +- Rename `ArrayExpression` to `ArrayValue` and move it to `Yiisoft\Db\Expression\Value` namespace; +- Rename `ArrayExpressionBuilder` to `ArrayValueBuilder` and move it to `Yiisoft\Db\Expression\Value\Builder` namespace; +- Rename `JsonExpression` to `JsonValue` and move it to `Yiisoft\Db\Expression\Value` namespace; +- Rename `JsonExpressionBuilder` to `JsonValueBuilder` and move it to `Yiisoft\Db\Expression\Value\Builder` namespace; +- Rename `StructuredExpression` to `StructuredValue` and move it to `Yiisoft\Db\Expression\Value` namespace;` +- Rename `StructuredExpressionBuilder` to `StructuredValueBuilder` and move it to `Yiisoft\Db\Expression\Value\Builder` + namespace;