0 follower

Abstract Class Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder

InheritanceYiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
ImplementsYiisoft\Db\QueryBuilder\DDLQueryBuilderInterface

It's used to create and change the structure of database objects in a database.

These database objects include views, schemas, tables, indexes, etc.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addCheck() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addColumn() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addCommentOnColumn() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addCommentOnTable() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addDefaultValue() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addForeignKey() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addPrimaryKey() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
addUnique() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
alterColumn() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
checkIntegrity() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
createIndex() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
createTable() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
createView() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropCheck() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropColumn() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropCommentFromColumn() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropCommentFromTable() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropDefaultValue() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropForeignKey() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropIndex() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropPrimaryKey() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropTable() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropUnique() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
dropView() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
renameColumn() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
renameTable() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
truncateTable() Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder

Property Details

Hide inherited properties

$queryBuilder protected property
$quoter protected property
$schema protected property

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder, Yiisoft\Db\Schema\QuoterInterface $quoter, Yiisoft\Db\Schema\SchemaInterface $schema )
$queryBuilder Yiisoft\Db\QueryBuilder\QueryBuilderInterface
$quoter Yiisoft\Db\Schema\QuoterInterface
$schema Yiisoft\Db\Schema\SchemaInterface

                public function __construct(
    protected QueryBuilderInterface $queryBuilder,
    protected QuoterInterface $quoter,
    protected SchemaInterface $schema,
) {}

            
addCheck() public method

public string addCheck ( string $table, string $name, string $expression )
$table string
$name string
$expression string

                public function addCheck(string $table, string $name, string $expression): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' ADD CONSTRAINT '
        . $this->quoter->quoteColumnName($name)
        . ' CHECK (' . $this->quoter->quoteSql($expression) . ')';
}

            
addColumn() public method

public string addColumn ( string $table, string $column, Yiisoft\Db\Schema\Column\ColumnInterface|string $type )
$table string
$column string
$type Yiisoft\Db\Schema\Column\ColumnInterface|string

                public function addColumn(string $table, string $column, ColumnInterface|string $type): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' ADD '
        . $this->quoter->quoteColumnName($column)
        . ' '
        . $this->queryBuilder->buildColumnDefinition($type);
}

            
addCommentOnColumn() public method

public string addCommentOnColumn ( string $table, string $column, string $comment )
$table string
$column string
$comment string

                public function addCommentOnColumn(string $table, string $column, string $comment): string
{
    return 'COMMENT ON COLUMN '
        . $this->quoter->quoteTableName($table)
        . '.'
        . $this->quoter->quoteColumnName($column)
        . ' IS '
        . $this->quoter->quoteValue($comment);
}

            
addCommentOnTable() public method

public string addCommentOnTable ( string $table, string $comment )
$table string
$comment string

                public function addCommentOnTable(string $table, string $comment): string
{
    return 'COMMENT ON TABLE '
        . $this->quoter->quoteTableName($table)
        . ' IS '
        . $this->quoter->quoteValue($comment);
}

            
addDefaultValue() public method

public string addDefaultValue ( string $table, string $name, string $column, mixed $value )
$table string
$name string
$column string
$value mixed

                public function addDefaultValue(string $table, string $name, string $column, mixed $value): string
{
    throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
}

            
addForeignKey() public method

public string addForeignKey ( string $table, string $name, array|string $columns, string $referenceTable, array|string $referenceColumns, string|null $delete null, string|null $update null )
$table string
$name string
$columns array|string
$referenceTable string
$referenceColumns array|string
$delete string|null
$update string|null

                public function addForeignKey(
    string $table,
    string $name,
    array|string $columns,
    string $referenceTable,
    array|string $referenceColumns,
    ?string $delete = null,
    ?string $update = null,
): string {
    $sql = 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name)
        . ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')'
        . ' REFERENCES ' . $this->quoter->quoteTableName($referenceTable)
        . ' (' . $this->queryBuilder->buildColumns($referenceColumns) . ')';
    if ($delete !== null) {
        $sql .= ' ON DELETE ' . $delete;
    }
    if ($update !== null) {
        $sql .= ' ON UPDATE ' . $update;
    }
    return $sql;
}

            
addPrimaryKey() public method

