diff --git a/CHANGELOG.md b/CHANGELOG.md index f322f100..ba126669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Enh #324: Use constructor to create columns and initialize properties (@Tigrov) - Enh #327: Refactor `Schema::findColumns()` method (@Tigrov) - Enh #328: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov) +- Enh #331: Refactor according to changes #902 in `yiisoft/db` package (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 84e05220..d36cc5d4 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -10,6 +10,10 @@ use Yiisoft\Db\Schema\Column\AbstractColumnFactory; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; +use function hex2bin; +use function str_starts_with; +use function substr; + final class ColumnFactory extends AbstractColumnFactory { /** @@ -93,6 +97,10 @@ protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnSche $defaultValue = substr($defaultValue, 1, -1); } + if (str_starts_with($defaultValue, '0x')) { + return hex2bin(substr($defaultValue, 2)); + } + return parent::normalizeNotNullDefaultValue($defaultValue, $column); } } diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index e446077c..a9f2685f 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -44,7 +44,7 @@ public function addDefaultValue(string $table, string $name, string $column, mix . $this->quoter->quoteTableName($table) . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) - . ' DEFAULT ' . ($value === null ? 'NULL' : (string) $this->quoter->quoteValue($value)) + . ' DEFAULT ' . $this->queryBuilder->prepareValue($value) . ' FOR ' . $this->quoter->quoteColumnName($column); } @@ -194,9 +194,9 @@ private function buildAddCommentSql(string $comment, string $table, string $colu $schemaName = $tableSchema->getSchemaName() ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; - $tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); - $columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; - $comment = 'N' . (string) $this->quoter->quoteValue($comment); + $tableName = 'N' . $this->quoter->quoteValue($tableSchema->getName()); + $columnName = $column ? 'N' . $this->quoter->quoteValue($column) : null; + $comment = 'N' . $this->quoter->quoteValue($comment); $functionParams = " @name = N'MS_description', @value = $comment, @@ -245,8 +245,8 @@ private function buildRemoveCommentSql(string $table, string $column = null): st $schemaName = $tableSchema->getSchemaName() ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; - $tableName = 'N' . (string) $this->quoter->quoteValue($tableSchema->getName()); - $columnName = $column ? 'N' . (string) $this->quoter->quoteValue($column) : null; + $tableName = 'N' . $this->quoter->quoteValue($tableSchema->getName()); + $columnName = $column ? 'N' . $this->quoter->quoteValue($column) : null; return " IF EXISTS ( diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 5d739626..f52d0dfe 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -19,6 +19,10 @@ */ final class QueryBuilder extends AbstractQueryBuilder { + protected const FALSE_VALUE = '0'; + + protected const TRUE_VALUE = '1'; + /** * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values). */ diff --git a/src/Schema.php b/src/Schema.php index 84193b1d..06bc0686 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -144,7 +144,7 @@ protected function findTableComment(TableSchemaInterface $tableSchema): void { $schemaName = $tableSchema->getSchemaName() ? "N'" . (string) $tableSchema->getSchemaName() . "'" : 'SCHEMA_NAME()'; - $tableName = 'N' . (string) $this->db->getQuoter()->quoteValue($tableSchema->getName()); + $tableName = 'N' . $this->db->getQuoter()->quoteValue($tableSchema->getName()); $sql = << '0', + 'TRUE' => '1', + ]); + } + + return $rawSql; + } } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 3e3c931d..9c5aa76f 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -411,7 +411,7 @@ public static function buildColumnDefinition(): array $values["defaultValue('')"][0] = "nvarchar(255) DEFAULT ''"; $values['defaultValue(null)'][0] = 'nvarchar(255) DEFAULT NULL'; $values['defaultValue($expression)'][0] = 'int DEFAULT (1 + 2)'; - $values['notNull()->defaultValue(null)'][0] = 'nvarchar(255) NOT NULL'; + $values['defaultValue($emptyExpression)'][0] = 'int'; $values["integer()->defaultValue('')"][0] = 'int DEFAULT NULL'; $values['notNull()'][0] = 'nvarchar(255) NOT NULL'; $values['null()'][0] = 'nvarchar(255) NULL'; @@ -425,4 +425,24 @@ public static function buildColumnDefinition(): array return $values; } + + public static function prepareParam(): array + { + $values = parent::prepareParam(); + + $values['true'][0] = '1'; + $values['false'][0] = '0'; + + return $values; + } + + public static function prepareValue(): array + { + $values = parent::prepareValue(); + + $values['true'][0] = '1'; + $values['false'][0] = '0'; + + return $values; + } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 273895e0..c958d82f 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Mssql\Tests; use JsonException; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Throwable; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; @@ -15,6 +16,7 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Mssql\Column; +use Yiisoft\Db\Mssql\Tests\Provider\QueryBuilderProvider; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; @@ -1152,9 +1154,21 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e parent::testSelectScalar($columns, $expected); } - /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\QueryBuilderProvider::buildColumnDefinition() */ + #[DataProviderExternal(QueryBuilderProvider::class, 'buildColumnDefinition')] public function testBuildColumnDefinition(string $expected, ColumnSchemaInterface|string $column): void { parent::testBuildColumnDefinition($expected, $column); } + + #[DataProviderExternal(QueryBuilderProvider::class, 'prepareParam')] + public function testPrepareParam(string $expected, mixed $value, int $type): void + { + parent::testPrepareParam($expected, $value, $type); + } + + #[DataProviderExternal(QueryBuilderProvider::class, 'prepareValue')] + public function testPrepareValue(string $expected, mixed $value): void + { + parent::testPrepareValue($expected, $value); + } }