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 16 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

- New #913: Add methods `SchemaInterface::hasSchema()`, `SchemaInterface::hasTable()`, `SchemaInterface::hasView()` (@evil1)
- 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 @@

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

Check warning on line 581 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L581

Added line #L581 was not covered by tests
{
$tables = $this->getTableNames($schema, $refresh);

Check warning on line 583 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L583

Added line #L583 was not covered by tests

return in_array($tableName, $tables);

Check warning on line 585 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L585

Added line #L585 was not covered by tests
}

/**
* @throws Throwable
*/
public function hasSchema(string $schema, bool $refresh = false): bool

Check warning on line 591 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L591

Added line #L591 was not covered by tests
{
$schemas = $this->getSchemaNames($refresh);

Check warning on line 593 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L593

Added line #L593 was not covered by tests

return in_array($schema, $schemas);

Check warning on line 595 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L595

Added line #L595 was not covered by tests
}

/**
* @throws Throwable
*/
public function hasView(string $viewName, string $schema = '', bool $refresh = false): bool

Check warning on line 601 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L601

Added line #L601 was not covered by tests
{
$views = $this->getViewNames($schema, $refresh);

Check warning on line 603 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L603

Added line #L603 was not covered by tests

return in_array($viewName, $views);

Check warning on line 605 in src/Schema/AbstractSchema.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L605

Added line #L605 was not covered by tests
}
}
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;
}
32 changes: 32 additions & 0 deletions tests/Common/CommonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,23 @@ 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

$db->close();
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchema
*/
Expand Down Expand Up @@ -482,6 +499,21 @@ 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));

$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