Skip to content

Commit

Permalink
Fix issue yiisoft#762
Browse files Browse the repository at this point in the history
  • Loading branch information
evil1 committed Dec 25, 2024
1 parent 4f9a12a commit 804c637
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Schema/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions src/Schema/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
33 changes: 33 additions & 0 deletions tests/Common/CommonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/Db/Schema/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 804c637

Please sign in to comment.