Skip to content

Commit

Permalink
Add command showDatabases(). (yiisoft#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Apr 5, 2023
1 parent e40370e commit 85a24ac
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
ports:
- 1521:1521
env:
ORACLE_DATABASE : yiitest
ORACLE_PASSWORD : root
options: >-
--name=oci
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
ports:
- 1521:1521
env:
ORACLE_DATABASE : yiitest
ORACLE_PASSWORD : root
options: >-
--name=oci
Expand Down
9 changes: 9 additions & 0 deletions src/CommandPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra
return $result;
}

public function showDatabases(): array
{
$sql = <<<SQL
SELECT PDB_NAME FROM DBA_PDBS WHERE PDB_NAME NOT IN ('PDB\$SEED', 'PDB\$ROOT', 'ORCLPDB1', 'XEPDB1')
SQL;

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

protected function getQueryBuilder(): QueryBuilderInterface
{
return $this->db->getQueryBuilder();
Expand Down
11 changes: 6 additions & 5 deletions 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(
private string $driver,
private string $host,
private string $databaseName,
private string|null $databaseName = null,
private string $port = '1521',
private array $options = []
) {
Expand All @@ -43,10 +43,11 @@ public function __construct(
*/
public function asString(): string
{
$dsn = match ($this->port) {
'' => "$this->driver:" . "dbname=$this->host/$this->databaseName",
default => "$this->driver:" . "dbname=$this->host:$this->port/$this->databaseName",
};
if (!empty($this->databaseName)) {
$dsn = "$this->driver:" . "dbname=$this->host:$this->port/$this->databaseName";
} else {
$dsn = "$this->driver:" . "dbname=$this->host:$this->port";
}

$parts = [];

Expand Down
15 changes: 15 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Oracle\ConnectionPDO;
use Yiisoft\Db\Oracle\Dsn;
use Yiisoft\Db\Oracle\PDODriver;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
use Yiisoft\Db\Tests\Support\Assert;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Transaction\TransactionInterface;

use function is_resource;
Expand Down Expand Up @@ -567,4 +571,15 @@ public function testProfilerData(string $sql = null): void
{
parent::testProfilerData('SELECT 123 FROM DUAL');
}

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

$command = $db->createCommand();

$this->assertSame('oci:dbname=localhost:1521', $db->getDriver()->getDsn());
$this->assertSame(['YIITEST'], $command->showDatabases());
}
}
24 changes: 24 additions & 0 deletions tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@
*/
final class DsnTest extends TestCase
{
public function testAsStringWithDatabaseName(): void
{
$this->assertSame(
'oci:dbname=localhost:1521;charset=AL32UTF8',
(new Dsn('oci', 'localhost', port: '1521', options: ['charset' => 'AL32UTF8']))->asString(),
);
}

public function testAsStringWithDatabaseNameWithEmptyString(): void
{
$this->assertSame(
'oci:dbname=localhost:1521;charset=AL32UTF8',
(new Dsn('oci', 'localhost', '', '1521', ['charset' => 'AL32UTF8']))->asString(),
);
}

public function testAsStringWithDatabaseNameWithNull(): void
{
$this->assertSame(
'oci:dbname=localhost:1521;charset=AL32UTF8',
(new Dsn('oci', 'localhost', null, '1521', ['charset' => 'AL32UTF8']))->asString(),
);
}

/**
* Oracle service name it support only in version 18 and higher, for docker image gvenzl/oracle-xe:18
*/
Expand Down
10 changes: 2 additions & 8 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ trait TestTrait
*/
protected function getConnection(bool $fixture = false): ConnectionPDOInterface
{
$db = new ConnectionPDO(
new PDODriver($this->getDsn(), 'system', 'root'),
DbHelper::getSchemaCache(),
);
$db = new ConnectionPDO(new PDODriver($this->getDsn(), 'system', 'root'), DbHelper::getSchemaCache());

if ($fixture) {
DbHelper::loadFixture($db, __DIR__ . '/Fixture/oci.sql');
Expand All @@ -38,10 +35,7 @@ protected static function getDb(): ConnectionPDOInterface
{
$dsn = (new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']))->asString();

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

protected function getDsn(): string
Expand Down

0 comments on commit 85a24ac

Please sign in to comment.