From 76ef13935f21a2f4dcc9dcaee70d4b7f502e2083 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Thu, 9 Jan 2025 17:36:10 +0700 Subject: [PATCH] Rename `ColumnSchemaInterface` to `ColumnInterface` (#372) --- CHANGELOG.md | 1 + src/Column/ColumnDefinitionBuilder.php | 6 +- src/Column/ColumnFactory.php | 6 +- src/DMLQueryBuilder.php | 19 ----- src/Schema.php | 10 +-- .../{ColumnSchemaTest.php => ColumnTest.php} | 32 ++++---- tests/Provider/ColumnFactoryProvider.php | 74 +++++++++---------- ...nSchemaProvider.php => ColumnProvider.php} | 2 +- tests/QueryBuilderTest.php | 6 +- tests/SchemaTest.php | 8 +- 10 files changed, 73 insertions(+), 91 deletions(-) rename tests/{ColumnSchemaTest.php => ColumnTest.php} (79%) rename tests/Provider/{ColumnSchemaProvider.php => ColumnProvider.php} (92%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d34010b3..cd0bdbc58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Chg #368: Update `QueryBuilder` constructor (@Tigrov) - Enh #367: Use `ColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov) - Enh #371: Remove `ColumnInterface` (@Tigrov) +- Enh #372: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index fcb7c4f6a..431f51895 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -6,7 +6,7 @@ use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder; -use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; +use Yiisoft\Db\Schema\Column\ColumnInterface; use function str_contains; use function version_compare; @@ -61,14 +61,14 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder 'fixed', ]; - protected function buildComment(ColumnSchemaInterface $column): string + protected function buildComment(ColumnInterface $column): string { $comment = $column->getComment(); return $comment === null ? '' : ' COMMENT ' . $this->queryBuilder->quoter()->quoteValue($comment); } - protected function getDbType(ColumnSchemaInterface $column): string + protected function getDbType(ColumnInterface $column): string { /** @psalm-suppress DocblockTypeContradiction */ $dbType = $column->getDbType() ?? match ($column->getType()) { diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index bd36e60ac..53a8d5e4f 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -7,7 +7,7 @@ use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; -use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; +use Yiisoft\Db\Schema\Column\ColumnInterface; use function bindec; use function in_array; @@ -64,7 +64,7 @@ protected function getType(string $dbType, array $info = []): string return parent::getType($dbType, $info); } - protected function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed + protected function normalizeDefaultValue(string|null $defaultValue, ColumnInterface $column): mixed { if ( $defaultValue === null @@ -77,7 +77,7 @@ protected function normalizeDefaultValue(string|null $defaultValue, ColumnSchema return $this->normalizeNotNullDefaultValue($defaultValue, $column); } - protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnSchemaInterface $column): mixed + protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInterface $column): mixed { if ($defaultValue === '') { return $column->phpTypecast($defaultValue); diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index 516b76aa9..b7d97cf30 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -6,10 +6,8 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; -use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\Expression; -use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder; @@ -88,23 +86,6 @@ public function upsert( return $insertSql . ' ON DUPLICATE KEY UPDATE ' . implode(', ', $updates); } - /** - * Prepares a `VALUES` part for an `INSERT` SQL statement. - * - * @param string $table The table to insert new rows into. - * @param array|QueryInterface $columns The column data (name => value) to insert into the table or instance of - * {@see Query} to perform `INSERT INTO ... SELECT` SQL statement. - * @param array $params The binding parameters that will be generated by this method. - * They should be bound to the DB command later. - * - * @throws Exception - * @throws InvalidArgumentException - * @throws InvalidConfigException - * @throws NotSupportedException - * - * @return array Array of quoted column names, placeholders, values, and params. - * @psalm-return array{0: string[], 1: string[], 2: string, 3: array} - */ protected function prepareInsertValues(string $table, QueryInterface|array $columns, array $params = []): array { if (empty($columns)) { diff --git a/src/Schema.php b/src/Schema.php index 6afb59e55..e5d67fd93 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -16,7 +16,7 @@ use Yiisoft\Db\Helper\DbArrayHelper; use Yiisoft\Db\Mysql\Column\ColumnFactory; use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; -use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; +use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\TableSchemaInterface; use function array_change_key_case; @@ -173,7 +173,7 @@ protected function findColumns(TableSchemaInterface $table): bool } /** @psalm-var ColumnArray $info */ - $column = $this->loadColumnSchema($info); + $column = $this->loadColumn($info); $table->column($info['column_name'], $column); if ($column->isPrimaryKey()) { @@ -390,15 +390,15 @@ protected function getCreateTableSql(TableSchemaInterface $table): string } /** - * Loads the column information into a {@see ColumnSchemaInterface} object. + * Loads the column information into a {@see ColumnInterface} object. * * @param array $info The column information. * - * @return ColumnSchemaInterface The column schema object. + * @return ColumnInterface The column object. * * @psalm-param ColumnArray $info The column information. */ - private function loadColumnSchema(array $info): ColumnSchemaInterface + private function loadColumn(array $info): ColumnInterface { $extra = trim(str_ireplace('auto_increment', '', $info['extra'], $autoIncrement)); diff --git a/tests/ColumnSchemaTest.php b/tests/ColumnTest.php similarity index 79% rename from tests/ColumnSchemaTest.php rename to tests/ColumnTest.php index 24daf6089..31cb73c38 100644 --- a/tests/ColumnSchemaTest.php +++ b/tests/ColumnTest.php @@ -9,13 +9,13 @@ use Yiisoft\Db\Mysql\Column\ColumnBuilder; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; -use Yiisoft\Db\Schema\Column\BinaryColumnSchema; -use Yiisoft\Db\Schema\Column\BooleanColumnSchema; -use Yiisoft\Db\Schema\Column\DoubleColumnSchema; -use Yiisoft\Db\Schema\Column\IntegerColumnSchema; -use Yiisoft\Db\Schema\Column\JsonColumnSchema; -use Yiisoft\Db\Schema\Column\StringColumnSchema; -use Yiisoft\Db\Tests\Common\CommonColumnSchemaTest; +use Yiisoft\Db\Schema\Column\BinaryColumn; +use Yiisoft\Db\Schema\Column\BooleanColumn; +use Yiisoft\Db\Schema\Column\DoubleColumn; +use Yiisoft\Db\Schema\Column\IntegerColumn; +use Yiisoft\Db\Schema\Column\JsonColumn; +use Yiisoft\Db\Schema\Column\StringColumn; +use Yiisoft\Db\Tests\Common\CommonColumnTest; use function str_repeat; @@ -24,12 +24,12 @@ * * @psalm-suppress PropertyNotSetInConstructor */ -final class ColumnSchemaTest extends CommonColumnSchemaTest +final class ColumnTest extends CommonColumnTest { use TestTrait; /** - * @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\ColumnSchemaProvider::bigIntValue + * @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\ColumnProvider::bigIntValue * * @throws Exception * @throws Throwable @@ -99,18 +99,18 @@ public function testPhpTypeCast(): void $db->close(); } - public function testColumnSchemaInstance(): void + public function testColumnInstance(): void { $db = $this->getConnection(true); $schema = $db->getSchema(); $tableSchema = $schema->getTableSchema('type'); - $this->assertInstanceOf(IntegerColumnSchema::class, $tableSchema->getColumn('int_col')); - $this->assertInstanceOf(StringColumnSchema::class, $tableSchema->getColumn('char_col')); - $this->assertInstanceOf(DoubleColumnSchema::class, $tableSchema->getColumn('float_col')); - $this->assertInstanceOf(BinaryColumnSchema::class, $tableSchema->getColumn('blob_col')); - $this->assertInstanceOf(BooleanColumnSchema::class, $tableSchema->getColumn('bool_col')); - $this->assertInstanceOf(JsonColumnSchema::class, $tableSchema->getColumn('json_col')); + $this->assertInstanceOf(IntegerColumn::class, $tableSchema->getColumn('int_col')); + $this->assertInstanceOf(StringColumn::class, $tableSchema->getColumn('char_col')); + $this->assertInstanceOf(DoubleColumn::class, $tableSchema->getColumn('float_col')); + $this->assertInstanceOf(BinaryColumn::class, $tableSchema->getColumn('blob_col')); + $this->assertInstanceOf(BooleanColumn::class, $tableSchema->getColumn('bool_col')); + $this->assertInstanceOf(JsonColumn::class, $tableSchema->getColumn('json_col')); } public function testLongtextType(): void diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 17fd9439b..03fe197fd 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -6,13 +6,13 @@ use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Expression\Expression; -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; -use Yiisoft\Db\Schema\Column\JsonColumnSchema; -use Yiisoft\Db\Schema\Column\StringColumnSchema; +use Yiisoft\Db\Schema\Column\BinaryColumn; +use Yiisoft\Db\Schema\Column\BitColumn; +use Yiisoft\Db\Schema\Column\BooleanColumn; +use Yiisoft\Db\Schema\Column\DoubleColumn; +use Yiisoft\Db\Schema\Column\IntegerColumn; +use Yiisoft\Db\Schema\Column\JsonColumn; +use Yiisoft\Db\Schema\Column\StringColumn; final class ColumnFactoryProvider extends \Yiisoft\Db\Tests\Provider\ColumnFactoryProvider { @@ -20,35 +20,35 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['bit', ColumnType::BIT, BitColumnSchema::class], - ['tinyint', ColumnType::TINYINT, IntegerColumnSchema::class], - ['smallint', ColumnType::SMALLINT, IntegerColumnSchema::class], - ['mediumint', ColumnType::INTEGER, IntegerColumnSchema::class], - ['int', ColumnType::INTEGER, IntegerColumnSchema::class], - ['integer', ColumnType::INTEGER, IntegerColumnSchema::class], - ['bigint', ColumnType::BIGINT, IntegerColumnSchema::class], - ['float', ColumnType::FLOAT, DoubleColumnSchema::class], - ['real', ColumnType::FLOAT, DoubleColumnSchema::class], - ['double', ColumnType::DOUBLE, DoubleColumnSchema::class], - ['decimal', ColumnType::DECIMAL, DoubleColumnSchema::class], - ['numeric', ColumnType::DECIMAL, DoubleColumnSchema::class], - ['char', ColumnType::CHAR, StringColumnSchema::class], - ['varchar', ColumnType::STRING, StringColumnSchema::class], - ['string', ColumnType::STRING, StringColumnSchema::class], - ['enum', ColumnType::STRING, StringColumnSchema::class], - ['tinytext', ColumnType::TEXT, StringColumnSchema::class], - ['mediumtext', ColumnType::TEXT, StringColumnSchema::class], - ['longtext', ColumnType::TEXT, StringColumnSchema::class], - ['text', ColumnType::TEXT, StringColumnSchema::class], - ['varbinary', ColumnType::BINARY, BinaryColumnSchema::class], - ['blob', ColumnType::BINARY, BinaryColumnSchema::class], - ['longblob', ColumnType::BINARY, BinaryColumnSchema::class], - ['year', ColumnType::DATE, StringColumnSchema::class], - ['date', ColumnType::DATE, StringColumnSchema::class], - ['time', ColumnType::TIME, StringColumnSchema::class], - ['datetime', ColumnType::DATETIME, StringColumnSchema::class], - ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], - ['json', ColumnType::JSON, JsonColumnSchema::class], + ['bit', ColumnType::BIT, BitColumn::class], + ['tinyint', ColumnType::TINYINT, IntegerColumn::class], + ['smallint', ColumnType::SMALLINT, IntegerColumn::class], + ['mediumint', ColumnType::INTEGER, IntegerColumn::class], + ['int', ColumnType::INTEGER, IntegerColumn::class], + ['integer', ColumnType::INTEGER, IntegerColumn::class], + ['bigint', ColumnType::BIGINT, IntegerColumn::class], + ['float', ColumnType::FLOAT, DoubleColumn::class], + ['real', ColumnType::FLOAT, DoubleColumn::class], + ['double', ColumnType::DOUBLE, DoubleColumn::class], + ['decimal', ColumnType::DECIMAL, DoubleColumn::class], + ['numeric', ColumnType::DECIMAL, DoubleColumn::class], + ['char', ColumnType::CHAR, StringColumn::class], + ['varchar', ColumnType::STRING, StringColumn::class], + ['string', ColumnType::STRING, StringColumn::class], + ['enum', ColumnType::STRING, StringColumn::class], + ['tinytext', ColumnType::TEXT, StringColumn::class], + ['mediumtext', ColumnType::TEXT, StringColumn::class], + ['longtext', ColumnType::TEXT, StringColumn::class], + ['text', ColumnType::TEXT, StringColumn::class], + ['varbinary', ColumnType::BINARY, BinaryColumn::class], + ['blob', ColumnType::BINARY, BinaryColumn::class], + ['longblob', ColumnType::BINARY, BinaryColumn::class], + ['year', ColumnType::DATE, StringColumn::class], + ['date', ColumnType::DATE, StringColumn::class], + ['time', ColumnType::TIME, StringColumn::class], + ['datetime', ColumnType::DATETIME, StringColumn::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumn::class], + ['json', ColumnType::JSON, JsonColumn::class], ]; } @@ -56,7 +56,7 @@ public static function definitions(): array { $definitions = parent::definitions(); - $definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'bit', 'getSize' => 1]]; + $definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumn::class, ['getDbType' => 'bit', 'getSize' => 1]]; return $definitions; } diff --git a/tests/Provider/ColumnSchemaProvider.php b/tests/Provider/ColumnProvider.php similarity index 92% rename from tests/Provider/ColumnSchemaProvider.php rename to tests/Provider/ColumnProvider.php index 310289ebf..ecde65370 100644 --- a/tests/Provider/ColumnSchemaProvider.php +++ b/tests/Provider/ColumnProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Mysql\Tests\Provider; -final class ColumnSchemaProvider +final class ColumnProvider { public static function bigIntValue(): array { diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index ff1c731e8..beac8535b 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -17,7 +17,7 @@ use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition; -use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; +use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; use function str_contains; @@ -146,7 +146,7 @@ public function testAddUnique(string $name, string $table, array|string $columns } #[DataProviderExternal(QueryBuilderProvider::class, 'alterColumn')] - public function testAlterColumn(string|ColumnSchemaInterface $type, string $expected): void + public function testAlterColumn(string|ColumnInterface $type, string $expected): void { parent::testAlterColumn($type, $expected); } @@ -752,7 +752,7 @@ public function testJsonOverlapsConditionOperator(iterable|ExpressionInterface $ } /** @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\QueryBuilderProvider::buildColumnDefinition() */ - public function testBuildColumnDefinition(string $expected, ColumnSchemaInterface|string $column): void + public function testBuildColumnDefinition(string $expected, ColumnInterface|string $column): void { parent::testBuildColumnDefinition($expected, $column); } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 33215c3e0..99b4c3546 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -38,7 +38,7 @@ final class SchemaTest extends CommonSchemaTest * * @throws Exception */ - public function testColumnSchema(array $columns, string $tableName): void + public function testColumns(array $columns, string $tableName): void { $db = $this->getConnection(); $serverVersion = $db->getServerInfo()->getVersion(); @@ -74,15 +74,15 @@ public function testColumnSchema(array $columns, string $tableName): void } } - parent::testColumnSchema($columns, $tableName); + parent::testColumns($columns, $tableName); } /** * @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\SchemaProvider::columnsTypeBit */ - public function testColumnSchemaWithTypeBit(array $columns): void + public function testColumnWithTypeBit(array $columns): void { - $this->columnSchema($columns, 'type_bit'); + $this->assertTableColumns($columns, 'type_bit'); } /**