Final Class Yiisoft\Db\Mssql\DDLQueryBuilder
| Inheritance | Yiisoft\Db\Mssql\DDLQueryBuilder » Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder |
|---|
Implements a (Data Definition Language) SQL statements for MSSQL Server.
Public Methods
Method Details
| public string addCommentOnColumn ( string $table, string $column, string $comment ) | ||
| $table | string | |
| $column | string | |
| $comment | string | |
| throws | InvalidArgumentException | |
|---|---|---|
public function addCommentOnColumn(string $table, string $column, string $comment): string
{
return $this->buildAddCommentSql($comment, $table, $column);
}
| public string addCommentOnTable ( string $table, string $comment ) | ||
| $table | string | |
| $comment | string | |
| throws | InvalidArgumentException | |
|---|---|---|
public function addCommentOnTable(string $table, string $comment): string
{
return $this->buildAddCommentSql($comment, $table);
}
| public string addDefaultValue ( string $table, string $name, string $column, mixed $value ) | ||
| $table | string | |
| $name | string | |
| $column | string | |
| $value | mixed | |
| throws | Exception | |
|---|---|---|
public function addDefaultValue(string $table, string $name, string $column, mixed $value): string
{
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' ADD CONSTRAINT '
. $this->quoter->quoteColumnName($name)
. ' DEFAULT ' . $this->queryBuilder->prepareValue($value)
. ' FOR ' . $this->quoter->quoteColumnName($column);
}
| 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 | |
| throws | Exception | |
|---|---|---|
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
{
$columnName = $this->quoter->quoteColumnName($column);
$tableName = $this->quoter->quoteTableName($table);
$constraintBase = preg_replace('/\W/', '', $table . '_' . $column);
if (is_string($type)) {
$type = $this->queryBuilder->getColumnFactory()->fromDefinition($type);
}
$columnDefinitionBuilder = $this->queryBuilder->getColumnDefinitionBuilder();
$statements = [
$this->dropConstraintsForColumn($table, $column, 'D'),
"ALTER TABLE $tableName ALTER COLUMN $columnName " . $columnDefinitionBuilder->buildAlter($type),
];
if ($type->hasDefaultValue()) {
$defaultValue = $type->dbTypecast($type->getDefaultValue());
$statements[] = $this->addDefaultValue($table, "DF_$constraintBase", $column, $defaultValue);
}
$checkValue = $type->getCheck();
if (!empty($checkValue)) {
$statements[] = "ALTER TABLE $tableName ADD CONSTRAINT "
. $this->quoter->quoteColumnName("CK_$constraintBase")
. " CHECK ($checkValue)";
}
if ($type->isUnique()) {
$statements[] = "ALTER TABLE $tableName ADD CONSTRAINT "
. $this->quoter->quoteColumnName("UQ_$constraintBase")
. " UNIQUE ($columnName)";
}
return implode("\n", $statements);
}
| public string checkIntegrity ( string $schema = '', string $table = '', boolean $check = true ) | ||
| $schema | string | |
| $table | string | |
| $check | boolean | |
| throws | \Yiisoft\Db\Exception\NotSupportedException | |
|---|---|---|
| throws | Throwable | |
| throws | \Yiisoft\Db\Exception\Exception | |
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
{
$enable = $check ? 'CHECK' : 'NOCHECK';
/** @psalm-var Schema $schemaInstance */
$schemaInstance = $this->schema;
$defaultSchema = $schema ?: $schemaInstance->getDefaultSchema();
/** @psalm-var string[] $tableNames */
$tableNames = $schemaInstance->getTableSchema($table)
? [$table] : $schemaInstance->getTableNames($defaultSchema);
$viewNames = $schemaInstance->getViewNames($defaultSchema);
$tableNames = array_diff($tableNames, $viewNames);
$command = '';
foreach ($tableNames as $tableName) {
$tableName = $this->quoter->quoteTableName("$defaultSchema.$tableName");
$command .= "ALTER TABLE $tableName $enable CONSTRAINT ALL; ";
}
return $command;
}
| 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)
. (!empty($columns) ? ' (' . $this->queryBuilder->buildColumns($columns) . ')' : '')
. (!empty($indexMethod) ? " USING $indexMethod" : '');
}
| public string dropColumn ( string $table, string $column ) | ||
| $table | string | |
| $column | string | |
public function dropColumn(string $table, string $column): string
{
return $this->dropConstraintsForColumn($table, $column)
. "\nALTER TABLE "
. $this->quoter->quoteTableName($table)
. ' DROP COLUMN '
. $this->quoter->quoteColumnName($column);
}
| public string dropCommentFromColumn ( string $table, string $column ) | ||
| $table | string | |
| $column | string | |
| throws | InvalidArgumentException | |
|---|---|---|
public function dropCommentFromColumn(string $table, string $column): string
{
return $this->buildRemoveCommentSql($table, $column);
}
| public string dropCommentFromTable ( string $table ) | ||
| $table | string | |
| throws | InvalidArgumentException | |
|---|---|---|
public function dropCommentFromTable(string $table): string
{
return $this->buildRemoveCommentSql($table);
}
| public string dropDefaultValue ( string $table, string $name ) | ||
| $table | string | |
| $name | string | |
public function dropDefaultValue(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 | |
| throws | \Yiisoft\Db\Exception\NotSupportedException |
MSSQL doesn't support cascade drop table. |
|---|---|---|
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
{
if ($cascade) {
throw new NotSupportedException('MSSQL doesn\'t support cascade drop table.');
}
return parent::dropTable($table, $ifExists, false);
}
| 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 'sp_rename '
. "'" . $this->quoter->quoteTableName($table) . '.' . $this->quoter->quoteColumnName($oldName) . "'" . ', '
. $this->quoter->quoteColumnName($newName) . ', '
. "'COLUMN'";
}
Signup or Login in order to comment.