Abstract Class Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder
| Inheritance | Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder |
|---|---|
| Implements | Yiisoft\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 Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder |
Public Methods
Protected Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| AUTO_INCREMENT_KEYWORD | '' | Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder | |
| TYPES_WITH_SCALE | [] | Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder | |
| TYPES_WITH_SIZE | [] | Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder |
Property Details
Method Details
| public mixed __construct ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder ) | ||
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | |
public function __construct(
protected QueryBuilderInterface $queryBuilder,
) {}
| 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);
}
| 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);
}
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 => '',
};
}
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)" : '';
}
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();
}
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 '';
}
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";
}
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" : '';
}
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 |
|---|---|---|
protected function buildNotNull(ColumnInterface $column): string
{
return match ($column->isNotNull()) {
true => ' NOT NULL',
false => ' NULL',
default => '',
};
}
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";
}
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";
}
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' : '';
}
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;
}
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";
}
| 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)";
}
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' : '';
}
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' : '';
}
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;
Get the expression used to generate a UUID as a default value.
| protected string getDefaultUuidExpression ( ) |
protected function getDefaultUuidExpression(): string
{
return '';
}
Signup or Login in order to comment.