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 normalizeDefaultValue() #328

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,13 @@ protected function getColumnClass(string $type, array $info = []): string

return parent::getColumnClass($type, $info);
}

protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnSchemaInterface $column): mixed
{
if ($defaultValue[0] === '(' && $defaultValue[-1] === ')') {
$defaultValue = substr($defaultValue, 1, -1);
}

return parent::normalizeNotNullDefaultValue($defaultValue, $column);
}
}
47 changes: 2 additions & 45 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use function array_map;
use function is_array;
use function md5;
use function preg_match;
use function serialize;
use function str_replace;

Expand Down Expand Up @@ -369,10 +368,11 @@ protected function loadTableDefaultValues(string $tableName): array
*/
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$column = $this->getColumnFactory()->fromDbType($info['data_type'], [
return $this->getColumnFactory()->fromDbType($info['data_type'], [
'autoIncrement' => $info['is_identity'] === '1',
'comment' => $info['comment'],
'computed' => $info['is_computed'] === '1',
'defaultValueRaw' => $info['column_default'],
'name' => $info['column_name'],
'notNull' => $info['is_nullable'] !== 'YES',
'primaryKey' => $info['primaryKey'],
Expand All @@ -381,36 +381,6 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
'size' => $info['size'] !== null ? (int) $info['size'] : null,
'table' => $info['table'],
]);

$column->defaultValue($this->normalizeDefaultValue($info['column_default'], $column));

return $column;
}

/**
* Converts column's default value according to {@see ColumnSchema::phpType} after retrieval from the database.
*
* @param string|null $defaultValue The default value retrieved from the database.
* @param ColumnSchemaInterface $column The column schema object.
*
* @return mixed The normalized default value.
*/
private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed
{
if (
$defaultValue === null
|| $defaultValue === '(NULL)'
|| $column->isPrimaryKey()
|| $column->isComputed()
) {
return null;
}

$value = $this->parseDefaultValue($defaultValue);

return is_numeric($value)
? $column->phpTypeCast($value)
: $value;
}

/**
Expand Down Expand Up @@ -828,17 +798,4 @@ protected function getCacheTag(): string
{
return md5(serialize([self::class, ...$this->generateCacheKey()]));
}

private function parseDefaultValue(string $value): string
{
if (preg_match('/^\'(.*)\'$/', $value, $matches)) {
return $matches[1];
}

if (preg_match('/^\((.*)\)$/', $value, $matches)) {
return $this->parseDefaultValue($matches[1]);
}

return $value;
}
}
16 changes: 12 additions & 4 deletions tests/ColumnFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Db\Mssql\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\AbstractColumnFactoryTest;

Expand All @@ -14,13 +16,13 @@ final class ColumnFactoryTest extends AbstractColumnFactoryTest
{
use TestTrait;

/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::dbTypes */
#[DataProviderExternal(ColumnFactoryProvider::class, 'dbTypes')]
public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void
{
parent::testFromDbType($dbType, $expectedType, $expectedInstanceOf);
}

/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::definitions */
#[DataProviderExternal(ColumnFactoryProvider::class, 'definitions')]
public function testFromDefinition(
string $definition,
string $expectedType,
Expand All @@ -30,7 +32,7 @@ public function testFromDefinition(
parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedMethodResults);
}

/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::pseudoTypes */
#[DataProviderExternal(ColumnFactoryProvider::class, 'pseudoTypes')]
public function testFromPseudoType(
string $pseudoType,
string $expectedType,
Expand All @@ -40,9 +42,15 @@ public function testFromPseudoType(
parent::testFromPseudoType($pseudoType, $expectedType, $expectedInstanceOf, $expectedMethodResults);
}

/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::types */
#[DataProviderExternal(ColumnFactoryProvider::class, 'types')]
public function testFromType(string $type, string $expectedType, string $expectedInstanceOf): void
{
parent::testFromType($type, $expectedType, $expectedInstanceOf);
}

#[DataProviderExternal(ColumnFactoryProvider::class, 'defaultValueRaw')]
public function testFromTypeDefaultValueRaw(string $type, string|null $defaultValueRaw, mixed $expected): void
{
parent::testFromTypeDefaultValueRaw($type, $defaultValueRaw, $expected);
}
}
14 changes: 14 additions & 0 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ public static function dbTypes(): array
['table', ColumnType::STRING, StringColumnSchema::class],
];
}

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

