From bedc095947ef0de73ac0d5315aeccdf80d3d1b38 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Thu, 8 Aug 2024 11:40:02 +0700 Subject: [PATCH] Add `bit` abstract type (#860) --- CHANGELOG.md | 1 + src/Schema/AbstractSchema.php | 6 ++++ src/Schema/Column/BitColumnSchema.php | 39 +++++++++++++++++++++++++ src/Schema/SchemaInterface.php | 4 +++ tests/Provider/ColumnSchemaProvider.php | 27 +++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 src/Schema/Column/BitColumnSchema.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 677a5e190..3f35dc0ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Chg #847: Remove `SchemaInterface::getRawTableName()` and `AbstractSchema::getRawTableName()` methods (@Tigrov) - Enh #852: Add method chaining for column classes (@Tigrov) - Enh #855: Add array and JSON overlaps conditions (@Tigrov) +- Enh #860 Add `bit` abstract type (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 2b6abea37..207bbc892 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -13,6 +13,7 @@ use Yiisoft\Db\Constraint\IndexConstraint; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Schema\Column\BinaryColumnSchema; +use Yiisoft\Db\Schema\Column\BitColumnSchema; use Yiisoft\Db\Schema\Column\BooleanColumnSchema; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; @@ -413,6 +414,7 @@ protected function getColumnPhpType(string $type, bool $isUnsigned = false): str SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8 || $isUnsigned ? SchemaInterface::PHP_TYPE_STRING : SchemaInterface::PHP_TYPE_INTEGER, + SchemaInterface::TYPE_BIT => SchemaInterface::PHP_TYPE_INTEGER, SchemaInterface::TYPE_BOOLEAN => SchemaInterface::PHP_TYPE_BOOLEAN, SchemaInterface::TYPE_DECIMAL => SchemaInterface::PHP_TYPE_DOUBLE, SchemaInterface::TYPE_FLOAT => SchemaInterface::PHP_TYPE_DOUBLE, @@ -425,6 +427,10 @@ protected function getColumnPhpType(string $type, bool $isUnsigned = false): str protected function createColumnSchemaFromPhpType(string $phpType, string $type): ColumnSchemaInterface { + if ($type === SchemaInterface::TYPE_BIT) { + return new BitColumnSchema($type, $phpType); + } + return match ($phpType) { SchemaInterface::PHP_TYPE_STRING => match ($type) { SchemaInterface::TYPE_INTEGER => new BigIntColumnSchema($type, $phpType), diff --git a/src/Schema/Column/BitColumnSchema.php b/src/Schema/Column/BitColumnSchema.php new file mode 100644 index 000000000..bc7a1ff5c --- /dev/null +++ b/src/Schema/Column/BitColumnSchema.php @@ -0,0 +1,39 @@ + null, + default => $value instanceof ExpressionInterface ? $value : (int) $value, + }; + } + + public function phpTypecast(mixed $value): int|null + { + if ($value === null) { + return null; + } + + return (int) $value; + } +} diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index d03341fb4..4a3c6089b 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -210,6 +210,10 @@ interface SchemaInterface extends ConstraintSchemaInterface * Define the abstract column type as `boolean`. */ public const TYPE_BOOLEAN = 'boolean'; + /** + * Define the abstract column type as `bit`. + */ + public const TYPE_BIT = 'bit'; /** * Define the abstract column type as `money`. */ diff --git a/tests/Provider/ColumnSchemaProvider.php b/tests/Provider/ColumnSchemaProvider.php index 5c90ca91f..08f9bb83b 100644 --- a/tests/Provider/ColumnSchemaProvider.php +++ b/tests/Provider/ColumnSchemaProvider.php @@ -11,6 +11,7 @@ use Yiisoft\Db\Expression\JsonExpression; use Yiisoft\Db\Schema\Column\BigIntColumnSchema; use Yiisoft\Db\Schema\Column\BinaryColumnSchema; +use Yiisoft\Db\Schema\Column\BitColumnSchema; use Yiisoft\Db\Schema\Column\BooleanColumnSchema; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; use Yiisoft\Db\Schema\Column\IntegerColumnSchema; @@ -31,6 +32,7 @@ public static function predefinedTypes(): array 'double' => [DoubleColumnSchema::class, SchemaInterface::TYPE_DOUBLE, SchemaInterface::PHP_TYPE_DOUBLE], 'string' => [StringColumnSchema::class, SchemaInterface::TYPE_STRING, SchemaInterface::PHP_TYPE_STRING], 'binary' => [BinaryColumnSchema::class, SchemaInterface::TYPE_BINARY, SchemaInterface::PHP_TYPE_RESOURCE], + 'bit' => [BitColumnSchema::class, SchemaInterface::TYPE_BIT, SchemaInterface::PHP_TYPE_INTEGER], 'boolean' => [BooleanColumnSchema::class, SchemaInterface::TYPE_BOOLEAN, SchemaInterface::PHP_TYPE_BOOLEAN], 'json' => [JsonColumnSchema::class, SchemaInterface::TYPE_JSON, SchemaInterface::PHP_TYPE_ARRAY], ]; @@ -105,6 +107,21 @@ public static function dbTypecastColumns(): array [$expression = new Expression('expression'), $expression], ], ], + 'bit' => [ + BitColumnSchema::class, + [ + [null, null], + [null, ''], + [1, 1], + [1, 1.0], + [1, '1'], + [10, 10], + [10, '10'], + [1, true], + [0, false], + [$expression = new Expression('expression'), $expression], + ], + ], 'boolean' => [ BooleanColumnSchema::class, [ @@ -187,6 +204,16 @@ public static function phpTypecastColumns(): array [$resource = fopen('php://memory', 'rb'), $resource], ], ], + 'bit' => [ + BitColumnSchema::class, + [ + [null, null], + [1, 1], + [1, '1'], + [10, 10], + [10, '10'], + ], + ], 'boolean' => [ BooleanColumnSchema::class, [