Skip to content

Commit

Permalink
Revert "Realize column factory"
Browse files Browse the repository at this point in the history
This reverts commit 33713a1.
  • Loading branch information
Tigrov committed Aug 13, 2024
1 parent fe8093e commit 883758a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 187 deletions.
73 changes: 0 additions & 73 deletions src/Column/ColumnFactory.php

This file was deleted.

89 changes: 80 additions & 9 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;

Expand Down Expand Up @@ -79,16 +77,48 @@
*/
final class Schema extends AbstractPdoSchema
{
/**
* Mapping from physical column types (keys) to abstract column types (values).
*
* @var string[]
*/
private const TYPE_MAP = [
'tinyint' => self::TYPE_TINYINT,
'bit' => self::TYPE_BIT,
'smallint' => self::TYPE_SMALLINT,
'mediumint' => self::TYPE_INTEGER,
'int' => self::TYPE_INTEGER,
'integer' => self::TYPE_INTEGER,
'bigint' => self::TYPE_BIGINT,
'float' => self::TYPE_FLOAT,
'double' => self::TYPE_DOUBLE,
'real' => self::TYPE_FLOAT,
'decimal' => self::TYPE_DECIMAL,
'numeric' => self::TYPE_DECIMAL,
'tinytext' => self::TYPE_TEXT,
'mediumtext' => self::TYPE_TEXT,
'longtext' => self::TYPE_TEXT,
'longblob' => self::TYPE_BINARY,
'blob' => self::TYPE_BINARY,
'text' => self::TYPE_TEXT,
'varchar' => self::TYPE_STRING,
'string' => self::TYPE_STRING,
'char' => self::TYPE_CHAR,
'datetime' => self::TYPE_DATETIME,
'year' => self::TYPE_DATE,
'date' => self::TYPE_DATE,
'time' => self::TYPE_TIME,
'timestamp' => self::TYPE_TIMESTAMP,
'enum' => self::TYPE_STRING,
'varbinary' => self::TYPE_BINARY,
'json' => self::TYPE_JSON,
];

public function createColumn(string $type, array|int|string $length = null): ColumnInterface
{
return new Column($type, $length);
}

public function getColumnFactory(): ColumnFactoryInterface
{
return new ColumnFactory();
}

/**
* Returns all unique indexes for the given table.
*
Expand Down Expand Up @@ -428,9 +458,15 @@ protected function getCreateTableSql(TableSchemaInterface $table): string
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$dbType = $info['type'];
$type = $this->getColumnType($dbType, $info);
$isUnsigned = stripos($dbType, 'unsigned') !== false;
/** @psalm-var ColumnInfoArray $info */
$column = $this->getColumnFactory()->fromDefinition($dbType);
$column = $this->createColumnSchema($type, unsigned: $isUnsigned);
$column->name($info['field']);
$column->enumValues($info['enum_values'] ?? null);
$column->size($info['size'] ?? null);
$column->precision($info['precision'] ?? null);
$column->scale($info['scale'] ?? null);
$column->allowNull($info['null'] === 'YES');
$column->primaryKey(str_contains($info['key'], 'PRI'));
$column->autoIncrement(stripos($info['extra'], 'auto_increment') !== false);
Expand All @@ -443,7 +479,7 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
empty($extra)
&& !empty($info['extra_default_value'])
&& !str_starts_with($info['extra_default_value'], '\'')
&& in_array($column->getType(), [
&& in_array($type, [
self::TYPE_CHAR, self::TYPE_STRING, self::TYPE_TEXT,
self::TYPE_DATETIME, self::TYPE_TIMESTAMP, self::TYPE_TIME, self::TYPE_DATE,
], true)
Expand All @@ -461,6 +497,41 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
return $column;
}

/**
* Get the abstract data type for the database data type.
*
* @param string $dbType The database data type
* @param array $info Column information.
*
* @return string The abstract data type.
*/
private function getColumnType(string $dbType, array &$info): string
{
preg_match('/^(\w*)(?:\(([^)]+)\))?/', $dbType, $matches);
$dbType = strtolower($matches[1]);

if (!empty($matches[2])) {
if ($dbType === 'enum') {
preg_match_all("/'([^']*)'/", $matches[2], $values);
$info['enum_values'] = $values[1];
} else {
$values = explode(',', $matches[2], 2);
$info['size'] = (int) $values[0];
$info['precision'] = (int) $values[0];

if (isset($values[1])) {
$info['scale'] = (int) $values[1];
}

if ($dbType === 'bit' && $info['size'] === 1) {
return self::TYPE_BOOLEAN;
}
}
}

return self::TYPE_MAP[$dbType] ?? self::TYPE_STRING;
}

/**
* Converts column's default value according to {@see ColumnSchema::phpType} after retrieval from the database.
*
Expand Down
34 changes: 0 additions & 34 deletions tests/ColumnFactoryTest.php

This file was deleted.

62 changes: 0 additions & 62 deletions tests/Provider/ColumnFactoryProvider.php

This file was deleted.

9 changes: 0 additions & 9 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mysql\Column;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\Mysql\Schema;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
Expand Down Expand Up @@ -563,12 +562,4 @@ public function testInsertDefaultValues()
'numeric_col' => '-33.22',
], $row);
}

public function testGetColumnFactory(): void
{
$db = $this->getConnection();
$factory = $db->getSchema()->getColumnFactory();

$this->assertInstanceOf(ColumnFactory::class, $factory);
}
}

0 comments on commit 883758a

Please sign in to comment.