0 follower

Abstract Class Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder

InheritanceYiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
ImplementsYiisoft\Db\QueryBuilder\ColumnDefinitionBuilderInterface

Builds column definition from {@see ColumnInterface} object. Column definition is a string that represents the column type and all constraints associated with the column. For example: VARCHAR(128) NOT NULL DEFAULT 'foo'.

Protected Methods

Hide inherited methods

Method Description Defined By
buildAutoIncrement() Builds the auto increment clause for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildCheck() Builds the check constraint for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildCollate() Builds the collation clause for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildComment() Builds the comment clause for the column. Default is empty string. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildDefault() Builds the default value specification for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildExtra() Builds the custom string that's appended to column definition. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildNotNull() Builds the not null constraint for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildOnDelete() Builds the ON DELETE clause for the column reference. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildOnUpdate() Builds the ON UPDATE clause for the column reference. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildPrimaryKey() Builds the primary key clause for column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildReferenceDefinition() Builds the reference definition for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildReferences() Builds the references clause for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildUnique() Builds the unique constraint for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
buildUnsigned() Builds the unsigned string for the column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
getDbType() Get the database column type for the given column. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
getDefaultUuidExpression() Get the expression used to generate a UUID as a default value. Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder

Property Details

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder )
$queryBuilder Yiisoft\Db\QueryBuilder\QueryBuilderInterface

                public function __construct(
    protected QueryBuilderInterface $queryBuilder,
) {}

            
build() public method

public string build ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface

                public function build(ColumnInterface $column): string
{
    return $this->buildType($column)
        . $this->buildUnsigned($column)
        . $this->buildNotNull($column)
        . $this->buildPrimaryKey($column)
        . $this->buildAutoIncrement($column)
        . $this->buildUnique($column)
        . $this->buildDefault($column)
        . $this->buildComment($column)
        . $this->buildCheck($column)
        . $this->buildCollate($column)
        . $this->buildReferences($column)
        . $this->buildExtra($column);
}

            
buildAlter() public method

public string buildAlter ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface

                public function buildAlter(ColumnInterface $column): string
{
    return $this->build($column);
}

            
buildAutoIncrement() protected method

Builds the auto increment clause for the column.

protected string buildAutoIncrement ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the {@see \Yiisoft\Db\QueryBuilder\AUTO_INCREMENT_KEYWORD} keyword.

                protected function buildAutoIncrement(ColumnInterface $column): string
{
    if (empty(static::AUTO_INCREMENT_KEYWORD) || !$column->isAutoIncrement()) {
        return '';
    }
    return match ($column->getType()) {
        ColumnType::TINYINT,
        ColumnType::SMALLINT,
        ColumnType::INTEGER,
        ColumnType::BIGINT => ' ' . static::AUTO_INCREMENT_KEYWORD,
        default => '',
    };
}

            
buildCheck() protected method

Builds the check constraint for the column.

protected string buildCheck ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the CHECK constraint.

                protected function buildCheck(ColumnInterface $column): string
{
    $check = $column->getCheck();
    if (empty($check)
        && $column instanceof EnumColumn
        && $column->getDbType() === null
    ) {
        $name = $column->getName();
        if (!empty($name)) {
            $quoter = $this->queryBuilder->getQuoter();
            $quotedItems = implode(
                ',',
                array_map(
                    $quoter->quoteValue(...),
                    $column->getValues(),
                ),
            );
            $quotedName = $quoter->quoteColumnName($name);
            $check = "$quotedName IN ($quotedItems)";
        }
    }
    return !empty($check) ? " CHECK ($check)" : '';
}

            
buildCollate() protected method

Builds the collation clause for the column.

protected string buildCollate ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the COLLATE keyword and the collation name.

                protected function buildCollate(ColumnInterface $column): string
{
    if (!$column instanceof CollatableColumnInterface || empty($column->getCollation())) {
        return '';
    }
    /** @psalm-suppress PossiblyNullOperand */
    return ' COLLATE ' . $column->getCollation();
}

            
buildComment() protected method

Builds the comment clause for the column. Default is empty string.

protected string buildComment ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the COMMENT keyword and the comment itself.

                protected function buildComment(ColumnInterface $column): string
{
    return '';
}

            
buildDefault() protected method

Builds the default value specification for the column.

protected string buildDefault ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the DEFAULT keyword and the default value.

                protected function buildDefault(ColumnInterface $column): string
{
    $uuidExpression = $this->getDefaultUuidExpression();
    if (!empty($uuidExpression)
        && $column->getType() === ColumnType::UUID
        && $column->isAutoIncrement()
        && $column->getDefaultValue() === null
    ) {
        return " DEFAULT $uuidExpression";
    }
    if ($column->isAutoIncrement() && $column->getType() !== ColumnType::UUID
        || !$column->hasDefaultValue()
    ) {
        return '';
    }
    $defaultValue = $column->dbTypecast($column->getDefaultValue());
    $defaultValue = $this->queryBuilder->prepareValue($defaultValue);
    if ($defaultValue === '') {
        return '';
    }
    return " DEFAULT $defaultValue";
}

            
buildExtra() protected method

