Abstract Class Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder
| Inheritance | Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder |
|---|---|
| Implements | Yiisoft\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.
Protected Properties
Public Methods
Property Details
Method Details
| 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,
) {}
| 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) . ')';
}
| 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);
}
| 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);
}
| 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);
}
| 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.');
}
| 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;
}
| 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) . ')';
}
| 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) . ')';
}
| 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);
}
| 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.');
}
| 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) . ')';
}
| 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;
}
| 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;
}
| 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);
}
| 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);
}
| 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';
}
| public string dropCommentFromTable ( string $table ) | ||
| $table | string | |
public function dropCommentFromTable(string $table): string
{
return 'COMMENT ON TABLE '
. $this->quoter->quoteTableName($table)
. ' IS NULL';
}
| 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.');
}
| 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);
}
| 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);
}
| 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);
}
| 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' : '');
}
| 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);
}
| public string dropView ( string $viewName ) | ||
| $viewName | string | |
public function dropView(string $viewName): string
{
return 'DROP VIEW ' . $this->quoter->quoteTableName($viewName);
}
| 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);
}
| 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);
}
| public string truncateTable ( string $table ) | ||
| $table | string | |
public function truncateTable(string $table): string
{
return 'TRUNCATE TABLE ' . $this->quoter->quoteTableName($table);
}
Signup or Login in order to comment.