From 62a238b02c81e911480dc936740016e0d64730c2 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 9 Aug 2024 19:05:34 +0700 Subject: [PATCH] Refactor column PHP type (#275) --- CHANGELOG.md | 1 + src/Command.php | 4 ++-- src/Schema.php | 8 ++++---- tests/Provider/SchemaProvider.php | 20 ++++++++++---------- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a4161c1..b0e82790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Enh #236: Implement `ColumnSchemaInterface` classes according to the data type of database table columns for type casting performance. Related with yiisoft/db#752 (@Tigrov) - Chg #272: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov) +- Enh #275: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Command.php b/src/Command.php index 672bdb32..15386b3d 100644 --- a/src/Command.php +++ b/src/Command.php @@ -5,9 +5,9 @@ namespace Yiisoft\Db\Oracle; use PDO; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; -use Yiisoft\Db\Schema\SchemaInterface; use function array_keys; use function count; @@ -48,7 +48,7 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra 'value' => '', ]; - if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== SchemaInterface::PHP_TYPE_INTEGER) { + if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== PhpType::INT) { $returnParams[$phName]['dataType'] = PDO::PARAM_STR; } else { $returnParams[$phName]['dataType'] = PDO::PARAM_INT; diff --git a/src/Schema.php b/src/Schema.php index f92dc5fb..755088e6 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -477,13 +477,13 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface return $column; } - protected function createColumnSchemaFromPhpType(string $phpType, string $type): ColumnSchemaInterface + protected function createColumnSchemaFromType(string $type, bool $isUnsigned = false): ColumnSchemaInterface { - if ($phpType === self::PHP_TYPE_RESOURCE) { - return new BinaryColumnSchema($type, $phpType); + if ($type === self::TYPE_BINARY) { + return new BinaryColumnSchema($type); } - return parent::createColumnSchemaFromPhpType($phpType, $type); + return parent::createColumnSchemaFromType($type, $isUnsigned); } /** diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index e36462d8..fcdb6349 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -18,7 +18,7 @@ public static function columns(): array 'int_col' => [ 'type' => 'integer', 'dbType' => 'NUMBER', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => false, 'autoIncrement' => false, @@ -31,7 +31,7 @@ public static function columns(): array 'int_col2' => [ 'type' => 'integer', 'dbType' => 'NUMBER', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -44,7 +44,7 @@ public static function columns(): array 'tinyint_col' => [ 'type' => 'integer', 'dbType' => 'NUMBER', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -57,7 +57,7 @@ public static function columns(): array 'smallint_col' => [ 'type' => 'integer', 'dbType' => 'NUMBER', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -122,7 +122,7 @@ public static function columns(): array 'float_col' => [ 'type' => 'double', 'dbType' => 'FLOAT', - 'phpType' => 'double', + 'phpType' => 'float', 'primaryKey' => false, 'allowNull' => false, 'autoIncrement' => false, @@ -135,7 +135,7 @@ public static function columns(): array 'float_col2' => [ 'type' => 'double', 'dbType' => 'FLOAT', - 'phpType' => 'double', + 'phpType' => 'float', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -148,7 +148,7 @@ public static function columns(): array 'blob_col' => [ 'type' => 'binary', 'dbType' => 'BLOB', - 'phpType' => 'resource', + 'phpType' => 'mixed', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -161,7 +161,7 @@ public static function columns(): array 'numeric_col' => [ 'type' => 'decimal', 'dbType' => 'NUMBER', - 'phpType' => 'double', + 'phpType' => 'float', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -252,7 +252,7 @@ public static function columns(): array 'bit_col' => [ 'type' => 'integer', 'dbType' => 'NUMBER', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => false, 'autoIncrement' => false, @@ -270,7 +270,7 @@ public static function columns(): array 'id' => [ 'type' => 'integer', 'dbType' => 'NUMBER', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => true, 'allowNull' => false, 'autoIncrement' => true,