From e9c25e31e26537c32c399d9cc848a6b5ff82a739 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Wed, 5 Apr 2023 03:30:42 -0400 Subject: [PATCH] Add command `showDatabases()`. (#275) --- src/CommandPDO.php | 9 +++++++++ src/Dsn.php | 2 +- tests/CommandTest.php | 15 +++++++++++++++ tests/DsnTest.php | 15 +++++++++++++++ tests/Support/TestTrait.php | 10 ++-------- 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/CommandPDO.php b/src/CommandPDO.php index ab9485530..64e3d84db 100644 --- a/src/CommandPDO.php +++ b/src/CommandPDO.php @@ -44,6 +44,15 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra return $result; } + public function showDatabases(): array + { + $sql = <<setSql($sql)->queryColumn(); + } + protected function getQueryBuilder(): QueryBuilderInterface { return $this->db->getQueryBuilder(); diff --git a/src/Dsn.php b/src/Dsn.php index 4c46a0506..77b92b021 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -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 = [] ) { diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 0731c937f..fb3ee0fc6 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -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 @@ -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()); + } } diff --git a/tests/DsnTest.php b/tests/DsnTest.php index a72390e5f..052094d87 100644 --- a/tests/DsnTest.php +++ b/tests/DsnTest.php @@ -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( diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index c0559f4f5..74b8ebcac 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -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'); @@ -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