From 804c63752c6d3679d6a106dd3f5c1aedaf6a68bc Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Wed, 25 Dec 2024 08:41:49 +0500 Subject: [PATCH] Fix issue #762 --- src/Schema/AbstractSchema.php | 38 +++++++++++++++++++++++++++++++ src/Schema/SchemaInterface.php | 28 +++++++++++++++++++++++ tests/Common/CommonSchemaTest.php | 33 +++++++++++++++++++++++++++ tests/Db/Schema/SchemaTest.php | 14 ++++++++++++ 4 files changed, 113 insertions(+) diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 78b42cc95..5a52d9322 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -574,4 +574,42 @@ public function getViewNames(string $schema = '', bool $refresh = false): array return (array) $this->viewNames[$schema]; } + + /** + * @param string $tableName The table name. + * @param string $schema The schema of the tables. + * @return bool + * @throws Throwable + */ + public function hasTableName(string $tableName, string $schema = ''): bool + { + $tables = $this->getTableNames($schema); + + return in_array($tableName, $tables); + } + + /** + * @param string $schema The schema name. + * @return bool + * @throws Throwable + */ + public function hasSchemaName(string $schema): bool + { + $schemas = $this->getSchemaNames(); + + return in_array($schema, $schemas); + } + + /** + * @param string $viewName The view name. + * @param string $schema The schema of the views. + * @return bool + * @throws Throwable + */ + public function hasViewName(string $viewName, string $schema = ''): bool + { + $views = $this->getViewNames($schema); + + return in_array($viewName, $views); + } } diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 2632f16e4..9f05ec54d 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -416,4 +416,32 @@ public function enableCache(bool $value): void; * @return array All view names in the database. */ public function getViewNames(string $schema = '', bool $refresh = false): array; + + /** + * Determines if a specified table exists in the database. + * + * @param string $tableName The table name to search for + * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema + * name. If not empty, the table will be searched in the specified schema. + * @return bool Whether table exists or not + */ + public function hasTableName(string $tableName, string $schema = ''): bool; + + /** + * Determines if a specified schema exists in the database. + * + * @param string $schema The table name to search for + * @return bool Whether schema exists or not + */ + public function hasSchemaName(string $schema): bool; + + /** + * Determines if a specified view exists in the database. + * + * @param string $viewName The table name to search for + * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema + * name. If not empty, the table will be searched in the specified schema. + * @return bool Whether view exists or not + */ + public function hasViewName(string $viewName, string $schema = ''): bool; } diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index bbf5447da..5e6ce718d 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,6 +405,27 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } + public function testHasTableName(array $pdoAttributes): void + { + $db = $this->getConnection(true); + + foreach ($pdoAttributes as $name => $value) { + if ($name === PDO::ATTR_EMULATE_PREPARES) { + continue; + } + + $db->getPDO()?->setAttribute($name, $value); + } + + $schema = $db->getSchema(); + + $this->assertTrue($schema->hasTableName('customer')); + $this->assertTrue($schema->hasTableName('category')); + $this->assertFalse($schema->hasTableName('no_such_table')); + + $db->close(); + } + /** * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchema */ @@ -482,6 +503,18 @@ public function testGetViewNames(): void $db->close(); } + public function hasViewName(): void + { + $db = $this->getConnection(true); + + $schema = $db->getSchema(); + + $this->assertTrue($schema->hasViewName('animal_view')); + $this->assertFalse($schema->hasViewName('no_such_view')); + + $db->close(); + } + public function testNegativeDefaultValues(): void { $db = $this->getConnection(true); diff --git a/tests/Db/Schema/SchemaTest.php b/tests/Db/Schema/SchemaTest.php index 365a97d72..e4e7453ca 100644 --- a/tests/Db/Schema/SchemaTest.php +++ b/tests/Db/Schema/SchemaTest.php @@ -202,6 +202,20 @@ public function testGetSchemaNamesWithSchema(): void $this->assertSame(['dbo', 'public'], $schema->getSchemaNames()); } + public function testHasSchemaName(): void + { + $db = $this->getConnection(); + + $schema = $db->getSchema(); + Assert::setInaccessibleProperty($schema, 'schemaNames', ['dbo', 'public']); + + $this->assertTrue($schema->hasSchemaName('dbo')); + $this->assertTrue($schema->hasSchemaName('public')); + $this->assertFalse($schema->hasSchemaName('no_such_schema')); + + $db->close(); + } + /** * @throws NotSupportedException */