Skip to content

Commit

Permalink
Separate column type constants (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Sep 13, 2024
1 parent e6382dd commit e3e277d
Show file tree
Hide file tree
Showing 32 changed files with 485 additions and 254 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- Enh #872: Use `#[\SensitiveParameter]` attribute to mark sensitive parameters (@heap-s)
- Enh #864: Realize column factory (@Tigrov)
- Enh #875: Ignore "Packets out of order..." warnings in `AbstractPdoCommand::internalExecute()` method (@Tigrov)
- Enh #877: Separate column type constants (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ and the following changes were made:

- `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.

### New classes for table columns

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

declare(strict_types=1);

namespace Yiisoft\Db\Constant;

/**
* Defines the available abstract column types.
*/
final class ColumnType
{
/**
* Define the abstract column type as `boolean`.
*/
public const BOOLEAN = 'boolean';
/**
* Define the abstract column type as `bit`.
*/
public const BIT = 'bit';
/**
* Define the abstract column type as `tinyint`.
*/
public const TINYINT = 'tinyint';
/**
* Define the abstract column type as `smallint`.
*/
public const SMALLINT = 'smallint';
/**
* Define the abstract column type as `integer`.
*/
public const INTEGER = 'integer';
/**
* Define the abstract column type as `bigint`.
*/
public const BIGINT = 'bigint';
/**
* Define the abstract column type as `float`.
*/
public const FLOAT = 'float';
/**
* Define the abstract column type as `double`.
*/
public const DOUBLE = 'double';
/**
* Define the abstract column type as `decimal`.
*/
public const DECIMAL = 'decimal';
/**
* Define the abstract column type as `money`.
*/
public const MONEY = 'money';
/**
* Define the abstract column type as `char`.
*/
public const CHAR = 'char';
/**
* Define the abstract column type as `string`.
*/
public const STRING = 'string';
/**
* Define the abstract column type as `text`.
*/
public const TEXT = 'text';
/**
* Define the abstract column type as `binary`.
*/
public const BINARY = 'binary';
/**
* Define the abstract column type as `uuid`.
*/
public const UUID = 'uuid';
/**
* Define the abstract column type as `datetime`.
*/
public const DATETIME = 'datetime';
/**
* Define the abstract column type as `timestamp`.
*/
public const TIMESTAMP = 'timestamp';
/**
* Define the abstract column type as `date`.
*/
public const DATE = 'date';
/**
* Define the abstract column type as `time`.
*/
public const TIME = 'time';
/**
* Define the abstract column type as `array`.
*/
public const ARRAY = 'array';
/**
* Define the abstract column type as `structured`.
*/
public const STRUCTURED = 'structured';
/**
* Define the abstract column type as `json`.
*/
public const JSON = 'json';
}
37 changes: 37 additions & 0 deletions src/Constant/PseudoType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Constant;

/**
* Defines the available column pseudo-types.
* Used to define column primary key types when creating or updating a table schema.
*/
final class PseudoType
{
/**
* Define the column pseudo-type a primary key.
*/
public const PK = 'pk';
/**
* Define the column pseudo-type as an `unsigned` primary key.
*/
public const UPK = 'upk';
/**
* Define the column pseudo-type as a big primary key.
*/
public const BIGPK = 'bigpk';
/**
* Define the column pseudo-type as an `unsigned` big primary key.
*/
public const UBIGPK = 'ubigpk';
/**
* Define the column pseudo-type as an `uuid` primary key.
*/
public const UUID_PK = 'uuid_pk';
/**
* Define the column pseudo-type as an `uuid` primary key with a sequence.
*/
public const UUID_PK_SEQ = 'uuid_pk_seq';
}
55 changes: 28 additions & 27 deletions src/Schema/Builder/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Yiisoft\Db\Schema\Builder;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbStringHelper;
use Yiisoft\Db\Schema\SchemaInterface;

use function gettype;
use function implode;
Expand All @@ -21,7 +22,7 @@
* For example, the following code creates a column schema for an integer column:
*
* ```php
* $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
* $column = (new Column(ColumnType::INTEGER))->notNull()->defaultValue(0);
* ```
*
* Provides a fluent interface, which means that the methods can be chained together to create a column schema with
Expand Down Expand Up @@ -69,29 +70,29 @@ abstract class AbstractColumn implements ColumnInterface

/** @psalm-var string[] */
private array $categoryMap = [
SchemaInterface::TYPE_PK => self::TYPE_CATEGORY_PK,
SchemaInterface::TYPE_UPK => self::TYPE_CATEGORY_PK,
SchemaInterface::TYPE_BIGPK => self::TYPE_CATEGORY_PK,
SchemaInterface::TYPE_UBIGPK => self::TYPE_CATEGORY_PK,
SchemaInterface::TYPE_CHAR => self::TYPE_CATEGORY_STRING,
SchemaInterface::TYPE_STRING => self::TYPE_CATEGORY_STRING,
SchemaInterface::TYPE_TEXT => self::TYPE_CATEGORY_STRING,
SchemaInterface::TYPE_TINYINT => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_SMALLINT => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_INTEGER => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_BIGINT => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_FLOAT => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_DOUBLE => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_DECIMAL => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_DATETIME => self::TYPE_CATEGORY_TIME,
SchemaInterface::TYPE_TIMESTAMP => self::TYPE_CATEGORY_TIME,
SchemaInterface::TYPE_TIME => self::TYPE_CATEGORY_TIME,
SchemaInterface::TYPE_DATE => self::TYPE_CATEGORY_TIME,
SchemaInterface::TYPE_BINARY => self::TYPE_CATEGORY_OTHER,
SchemaInterface::TYPE_BOOLEAN => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_MONEY => self::TYPE_CATEGORY_NUMERIC,
SchemaInterface::TYPE_UUID => self::TYPE_CATEGORY_UUID,
SchemaInterface::TYPE_UUID_PK => self::TYPE_CATEGORY_UUID_PK,
PseudoType::PK => self::TYPE_CATEGORY_PK,
PseudoType::UPK => self::TYPE_CATEGORY_PK,
PseudoType::BIGPK => self::TYPE_CATEGORY_PK,
PseudoType::UBIGPK => self::TYPE_CATEGORY_PK,
ColumnType::CHAR => self::TYPE_CATEGORY_STRING,
ColumnType::STRING => self::TYPE_CATEGORY_STRING,
ColumnType::TEXT => self::TYPE_CATEGORY_STRING,
ColumnType::TINYINT => self::TYPE_CATEGORY_NUMERIC,
ColumnType::SMALLINT => self::TYPE_CATEGORY_NUMERIC,
ColumnType::INTEGER => self::TYPE_CATEGORY_NUMERIC,
ColumnType::BIGINT => self::TYPE_CATEGORY_NUMERIC,
ColumnType::FLOAT => self::TYPE_CATEGORY_NUMERIC,
ColumnType::DOUBLE => self::TYPE_CATEGORY_NUMERIC,
ColumnType::DECIMAL => self::TYPE_CATEGORY_NUMERIC,
ColumnType::DATETIME => self::TYPE_CATEGORY_TIME,
ColumnType::TIMESTAMP => self::TYPE_CATEGORY_TIME,
ColumnType::TIME => self::TYPE_CATEGORY_TIME,
ColumnType::DATE => self::TYPE_CATEGORY_TIME,
ColumnType::BINARY => self::TYPE_CATEGORY_OTHER,
ColumnType::BOOLEAN => self::TYPE_CATEGORY_NUMERIC,
ColumnType::MONEY => self::TYPE_CATEGORY_NUMERIC,
ColumnType::UUID => self::TYPE_CATEGORY_UUID,
PseudoType::UUID_PK => self::TYPE_CATEGORY_UUID_PK,
];

/**
Expand Down Expand Up @@ -150,8 +151,8 @@ public function comment(string|null $comment): static
public function unsigned(): static
{
$this->type = match ($this->type) {
SchemaInterface::TYPE_PK => SchemaInterface::TYPE_UPK,
SchemaInterface::TYPE_BIGPK => SchemaInterface::TYPE_UBIGPK,
PseudoType::PK => PseudoType::UPK,
PseudoType::BIGPK => PseudoType::UBIGPK,
default => $this->type,
};

Expand Down
25 changes: 13 additions & 12 deletions src/Schema/Column/AbstractColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Db\Schema\Column;

use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Constant\ColumnType;

use function explode;
use function preg_match;
Expand Down Expand Up @@ -34,6 +34,7 @@ abstract class AbstractColumnFactory implements ColumnFactoryInterface
* @return string The abstract database type.
*
* @psalm-param ColumnInfo $info
* @psalm-return ColumnType::*
*/
abstract protected function getType(string $dbType, array $info = []): string;

Expand Down Expand Up @@ -85,21 +86,21 @@ public function fromDefinition(string $definition, array $info = []): ColumnSche
public function fromType(string $type, array $info = []): ColumnSchemaInterface
{
$column = match ($type) {
SchemaInterface::TYPE_BOOLEAN => new BooleanColumnSchema($type),
SchemaInterface::TYPE_BIT => new BitColumnSchema($type),
SchemaInterface::TYPE_TINYINT => new IntegerColumnSchema($type),
SchemaInterface::TYPE_SMALLINT => new IntegerColumnSchema($type),
SchemaInterface::TYPE_INTEGER => PHP_INT_SIZE !== 8 && !empty($info['unsigned'])
ColumnType::BOOLEAN => new BooleanColumnSchema($type),
ColumnType::BIT => new BitColumnSchema($type),
ColumnType::TINYINT => new IntegerColumnSchema($type),
ColumnType::SMALLINT => new IntegerColumnSchema($type),
ColumnType::INTEGER => PHP_INT_SIZE !== 8 && !empty($info['unsigned'])
? new BigIntColumnSchema($type)
: new IntegerColumnSchema($type),
SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8 || !empty($info['unsigned'])
ColumnType::BIGINT => PHP_INT_SIZE !== 8 || !empty($info['unsigned'])
? new BigIntColumnSchema($type)
: new IntegerColumnSchema($type),
SchemaInterface::TYPE_DECIMAL => new DoubleColumnSchema($type),
SchemaInterface::TYPE_FLOAT => new DoubleColumnSchema($type),
SchemaInterface::TYPE_DOUBLE => new DoubleColumnSchema($type),
SchemaInterface::TYPE_BINARY => new BinaryColumnSchema($type),
SchemaInterface::TYPE_JSON => new JsonColumnSchema($type),
ColumnType::DECIMAL => new DoubleColumnSchema($type),
ColumnType::FLOAT => new DoubleColumnSchema($type),
ColumnType::DOUBLE => new DoubleColumnSchema($type),
ColumnType::BINARY => new BinaryColumnSchema($type),
ColumnType::JSON => new JsonColumnSchema($type),
default => new StringColumnSchema($type),
};

Expand Down
4 changes: 4 additions & 0 deletions src/Schema/Column/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Schema\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PhpType;

use function is_array;
Expand Down Expand Up @@ -50,6 +51,9 @@ abstract class AbstractColumnSchema implements ColumnSchemaInterface
private int|null $size = null;
private bool $unsigned = false;

/**
* @psalm-param ColumnType::* $type
*/
public function __construct(
private string $type,
) {
Expand Down
7 changes: 5 additions & 2 deletions src/Schema/Column/BigIntColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Yiisoft\Db\Schema\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Constant\GettypeResult;
use Yiisoft\Db\Constant\PhpType;
use Yiisoft\Db\Schema\SchemaInterface;

use function gettype;

Expand All @@ -16,8 +16,11 @@

class BigIntColumnSchema extends AbstractColumnSchema
{
/**
* @psalm-param ColumnType::* $type
*/
public function __construct(
string $type = SchemaInterface::TYPE_BIGINT,
string $type = ColumnType::BIGINT,
) {
parent::__construct($type);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Schema/Column/BinaryColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

use PDO;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Constant\GettypeResult;
use Yiisoft\Db\Schema\SchemaInterface;

use function gettype;

class BinaryColumnSchema extends AbstractColumnSchema
{
/**
* @psalm-param ColumnType::* $type
*/
public function __construct(
string $type = SchemaInterface::TYPE_BINARY,
string $type = ColumnType::BINARY,
) {
parent::__construct($type);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Schema/Column/BitColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace Yiisoft\Db\Schema\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PhpType;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Schema\SchemaInterface;

class BitColumnSchema extends AbstractColumnSchema
{
/**
* @psalm-param ColumnType::* $type
*/
public function __construct(
string $type = SchemaInterface::TYPE_BIT,
string $type = ColumnType::BIT,
) {
parent::__construct($type);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Schema/Column/BooleanColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace Yiisoft\Db\Schema\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Constant\PhpType;
use Yiisoft\Db\Schema\SchemaInterface;

class BooleanColumnSchema extends AbstractColumnSchema
{
/**
* @psalm-param ColumnType::* $type
*/
public function __construct(
string $type = SchemaInterface::TYPE_BOOLEAN,
string $type = ColumnType::BOOLEAN,
) {
parent::__construct($type);
}
Expand Down
Loading

0 comments on commit e3e277d

Please sign in to comment.