Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #762 #913

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.0.0 under development

- Enh #762: Add methods `SchemaInterface::hasSchema()`, `SchemaInterface::hasTable()`, `SchemaInterface::hasView()` (@evil1)
evil1 marked this conversation as resolved.
Show resolved Hide resolved
- Enh #820: Support `Traversable` values for `AbstractDMLQueryBuilder::batchInsert()` method with empty columns (@Tigrov)
- Enh #815: Refactor `Query::column()` method (@Tigrov)
- Enh #816: Allow scalar values for `$columns` parameter of `Query::select()` and `Query::addSelect()` methods (@Tigrov)
Expand Down
30 changes: 30 additions & 0 deletions src/Schema/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,34 @@ public function getViewNames(string $schema = '', bool $refresh = false): array

return (array) $this->viewNames[$schema];
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for PHPDoc since it implements interface and there phpdoc already exists.

* @throws Throwable
*/
public function hasTable(string $tableName, string $schema = '', bool $refresh = false): bool
{
$tables = $this->getTableNames($schema, $refresh);

return in_array($tableName, $tables);
}

/**
* @throws Throwable
*/
public function hasSchema(string $schema, bool $refresh = false): bool
{
$schemas = $this->getSchemaNames($refresh);

return in_array($schema, $schemas);
}

/**
* @throws Throwable
*/
public function hasView(string $viewName, string $schema = '', bool $refresh = false): bool
{
$views = $this->getViewNames($schema, $refresh);

return in_array($viewName, $views);
}
}
37 changes: 37 additions & 0 deletions src/Schema/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,41 @@ 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
evil1 marked this conversation as resolved.
Show resolved Hide resolved
* @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.
* @param bool $refresh Whether to fetch the latest available table names. If this is false, view names fetched
* before (if available) will be returned.
*
* @return bool Whether table exists.
*/
public function hasTable(string $tableName, string $schema = '', bool $refresh = false): bool;

/**
* Determines if a specified schema exists in the database.
*
* @param string $schema The schema name to search for
* @param bool $refresh Whether to fetch the latest available schema names. If this is false, view names fetched
* before (if available) will be returned.
*
* @return bool Whether schema exists.
*/
public function hasSchema(string $schema, bool $refresh = false): bool;

/**
* Determines if a specified view exists in the database.
*
* @param string $viewName The view 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.
* @param bool $refresh Whether to fetch the latest available view names. If this is false, view names fetched
* before (if available) will be returned.
*
* @return bool Whether view exists.
*/
public function hasView(string $viewName, string $schema = '', bool $refresh = false): bool;
}
41 changes: 41 additions & 0 deletions tests/Common/CommonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,28 @@ public function testGetTableNames(array $pdoAttributes): void
$db->close();
}

public function testHasTable(): void
{
$db = $this->getConnection(true);

$schema = $db->getSchema();

$this->assertTrue($schema->hasTable('customer'));
$this->assertTrue($schema->hasTable('category'));
$this->assertFalse($schema->hasTable('no_such_table'));

$this->assertTrue($schema->hasTable('customer', '', true));
$this->assertTrue($schema->hasTable('category', '', true));
$this->assertFalse($schema->hasTable('no_such_table', '', true));
vjik marked this conversation as resolved.
Show resolved Hide resolved

if ($db->getDriverName() !== 'sqlite') {
$this->assertFalse($schema->hasTable('customer', 'no_such_schema', true));
$this->assertFalse($schema->hasTable('category', 'no_such_schema', true));
}

$db->close();
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchema
*/
Expand Down Expand Up @@ -482,6 +504,25 @@ public function testGetViewNames(): void
$db->close();
}

public function testHasView(): void
{
$db = $this->getConnection(true);

$schema = $db->getSchema();

$this->assertTrue($schema->hasView('animal_view'));
$this->assertFalse($schema->hasView('no_such_view'));

Tigrov marked this conversation as resolved.
Show resolved Hide resolved
$this->assertTrue($schema->hasView('animal_view', '', true));
$this->assertFalse($schema->hasView('no_such_view', '', true));

if ($db->getDriverName() !== 'sqlite') {
$this->assertFalse($schema->hasView('animal_view', 'no_such_schema', true));
}

$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 testHasSchema(): void
{
$db = $this->getConnection();

$schema = $db->getSchema();
Assert::setInaccessibleProperty($schema, 'schemaNames', ['dbo', 'public']);

$this->assertTrue($schema->hasSchema('dbo'));
$this->assertTrue($schema->hasSchema('public'));
$this->assertFalse($schema->hasSchema('no_such_schema'));

$db->close();
}

/**
* @throws NotSupportedException
*/
Expand Down
Loading