Skip to content

Commit

Permalink
Add command showDatabases(). (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Apr 5, 2023
1 parent cd145f1 commit e9c25e3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/CommandPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra
return $result;
}

public function showDatabases(): array
{
$sql = <<<SQL
SHOW DATABASES WHERE `Database` NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
SQL;

return $this->setSql($sql)->queryColumn();
}

protected function getQueryBuilder(): QueryBuilderInterface
{
return $this->db->getQueryBuilder();
Expand Down
2 changes: 1 addition & 1 deletion src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Dsn extends AbstractDsn
public function __construct(
string $driver,
string $host,
string $databaseName,
string|null $databaseName = null,
string $port = '3306',
array $options = []
) {
Expand Down
15 changes: 15 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Mysql\ConnectionPDO;
use Yiisoft\Db\Mysql\Dsn;
use Yiisoft\Db\Mysql\PDODriver;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
use Yiisoft\Db\Tests\Support\DbHelper;

/**
* @group mysql
Expand Down Expand Up @@ -133,4 +137,15 @@ public function testUpsert(array $firstData, array $secondData): void
{
parent::testUpsert($firstData, $secondData);
}

public function testShowDatabases(): void
{
$dsn = new Dsn('mysql', '127.0.0.1', );
$db = new ConnectionPDO(new PDODriver($dsn->asString(), 'root', ''), DbHelper::getSchemaCache());

$command = $db->createCommand();

$this->assertSame('mysql:host=127.0.0.1;port=3306', $db->getDriver()->getDsn());
$this->assertSame(['yiitest'], $command->showDatabases());
}
}
15 changes: 15 additions & 0 deletions tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ public function testAsString(): void
);
}

public function testAsStringWithDatabaseName(): void
{
$this->assertSame('mysql:host=localhost;port=3306', (new Dsn('mysql', 'localhost'))->asString());
}

public function testAsStringWithDatabaseNameWithEmptyString(): void
{
$this->assertSame('mysql:host=localhost;port=3306', (new Dsn('mysql', 'localhost', ''))->asString());
}

public function testAsStringWithDatabaseNameWithNull(): void
{
$this->assertSame('mysql:host=localhost;port=3306', (new Dsn('mysql', 'localhost', null))->asString());
}

public function testAsStringWithOptions(): void
{
$this->assertSame(
Expand Down
10 changes: 2 additions & 8 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ trait TestTrait

protected function getConnection(bool $fixture = false): ConnectionPDOInterface
{
$db = new ConnectionPDO(
new PDODriver($this->getDsn(), 'root', ''),
DbHelper::getSchemaCache(),
);
$db = new ConnectionPDO(new PDODriver($this->getDsn(), 'root', ''), DbHelper::getSchemaCache());

if ($fixture) {
DbHelper::loadFixture($db, __DIR__ . '/Fixture/mysql.sql');
Expand All @@ -32,10 +29,7 @@ protected static function getDb(): ConnectionPDOInterface
{
$dsn = (new Dsn('mysql', '127.0.0.1', 'yiitest', '3306', ['charset' => 'utf8mb4']))->asString();

return new ConnectionPDO(
new PDODriver($dsn, 'root', ''),
DbHelper::getSchemaCache(),
);
return new ConnectionPDO(new PDODriver($dsn, 'root', ''), DbHelper::getSchemaCache());
}

protected function getDsn(): string
Expand Down

0 comments on commit e9c25e3

Please sign in to comment.