Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 63 additions & 28 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,9 @@ protected function getRenameColumnSQL(string $tableName, string $oldColumnName,
*/
private function getRenameSQL(string ...$arguments): string
{
return 'EXEC sp_rename '
. implode(', ', array_map(function (string $argument): string {
return 'N' . $this->quoteStringLiteral($argument);
}, $arguments));
return $this->getExecSQL('sp_rename', ...array_map(function (string $argument): string {
return $this->quoteNationalStringLiteral($argument);
}, $arguments));
}

/**
Expand Down Expand Up @@ -692,14 +691,21 @@ protected function getAddExtendedPropertySQL(
?string $level2Type = null,
?string $level2Name = null,
): string {
return 'EXEC sp_addextendedproperty ' .
'N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral($value ?? '') . ', ' .
'N' . $this->quoteStringLiteral($level0Type ?? '') . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral($level1Type ?? '') . ', ' . $level1Name .
($level2Type !== null || $level2Name !== null
? ', N' . $this->quoteStringLiteral($level2Type ?? '') . ', ' . $level2Name
: ''
);
$arguments = [
$this->quoteNationalStringLiteral($name),
$this->quoteNationalStringLiteral($value ?? ''),
$this->quoteNationalStringLiteral($level0Type ?? ''),
$level0Name ?? '',
$this->quoteNationalStringLiteral($level1Type ?? ''),
$level1Name ?? '',
];

if ($level2Type !== null || $level2Name !== null) {
$arguments[] = $this->quoteNationalStringLiteral($level2Type ?? '');
$arguments[] = $level2Name ?? '';
}

return $this->getExecSQL('sp_addextendedproperty', ...$arguments);
}

/**
Expand All @@ -726,14 +732,20 @@ protected function getDropExtendedPropertySQL(
?string $level2Type = null,
?string $level2Name = null,
): string {
return 'EXEC sp_dropextendedproperty ' .
'N' . $this->quoteStringLiteral($name) . ', ' .
'N' . $this->quoteStringLiteral($level0Type ?? '') . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral($level1Type ?? '') . ', ' . $level1Name .
($level2Type !== null || $level2Name !== null
? ', N' . $this->quoteStringLiteral($level2Type ?? '') . ', ' . $level2Name
: ''
);
$arguments = [
$this->quoteNationalStringLiteral($name),
$this->quoteNationalStringLiteral($level0Type ?? ''),
$level0Name ?? '',
$this->quoteNationalStringLiteral($level1Type ?? ''),
$level1Name ?? '',
];

if ($level2Type !== null || $level2Name !== null) {
$arguments[] = $this->quoteNationalStringLiteral($level2Type ?? '');
$arguments[] = $level2Name ?? '';
}

return $this->getExecSQL('sp_dropextendedproperty', ...$arguments);
}

/**
Expand Down Expand Up @@ -762,14 +774,37 @@ protected function getUpdateExtendedPropertySQL(
?string $level2Type = null,
?string $level2Name = null,
): string {
return 'EXEC sp_updateextendedproperty ' .
'N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral($value ?? '') . ', ' .
'N' . $this->quoteStringLiteral($level0Type ?? '') . ', ' . $level0Name . ', ' .
'N' . $this->quoteStringLiteral($level1Type ?? '') . ', ' . $level1Name .
($level2Type !== null || $level2Name !== null
? ', N' . $this->quoteStringLiteral($level2Type ?? '') . ', ' . $level2Name
: ''
);
$arguments = [
$this->quoteNationalStringLiteral($name),
$this->quoteNationalStringLiteral($value ?? ''),
$this->quoteNationalStringLiteral($level0Type ?? ''),
$level0Name ?? '',
$this->quoteNationalStringLiteral($level1Type ?? ''),
$level1Name ?? '',
];

if ($level2Type !== null || $level2Name !== null) {
$arguments[] = $this->quoteNationalStringLiteral($level2Type ?? '');
$arguments[] = $level2Name ?? '';
}

return $this->getExecSQL('sp_updateextendedproperty', ...$arguments);
}

/**
* Returns the SQL statement that will execute the given stored procedure with the given arguments.
*
* @param string $procedureName The name of the stored procedure to execute.
* @param string ...$arguments The SQL fragments representing the arguments to pass to the stored procedure.
*/
private function getExecSQL(string $procedureName, string ...$arguments): string
{
return 'EXEC ' . $this->quoteSingleIdentifier($procedureName) . ' ' . implode(', ', $arguments);
}

private function quoteNationalStringLiteral(string $value): string
{
return 'N' . $this->quoteStringLiteral($value);
}

public function getEmptyIdentityInsertSQL(string $quotedTableName, string $quotedIdentifierColumnName): string
Expand Down
22 changes: 11 additions & 11 deletions tests/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public function testCreateTableWithSchemaColumnComments(): void

$expectedSql = [
'CREATE TABLE testschema.test (id INT NOT NULL, PRIMARY KEY (id))',
"EXEC sp_addextendedproperty N'MS_Description', N'This is a comment', "
"EXEC [sp_addextendedproperty] N'MS_Description', N'This is a comment', "
. "N'SCHEMA', 'testschema', N'TABLE', 'test', N'COLUMN', 'id'",
];

Expand All @@ -678,7 +678,7 @@ public function testAlterTableWithSchemaColumnComments(): void

$expectedSql = [
'ALTER TABLE testschema.mytable ADD quota INT NOT NULL',
"EXEC sp_addextendedproperty N'MS_Description', N'A comment', "
"EXEC [sp_addextendedproperty] N'MS_Description', N'A comment', "
. "N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', 'quota'",
];

Expand All @@ -697,7 +697,7 @@ public function testAlterTableWithSchemaDropColumnComments(): void
]);

