Skip to content

Commit

Permalink
Merge branch 'master' into ease-local-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Dec 2, 2024
2 parents 7d72e82 + ca73018 commit e8b8a86
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -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);
}
}
12 changes: 6 additions & 6 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<SQL
SELECT [value]
Expand Down
3 changes: 3 additions & 0 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public static function defaultValueRaw(): array
$defaultValueRaw[] = [ColumnType::TEXT, "(('str''ing'))", "str'ing"];
$defaultValueRaw[] = [ColumnType::INTEGER, '((-1))', -1];
$defaultValueRaw[] = [ColumnType::TIMESTAMP, '((now()))', new Expression('(now())')];
$defaultValueRaw[] = [ColumnType::BOOLEAN, '((1))', true];
$defaultValueRaw[] = [ColumnType::BOOLEAN, '((0))', false];
$defaultValueRaw[] = [ColumnType::BINARY, '(0x737472696e67)', 'string'];

return $defaultValueRaw;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function json_encode;
use function serialize;
use function strtr;

final class CommandProvider extends \Yiisoft\Db\Tests\Provider\CommandProvider
{
Expand Down Expand Up @@ -55,4 +56,18 @@ public static function dataInsertVarbinary(): array
],
];
}

public static function rawSql(): array
{
$rawSql = parent::rawSql();

foreach ($rawSql as &$values) {
$values[2] = strtr($values[2], [
'FALSE' => '0',
'TRUE' => '1',
]);
}

return $rawSql;
}
}
22 changes: 21 additions & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}
}
16 changes: 15 additions & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit e8b8a86

Please sign in to comment.