From c161161717a17874a8cd93748f30c23a05bafe57 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 10 Feb 2025 10:21:57 +0700 Subject: [PATCH] Add `ReferentialAction` class --- docs/guide/en/command/ddl.md | 5 +- docs/guide/pt-BR/command/ddl.md | 5 +- src/Command/CommandInterface.php | 18 +++-- src/Constant/ReferentialAction.php | 46 +++++++++++++ src/Constraint/ForeignKeyConstraint.php | 14 ++++ .../AbstractColumnDefinitionBuilder.php | 5 ++ src/QueryBuilder/DDLQueryBuilderInterface.php | 11 +-- tests/Db/Command/CommandTest.php | 14 ++-- tests/Provider/CommandProvider.php | 69 +++++++++++-------- tests/Provider/QueryBuilderProvider.php | 13 ++-- tests/Provider/SchemaProvider.php | 5 +- 11 files changed, 142 insertions(+), 63 deletions(-) create mode 100644 src/Constant/ReferentialAction.php diff --git a/docs/guide/en/command/ddl.md b/docs/guide/en/command/ddl.md index 96519aa4c..d740e3bd9 100644 --- a/docs/guide/en/command/ddl.md +++ b/docs/guide/en/command/ddl.md @@ -181,6 +181,7 @@ method: ```php use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constant\ReferentialAction; /** @var ConnectionInterface $db */ $db->createCommand()->addForeignKey( @@ -189,8 +190,8 @@ $db->createCommand()->addForeignKey( 'profile_id', '{{%profile}}', 'id', - 'CASCADE', - 'CASCADE' + ReferentialAction::CASCADE, + ReferentialAction::CASCADE )->execute(); ``` diff --git a/docs/guide/pt-BR/command/ddl.md b/docs/guide/pt-BR/command/ddl.md index 82d22cf7f..ebaa68498 100644 --- a/docs/guide/pt-BR/command/ddl.md +++ b/docs/guide/pt-BR/command/ddl.md @@ -177,6 +177,7 @@ Para adicionar uma chave estrangeira a uma tabela existente, vocĂȘ pode usar o m ```php use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constant\ReferentialAction; /** @var ConnectionInterface $db */ $db->createCommand()->addForeignKey( @@ -185,8 +186,8 @@ $db->createCommand()->addForeignKey( 'profile_id', '{{%profile}}', 'id', - 'CASCADE', - 'CASCADE' + ReferentialAction::CASCADE, + ReferentialAction::CASCADE )->execute(); ``` diff --git a/src/Command/CommandInterface.php b/src/Command/CommandInterface.php index f88b469bf..8801050cd 100644 --- a/src/Command/CommandInterface.php +++ b/src/Command/CommandInterface.php @@ -12,6 +12,7 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\IndexType; use Yiisoft\Db\Constant\PseudoType; +use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -99,8 +100,7 @@ public function addDefaultValue(string $table, string $name, string $column, mix /** * Creates an SQL command for adding a foreign key constraint to an existing table. - * - * The method will quote the table and column names. + * The method will quote the `name`, `table`, `referenceTable` parameters before using them in the generated SQL. * * @param string $table The name of the table to add foreign key constraint to. * @param string $name The name of the foreign key constraint. @@ -109,16 +109,14 @@ public function addDefaultValue(string $table, string $name, string $column, mix * @param string $referenceTable The name of the table that the foreign key references to. * @param array|string $referenceColumns The name of the column that the foreign key references to. If there are * many columns, separate them with commas. - * @param string|null $delete The `ON DELETE` option. Most DBMS support these options: `RESTRICT`, `CASCADE`, `NO ACTION`, - * `SET DEFAULT`, `SET NULL`. - * @param string|null $update The `ON UPDATE` option. Most DBMS support these options: `RESTRICT`, `CASCADE`, `NO ACTION`, - * `SET DEFAULT`, `SET NULL`. + * @param string|null $delete The `ON DELETE` option. See {@see ReferentialAction} class for possible values. + * @param string|null $update The `ON UPDATE` option. See {@see ReferentialAction} class for possible values. * * @throws Exception * @throws InvalidArgumentException * - * Note: The method will quote the `name`, `table`, `referenceTable` parameters before using them in the generated - * SQL. + * @psalm-param ReferentialAction::*|null $delete + * @psalm-param ReferentialAction::*|null $update */ public function addForeignKey( string $table, @@ -126,8 +124,8 @@ public function addForeignKey( array|string $columns, string $referenceTable, array|string $referenceColumns, - string $delete = null, - string $update = null + string|null $delete = null, + string|null $update = null ): static; /** diff --git a/src/Constant/ReferentialAction.php b/src/Constant/ReferentialAction.php new file mode 100644 index 000000000..f264f3463 --- /dev/null +++ b/src/Constant/ReferentialAction.php @@ -0,0 +1,46 @@ +getConnection(); - $command = $db->createCommand(); - $sql = $command->addForeignKey($tableName, $name, $column1, $tableName, $column2, $delete, $update)->getSql(); + + $name = '{{fk_constraint}}'; + $tableName = '{{fk_table}}'; + $referenceTable = '{{fk_referenced_table}}'; + + $sql = $command->addForeignKey($tableName, $name, $columns, $referenceTable, $referenceColumns, $delete, $update)->getSql(); $this->assertSame($expected, $sql); } diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 7f559e01f..bb3bf6d8d 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -11,6 +11,7 @@ use Yiisoft\Db\Command\Param; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\IndexType; +use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnBuilder; @@ -38,113 +39,121 @@ public static function addForeignKeySql(): array { return [ [ - '{{test_fk_constraint_1}}', - '{{test_fk}}', 'int1', 'int3', null, null, DbHelper::replaceQuotes( <<foreignColumnNames(['id']); $reference->foreignTableName('ref_table'); - $reference->onDelete('CASCADE'); - $reference->onUpdate('CASCADE'); + $reference->onDelete(ReferentialAction::CASCADE); + $reference->onUpdate(ReferentialAction::CASCADE); $referenceWithSchema = clone $reference; $referenceWithSchema->foreignSchemaName('ref_schema'); diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index fe840547d..970826173 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -6,6 +6,7 @@ use PDO; use Yiisoft\Db\Constant\IndexType; +use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Constraint\CheckConstraint; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Constraint\ForeignKeyConstraint; @@ -105,8 +106,8 @@ public static function constraints(): array ->columnNames(['C_fk_id_1', 'C_fk_id_2']) ->foreignTableName('T_constraints_2') ->foreignColumnNames(['C_id_1', 'C_id_2']) - ->onDelete('CASCADE') - ->onUpdate('CASCADE'), + ->onDelete(ReferentialAction::CASCADE) + ->onUpdate(ReferentialAction::CASCADE), ], ], '3: unique' => ['T_constraints_3', SchemaInterface::UNIQUES, []],