Skip to content

Commit 36275f2

Browse files
authored
Create IndexType class (#301)
1 parent 5571e08 commit 36275f2

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- Enh #299: Add `ColumnDefinitionParser` class (@Tigrov)
3131
- Enh #299: Convert database types to lower case (@Tigrov)
3232
- Enh #300: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov)
33+
- New #301: Add `IndexType` class (@Tigrov)
3334

3435
## 1.3.0 March 21, 2024
3536

src/IndexType.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Oracle;
6+
7+
/**
8+
* Defines the available index types for {@see DDLQueryBuilder::createIndex()} method.
9+
*/
10+
final class IndexType
11+
{
12+
/**
13+
* Define the type of the index as `UNIQUE`.
14+
*/
15+
public const UNIQUE = 'UNIQUE';
16+
/**
17+
* Define the type of the index as `BITMAP`.
18+
*/
19+
public const BITMAP = 'BITMAP';
20+
/**
21+
* Define the type of the index as `MULTIVALUE`.
22+
*/
23+
public const MULTIVALUE = 'MULTIVALUE';
24+
/**
25+
* Define the type of the index as `SEARCH`.
26+
*/
27+
public const SEARCH = 'SEARCH';
28+
}

src/Schema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ protected function loadTableIndexes(string $tableName): array
267267
foreach ($indexes as $name => $index) {
268268
$columnNames = array_column($index, 'column_name');
269269

270-
if ($columnNames[0] === null) {
271-
$columnNames[0] = '';
270+
if ($columnNames === [null]) {
271+
$columnNames = [];
272272
}
273273

274274
$result[] = (new IndexConstraint())

tests/CommandTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Db\Oracle\Tests;
66

7+
use PHPUnit\Framework\Attributes\DataProviderExternal;
78
use ReflectionException;
89
use Throwable;
910
use Yiisoft\Db\Constant\ColumnType;
@@ -13,9 +14,12 @@
1314
use Yiisoft\Db\Exception\InvalidCallException;
1415
use Yiisoft\Db\Exception\InvalidConfigException;
1516
use Yiisoft\Db\Exception\NotSupportedException;
17+
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
1618
use Yiisoft\Db\Oracle\Connection;
1719
use Yiisoft\Db\Oracle\Dsn;
1820
use Yiisoft\Db\Oracle\Driver;
21+
use Yiisoft\Db\Oracle\IndexType;
22+
use Yiisoft\Db\Oracle\Tests\Provider\CommandProvider;
1923
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
2024
use Yiisoft\Db\Query\Query;
2125
use Yiisoft\Db\Tests\Common\CommonCommandTest;
@@ -26,6 +30,7 @@
2630
use function is_resource;
2731
use function str_pad;
2832
use function stream_get_contents;
33+
use function version_compare;
2934

3035
/**
3136
* @group oracle
@@ -636,4 +641,47 @@ public function testShowDatabases(): void
636641
$this->assertSame('oci:dbname=localhost:1521', $db->getDriver()->getDsn());
637642
$this->assertSame(['YIITEST'], $command->showDatabases());
638643
}
644+
645+
#[DataProviderExternal(CommandProvider::class, 'createIndex')]
646+
public function testCreateIndex(array $columns, array $indexColumns, string|null $indexType, string|null $indexMethod): void
647+
{
648+
parent::testCreateIndex($columns, $indexColumns, $indexType, $indexMethod);
649+
}
650+
651+
public function testCreateSearchIndex()
652+
{
653+
$db = $this->getConnection();
654+
655+
if (version_compare($db->getServerInfo()->getVersion(), '21', '<')) {
656+
$this->markTestSkipped('Search index is supported since Oracle 21');
657+
}
658+
659+
$command = $db->createCommand();
660+
$schema = $db->getSchema();
661+
662+
$tableName = 'test_create_index';
663+
$indexName = 'test_index_name';
664+
665+
if ($schema->getTableSchema($tableName) !== null) {
666+
$command->dropTable($tableName)->execute();
667+
}
668+
669+
$command->createTable($tableName, ['col1' => ColumnBuilder::text()])->execute();
670+
$command->createIndex($tableName, $indexName, ['col1'], IndexType::SEARCH)->execute();
671+
672+
$this->assertCount(2, $schema->getTableIndexes($tableName));
673+
674+
$index = $schema->getTableIndexes($tableName)[0];
675+
676+
$this->assertSame(['col1'], $index->getColumnNames());
677+
$this->assertFalse($index->isUnique());
678+
$this->assertFalse($index->isPrimary());
679+
680+
$sysIndex = $schema->getTableIndexes($tableName)[1];
681+
$this->assertSame([], $sysIndex->getColumnNames());
682+
$this->assertTrue($sysIndex->isUnique());
683+
$this->assertFalse($sysIndex->isPrimary());
684+
685+
$db->close();
686+
}
639687
}

tests/Provider/CommandProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use JsonException;
88
use PDO;
99
use Yiisoft\Db\Command\Param;
10+
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
11+
use Yiisoft\Db\Oracle\IndexType;
1012
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
1113
use Yiisoft\Db\Tests\Support\DbHelper;
1214

@@ -92,4 +94,13 @@ public static function insertVarbinary(): array
9294
['simple string', 'simple string'],
9395
];
9496
}
97+
98+
public static function createIndex(): array
99+
{
100+
return [
101+
...parent::createIndex(),
102+
[['col1' => ColumnBuilder::integer()], ['col1'], IndexType::UNIQUE, null],
103+
[['col1' => ColumnBuilder::integer()], ['col1'], IndexType::BITMAP, null],
104+
];
105+
}
95106
}

0 commit comments

Comments
 (0)