Fixes after review

This commit is contained in:
DarkDef
2021-04-27 12:15:31 +03:00
parent 12ccd2adde
commit 2d76f33dd4
2 changed files with 10 additions and 7 deletions

View File

@ -13,8 +13,8 @@ use yii\db\Expression;
/** /**
* ColumnSchemaBuilder is the schema builder for MSSQL databases. * ColumnSchemaBuilder is the schema builder for MSSQL databases.
* *
* @author Chris Harris <chris@buckshotsoftware.com> * @author Valerii Gorbachev <darkdef@gmail.com>
* @since 2.0.8 * @since 2.0.42
*/ */
class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
{ {
@ -64,7 +64,7 @@ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
*/ */
public function getCheckValue() public function getCheckValue()
{ {
return $this->check !== null ? "{$this->check}" : null; return $this->check !== null ? (string) $this->check : null;
} }
/** /**

View File

@ -176,13 +176,14 @@ class QueryBuilder extends \yii\db\QueryBuilder
{ {
$sqlAfter = [$this->dropConstraintsForColumn($table, $column, 'D')]; $sqlAfter = [$this->dropConstraintsForColumn($table, $column, 'D')];
if ($type instanceof \yii\db\mssql\ColumnSchemaBuilder) {
$type->setAlterColumnFormat();
$columnName = $this->db->quoteColumnName($column); $columnName = $this->db->quoteColumnName($column);
$tableName = $this->db->quoteTableName($table); $tableName = $this->db->quoteTableName($table);
$constraintBase = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column); $constraintBase = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column);
if ($type instanceof \yii\db\mssql\ColumnSchemaBuilder) {
$type->setAlterColumnFormat();
$defaultValue = $type->getDefaultValue(); $defaultValue = $type->getDefaultValue();
if ($defaultValue !== null) { if ($defaultValue !== null) {
$sqlAfter[] = $this->addDefaultValue( $sqlAfter[] = $this->addDefaultValue(
@ -195,7 +196,9 @@ class QueryBuilder extends \yii\db\QueryBuilder
$checkValue = $type->getCheckValue(); $checkValue = $type->getCheckValue();
if ($checkValue !== null) { if ($checkValue !== null) {
$sqlAfter[] = "ALTER TABLE {$tableName} ADD CONSTRAINT " . $this->db->quoteColumnName("CK_{$constraintBase}") . " CHECK ({$checkValue})"; $sqlAfter[] = "ALTER TABLE {$tableName} ADD CONSTRAINT " .
$this->db->quoteColumnName("CK_{$constraintBase}") .
" CHECK (" . ($defaultValue instanceof Expression ? $checkValue : new Expression($checkValue)) . ")";
} }
if ($type->isUnique()) { if ($type->isUnique()) {