$defaultValueRaw['(now())'][2] = new Expression('now()');

$defaultValueRaw[] = [ColumnType::TEXT, '(NULL)', null];
$defaultValueRaw[] = [ColumnType::TEXT, "(('str''ing'))", "str'ing"];
$defaultValueRaw[] = [ColumnType::INTEGER, '((-1))', -1];
$defaultValueRaw[] = [ColumnType::TIMESTAMP, '((now()))', new Expression('(now())')];

return $defaultValueRaw;
}
}
6 changes: 4 additions & 2 deletions tests/Provider/Type/BinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Yiisoft\Db\Mssql\Tests\Provider\Type;

use Yiisoft\Db\Expression\Expression;

final class BinaryProvider
{
public static function columns(): array
{
return [
['Mybinary1', 'binary', 'mixed', 10, 'CONVERT([binary](10),\'binary\')'],
['Mybinary2', 'binary', 'mixed', 1, 'CONVERT([binary](1),\'b\')'],
['Mybinary1', 'binary', 'mixed', 10, new Expression("CONVERT([binary](10),'binary')")],
['Mybinary2', 'binary', 'mixed', 1, new Expression("CONVERT([binary](1),'b')")],
];
}
}
4 changes: 3 additions & 1 deletion tests/Provider/Type/GeometryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Yiisoft\Db\Mssql\Tests\Provider\Type;

use Yiisoft\Db\Expression\Expression;

