Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- Enh #406: Adapt to `DQLQueryBuilderInterface::buildWithQueries()` signature changes in `yiisoft/db` package (@vjik)
- Bug #408, #409: Add `identity` support in `Schema::loadResultColumn()` (@vjik)
- Chg #413: Throw exception on "unsigned" column usage (@vjik)
- New #415: Add enumeration column type support (@vjik)

## 1.2.0 March 21, 2024

Expand Down
17 changes: 7 additions & 10 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Db\Schema\Column\ColumnInterface;

use function ceil;
use function in_array;
use function strtoupper;

final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
Expand Down Expand Up @@ -68,19 +69,14 @@ protected function buildCheck(ColumnInterface $column): string

if (empty($check)) {
$name = $column->getName();

if (empty($name)) {
return '';
if (!empty($name)
&& in_array($column->getType(), [ColumnType::ARRAY, ColumnType::STRUCTURED, ColumnType::JSON], true)
) {
return ' CHECK (isjson(' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ') > 0)';
}

return match ($column->getType()) {
ColumnType::ARRAY, ColumnType::STRUCTURED, ColumnType::JSON
=> ' CHECK (isjson(' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ') > 0)',
default => '',
};
}

return " CHECK ($check)";
return parent::buildCheck($column);
}

protected function buildOnDelete(string $onDelete): string
Expand Down Expand Up @@ -139,6 +135,7 @@ protected function getDbType(ColumnInterface $column): string
ColumnType::ARRAY => 'nvarchar(max)',
ColumnType::STRUCTURED => 'nvarchar(max)',
ColumnType::JSON => 'nvarchar(max)',
ColumnType::ENUM => 'nvarchar',
default => 'nvarchar',
};

Expand Down
29 changes: 29 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ private function loadColumn(array $info): ColumnInterface
'schema' => $info['schema'],
'size' => $info['size'] !== null ? (int) $info['size'] : null,
'table' => $info['table'],
'values' => $this->tryGetEnumValuesFromCheck($info['column_name'], $info['check']),
]);
}

Expand Down Expand Up @@ -502,4 +503,32 @@ private function loadTableConstraints(string $tableName, string $returnType): ar

return $result[$returnType];
}

/**
* @psalm-return list<string>|null
*/
private function tryGetEnumValuesFromCheck(string $columnName, ?string $check): ?array
{
if ($check === null) {
return null;
}

$quotedColumnName = preg_quote($columnName, '~');
preg_match_all(
"~^\((?:\[$quotedColumnName\]='(?:''|[^'])*'(?:| OR ))+\)$~i",
Comment thread
vjik marked this conversation as resolved.
$check,
$block,
);

if (empty($block[0][0])) {
return null;
}

preg_match_all("~'((?:''|[^'])*)'~", $block[0][0], $matches);

return array_map(
static fn($v) => str_replace("''", "'", $v),
$matches[1],
);
}
}
56 changes: 56 additions & 0 deletions tests/Column/EnumColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mssql\Tests\Column;

use PHPUnit\Framework\Attributes\TestWith;
use Yiisoft\Db\Mssql\Tests\Support\IntegrationTestTrait;
use Yiisoft\Db\Schema\Column\EnumColumn;
use Yiisoft\Db\Tests\Common\CommonEnumColumnTest;

final class EnumColumnTest extends CommonEnumColumnTest
{
use IntegrationTestTrait;

#[TestWith(['INTEGER CHECK (status IN (1, 2, 3))'])]
#[TestWith(["NVARCHAR CHECK (status != 'abc')"])]
public function testNonEnumCheck(string $columnDefinition): void
{
$this->dropTable('test_enum_table');
$this->executeStatements(
<<<SQL
CREATE TABLE test_enum_table (
id INTEGER,
status $columnDefinition
)
SQL,
);

$db = $this->getSharedConnection();
$column = $db->getTableSchema('test_enum_table')->getColumn('status');

$this->assertNotInstanceOf(EnumColumn::class, $column);

$this->dropTable('test_enum_table');
}

protected function createDatabaseObjectsStatements(): array
{
return [
<<<SQL
CREATE TABLE tbl_enum (
id INT,
status NVARCHAR(8) CHECK(status IN ('pending', 'unactive', 'active'))
Comment thread
vjik marked this conversation as resolved.
)
SQL,
];
}

protected function dropDatabaseObjectsStatements(): array
{
return [
'DROP TABLE IF EXISTS tbl_enum',
];
}
}
Loading