public string addPrimaryKey ( string $table, string $name, array|string $columns )
$table string
$name string
$columns array|string

                public function addPrimaryKey(string $table, string $name, array|string $columns): string
{
    if (is_string($columns)) {
        /** @psalm-var list<string> */
        $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
    }
    foreach ($columns as $i => $col) {
        $columns[$i] = $this->quoter->quoteColumnName($col);
    }
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name)
        . ' PRIMARY KEY (' . implode(', ', $columns) . ')';
}

            
addUnique() public method

public string addUnique ( string $table, string $name, array|string $columns )
$table string
$name string
$columns array|string

                public function addUnique(string $table, string $name, array|string $columns): string
{
    if (is_string($columns)) {
        /** @psalm-var list<string> */
        $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
    }
    foreach ($columns as $i => $col) {
        $columns[$i] = $this->quoter->quoteColumnName($col);
    }
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name)
        . ' UNIQUE (' . implode(', ', $columns) . ')';
}

            
alterColumn() public method

public string alterColumn ( string $table, string $column, Yiisoft\Db\Schema\Column\ColumnInterface|string $type )
$table string
$column string
$type Yiisoft\Db\Schema\Column\ColumnInterface|string

                public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' CHANGE '
        . $this->quoter->quoteColumnName($column)
        . ' '
        . $this->quoter->quoteColumnName($column) . ' '
        . $this->queryBuilder->buildColumnDefinition($type);
}

            
checkIntegrity() public method

public string checkIntegrity ( string $schema '', string $table '', boolean $check true )
$schema string
$table string
$check boolean

                public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
{
    throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
}

            
createIndex() public method

public string createIndex ( string $table, string $name, array|string $columns, string|null $indexType null, string|null $indexMethod null )
$table string
$name string
$columns array|string
$indexType string|null
$indexMethod string|null

                public function createIndex(
    string $table,
    string $name,
    array|string $columns,
    ?string $indexType = null,
    ?string $indexMethod = null,
): string {
    return 'CREATE ' . (!empty($indexType) ? $indexType . ' ' : '') . 'INDEX '
        . $this->quoter->quoteTableName($name)
        . ' ON ' . $this->quoter->quoteTableName($table)
        . ' (' . $this->queryBuilder->buildColumns($columns) . ')';
}

            
createTable() public method

public string createTable ( string $table, array $columns, string|null $options null )
$table string
$columns array
$options string|null

                public function createTable(string $table, array $columns, ?string $options = null): string
{
    $cols = [];
    $quoter = $this->quoter;
    $queryBuilder = $this->queryBuilder;
    foreach ($columns as $name => $type) {
        if (is_string($name)) {
            $columnDefinition = match (true) {
                $type instanceof ColumnInterface => $queryBuilder->buildColumnDefinition($type->withName($name)),
                $type instanceof ExpressionInterface => $queryBuilder->buildExpression($type),
                default => $queryBuilder->buildColumnDefinition($type),
            };
            $cols[] = "{$quoter->quoteColumnName($name)} $columnDefinition";
        } else {
            /** @var string $type */
            $cols[] = $type;
        }
    }
    $sql = "CREATE TABLE {$quoter->quoteTableName($table)} (\n\t"
        . implode(",\n\t", $cols)
        . "\n)";
    return $options === null ? $sql : $sql . ' ' . $options;
}

            
createView() public method

public string createView ( string $viewName, Yiisoft\Db\Query\QueryInterface|string $subQuery )
$viewName string
$subQuery Yiisoft\Db\Query\QueryInterface|string

                public function createView(string $viewName, QueryInterface|string $subQuery): string
{
    if ($subQuery instanceof QueryInterface) {
        [$rawQuery, $params] = $this->queryBuilder->build($subQuery);
        $params = array_map($this->queryBuilder->prepareValue(...), $params);
        $subQuery = $this->queryBuilder->replacePlaceholders($rawQuery, $params);
    }
    return 'CREATE VIEW ' . $this->quoter->quoteTableName($viewName) . ' AS ' . $subQuery;
}

            
dropCheck() public method