$expectedSql = [
"EXEC sp_dropextendedproperty N'MS_Description'"
"EXEC [sp_dropextendedproperty] N'MS_Description'"
. ", N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', 'quota'",
];

Expand All @@ -715,7 +715,7 @@ public function testAlterTableWithSchemaUpdateColumnComments(): void
),
]);

$expectedSql = ["EXEC sp_updateextendedproperty N'MS_Description', N'B comment', "
$expectedSql = ["EXEC [sp_updateextendedproperty] N'MS_Description', N'B comment', "
. "N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', 'quota'",
];

Expand Down Expand Up @@ -840,7 +840,7 @@ public function testGetVariableLengthBinaryTypeDeclarationSQLNoLength(): void
*/
protected function getAlterTableRenameIndexSQL(): array
{
return ["EXEC sp_rename N'mytable.idx_foo', N'idx_bar', N'INDEX'"];
return ["EXEC [sp_rename] N'mytable.idx_foo', N'idx_bar', N'INDEX'"];
}

/**
Expand All @@ -849,8 +849,8 @@ protected function getAlterTableRenameIndexSQL(): array
protected function getQuotedAlterTableRenameIndexSQL(): array
{
return [
"EXEC sp_rename N'[table].[create]', N'select', N'INDEX'",
"EXEC sp_rename N'[table].[foo]', N'bar', N'INDEX'",
"EXEC [sp_rename] N'[table].[create]', N'select', N'INDEX'",
"EXEC [sp_rename] N'[table].[foo]', N'bar', N'INDEX'",
];
}

Expand All @@ -859,7 +859,7 @@ protected function getQuotedAlterTableRenameIndexSQL(): array
*/
protected function getAlterTableRenameIndexInSchemaSQL(): array
{
return ["EXEC sp_rename N'myschema.mytable.idx_foo', N'idx_bar', N'INDEX'"];
return ["EXEC [sp_rename] N'myschema.mytable.idx_foo', N'idx_bar', N'INDEX'"];
}

/**
Expand All @@ -868,8 +868,8 @@ protected function getAlterTableRenameIndexInSchemaSQL(): array
protected function getQuotedAlterTableRenameIndexInSchemaSQL(): array
{
return [
"EXEC sp_rename N'[schema].[table].[create]', N'select', N'INDEX'",
"EXEC sp_rename N'[schema].[table].[foo]', N'bar', N'INDEX'",
"EXEC [sp_rename] N'[schema].[table].[create]', N'select', N'INDEX'",
"EXEC [sp_rename] N'[schema].[table].[foo]', N'bar', N'INDEX'",
];
}

Expand Down Expand Up @@ -933,7 +933,7 @@ protected function getAlterStringToFixedStringSQL(): array
*/
protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL(): array
{
return ["EXEC sp_rename N'mytable.idx_foo', N'idx_foo_renamed', N'INDEX'"];
return ["EXEC [sp_rename] N'mytable.idx_foo', N'idx_foo_renamed', N'INDEX'"];
}

protected function getLimitOffsetCastToIntExpectedQuery(): string
Expand Down
Loading