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

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
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
{
$tables = $this->getTableNames($schema);

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

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();

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

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);

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
}
}
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
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.
* @return bool Whether table exists or not
samdark marked this conversation as resolved.
Show resolved Hide resolved
*/
public function hasTable(string $tableName, string $schema = ''): bool;

/**
* Determines if a specified schema exists in the database.
*
* @param string $schema The table name to search for
samdark marked this conversation as resolved.
Show resolved Hide resolved
* @return bool Whether schema exists or not
samdark marked this conversation as resolved.
Show resolved Hide resolved
*/
public function hasSchema(string $schema): bool;

/**
* Determines if a specified view exists in the database.
*
* @param string $viewName The table name to search for
samdark 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.
* @return bool Whether view exists or not
samdark marked this conversation as resolved.
Show resolved Hide resolved
*/
public function hasView(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 testHasTable(array $pdoAttributes): void
{
$db = $this->getConnection(true);

foreach ($pdoAttributes as $name => $value) {
if ($name === PDO::ATTR_EMULATE_PREPARES) {
continue;
}

$db->getPDO()?->setAttribute($name, $value);
}
Tigrov marked this conversation as resolved.
Show resolved Hide resolved

$schema = $db->getSchema();

$this->assertTrue($schema->hasTable('customer'));
$this->assertTrue($schema->hasTable('category'));
$this->assertFalse($schema->hasTable('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 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
$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