public string dropCheck ( string $table, string $name )
$table string
$name string

                public function dropCheck(string $table, string $name): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' DROP CONSTRAINT '
        . $this->quoter->quoteColumnName($name);
}

            
dropColumn() public method

public string dropColumn ( string $table, string $column )
$table string
$column string

                public function dropColumn(string $table, string $column): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' DROP COLUMN '
        . $this->quoter->quoteColumnName($column);
}

            
dropCommentFromColumn() public method

public string dropCommentFromColumn ( string $table, string $column )
$table string
$column string

                public function dropCommentFromColumn(string $table, string $column): string
{
    return 'COMMENT ON COLUMN '
        . $this->quoter->quoteTableName($table)
        . '.'
        . $this->quoter->quoteColumnName($column)
        . ' IS NULL';
}

            
dropCommentFromTable() public method

public string dropCommentFromTable ( string $table )
$table string

                public function dropCommentFromTable(string $table): string
{
    return 'COMMENT ON TABLE '
         . $this->quoter->quoteTableName($table)
         . ' IS NULL';
}

            
dropDefaultValue() public method

public string dropDefaultValue ( string $table, string $name )
$table string
$name string

                public function dropDefaultValue(string $table, string $name): string
{
    throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
}

            
dropForeignKey() public method

public string dropForeignKey ( string $table, string $name )
$table string
$name string

                public function dropForeignKey(string $table, string $name): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' DROP CONSTRAINT '
        . $this->quoter->quoteColumnName($name);
}

            
dropIndex() public method

public string dropIndex ( string $table, string $name )
$table string
$name string

                public function dropIndex(string $table, string $name): string
{
    return 'DROP INDEX '
        . $this->quoter->quoteTableName($name)
        . ' ON '
        . $this->quoter->quoteTableName($table);
}

            
dropPrimaryKey() public method

public string dropPrimaryKey ( string $table, string $name )
$table string
$name string

                public function dropPrimaryKey(string $table, string $name): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' DROP CONSTRAINT '
        . $this->quoter->quoteColumnName($name);
}

            
dropTable() public method

public string dropTable ( string $table, boolean $ifExists false, boolean $cascade false )
$table string
$ifExists boolean
$cascade boolean

                public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
{
    return 'DROP TABLE '
        . ($ifExists ? 'IF EXISTS ' : '')
        . $this->quoter->quoteTableName($table)
        . ($cascade ? ' CASCADE' : '');
}

            
dropUnique() public method

public string dropUnique ( string $table, string $name )
$table string
$name string

                public function dropUnique(string $table, string $name): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' DROP CONSTRAINT '
        . $this->quoter->quoteColumnName($name);
}

            
dropView() public method

public string dropView ( string $viewName )
$viewName string

                public function dropView(string $viewName): string
{
    return 'DROP VIEW ' . $this->quoter->quoteTableName($viewName);
}

            
renameColumn() public method

public string renameColumn ( string $table, string $oldName, string $newName )
$table string
$oldName string
$newName string

                public function renameColumn(string $table, string $oldName, string $newName): string
{
    return 'ALTER TABLE '
        . $this->quoter->quoteTableName($table)
        . ' RENAME COLUMN ' . $this->quoter->quoteColumnName($oldName)
        . ' TO ' . $this->quoter->quoteColumnName($newName);
}

            
renameTable() public method

public string renameTable ( string $oldName, string $newName )
$oldName string
$newName string

                public function renameTable(string $oldName, string $newName): string
{
    return 'RENAME TABLE '
        . $this->quoter->quoteTableName($oldName)
        . ' TO ' . $this->quoter->quoteTableName($newName);
}

            
truncateTable() public method

public string truncateTable ( string $table )
$table string

                public function truncateTable(string $table): string
{
    return 'TRUNCATE TABLE ' . $this->quoter->quoteTableName($table);
}