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

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.

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

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L584

Added line #L584 was not covered by tests
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$tables = $this->getTableNames($schema);

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L586

Added line #L586 was not covered by tests

return in_array($tableName, $tables);

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L588

Added line #L588 was not covered by tests
}

/**
* @param string $schema The schema name.
* @return bool
* @throws Throwable
*/
public function hasSchemaName(string $schema): bool

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L596

Added line #L596 was not covered by tests
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$schemas = $this->getSchemaNames();

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L598

Added line #L598 was not covered by tests

return in_array($schema, $schemas);

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L600

Added line #L600 was not covered by tests
}

/**
* @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

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L609

Added line #L609 was not covered by tests
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$views = $this->getViewNames($schema);

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L611

Added line #L611 was not covered by tests

return in_array($viewName, $views);

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchema.php#L613

Added line #L613 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 hasTableName(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 hasSchemaName(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 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
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$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->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'));

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 testHasSchemaName(): void
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$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
Loading