Builds the custom string that's appended to column definition.

protected string buildExtra ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the custom SQL fragment appended to column definition.

                protected function buildExtra(ColumnInterface $column): string
{
    $extra = $column->getExtra();
    return !empty($extra) ? " $extra" : '';
}

            
buildNotNull() protected method

Builds the not null constraint for the column.

protected string buildNotNull ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string 'NOT NULL' if {@see \Yiisoft\Db\Schema\Column\ColumnInterface::isNotNull()} is true or an empty string otherwise.

                protected function buildNotNull(ColumnInterface $column): string
{
    return match ($column->isNotNull()) {
        true => ' NOT NULL',
        false => ' NULL',
        default => '',
    };
}

            
buildOnDelete() protected method

Builds the ON DELETE clause for the column reference.

protected string buildOnDelete ( string $onDelete )
$onDelete string

                protected function buildOnDelete(string $onDelete): string
{
    return " ON DELETE $onDelete";
}

            
buildOnUpdate() protected method

Builds the ON UPDATE clause for the column reference.

protected string buildOnUpdate ( string $onUpdate )
$onUpdate string

                protected function buildOnUpdate(string $onUpdate): string
{
    return " ON UPDATE $onUpdate";
}

            
buildPrimaryKey() protected method

Builds the primary key clause for column.

protected string buildPrimaryKey ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the PRIMARY KEY keyword.

                protected function buildPrimaryKey(ColumnInterface $column): string
{
    return $column->isPrimaryKey() ? ' PRIMARY KEY' : '';
}

            
buildReferenceDefinition() protected method

Builds the reference definition for the column.

protected string buildReferenceDefinition ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface

                protected function buildReferenceDefinition(ColumnInterface $column): string
{
    $reference = $column->getReference();
    if ($reference === null || $reference->foreignTableName === '') {
        return '';
    }
    $quoter = $this->queryBuilder->getQuoter();
    $sql = $reference->foreignSchemaName === ''
        ? $quoter->quoteTableName($reference->foreignTableName)
        : $quoter->quoteTableName($reference->foreignSchemaName)
            . '.' . $quoter->quoteTableName($reference->foreignTableName);
    if (!empty($reference->foreignColumnNames)) {
        $sql .= ' (' . $this->queryBuilder->buildColumns($reference->foreignColumnNames) . ')';
    }
    if ($reference->onDelete !== null) {
        $sql .= $this->buildOnDelete($reference->onDelete);
    }
    if ($reference->onUpdate !== null) {
        $sql .= $this->buildOnUpdate($reference->onUpdate);
    }
    return $sql;
}

            
buildReferences() protected method

Builds the references clause for the column.

protected string buildReferences ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface

                protected function buildReferences(ColumnInterface $column): string
{
    $reference = $this->buildReferenceDefinition($column);
    if ($reference === '') {
        return '';
    }
    return " REFERENCES $reference";
}

            
buildType() public method

public string buildType ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface

                public function buildType(ColumnInterface $column): string
{
    $dbType = $this->getDbType($column);
    if (empty($dbType)
        || $dbType[-1] === ')'
        || !in_array(strtolower($dbType), static::TYPES_WITH_SIZE, true)
    ) {
        return $dbType;
    }
    $size = $column->getSize();
    if ($size === null) {
        return $dbType;
    }
    $scale = $column->getScale();
    if ($scale === null || !in_array(strtolower($dbType), static::TYPES_WITH_SCALE, true)) {
        return "$dbType($size)";
    }
    return "$dbType($size,$scale)";
}

            
buildUnique() protected method

Builds the unique constraint for the column.

protected string buildUnique ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string 'UNIQUE' if {@see \Yiisoft\Db\Schema\Column\ColumnInterface::isUnique()} is true or an empty string otherwise.

                protected function buildUnique(ColumnInterface $column): string
{
    if ($column->isPrimaryKey()) {
        return '';
    }
    return $column->isUnique() ? ' UNIQUE' : '';
}

            
buildUnsigned() protected method

Builds the unsigned string for the column.

protected string buildUnsigned ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface
return string

A string containing the UNSIGNED keyword.

                protected function buildUnsigned(ColumnInterface $column): string
{
    return $column->isUnsigned() ? ' UNSIGNED' : '';
}

            
getDbType() protected abstract method

Get the database column type for the given column.

protected abstract string getDbType ( Yiisoft\Db\Schema\Column\ColumnInterface $column )
$column Yiisoft\Db\Schema\Column\ColumnInterface

The column object.

return string

The database column type.

                abstract protected function getDbType(ColumnInterface $column): string;

            
getDefaultUuidExpression() protected method

Get the expression used to generate a UUID as a default value.

protected string getDefaultUuidExpression ( )

                protected function getDefaultUuidExpression(): string
{
    return '';
}