Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Create IndexType class #301

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Enh #299: Add `ColumnDefinitionParser` class (@Tigrov)
- Enh #299: Convert database types to lower case (@Tigrov)
- Enh #300: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov)
- New #301: Add `IndexType` class (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
28 changes: 28 additions & 0 deletions src/IndexType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Oracle;

/**
* Defines the available index types for {@see DDLQueryBuilder::createIndex()} method.
*/
final class IndexType
{
/**
* Define the type of the index as `UNIQUE`.
*/
public const UNIQUE = 'UNIQUE';
/**
* Define the type of the index as `BITMAP`.
*/
public const BITMAP = 'BITMAP';
/**
* Define the type of the index as `MULTIVALUE`.
*/
public const MULTIVALUE = 'MULTIVALUE';
/**
* Define the type of the index as `SEARCH`.
*/
public const SEARCH = 'SEARCH';
}
4 changes: 2 additions & 2 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ protected function loadTableIndexes(string $tableName): array
foreach ($indexes as $name => $index) {
$columnNames = array_column($index, 'column_name');

if ($columnNames[0] === null) {
$columnNames[0] = '';
if ($columnNames === [null]) {
$columnNames = [];
}

$result[] = (new IndexConstraint())
Expand Down
48 changes: 48 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Oracle\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use ReflectionException;
use Throwable;
use Yiisoft\Db\Constant\ColumnType;
Expand All @@ -13,9 +14,12 @@
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
use Yiisoft\Db\Oracle\Connection;
use Yiisoft\Db\Oracle\Dsn;
use Yiisoft\Db\Oracle\Driver;
use Yiisoft\Db\Oracle\IndexType;
use Yiisoft\Db\Oracle\Tests\Provider\CommandProvider;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
Expand All @@ -26,6 +30,7 @@
use function is_resource;
use function str_pad;
use function stream_get_contents;
use function version_compare;

/**
* @group oracle
Expand Down Expand Up @@ -636,4 +641,47 @@ public function testShowDatabases(): void
$this->assertSame('oci:dbname=localhost:1521', $db->getDriver()->getDsn());
$this->assertSame(['YIITEST'], $command->showDatabases());
}

#[DataProviderExternal(CommandProvider::class, 'createIndex')]
public function testCreateIndex(array $columns, array $indexColumns, string|null $indexType, string|null $indexMethod): void
{
parent::testCreateIndex($columns, $indexColumns, $indexType, $indexMethod);
}

public function testCreateSearchIndex()
{
$db = $this->getConnection();

if (version_compare($db->getServerInfo()->getVersion(), '21', '<')) {
$this->markTestSkipped('Search index is supported since Oracle 21');
}

$command = $db->createCommand();
$schema = $db->getSchema();

$tableName = 'test_create_index';
$indexName = 'test_index_name';

if ($schema->getTableSchema($tableName) !== null) {
$command->dropTable($tableName)->execute();
}

$command->createTable($tableName, ['col1' => ColumnBuilder::text()])->execute();
$command->createIndex($tableName, $indexName, ['col1'], IndexType::SEARCH)->execute();

$this->assertCount(2, $schema->getTableIndexes($tableName));

$index = $schema->getTableIndexes($tableName)[0];

$this->assertSame(['col1'], $index->getColumnNames());
$this->assertFalse($index->isUnique());
$this->assertFalse($index->isPrimary());

$sysIndex = $schema->getTableIndexes($tableName)[1];
$this->assertSame([], $sysIndex->getColumnNames());
$this->assertTrue($sysIndex->isUnique());
$this->assertFalse($sysIndex->isPrimary());

$db->close();
}
}
11 changes: 11 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use JsonException;
use PDO;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
use Yiisoft\Db\Oracle\IndexType;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Support\DbHelper;

Expand Down Expand Up @@ -92,4 +94,13 @@ public static function insertVarbinary(): array
['simple string', 'simple string'],
];
}

public static function createIndex(): array
{
return [
...parent::createIndex(),
[['col1' => ColumnBuilder::integer()], ['col1'], IndexType::UNIQUE, null],
[['col1' => ColumnBuilder::integer()], ['col1'], IndexType::BITMAP, null],
];
}
}