Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor column PHP type #315

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -204,7 +204,7 @@ public static function columns(): array
'id' => [
'type' => 'integer',
'dbType' => 'int',
'phpType' => 'integer',
'phpType' => 'int',
'primaryKey' => true,
'allowNull' => false,
'autoIncrement' => true,
Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/Type/BinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\')'],
];
}
}
4 changes: 2 additions & 2 deletions tests/Provider/Type/BitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
}
6 changes: 3 additions & 3 deletions tests/Provider/Type/VarBinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\')'],
];
}
}
4 changes: 2 additions & 2 deletions tests/Type/BigIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/DecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions tests/Type/FloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/IntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/NumericTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions tests/Type/RealTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/SmallIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/TinyIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down