Skip to content

Commit

Permalink
Add command showDatabases(). (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Apr 5, 2023
1 parent 9114f6b commit 8cc508c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/CommandPDO.php
Expand Up @@ -17,6 +17,15 @@
*/
final class CommandPDO extends AbstractCommandPDO
{
public function showDatabases(): array
{
$sql = <<<SQL
SELECT datname FROM pg_database WHERE datistemplate = false AND datname NOT IN ('postgres', 'template0', 'template1')
SQL;

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

protected function getQueryBuilder(): QueryBuilderInterface
{
return $this->db->getQueryBuilder();
Expand Down
6 changes: 5 additions & 1 deletion src/Dsn.php
Expand Up @@ -19,10 +19,14 @@ final class Dsn extends AbstractDsn
public function __construct(
string $driver,
string $host,
string $databaseName,
string|null $databaseName = 'postgres',
string $port = '5432',
array $options = []
) {
if (empty($databaseName)) {
$databaseName = 'postgres';
}

parent::__construct($driver, $host, $databaseName, $port, $options);
}
}
15 changes: 15 additions & 0 deletions tests/CommandTest.php
Expand Up @@ -9,8 +9,12 @@
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Pgsql\ConnectionPDO;
use Yiisoft\Db\Pgsql\Dsn;
use Yiisoft\Db\Pgsql\PDODriver;
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
use Yiisoft\Db\Tests\Support\DbHelper;

use function serialize;

Expand Down Expand Up @@ -315,4 +319,15 @@ public function testinsertWithReturningPksUuid(): void

$db->close();
}

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

$command = $db->createCommand();

$this->assertSame('pgsql:host=127.0.0.1;dbname=postgres;port=5432', $db->getDriver()->getDsn());
$this->assertSame(['yiitest'], $command->showDatabases());
}
}
24 changes: 24 additions & 0 deletions tests/DsnTest.php
Expand Up @@ -22,6 +22,30 @@ public function testAsString(): void
);
}

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

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

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

public function testAsStringWithOptions(): void
{
$this->assertSame(
Expand Down
10 changes: 2 additions & 8 deletions tests/Support/TestTrait.php
Expand Up @@ -25,10 +25,7 @@ protected function getConnection(bool $fixture = false): ConnectionPDOInterface
{
$pdoDriver = new PDODriver($this->getDsn(), 'root', 'root');
$pdoDriver->charset('utf8');
$db = new ConnectionPDO(
$pdoDriver,
DbHelper::getSchemaCache(),
);
$db = new ConnectionPDO($pdoDriver, DbHelper::getSchemaCache());

if ($fixture) {
DbHelper::loadFixture($db, __DIR__ . "/Fixture/$this->fixture");
Expand All @@ -41,10 +38,7 @@ protected static function getDb(): ConnectionPDOInterface
{
$dsn = (new Dsn('pgsql', '127.0.0.1', 'yiitest', '5432'))->asString();

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

protected function getDsn(): string
Expand Down

0 comments on commit 8cc508c

Please sign in to comment.