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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
- Enh #361, #362: Refactor `DMLQueryBuilder::insertWithReturningPks()` method (@Tigrov)
- New #361, #362: Implement `DMLQueryBuilder::upsertReturning()` method (@Tigrov)
- Chg #363: Add alias in `DQLQueryBuilder::selectExists()` method for consistency with other DBMS (@Tigrov)
- Enh #364: Refactor constraints (@Tigrov)
- Enh #364, #377: Refactor constraints (@Tigrov)
- Chg #366: Rename `DMLQueryBuilder::insertWithReturningPks()` to `DMLQueryBuilder::insertReturningPks()` (@Tigrov)
- Enh #372: Provide `yiisoft/db-implementation` virtual package (@vjik)
- Enh #375: Adapt to `Param` refactoring in `yiisoft/db` package (@vjik)
Expand Down
2 changes: 1 addition & 1 deletion src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private function prepareUpsertParts(

foreach ($constraints as $constraint) {
$constraintCondition = ['and'];
$columnNames = $constraint->getColumnNames();
$columnNames = $constraint->columnNames;

foreach ($columnNames as $name) {
$quotedName = $this->quoter->quoteColumnName($name);
Expand Down
41 changes: 21 additions & 20 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Yiisoft\Db\Mssql;

use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\DefaultValueConstraint;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Constraint\Check;
use Yiisoft\Db\Constraint\DefaultValue;
use Yiisoft\Db\Constraint\ForeignKey;
use Yiisoft\Db\Constraint\Index;
use Yiisoft\Db\Driver\Pdo\AbstractPdoSchema;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Helper\DbArrayHelper;
Expand Down Expand Up @@ -197,15 +197,15 @@ protected function loadTableSchema(string $name): TableSchemaInterface|null
return null;
}

protected function loadTablePrimaryKey(string $tableName): IndexConstraint|null
protected function loadTablePrimaryKey(string $tableName): Index|null
{
/** @var IndexConstraint|null */
/** @var Index|null */
return $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
}

protected function loadTableForeignKeys(string $tableName): array
{
/** @var ForeignKeyConstraint[] */
/** @var ForeignKey[] */
return $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
}

Expand Down Expand Up @@ -241,7 +241,7 @@ protected function loadTableIndexes(string $tableName): array
* > $indexes
*/
foreach ($indexes as $name => $index) {
$result[] = new IndexConstraint(
$result[] = new Index(
$name,
array_column($index, 'column_name'),
(bool) $index[0]['is_unique'],
Expand All @@ -254,19 +254,19 @@ protected function loadTableIndexes(string $tableName): array

protected function loadTableUniques(string $tableName): array
{
/** @var IndexConstraint[] */
/** @var Index[] */
return $this->loadTableConstraints($tableName, self::UNIQUES);
}

protected function loadTableChecks(string $tableName): array
{
/** @var CheckConstraint[] */
/** @var Check[] */
return $this->loadTableConstraints($tableName, self::CHECKS);
}

protected function loadTableDefaultValues(string $tableName): array
{
/** @var DefaultValueConstraint[] */
/** @var DefaultValue[] */
return $this->loadTableConstraints($tableName, self::DEFAULTS);
}

Expand Down Expand Up @@ -537,10 +537,10 @@ public function findUniqueIndexes(TableSchemaInterface $table): array
* - checks
* - defaults
*
* @return CheckConstraint[]|DefaultValueConstraint[]|ForeignKeyConstraint[]|IndexConstraint|IndexConstraint[]|null
* @return Check[]|DefaultValue[]|ForeignKey[]|Index|Index[]|null
* Constraints of the specified type.
*/
private function loadTableConstraints(string $tableName, string $returnType): array|IndexConstraint|null
private function loadTableConstraints(string $tableName, string $returnType): array|Index|null
{
$sql = <<<SQL
SELECT
Expand Down Expand Up @@ -604,31 +604,32 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
foreach ($names as $name => $constraint) {
/** @psalm-suppress ArgumentTypeCoercion */
match ($type) {
'PK' => $result[self::PRIMARY_KEY] = new IndexConstraint(
'PK' => $result[self::PRIMARY_KEY] = new Index(
$name,
array_column($constraint, 'column_name'),
true,
true,
),
'F' => $result[self::FOREIGN_KEYS][] = new ForeignKeyConstraint(
'F' => $result[self::FOREIGN_KEYS][] = new ForeignKey(
$name,
array_column($constraint, 'column_name'),
$constraint[0]['foreign_table_schema'] . '.' . $constraint[0]['foreign_table_name'],
$constraint[0]['foreign_table_schema'],
$constraint[0]['foreign_table_name'],
array_column($constraint, 'foreign_column_name'),
str_replace('_', ' ', $constraint[0]['on_update']),
str_replace('_', ' ', $constraint[0]['on_delete']),
str_replace('_', ' ', $constraint[0]['on_update']),
),
'UQ' => $result[self::UNIQUES][] = new IndexConstraint(
'UQ' => $result[self::UNIQUES][] = new Index(
$name,
array_column($constraint, 'column_name'),
true,
),
'C' => $result[self::CHECKS][] = new CheckConstraint(
'C' => $result[self::CHECKS][] = new Check(
$name,
array_column($constraint, 'column_name'),
$constraint[0]['check_expr'],
),
'D' => $result[self::DEFAULTS][] = new DefaultValueConstraint(
'D' => $result[self::DEFAULTS][] = new DefaultValue(
$name,
array_column($constraint, 'column_name'),
$constraint[0]['default_expr'],
Expand Down
17 changes: 7 additions & 10 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Mssql\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Constraint\Index;
use Yiisoft\Db\Exception\IntegrityException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
Expand Down Expand Up @@ -301,13 +302,10 @@ public function testCreateClusteredColumnstoreIndex(): void
$command->createTable($tableName, ['col1' => ColumnBuilder::integer()])->execute();
$command->createIndex($tableName, $indexName, '', IndexType::CLUSTERED_COLUMNSTORE)->execute();

$this->assertCount(1, $schema->getTableIndexes($tableName));

$index = $schema->getTableIndexes($tableName)[0];

$this->assertSame(['col1'], $index->getColumnNames());
$this->assertFalse($index->isUnique());
$this->assertFalse($index->isPrimaryKey());
$this->assertEquals(
[new Index($indexName, ['col1'])],
$schema->getTableIndexes($tableName),
);

$db->close();
}
Expand All @@ -333,10 +331,9 @@ public function testCreateXmlIndex(): void

$this->assertCount(3, $schema->getTableIndexes($tableName));

$index = array_filter($schema->getTableIndexes($tableName), static fn ($index) => !$index->isPrimaryKey())[1];
$index = array_filter($schema->getTableIndexes($tableName), static fn ($index) => !$index->isPrimaryKey)[1];

$this->assertSame($indexColumns, $index->getColumnNames());
$this->assertFalse($index->isUnique());
$this->assertEquals(new Index($xmlIndexName, $indexColumns), $index);

$db->close();
}
Expand Down
19 changes: 10 additions & 9 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Constant\ReferentialAction;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Constraint\ForeignKey;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mssql\Column\ColumnBuilder;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
Expand Down Expand Up @@ -774,14 +774,15 @@ public static function buildColumnDefinition(): array
$values['unique()'][0] = 'nvarchar(255) UNIQUE';
$values['unsigned()'][0] = 'int';
$values['integer(8)->scale(2)'][0] = 'int';
$values['reference($reference)'][0] = 'int REFERENCES [ref_table] ([id]) ON DELETE CASCADE ON UPDATE CASCADE';
$values['reference($referenceWithSchema)'][0] = 'int REFERENCES [ref_schema].[ref_table] ([id]) ON DELETE CASCADE ON UPDATE CASCADE';

$referenceRestrict = new ForeignKeyConstraint();
$referenceRestrict->foreignColumnNames(['id']);
$referenceRestrict->foreignTableName('ref_table');
$referenceRestrict->onDelete(ReferentialAction::RESTRICT);
$referenceRestrict->onUpdate(ReferentialAction::RESTRICT);
$values['reference($reference)'][0] = 'int REFERENCES [ref_table] ([id]) ON DELETE SET NULL ON UPDATE CASCADE';
$values['reference($referenceWithSchema)'][0] = 'int REFERENCES [ref_schema].[ref_table] ([id]) ON DELETE SET NULL ON UPDATE CASCADE';

$referenceRestrict = new ForeignKey(
foreignTableName: 'ref_table',
foreignColumnNames: ['id'],
onDelete: ReferentialAction::RESTRICT,
onUpdate: ReferentialAction::RESTRICT,
);

$values[] = ['int REFERENCES [ref_table] ([id])', ColumnBuilder::integer()->reference($referenceRestrict)];

Expand Down
11 changes: 5 additions & 6 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use DateTimeImmutable;
use DateTimeZone;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constraint\DefaultValueConstraint;
use Yiisoft\Db\Constraint\DefaultValue;
use Yiisoft\Db\Mssql\Column\BinaryColumn;
use Yiisoft\Db\Mssql\Column\DateTimeColumn;
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\Support\Assert;

final class SchemaProvider extends \Yiisoft\Db\Tests\Provider\SchemaProvider
{
Expand Down Expand Up @@ -136,12 +137,10 @@ public static function constraints(): array
{
$constraints = parent::constraints();

$constraints['1: check'][2][0]->expression('([C_check]<>\'\')');
$constraints['1: default'][2] = [new DefaultValueConstraint('', ['C_default'], '((0))')];

Assert::setPropertyValue($constraints['1: check'][2][0], 'expression', "([C_check]<>'')");
$constraints['1: default'][2] = [new DefaultValue('', ['C_default'], '((0))')];
$constraints['2: default'][2] = [];

$constraints['3: foreign key'][2][0]->foreignTableName('dbo.T_constraints_2');
Assert::setPropertyValue($constraints['3: foreign key'][2][0], 'foreignSchemaName', 'dbo');
$constraints['3: index'][2] = [];
$constraints['3: default'][2] = [];
$constraints['4: default'][2] = [];
Expand Down