From bfd17431f65d46ef613aaf929558f09bee8f73bc Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 8 Aug 2024 16:18:56 +0700 Subject: [PATCH 1/2] Refactor column PHP type --- src/Schema.php | 8 ++++---- tests/Provider/SchemaProvider.php | 22 +++++++++++----------- tests/Provider/Type/BinaryProvider.php | 4 ++-- tests/Provider/Type/BitProvider.php | 4 ++-- tests/Provider/Type/VarBinaryProvider.php | 6 +++--- tests/Type/BigIntTest.php | 4 ++-- tests/Type/DecimalTest.php | 4 ++-- tests/Type/FloatTest.php | 4 ++-- tests/Type/ImageTest.php | 4 ++-- tests/Type/IntTest.php | 4 ++-- tests/Type/NumericTest.php | 4 ++-- tests/Type/RealTest.php | 4 ++-- tests/Type/SmallIntTest.php | 4 ++-- tests/Type/TinyIntTest.php | 4 ++-- 14 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/Schema.php b/src/Schema.php index b2fcce0b8..226c7e743 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -464,13 +464,13 @@ private function getColumnType(string $dbType, array &$info): string return self::TYPE_MAP[$dbType] ?? self::TYPE_STRING; } - 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 fd8b8ac6a..cf38b620c 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -17,7 +17,7 @@ public static function columns(): array 'int_col' => [ 'type' => 'integer', 'dbType' => 'int', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => false, 'autoIncrement' => false, @@ -30,7 +30,7 @@ public static function columns(): array 'int_col2' => [ 'type' => 'integer', 'dbType' => 'int', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -43,7 +43,7 @@ public static function columns(): array 'tinyint_col' => [ 'type' => 'tinyint', 'dbType' => 'tinyint', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -56,7 +56,7 @@ public static function columns(): array 'smallint_col' => [ 'type' => 'smallint', 'dbType' => 'smallint', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -108,7 +108,7 @@ public static function columns(): array 'float_col' => [ 'type' => 'decimal', 'dbType' => 'decimal(4,3)', - 'phpType' => 'double', + 'phpType' => 'float', 'primaryKey' => false, 'allowNull' => false, 'autoIncrement' => false, @@ -121,7 +121,7 @@ public static function columns(): array 'float_col2' => [ 'type' => 'float', 'dbType' => 'float', - 'phpType' => 'double', + 'phpType' => 'float', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -134,7 +134,7 @@ public static function columns(): array 'blob_col' => [ 'type' => 'binary', 'dbType' => 'varbinary', - 'phpType' => 'resource', + 'phpType' => 'mixed', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -147,7 +147,7 @@ public static function columns(): array 'numeric_col' => [ 'type' => 'decimal', 'dbType' => 'decimal(5,2)', - 'phpType' => 'double', + 'phpType' => 'float', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -173,7 +173,7 @@ public static function columns(): array 'bool_col' => [ 'type' => 'boolean', 'dbType' => 'bit', - 'phpType' => 'boolean', + 'phpType' => 'bool', 'primaryKey' => false, 'allowNull' => false, 'autoIncrement' => false, @@ -186,7 +186,7 @@ public static function columns(): array 'bool_col2' => [ 'type' => 'boolean', 'dbType' => 'bit', - 'phpType' => 'boolean', + 'phpType' => 'bool', 'primaryKey' => false, 'allowNull' => true, 'autoIncrement' => false, @@ -204,7 +204,7 @@ public static function columns(): array 'id' => [ 'type' => 'integer', 'dbType' => 'int', - 'phpType' => 'integer', + 'phpType' => 'int', 'primaryKey' => true, 'allowNull' => false, 'autoIncrement' => true, diff --git a/tests/Provider/Type/BinaryProvider.php b/tests/Provider/Type/BinaryProvider.php index 717288356..2e14827b0 100644 --- a/tests/Provider/Type/BinaryProvider.php +++ b/tests/Provider/Type/BinaryProvider.php @@ -9,8 +9,8 @@ final class BinaryProvider public static function columns(): array { return [ - ['Mybinary1', 'binary(10)', 'resource', 10, 'CONVERT([binary](10),\'binary\')'], - ['Mybinary2', 'binary(1)', 'resource', 1, 'CONVERT([binary](1),\'b\')'], + ['Mybinary1', 'binary(10)', 'mixed', 10, 'CONVERT([binary](10),\'binary\')'], + ['Mybinary2', 'binary(1)', 'mixed', 1, 'CONVERT([binary](1),\'b\')'], ]; } } diff --git a/tests/Provider/Type/BitProvider.php b/tests/Provider/Type/BitProvider.php index 7872310bf..8d8f8f9f8 100644 --- a/tests/Provider/Type/BitProvider.php +++ b/tests/Provider/Type/BitProvider.php @@ -9,8 +9,8 @@ final class BitProvider public static function columns(): array { return [ - ['Mybit1', 'bit', 'boolean', false], - ['Mybit2', 'bit', 'boolean', true], + ['Mybit1', 'bit', 'bool', false], + ['Mybit2', 'bit', 'bool', true], ]; } } diff --git a/tests/Provider/Type/VarBinaryProvider.php b/tests/Provider/Type/VarBinaryProvider.php index 01af28cf7..d1ea57620 100644 --- a/tests/Provider/Type/VarBinaryProvider.php +++ b/tests/Provider/Type/VarBinaryProvider.php @@ -9,9 +9,9 @@ final class VarBinaryProvider public static function columns(): array { return [ - ['Myvarbinary1', 'varbinary(10)', 'resource', 10, 'CONVERT([varbinary](10),\'varbinary\')'], - ['Myvarbinary2', 'varbinary(100)', 'resource', 100, 'CONVERT([varbinary](100),\'v\')'], - ['Myvarbinary3', 'varbinary(20)', 'resource', 20, 'hashbytes(\'MD5\',\'test string\')'], + ['Myvarbinary1', 'varbinary(10)', 'mixed', 10, 'CONVERT([varbinary](10),\'varbinary\')'], + ['Myvarbinary2', 'varbinary(100)', 'mixed', 100, 'CONVERT([varbinary](100),\'v\')'], + ['Myvarbinary3', 'varbinary(20)', 'mixed', 20, 'hashbytes(\'MD5\',\'test string\')'], ]; } } diff --git a/tests/Type/BigIntTest.php b/tests/Type/BigIntTest.php index 405f8b39b..e2b002c0f 100644 --- a/tests/Type/BigIntTest.php +++ b/tests/Type/BigIntTest.php @@ -38,7 +38,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('bigint_default'); $this->assertSame('bigint', $tableSchema?->getColumn('Mybigint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Mybigint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Mybigint')->getPhpType()); $this->assertSame(9_223_372_036_854_775_807, $tableSchema?->getColumn('Mybigint')->getDefaultValue()); $db->createCommand()->dropTable('bigint_default')->execute(); @@ -85,7 +85,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('bigint_default'); $this->assertSame('bigint', $tableSchema?->getColumn('Mybigint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Mybigint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Mybigint')->getPhpType()); $this->assertSame(9_223_372_036_854_775_807, $tableSchema?->getColumn('Mybigint')->getDefaultValue()); $db->createCommand()->dropTable('bigint_default')->execute(); diff --git a/tests/Type/DecimalTest.php b/tests/Type/DecimalTest.php index 9f0e5ccaa..f11d40110 100644 --- a/tests/Type/DecimalTest.php +++ b/tests/Type/DecimalTest.php @@ -39,7 +39,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('decimal_default'); $this->assertSame('decimal(38,0)', $tableSchema?->getColumn('Mydecimal')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Mydecimal')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Mydecimal')->getPhpType()); $this->assertSame(38, $tableSchema?->getColumn('Mydecimal')->getSize()); $this->assertSame(9.9999999999999998e+037, $tableSchema?->getColumn('Mydecimal')->getDefaultValue()); @@ -87,7 +87,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('decimal_default'); $this->assertSame('decimal(38,0)', $tableSchema?->getColumn('Mydecimal')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Mydecimal')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Mydecimal')->getPhpType()); $this->assertSame(38, $tableSchema?->getColumn('Mydecimal')->getSize()); $this->assertSame(9.9999999999999998e+037, $tableSchema?->getColumn('Mydecimal')->getDefaultValue()); diff --git a/tests/Type/FloatTest.php b/tests/Type/FloatTest.php index 2d38ff033..110fe1194 100644 --- a/tests/Type/FloatTest.php +++ b/tests/Type/FloatTest.php @@ -39,7 +39,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('float_default'); $this->assertSame('float', $tableSchema?->getColumn('Myfloat')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Myfloat')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Myfloat')->getPhpType()); $this->assertSame(2.2300000000000001e-308, $tableSchema?->getColumn('Myfloat')->getDefaultValue()); $db->createCommand()->insert('float_default', [])->execute(); @@ -86,7 +86,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('float_default'); $this->assertSame('float', $tableSchema?->getColumn('Myfloat')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Myfloat')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Myfloat')->getPhpType()); $this->assertSame(2.2300000000000001e-308, $tableSchema?->getColumn('Myfloat')->getDefaultValue()); $command = $db->createCommand(); diff --git a/tests/Type/ImageTest.php b/tests/Type/ImageTest.php index 626c63829..a9fdd0f8c 100644 --- a/tests/Type/ImageTest.php +++ b/tests/Type/ImageTest.php @@ -39,7 +39,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('image_default'); $this->assertSame('image', $tableSchema?->getColumn('Myimage')->getDbType()); - $this->assertSame('resource', $tableSchema?->getColumn('Myimage')->getPhpType()); + $this->assertSame('mixed', $tableSchema?->getColumn('Myimage')->getPhpType()); $this->assertSame('image', $tableSchema?->getColumn('Myimage')->getDefaultValue()); $db->createCommand()->dropTable('image_default')->execute(); @@ -86,7 +86,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('image_default'); $this->assertSame('image', $tableSchema?->getColumn('Myimage')->getDbType()); - $this->assertSame('resource', $tableSchema?->getColumn('Myimage')->getPhpType()); + $this->assertSame('mixed', $tableSchema?->getColumn('Myimage')->getPhpType()); $this->assertSame('image', $tableSchema?->getColumn('Myimage')->getDefaultValue()); $db->createCommand()->dropTable('image_default')->execute(); diff --git a/tests/Type/IntTest.php b/tests/Type/IntTest.php index 93bf47105..4d513f97b 100644 --- a/tests/Type/IntTest.php +++ b/tests/Type/IntTest.php @@ -38,7 +38,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('int_default'); $this->assertSame('int', $tableSchema?->getColumn('Myint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Myint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Myint')->getPhpType()); $this->assertSame(2_147_483_647, $tableSchema?->getColumn('Myint')->getDefaultValue()); $db->createCommand()->dropTable('int_default')->execute(); @@ -85,7 +85,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('int_default'); $this->assertSame('int', $tableSchema?->getColumn('Myint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Myint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Myint')->getPhpType()); $this->assertSame(2_147_483_647, $tableSchema?->getColumn('Myint')->getDefaultValue()); $db->createCommand()->dropTable('int_default')->execute(); diff --git a/tests/Type/NumericTest.php b/tests/Type/NumericTest.php index 5b5bcca21..36470b7f8 100644 --- a/tests/Type/NumericTest.php +++ b/tests/Type/NumericTest.php @@ -39,7 +39,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('numeric_default'); $this->assertSame('numeric(38,0)', $tableSchema?->getColumn('Mynumeric')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Mynumeric')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Mynumeric')->getPhpType()); $this->assertSame(38, $tableSchema?->getColumn('Mynumeric')->getSize()); $this->assertSame(9.9999999999999998e+037, $tableSchema?->getColumn('Mynumeric')->getDefaultValue()); @@ -87,7 +87,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('numeric_default'); $this->assertSame('numeric(38,0)', $tableSchema?->getColumn('Mynumeric')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Mynumeric')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Mynumeric')->getPhpType()); $this->assertSame(38, $tableSchema?->getColumn('Mynumeric')->getSize()); $this->assertSame(9.9999999999999998e+037, $tableSchema?->getColumn('Mynumeric')->getDefaultValue()); diff --git a/tests/Type/RealTest.php b/tests/Type/RealTest.php index 789edc37f..74ea36c99 100644 --- a/tests/Type/RealTest.php +++ b/tests/Type/RealTest.php @@ -39,7 +39,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('real_default'); $this->assertSame('real', $tableSchema?->getColumn('Myreal')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Myreal')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Myreal')->getPhpType()); $this->assertSame(3.4000000000000000e+038, $tableSchema?->getColumn('Myreal')->getDefaultValue()); $db->createCommand()->dropTable('real_default')->execute(); @@ -86,7 +86,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('real_default'); $this->assertSame('real', $tableSchema?->getColumn('Myreal')->getDbType()); - $this->assertSame('double', $tableSchema?->getColumn('Myreal')->getPhpType()); + $this->assertSame('float', $tableSchema?->getColumn('Myreal')->getPhpType()); $this->assertSame(3.4000000000000000e+038, $tableSchema?->getColumn('Myreal')->getDefaultValue()); $db->createCommand()->dropTable('real_default')->execute(); diff --git a/tests/Type/SmallIntTest.php b/tests/Type/SmallIntTest.php index e0b3c5780..46a01d272 100644 --- a/tests/Type/SmallIntTest.php +++ b/tests/Type/SmallIntTest.php @@ -38,7 +38,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('smallint_default'); $this->assertSame('smallint', $tableSchema?->getColumn('Mysmallint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Mysmallint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Mysmallint')->getPhpType()); $this->assertSame(32767, $tableSchema?->getColumn('Mysmallint')->getDefaultValue()); $db->createCommand()->dropTable('smallint_default')->execute(); @@ -85,7 +85,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('smallint_default'); $this->assertSame('smallint', $tableSchema?->getColumn('Mysmallint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Mysmallint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Mysmallint')->getPhpType()); $this->assertSame(32767, $tableSchema?->getColumn('Mysmallint')->getDefaultValue()); $db->createCommand()->dropTable('smallint_default')->execute(); diff --git a/tests/Type/TinyIntTest.php b/tests/Type/TinyIntTest.php index 42ecf39d2..9fc3bc786 100644 --- a/tests/Type/TinyIntTest.php +++ b/tests/Type/TinyIntTest.php @@ -38,7 +38,7 @@ public function testCreateTableWithDefaultValue(): void $tableSchema = $db->getTableSchema('tinyint_default'); $this->assertSame('tinyint', $tableSchema?->getColumn('Mytinyint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Mytinyint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Mytinyint')->getPhpType()); $this->assertSame(255, $tableSchema?->getColumn('Mytinyint')->getDefaultValue()); $db->createCommand()->dropTable('tinyint_default')->execute(); @@ -85,7 +85,7 @@ public function testDefaultValue(): void $tableSchema = $db->getTableSchema('tinyint_default'); $this->assertSame('tinyint', $tableSchema?->getColumn('Mytinyint')->getDbType()); - $this->assertSame('integer', $tableSchema?->getColumn('Mytinyint')->getPhpType()); + $this->assertSame('int', $tableSchema?->getColumn('Mytinyint')->getPhpType()); $this->assertSame(255, $tableSchema?->getColumn('Mytinyint')->getDefaultValue()); $db->createCommand()->dropTable('tinyint_default')->execute(); From e9db631f097b623e56c43fb0399498794a4ec629 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 9 Aug 2024 16:58:25 +0700 Subject: [PATCH 2/2] Add line to CHANGELOG.md [skip ci] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73599c96a..aae45259d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Chg #306: Remove parameter `$withColumn` from `Quoter::getTableNameParts()` method (@Tigrov) - Chg #308: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov) - Enh #312: Refactor `bit` type (@Tigrov) +- Enh #315: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) ## 1.2.0 March 21, 2024