From 524378bdd0e381c6a0b04c6605c019f13f60da7f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 17 Sep 2024 14:41:54 +0300 Subject: [PATCH] Add `$ifExists` and `$cascade` to `dropTable()` methods --- CHANGELOG.md | 1 + src/Command.php | 9 +++++++++ src/Connection.php | 2 +- tests/CommandTest.php | 13 +++++++++---- tests/Support/TestTrait.php | 6 +----- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc3a3236..b1ae5016 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Enh #314: Implement `ColumnFactory` class (@Tigrov) - Enh #317: Separate column type constants (@Tigrov) - Enh #318: Realize `ColumnBuilder` class (@Tigrov) +- Enh #319: Set more specific result type in `Connection::createCommand()` method (@vjik) ## 1.2.0 March 21, 2024 diff --git a/src/Command.php b/src/Command.php index 6996cd7f..f46647e9 100644 --- a/src/Command.php +++ b/src/Command.php @@ -8,6 +8,7 @@ use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; +use Yiisoft\Db\Exception\NotSupportedException; use function array_pop; use function count; @@ -21,6 +22,14 @@ */ final class Command extends AbstractPdoCommand { + public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): static + { + if ($cascade) { + throw new NotSupportedException('SQLite doesn\'t support cascade drop table.'); + } + return parent::dropTable($table, $ifExists); + } + public function insertWithReturningPks(string $table, array $columns): bool|array { $params = []; diff --git a/src/Connection.php b/src/Connection.php index c1308d87..5aaac6a8 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -36,7 +36,7 @@ public function __clone() } } - public function createCommand(string $sql = null, array $params = []): PdoCommandInterface + public function createCommand(string $sql = null, array $params = []): Command { $command = new Command($this); diff --git a/tests/CommandTest.php b/tests/CommandTest.php index c68b5ffa..facb871f 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -292,10 +292,15 @@ public function testDropPrimaryKey(): void $command->dropPrimaryKey('{{table}}', '{{name}}'); } - /** - * @throws Exception - * @throws InvalidConfigException - */ + public function testDropTableCascade(): void + { + $command = $this->getConnection()->createCommand(); + + $this->expectException(NotSupportedException::class); + $this->expectExceptionMessage('SQLite doesn\'t support cascade drop table.'); + $command->dropTable('{{table}}', cascade: true); + } + public function testDropUnique(): void { $db = $this->getConnection(); diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 93cda560..32db1d8c 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -16,11 +16,7 @@ trait TestTrait { private string $dsn = ''; - /** - * @throws Exception - * @throws InvalidConfigException - */ - protected function getConnection(bool $fixture = false): PdoConnectionInterface + protected function getConnection(bool $fixture = false): Connection { $db = new Connection(new Driver($this->getDsn()), DbHelper::getSchemaCache());