final class GeometryProvider
{
public static function columns(): array
{
return [
['Mygeometry1', 'geometry', 'string', '[geometry]::STGeomFromText(\'POINT(0 0)\',(0))'],
['Mygeometry1', 'geometry', 'string', new Expression("[geometry]::STGeomFromText('POINT(0 0)',(0))")],
['Mygeometry2', 'geometry', 'string', null],
];
}
Expand Down
8 changes: 5 additions & 3 deletions tests/Provider/Type/VarBinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace Yiisoft\Db\Mssql\Tests\Provider\Type;

use Yiisoft\Db\Expression\Expression;

final class VarBinaryProvider
{
public static function columns(): array
{
return [
['Myvarbinary1', 'varbinary', 'mixed', 10, 'CONVERT([varbinary](10),\'varbinary\')'],
['Myvarbinary2', 'varbinary', 'mixed', 100, 'CONVERT([varbinary](100),\'v\')'],
['Myvarbinary3', 'varbinary', 'mixed', 20, 'hashbytes(\'MD5\',\'test string\')'],
['Myvarbinary1', 'varbinary', 'mixed', 10, new Expression("CONVERT([varbinary](10),'varbinary')")],
['Myvarbinary2', 'varbinary', 'mixed', 100, new Expression("CONVERT([varbinary](100),'v')")],
['Myvarbinary3', 'varbinary', 'mixed', 20, new Expression("hashbytes('MD5','test string')")],
];
}
}
4 changes: 3 additions & 1 deletion tests/Provider/Type/VarCharProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace Yiisoft\Db\Mssql\Tests\Provider\Type;

use Yiisoft\Db\Expression\Expression;

final class VarCharProvider
{
public static function columns(): array
{
return [
['Myvarchar1', 'varchar', 'string', 10, 'varchar'],
['Myvarchar2', 'varchar', 'string', 100, 'v'],
['Myvarchar3', 'varchar', 'string', 20, 'TRY_CAST(datepart(year,getdate()) AS [varchar](20))'],
['Myvarchar3', 'varchar', 'string', 20, new Expression('TRY_CAST(datepart(year,getdate()) AS [varchar](20))')],
];
}
}
2 changes: 1 addition & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public function testAlterColumnOnDb(): void
$db->createCommand($sql)->execute();
$schema = $db->getTableSchema('[foo1]', true);
$this->assertSame(ColumnType::DATETIME, $schema?->getColumn('bar')->getDbType());
$this->assertSame('getdate()', $schema?->getColumn('bar')->getDefaultValue());
$this->assertEquals(new Expression('getdate()'), $schema?->getColumn('bar')->getDefaultValue());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Type/BinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testCreateTableWithDefaultValue(
string $dbType,
string $phpType,
int $size,
string $defaultValue
Expression $defaultValue
): void {
$db = $this->buildTable();

Expand All @@ -48,7 +48,7 @@ public function testCreateTableWithDefaultValue(
$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($size, $tableSchema?->getColumn($column)->getSize());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('binary_default')->execute();
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public function testDefaultValue(
string $dbType,
string $phpType,
int $size,
string $defaultValue
Expression $defaultValue
): void {
$this->setFixture('Type/binary.sql');

Expand All @@ -103,7 +103,7 @@ public function testDefaultValue(
$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($size, $tableSchema?->getColumn($column)->getSize());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('binary_default')->execute();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Type/GeometryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public function testCreateTableWithDefaultValue(
string $column,
string $dbType,
string $phpType,
string|null $defaultValue
Expression|null $defaultValue
): void {
$db = $this->buildTable();

$tableSchema = $db->getTableSchema('geometry_default');

$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('geometry_default')->execute();
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public function testDefaultValue(
string $column,
string $dbType,
string $phpType,
string|null $defaultValue
Expression|null $defaultValue
): void {
$this->setFixture('Type/geometry.sql');

Expand All @@ -99,7 +99,7 @@ public function testDefaultValue(

$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('geometry_default')->execute();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Type/VarBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testCreateTableWithDefaultValue(
string $dbType,
string $phpType,
int $size,
string $defaultValue
Expression $defaultValue
): void {
$db = $this->buildTable();

Expand All @@ -50,7 +50,7 @@ public function testCreateTableWithDefaultValue(
$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($size, $tableSchema?->getColumn($column)->getSize());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('varbinary_default')->execute();
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public function testDefaultValue(
string $dbType,
string $phpType,
int $size,
string $defaultValue
Expression $defaultValue
): void {
$this->setFixture('Type/varbinary.sql');

Expand All @@ -105,7 +105,7 @@ public function testDefaultValue(
$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($size, $tableSchema?->getColumn($column)->getSize());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('varbinary_default')->execute();
}
Expand Down
9 changes: 5 additions & 4 deletions tests/Type/VarCharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;

/**
Expand Down Expand Up @@ -38,7 +39,7 @@ public function testCreateTableWithDefaultValue(
string $dbType,
string $phpType,
int $size,
string $defaultValue
string|Expression $defaultValue
): void {
$db = $this->buildTable();

Expand All @@ -47,7 +48,7 @@ public function testCreateTableWithDefaultValue(
$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($size, $tableSchema?->getColumn($column)->getSize());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('varchar_default')->execute();
}
Expand Down Expand Up @@ -92,7 +93,7 @@ public function testDefaultValue(
string $dbType,
string $phpType,
int $size,
string $defaultValue
string|Expression $defaultValue
): void {
$this->setFixture('Type/varchar.sql');

Expand All @@ -102,7 +103,7 @@ public function testDefaultValue(
$this->assertSame($dbType, $tableSchema?->getColumn($column)->getDbType());
$this->assertSame($phpType, $tableSchema?->getColumn($column)->getPhpType());
$this->assertSame($size, $tableSchema?->getColumn($column)->getSize());
$this->assertSame($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());
$this->assertEquals($defaultValue, $tableSchema?->getColumn($column)->getDefaultValue());

$db->createCommand()->dropTable('varchar_default')->execute();
}
Expand Down
Loading