diff --git a/CHANGELOG.md b/CHANGELOG.md index 933c7aad2..850972ef9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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) diff --git a/src/Builder/ArrayOverlapsBuilder.php b/src/Builder/ArrayOverlapsBuilder.php index 51a88c7de..443bc377f 100644 --- a/src/Builder/ArrayOverlapsBuilder.php +++ b/src/Builder/ArrayOverlapsBuilder.php @@ -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; @@ -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); diff --git a/src/Builder/ArrayExpressionBuilder.php b/src/Builder/ArrayValueBuilder.php similarity index 90% rename from src/Builder/ArrayExpressionBuilder.php rename to src/Builder/ArrayValueBuilder.php index 57147a7d1..926d35190 100644 --- a/src/Builder/ArrayExpressionBuilder.php +++ b/src/Builder/ArrayValueBuilder.php @@ -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; @@ -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); @@ -41,7 +41,7 @@ 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); @@ -49,7 +49,7 @@ protected function buildSubquery(QueryInterface $query, ArrayExpression $express 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); @@ -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; diff --git a/src/Builder/JsonExpressionBuilder.php b/src/Builder/JsonExpressionBuilder.php deleted file mode 100644 index 6dc14923e..000000000 --- a/src/Builder/JsonExpressionBuilder.php +++ /dev/null @@ -1,71 +0,0 @@ - - */ -final class JsonExpressionBuilder implements ExpressionBuilderInterface -{ - private BaseJsonExpressionBuilder $baseExpressionBuilder; - - public function __construct(QueryBuilderInterface $queryBuilder) - { - $this->baseExpressionBuilder = new BaseJsonExpressionBuilder($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 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 - { - $statement = $this->baseExpressionBuilder->build($expression, $params); - - if ($expression->getValue() instanceof ArrayExpression) { - $statement = 'array_to_json(' . $statement . ')'; - } - - return $statement . $this->getTypeHint($expression); - } - - /** - * @return string The typecast expression based on {@see JsonExpression::getType()}. - */ - private function getTypeHint(JsonExpression $expression): string - { - $type = $expression->getType(); - - if ($type === null) { - return ''; - } - - return '::' . $type; - } -} diff --git a/src/Builder/JsonOverlapsBuilder.php b/src/Builder/JsonOverlapsBuilder.php index f69b8d902..f9f0d3346 100644 --- a/src/Builder/JsonOverlapsBuilder.php +++ b/src/Builder/JsonOverlapsBuilder.php @@ -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; @@ -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); diff --git a/src/Builder/JsonValueBuilder.php b/src/Builder/JsonValueBuilder.php new file mode 100644 index 000000000..af8966565 --- /dev/null +++ b/src/Builder/JsonValueBuilder.php @@ -0,0 +1,60 @@ + + */ +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; + } +} diff --git a/src/Builder/StructuredExpressionBuilder.php b/src/Builder/StructuredValueBuilder.php similarity index 69% rename from src/Builder/StructuredExpressionBuilder.php rename to src/Builder/StructuredValueBuilder.php index 8e85119ef..134e38824 100644 --- a/src/Builder/StructuredExpressionBuilder.php +++ b/src/Builder/StructuredValueBuilder.php @@ -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; @@ -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 */ @@ -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() @@ -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(); diff --git a/src/DQLQueryBuilder.php b/src/DQLQueryBuilder.php index cf646eef5..dce9aeccf 100644 --- a/src/DQLQueryBuilder.php +++ b/src/DQLQueryBuilder.php @@ -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; @@ -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, diff --git a/tests/ArrayExpressionBuilderTest.php b/tests/ArrayValueBuilderTest.php similarity index 87% rename from tests/ArrayExpressionBuilderTest.php rename to tests/ArrayValueBuilderTest.php index 33cd6ecff..e0c992e85 100644 --- a/tests/ArrayExpressionBuilderTest.php +++ b/tests/ArrayValueBuilderTest.php @@ -9,12 +9,12 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\DataType; -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\JsonValue; use Yiisoft\Db\Expression\Value\Param; -use Yiisoft\Db\Expression\Value\StructuredExpression; -use Yiisoft\Db\Pgsql\Builder\ArrayExpressionBuilder; +use Yiisoft\Db\Expression\Value\StructuredValue; +use Yiisoft\Db\Pgsql\Builder\ArrayValueBuilder; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; @@ -28,7 +28,7 @@ /** * @group pgsql */ -final class ArrayExpressionBuilderTest extends TestCase +final class ArrayValueBuilderTest extends TestCase { use TestTrait; @@ -99,8 +99,8 @@ public static function buildProvider(): array 'ARRAY[1,2,3]::integer[]', ], 'Expression' => [[new Expression('now()')], null, 'ARRAY[now()]'], - 'JsonExpression w/o type' => [ - [new JsonExpression(['a' => null, 'b' => 123, 'c' => [4, 5]]), new JsonExpression([true])], + 'JsonValue w/o type' => [ + [new JsonValue(['a' => null, 'b' => 123, 'c' => [4, 5]]), new JsonValue([true])], null, 'ARRAY[:qp0,:qp1]', [ @@ -108,8 +108,8 @@ public static function buildProvider(): array ':qp1' => new Param('[true]', DataType::STRING), ], ], - 'JsonExpression' => [ - [new JsonExpression(['a' => null, 'b' => 123, 'c' => [4, 5]]), new JsonExpression([true])], + 'JsonValue' => [ + [new JsonValue(['a' => null, 'b' => 123, 'c' => [4, 5]]), new JsonValue([true])], 'jsonb', 'ARRAY[:qp0,:qp1]::jsonb[]', [ @@ -117,11 +117,11 @@ public static function buildProvider(): array ':qp1' => new Param('[true]', DataType::STRING), ], ], - 'StructuredExpression' => [ + 'StructuredValue' => [ [ null, - new StructuredExpression(['value' => 11.11, 'currency_code' => 'USD']), - new StructuredExpression(['value' => null, 'currency_code' => null]), + new StructuredValue(['value' => 11.11, 'currency_code' => 'USD']), + new StructuredValue(['value' => null, 'currency_code' => null]), ], null, 'ARRAY[NULL,ROW(11.11,:qp0),ROW(NULL,NULL)]', @@ -173,8 +173,8 @@ public function testBuild( $qb = $db->getQueryBuilder(); $params = []; - $builder = new ArrayExpressionBuilder($qb); - $expression = new ArrayExpression($value, $type); + $builder = new ArrayValueBuilder($qb); + $expression = new ArrayValue($value, $type); $this->assertSame($expected, $builder->build($expression, $params)); Assert::arraysEquals($expectedParams, $params); diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index b86c31c68..fff266486 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -9,9 +9,9 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Constant\ColumnType; -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\JsonValue; use Yiisoft\Db\Pgsql\Column\ArrayColumn; use Yiisoft\Db\Pgsql\Column\BigIntColumn; use Yiisoft\Db\Pgsql\Column\BinaryColumn; @@ -59,10 +59,10 @@ protected function insertTypeValues(ConnectionInterface $db): void 'intarray_col' => [1, -2, null, '42'], 'numericarray_col' => [null, 1.2, -2.2, null, null], 'varchararray_col' => ['', 'some text', '""', '\\\\', '[",","null",true,"false","f"]', null], - 'textarray2_col' => new ArrayExpression(null), + 'textarray2_col' => new ArrayValue(null), 'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], - 'jsonb_col' => new JsonExpression(new ArrayExpression([1, 2, 3])), - 'jsonarray_col' => [new ArrayExpression([[',', 'null', true, 'false', 'f']], ColumnType::JSON)], + 'jsonb_col' => new JsonValue(new ArrayValue([1, 2, 3])), + 'jsonarray_col' => [new ArrayValue([[',', 'null', true, 'false', 'f']], ColumnType::JSON)], ] )->execute(); } @@ -160,8 +160,8 @@ public function testDbTypeCastJson(): void $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('type'); - $this->assertEquals(new JsonExpression('', 'json'), $tableSchema->getColumn('json_col')->dbTypecast('')); - $this->assertEquals(new JsonExpression('', 'jsonb'), $tableSchema->getColumn('jsonb_col')->dbTypecast('')); + $this->assertEquals(new JsonValue('', 'json'), $tableSchema->getColumn('json_col')->dbTypecast('')); + $this->assertEquals(new JsonValue('', 'jsonb'), $tableSchema->getColumn('jsonb_col')->dbTypecast('')); $db->close(); } @@ -293,7 +293,7 @@ public function testStructuredType(): void $priceArray2 = $tableSchema->getColumn('price_array2'); $this->assertEquals( - new ArrayExpression( + new ArrayValue( [null, null], new ArrayColumn( dbType: 'currency_money_structured', @@ -365,9 +365,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/JsonExpressionBuilderTest.php b/tests/JsonValueBuilderTest.php similarity index 76% rename from tests/JsonExpressionBuilderTest.php rename to tests/JsonValueBuilderTest.php index e98132ccc..b54c6569b 100644 --- a/tests/JsonExpressionBuilderTest.php +++ b/tests/JsonValueBuilderTest.php @@ -8,10 +8,10 @@ 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\JsonExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; +use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Expression\Value\Param; -use Yiisoft\Db\Pgsql\Builder\JsonExpressionBuilder; +use Yiisoft\Db\Pgsql\Builder\JsonValueBuilder; use Yiisoft\Db\Pgsql\Column\IntegerColumn; use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; @@ -22,7 +22,7 @@ /** * @group pgsql */ -final class JsonExpressionBuilderTest extends TestCase +final class JsonValueBuilderTest extends TestCase { use TestTrait; @@ -54,27 +54,27 @@ 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); } - public function testBuildArrayExpression(): void + public function testBuildArrayValue(): void { $db = $this->getConnection(); $qb = $db->getQueryBuilder(); $params = []; - $builder = new JsonExpressionBuilder($qb); - $expression = new JsonExpression(new ArrayExpression([1,2,3])); + $builder = new JsonValueBuilder($qb); + $expression = new JsonValue(new ArrayValue([1,2,3])); $this->assertSame('array_to_json(ARRAY[1,2,3]::int[])', $builder->build($expression, $params)); $this->assertSame([], $params); $params = []; - $expression = new JsonExpression(new ArrayExpression([1,2,3]), 'jsonb'); + $expression = new JsonValue(new ArrayValue([1,2,3]), 'jsonb'); $this->assertSame('array_to_json(ARRAY[1,2,3]::int[])::jsonb', $builder->build($expression, $params)); $this->assertSame([], $params); @@ -86,8 +86,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); @@ -99,13 +99,13 @@ 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); - $expression = new JsonExpression((new Query($db))->select('json_field')->from('json_table'), 'jsonb'); + $expression = new JsonValue((new Query($db))->select('json_field')->from('json_table'), 'jsonb'); $this->assertSame('(SELECT "json_field" FROM "json_table")::jsonb', $builder->build($expression, $params)); $this->assertSame([], $params); @@ -117,8 +117,8 @@ public function testBuildWithType(): void $qb = $db->getQueryBuilder(); $params = []; - $builder = new JsonExpressionBuilder($qb); - $expression = new JsonExpression([1, 2, 3], 'jsonb'); + $builder = new JsonValueBuilder($qb); + $expression = new JsonValue([1, 2, 3], 'jsonb'); $this->assertSame(':qp0::jsonb', $builder->build($expression, $params)); $this->assertEquals([':qp0' => new Param('[1,2,3]', DataType::STRING)], $params); diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index a590ace1b..65c32876d 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -4,8 +4,8 @@ namespace Yiisoft\Db\Pgsql\Tests\Provider; -use Yiisoft\Db\Expression\Value\ArrayExpression; -use Yiisoft\Db\Expression\Value\JsonExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; +use Yiisoft\Db\Expression\Value\JsonValue; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\IndexMethod; use Yiisoft\Db\Pgsql\Tests\Support\TestTrait; @@ -24,11 +24,11 @@ public static function batchInsert(): array 'INSERT INTO "type" ("int_col", "char_col", "float_col", "bool_col", "json_col")' . ' VALUES (1, :qp0, 0, TRUE, :qp1::json), (2, :qp2, -1, FALSE, :qp3)'; - $batchInsert['binds params from jsonExpression'] = [ + $batchInsert['binds params from jsonValue'] = [ '{{%type}}', [ [ - new JsonExpression( + new JsonValue( ['username' => 'silverfire', 'is_active' => true, 'langs' => ['Ukrainian', 'Russian', 'English']] ), 1, @@ -47,9 +47,9 @@ public static function batchInsert(): array ], ]; - $batchInsert['binds params from arrayExpression'] = [ + $batchInsert['binds params from arrayValue'] = [ '{{%type}}', - [[new ArrayExpression([1, null, 3], 'int'), 1, 1.0, '', false]], + [[new ArrayValue([1, null, 3], 'int'), 1, 1.0, '', false]], ['intarray_col', 'int_col', 'float_col', 'char_col', 'bool_col'], 'expected' => << [':qp0' => ''], ]; - $batchInsert['binds params from jsonbExpression'] = [ + $batchInsert['binds params from jsonbValue'] = [ '{{%type}}', - [[new JsonExpression(['a' => true]), 1, 1.1, '', false]], + [[new JsonValue(['a' => true]), 1, 1.1, '', false]], ['jsonb_col', 'int_col', 'float_col', 'char_col', 'bool_col'], 'expected' => <<', 'id', new ArrayExpression([1])], '"id" @> ARRAY[1]::int[]', []], - [['<@', 'id', new ArrayExpression([1])], '"id" <@ ARRAY[1]::int[]', []], - [['=', 'id', new ArrayExpression([1])], '"id" = ARRAY[1]::int[]', []], - [['<>', 'id', new ArrayExpression([1])], '"id" <> ARRAY[1]::int[]', []], - [['>', 'id', new ArrayExpression([1])], '"id" > ARRAY[1]::int[]', []], - [['<', 'id', new ArrayExpression([1])], '"id" < ARRAY[1]::int[]', []], - [['>=', 'id', new ArrayExpression([1])], '"id" >= ARRAY[1]::int[]', []], - [['<=', 'id', new ArrayExpression([1])], '"id" <= ARRAY[1]::int[]', []], - [['&&', 'id', new ArrayExpression([1])], '"id" && ARRAY[1]::int[]', []], + [['@>', 'id', new ArrayValue([1])], '"id" @> ARRAY[1]::int[]', []], + [['<@', 'id', new ArrayValue([1])], '"id" <@ ARRAY[1]::int[]', []], + [['=', 'id', new ArrayValue([1])], '"id" = ARRAY[1]::int[]', []], + [['<>', 'id', new ArrayValue([1])], '"id" <> ARRAY[1]::int[]', []], + [['>', 'id', new ArrayValue([1])], '"id" > ARRAY[1]::int[]', []], + [['<', 'id', new ArrayValue([1])], '"id" < ARRAY[1]::int[]', []], + [['>=', 'id', new ArrayValue([1])], '"id" >= ARRAY[1]::int[]', []], + [['<=', 'id', new ArrayValue([1])], '"id" <= ARRAY[1]::int[]', []], + [['&&', 'id', new ArrayValue([1])], '"id" && ARRAY[1]::int[]', []], ]; } @@ -385,9 +385,9 @@ public static function overlapsCondition(): array $data['null'][1] = 0; $data['expression'][0] = new Expression("'{0,1,2,7}'"); - $data['query expression'][0] = (new Query(self::getDb()))->select(new ArrayExpression([0,1,2,7])); + $data['query expression'][0] = (new Query(self::getDb()))->select(new ArrayValue([0,1,2,7])); $data[] = [new Expression('ARRAY[0,1,2,7]'), 1]; - $data[] = [new ArrayExpression([0,1,2,7]), 1]; + $data[] = [new ArrayValue([0,1,2,7]), 1]; return $data; } @@ -641,7 +641,7 @@ public static function multiOperandFunctionBuilder(): array ], 'ArrayMerge with 4 operands' => [ ArrayMerge::class, - ['ARRAY[1,2,3]', [5, 6, 7], $stringParam, self::getDb()->select(new ArrayExpression([9, 10]))], + ['ARRAY[1,2,3]', [5, 6, 7], $stringParam, self::getDb()->select(new ArrayValue([9, 10]))], 'ARRAY(SELECT DISTINCT UNNEST(ARRAY[1,2,3] || ARRAY[5,6,7]::int[] || :qp0 || (SELECT ARRAY[9,10]::int[])))', [1, 2, 3, 4, 5, 6, 7, 9, 10], [ diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 8b303c53e..81245c7e8 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -10,7 +10,7 @@ use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; use Yiisoft\Db\Exception\IntegrityException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Expression\Value\ArrayExpression; +use Yiisoft\Db\Expression\Value\ArrayValue; use Yiisoft\Db\Expression\Statement\CaseX; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\ExpressionInterface; @@ -639,7 +639,7 @@ public function testArrayMergeWithTypeWithOrdering( 'ARRAY[2,1,3]', [6, 5, 7], $stringParam, - self::getDb()->select(new ArrayExpression([10, 9])), + self::getDb()->select(new ArrayValue([10, 9])), ))->type($type)->ordered(); $params = []; diff --git a/tests/StructuredExpressionBuilderTest.php b/tests/StructuredValueBuilderTest.php similarity index 94% rename from tests/StructuredExpressionBuilderTest.php rename to tests/StructuredValueBuilderTest.php index 9e624fbfe..528e6b93f 100644 --- a/tests/StructuredExpressionBuilderTest.php +++ b/tests/StructuredValueBuilderTest.php @@ -9,8 +9,8 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Expression\Value\Param; -use Yiisoft\Db\Expression\Value\StructuredExpression; -use Yiisoft\Db\Pgsql\Builder\StructuredExpressionBuilder; +use Yiisoft\Db\Expression\Value\StructuredValue; +use Yiisoft\Db\Pgsql\Builder\StructuredValueBuilder; use Yiisoft\Db\Pgsql\Column\ColumnBuilder; use Yiisoft\Db\Pgsql\Data\LazyArray; use Yiisoft\Db\Pgsql\Data\StructuredLazyArray; @@ -23,7 +23,7 @@ /** * @group pgsql */ -final class StructuredExpressionBuilderTest extends TestCase +final class StructuredValueBuilderTest extends TestCase { use TestTrait; @@ -141,8 +141,8 @@ public function testBuild( $qb = $db->getQueryBuilder(); $params = []; - $builder = new StructuredExpressionBuilder($qb); - $expression = new StructuredExpression($value, $type); + $builder = new StructuredValueBuilder($qb); + $expression = new StructuredValue($value, $type); $this->assertSame($expected, $builder->build($expression, $params)); Assert::arraysEquals($expectedParams, $params);