Skip to content

Commit

Permalink
Generalize the logic of sp_rename execution
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Nov 15, 2024
1 parent 836b5f8 commit 1300542
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\DBAL\Types\Types;
use InvalidArgumentException;

use function array_map;
use function array_merge;
use function array_unique;
use function array_values;
Expand Down Expand Up @@ -494,11 +495,7 @@ public function getAlterTableSQL(TableDiff $diff): array

public function getRenameTableSQL(string $oldName, string $newName): string
{
return sprintf(
'sp_rename %s, %s',
$this->quoteStringLiteral($oldName),
$this->quoteStringLiteral($newName),
);
return $this->getRenameSQL($oldName, $newName);
}

/**
Expand Down Expand Up @@ -632,13 +629,7 @@ protected function getDropColumnCommentSQL(string $tableName, string $columnName
*/
protected function getRenameIndexSQL(string $oldIndexName, Index $index, string $tableName): array
{
return [sprintf(
"EXEC sp_rename N'%s.%s', N'%s', N'INDEX'",
$tableName,
$oldIndexName,
$index->getName(),
),
];
return [$this->getRenameSQL($tableName . '.' . $oldIndexName, $index->getName(), 'INDEX')];
}

/**
Expand All @@ -652,12 +643,18 @@ protected function getRenameIndexSQL(string $oldIndexName, Index $index, string
*/
protected function getRenameColumnSQL(string $tableName, string $oldColumnName, string $newColumnName): array
{
return [sprintf(
"EXEC sp_rename %s, %s, 'COLUMN'",
$this->quoteStringLiteral($tableName . '.' . $oldColumnName),
$this->quoteStringLiteral($newColumnName),
),
];
return [$this->getRenameSQL($tableName . '.' . $oldColumnName, $newColumnName)];
}

/**
* Returns the SQL statement that will execute sp_rename with the given arguments.
*/
private function getRenameSQL(string ...$arguments): string
{
return 'EXEC sp_rename '
. implode(', ', array_map(function (string $argument): string {
return 'N' . $this->quoteStringLiteral($argument);
}, $arguments));
}

/**
Expand Down

0 comments on commit 1300542

Please sign in to comment.