Skip to content

Commit

Permalink
Move index constants to IndexType class
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 12, 2025
1 parent 828b5db commit 4140e36
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 17 deletions.
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
{
/**
* 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
7 changes: 4 additions & 3 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 Down Expand Up @@ -147,7 +148,7 @@ public function testFindUniquesIndexes(): void
'uniqueIndex',
'somecolUnique',
'somecol',
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
)->execute();
$tableSchema = $schema->getTableSchema('uniqueIndex', true);

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

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

Expand Down
5 changes: 3 additions & 2 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -512,7 +513,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 +551,7 @@ public static function createIndexSql(): array
'{{name}}',
'{{table}}',
['column1', 'column2'],
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
'',
DbHelper::replaceQuotes(
<<<SQL
Expand Down
5 changes: 3 additions & 2 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 Down Expand Up @@ -936,7 +937,7 @@ public static function createIndex(): array
$tableName,
$name1,
'C_index_1',
SchemaInterface::INDEX_UNIQUE,
IndexType::UNIQUE,
),
],
'create unique (2 columns)' => [
Expand All @@ -947,7 +948,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

0 comments on commit 4140e36

Please sign in to comment.