Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Move index type constants to IndexType class #920

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ and the following changes were made:
### New classes with constants

- `Yiisoft\Db\Constant\PhpType` with PHP types constants;
- `Yiisoft\Db\Constant\GettypeResult` with `gettype()` function results constants.
- `Yiisoft\Db\Constant\ColumnType` with abstract column types constants.
- `Yiisoft\Db\Constant\PseudoType` with column pseudo-types constants.
- `Yiisoft\Db\Constant\GettypeResult` with `gettype()` function results constants;
- `Yiisoft\Db\Constant\ColumnType` with abstract column types constants;
- `Yiisoft\Db\Constant\PseudoType` with column pseudo-types constants;
- `Yiisoft\Db\Constant\IndexType` with table index types.

### New classes for table columns

Expand Down
9 changes: 6 additions & 3 deletions src/Command/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Throwable;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\IndexType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -296,13 +297,15 @@ public function checkIntegrity(string $schema, string $table, bool $check = true
* @param string $name The name of the index.
* @param array|string $columns The column(s) to include in the index. If there are many columns,
* separate them with commas.
* @param string|null $indexType The type of index-supported DBMS - for example: `UNIQUE`, `FULLTEXT`, `SPATIAL`,
* `BITMAP` or null as default.
* @param string|null $indexMethod The setting index organization method (with `USING`, not all DBMS).
* @param string|null $indexType The type of the index supported by DBMS {@see IndexType} - for example: `UNIQUE`,
* `FULLTEXT`, `SPATIAL`, `BITMAP` or null as default.
* @param string|null $indexMethod The index organization method (with `USING`, not all DBMS).
*
* @throws Exception
* @throws InvalidArgumentException
*
* @psalm-param IndexType::*|null $indexType
*
* Note: The method will quote the `name`, `table`, and `column` parameters before using them in the generated SQL.
*/
public function createIndex(
Expand Down
78 changes: 78 additions & 0 deletions src/Constant/IndexType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Constant;

/**
* Defines the available index types. It is used in {@see DDLQueryBuilderInterface::createIndex()}.
*/
final class IndexType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each DBMS has own list of index types, which almost do not overlap. May be remove IndexType from Yii DB and create separate IndexType in each implementation package?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just remove it. As far as I see only UNIQUE keyword makes sense.

DBMS Index types
MYSQL UNIQUE, FULLTEXT, SPATIAL
PGSQL UNIQUE
MSSQL UNIQUE, UNIQUE CLUSTERED, CLUSTERED, SPATIAL, COLUMNSTORE, CLUSTERED COLUMNSTORE, XML, PRIMARY XML
SQLITE UNIQUE
ORACLE UNIQUE, BITMAP, MULTIVALUE
DBMS Index methods
MYSQL BTREE (by default), HASH, RTREE (MARIADB)
PGSQL BTREE (by default), HASH, GIST, SPGIST, GIN, BRIN
MSSQL BTREE (there is no option)
SQLITE BTREE (there is no option)
ORACLE BTREE (there is no option), depends on index type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also other indexes can potentially be added using extensions. It seems, there is such opportunity in PostgreSQL.

Сlasses with constants may be convient to use in userland. Remove it from Yii DB, but add for DBMS-specific packages. WDYT?

Copy link
Member Author

@Tigrov Tigrov Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks as a good solution.

{
/**
* Define the type of the index as `UNIQUE`.
*
* Supported by `MySQL`, `MariaDB`, `MSSQL`, `Oracle`, `PostgreSQL`, `SQLite`.
*/
public const UNIQUE = 'UNIQUE';
/**
* Define the type of the index as `BTREE`.
*
* Supported by `MySQL`, `PostgreSQL`.
*/
public const BTREE = 'BTREE';
/**
* Define the type of the index as `HASH`.
*
* Supported by `MySQL`, `PostgreSQL`.
*/
public const HASH = 'HASH';
/**
* Define the type of the index as `FULLTEXT`.
*
* Supported by `MySQL`.
*/
public const FULLTEXT = 'FULLTEXT';
/**
* Define the type of the index as `SPATIAL`.
*
* Supported by `MySQL`.
*/
public const SPATIAL = 'SPATIAL';
/**
* Define the type of the index as `GIST`.
*
* Supported by `PostgreSQL`.
*/
public const GIST = 'GIST';
/**
* Define the type of the index as `GIN`.
*
* Supported by `PostgreSQL`.
*/
public const GIN = 'GIN';
/**
* Define the type of the index as `BRIN`.
*
* Supported by `PostgreSQL`.
*/
public const BRIN = 'BRIN';
/**
* Define the type of the index as `CLUSTERED`.
*
* Supported by `MSSQL`.
*/
public const CLUSTERED = 'CLUSTERED';
/**
* Define the type of the index as `NONCLUSTERED`.
*
* Supported by `MSSQL`.
*/
public const NONCLUSTERED = 'NONCLUSTERED';
/**
* Define the type of the index as `BITMAP`.
*
* Supported by `Oracle`.
*/
public const BITMAP = 'BITMAP';
}
9 changes: 6 additions & 3 deletions src/QueryBuilder/DDLQueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\QueryBuilder;

use Yiisoft\Db\Constant\IndexType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -204,15 +205,17 @@ public function checkIntegrity(string $schema = '', string $table = '', bool $ch
* @param string $name The name of the index.
* @param array|string $columns The column(s) to include in the index.
* If there are many columns, separate them with commas or use an array to represent them.
* @param string|null $indexType Type of index-supported DBMS - for example, `UNIQUE`, `FULLTEXT`, `SPATIAL`, `BITMAP` or
* `null` as default
* @param string|null $indexMethod For setting index organization method (with `USING`, not all DBMS)
* @param string|null $indexType The type of the index supported by DBMS {@see IndexType} - for example, `UNIQUE`,
* `FULLTEXT`, `SPATIAL`, `BITMAP` or `null` as default.
* @param string|null $indexMethod The index organization method (with `USING`, not all DBMS).
*
* @throws Exception
* @throws InvalidArgumentException
*
* @return string The SQL statement for creating a new index.
*
* @psalm-param IndexType::*|null $indexType
*
* Note: The method will quote the `name`, `table`, and `column` parameters before using them in the generated SQL.
*/
public function createIndex(
Expand Down
22 changes: 22 additions & 0 deletions src/Schema/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,66 +58,88 @@ interface SchemaInterface extends ConstraintSchemaInterface
* Define the type of the index as `UNIQUE`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `MySQL`, `MariaDB`, `MSSQL`, `Oracle`, `PostgreSQL`, `SQLite`.
*
* @deprecated Use {@see IndexType::UNIQUE} instead. Will be removed in 2.0.
*/
public const INDEX_UNIQUE = 'UNIQUE';
/**
* Define the type of the index as `BTREE`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `MySQL`, `PostgreSQL`.
*
* @deprecated Use {@see IndexType::BTREE} instead. Will be removed in 2.0.
*/
public const INDEX_BTREE = 'BTREE';
/**
* Define the type of the index as `HASH`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `MySQL`, `PostgreSQL`.
*
* @deprecated Use {@see IndexType::HASH} instead. Will be removed in 2.0.
*/
public const INDEX_HASH = 'HASH';
/**
* Define the type of the index as `FULLTEXT`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `MySQL`.
*
* @deprecated Use {@see IndexType::FULLTEXT} instead. Will be removed in 2.0.
*/
public const INDEX_FULLTEXT = 'FULLTEXT';
/**
* Define the type of the index as `SPATIAL`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `MySQL`.
*
* @deprecated Use {@see IndexType::SPATIAL} instead. Will be removed in 2.0.
*/
public const INDEX_SPATIAL = 'SPATIAL';
/**
* Define the type of the index as `GIST`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `PostgreSQL`.
*
* @deprecated Use {@see IndexType::GIST} instead. Will be removed in 2.0.
*/
public const INDEX_GIST = 'GIST';
/**
* Define the type of the index as `GIN`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `PostgreSQL`.
*
* @deprecated Use {@see IndexType::GIN} instead. Will be removed in 2.0.
*/
public const INDEX_GIN = 'GIN';
/**
* Define the type of the index as `BRIN`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `PostgreSQL`.
*
* @deprecated Use {@see IndexType::BRIN} instead. Will be removed in 2.0.
*/
public const INDEX_BRIN = 'BRIN';
/**
* Define the type of the index as `CLUSTERED`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `MSSQL`.
*
* @deprecated Use {@see IndexType::CLUSTERED} instead. Will be removed in 2.0.
*/
public const INDEX_CLUSTERED = 'CLUSTERED';
/**
* Define the type of the index as `NONCLUSTERED`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `MSSQL`.
*
* @deprecated Use {@see IndexType::NONCLUSTERED} instead. Will be removed in 2.0.
*/
public const INDEX_NONCLUSTERED = 'NONCLUSTERED';
/**
* Define the type of the index as `BITMAP`, it's used in {@see DDLQueryBuilderInterface::createIndex()}.
*
* Supported by `Oracle`.
*
* @deprecated Use {@see IndexType::BITMAP} instead. Will be removed in 2.0.
*/
public const INDEX_BITMAP = 'BITMAP';
/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Common/CommonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Throwable;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\IndexType;
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Constraint\DefaultValueConstraint;
Expand All @@ -18,7 +19,6 @@
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
use Yiisoft\Db\Tests\AbstractSchemaTest;
use Yiisoft\Db\Tests\Support\AnyCaseValue;
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testFindUniquesIndexes(): void
'uniqueIndex',
'somecolUnique',
'somecol',
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
)->execute();
$tableSchema = $schema->getTableSchema('uniqueIndex', true);

Expand All @@ -166,7 +166,7 @@ public function testFindUniquesIndexes(): void
'uniqueIndex',
'someCol2Unique',
'someCol2',
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
)->execute();
$tableSchema = $schema->getTableSchema('uniqueIndex', true);

Expand All @@ -181,7 +181,7 @@ public function testFindUniquesIndexes(): void
'uniqueIndex',
'another unique index',
'someCol3',
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
)->execute();
$tableSchema = $schema->getTableSchema('uniqueIndex', true);

Expand Down
6 changes: 3 additions & 3 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\IndexType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Tests\Support\Stringable;
use Yiisoft\Db\Tests\Support\TestTrait;
Expand Down Expand Up @@ -512,7 +512,7 @@ public static function createIndex(): array
{
return [
['{{test_idx_constraint_1}}', '{{test_idx}}', 'int1', null, null],
['{{test_idx_constraint_2}}', '{{test_idx}}', ['int1'], SchemaInterface::INDEX_UNIQUE, null],
['{{test_idx_constraint_2}}', '{{test_idx}}', ['int1'], IndexType::UNIQUE, null],
['{{test_idx_constraint_3}}', '{{test_idx}}', ['int1', 'int2'], null, null],
];
}
Expand Down Expand Up @@ -550,7 +550,7 @@ public static function createIndexSql(): array
'{{name}}',
'{{table}}',
['column1', 'column2'],
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
'',
DbHelper::replaceQuotes(
<<<SQL
Expand Down
6 changes: 3 additions & 3 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\IndexType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Expression\Expression;
Expand All @@ -18,7 +19,6 @@
use Yiisoft\Db\QueryBuilder\Condition\LikeCondition;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Tests\Support\Stringable;
use Yiisoft\Db\Tests\Support\TestTrait;
Expand Down Expand Up @@ -936,7 +936,7 @@ public static function createIndex(): array
$tableName,
$name1,
'C_index_1',
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
),
],
'create unique (2 columns)' => [
Expand All @@ -947,7 +947,7 @@ public static function createIndex(): array
$tableName,
$name2,
'C_index_2_1, C_index_2_2',
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
),
],
];
Expand Down
3 changes: 2 additions & 1 deletion tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Tests\Provider;

use PDO;
use Yiisoft\Db\Constant\IndexType;
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
Expand Down Expand Up @@ -190,7 +191,7 @@ public static function withIndexDataProvider(): array
{
return [
[
'indexType' => SchemaInterface::INDEX_UNIQUE,
'indexType' => IndexType::UNIQUE,
'indexMethod' => null,
'columnType' => null,
'isPrimary' => false,
Expand Down
Loading