Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve MSSQL "extended property" DBAL fix #1190

Merged
merged 3 commits into from
Apr 7, 2024
Merged
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
135 changes: 22 additions & 113 deletions src/Persistence/Sql/Mssql/PlatformTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,165 +60,74 @@ public function getCreateIndexSQL(Index $index, $table)
}

// SQL Server DBAL platform has buggy identifier escaping, fix until fixed officially, see:
// https://github.com/doctrine/dbal/pull/4360
// https://github.com/doctrine/dbal/pull/6353

private function unquoteSingleIdentifier(string $possiblyQuotedName): string
{
return str_starts_with($possiblyQuotedName, '[') && str_ends_with($possiblyQuotedName, ']')
? substr($possiblyQuotedName, 1, -1)
: $possiblyQuotedName;
}

#[\Override]
protected function getCreateColumnCommentSQL($tableName, $columnName, $comment)
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName, 2);
[$schemaName, $tableName] = explode('.', $tableName);
} else {
$schemaName = 'dbo';
}

return $this->getAddExtendedPropertySQL(
'MS_Description',
(string) $comment,
$comment,
'SCHEMA',
$schemaName,
$this->quoteStringLiteral($this->unquoteSingleIdentifier($schemaName)),
'TABLE',
$tableName,
$this->quoteStringLiteral($this->unquoteSingleIdentifier($tableName)),
'COLUMN',
$columnName
$this->quoteStringLiteral($this->unquoteSingleIdentifier($columnName)),
);
}

#[\Override]
protected function getAlterColumnCommentSQL($tableName, $columnName, $comment)
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName, 2);
[$schemaName, $tableName] = explode('.', $tableName);
} else {
$schemaName = 'dbo';
}

return $this->getUpdateExtendedPropertySQL(
'MS_Description',
(string) $comment,
$comment,
'SCHEMA',
$schemaName,
$this->quoteStringLiteral($this->unquoteSingleIdentifier($schemaName)),
'TABLE',
$tableName,
$this->quoteStringLiteral($this->unquoteSingleIdentifier($tableName)),
'COLUMN',
$columnName
$this->quoteStringLiteral($this->unquoteSingleIdentifier($columnName)),
);
}

#[\Override]
protected function getDropColumnCommentSQL($tableName, $columnName)
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName, 2);
[$schemaName, $tableName] = explode('.', $tableName);
} else {
$schemaName = 'dbo';
}

return $this->getDropExtendedPropertySQL(
'MS_Description',
'SCHEMA',
$schemaName,
$this->quoteStringLiteral($this->unquoteSingleIdentifier($schemaName)),
'TABLE',
$tableName,
$this->quoteStringLiteral($this->unquoteSingleIdentifier($tableName)),
'COLUMN',
$columnName
);
}

private function quoteSingleIdentifierAsStringLiteral(string $levelName): string
{
return $this->quoteStringLiteral(preg_replace('~^\[|\]$~', '', $levelName));
}

#[\Override]
public function getAddExtendedPropertySQL(
$name,
$value = null,
$level0Type = null,
$level0Name = null,
$level1Type = null,
$level1Name = null,
$level2Type = null,
$level2Name = null
) {
return 'EXEC sp_addextendedproperty'
. ' N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral((string) $value)
. ', N' . $this->quoteStringLiteral((string) $level0Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level0Name)
. ', N' . $this->quoteStringLiteral((string) $level1Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level1Name)
. (
$level2Type !== null || $level2Name !== null
? ', N' . $this->quoteStringLiteral((string) $level2Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level2Name)
: ''
);
}

#[\Override]
public function getDropExtendedPropertySQL(
$name,
$level0Type = null,
$level0Name = null,
$level1Type = null,
$level1Name = null,
$level2Type = null,
$level2Name = null
) {
return 'EXEC sp_dropextendedproperty'
. ' N' . $this->quoteStringLiteral($name)
. ', N' . $this->quoteStringLiteral((string) $level0Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level0Name)
. ', N' . $this->quoteStringLiteral((string) $level1Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level1Name)
. (
$level2Type !== null || $level2Name !== null
? ', N' . $this->quoteStringLiteral((string) $level2Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level2Name)
: ''
);
}

#[\Override]
public function getUpdateExtendedPropertySQL(
$name,
$value = null,
$level0Type = null,
$level0Name = null,
$level1Type = null,
$level1Name = null,
$level2Type = null,
$level2Name = null
) {
return 'EXEC sp_updateextendedproperty'
. ' N' . $this->quoteStringLiteral($name) . ', N' . $this->quoteStringLiteral((string) $value)
. ', N' . $this->quoteStringLiteral((string) $level0Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level0Name)
. ', N' . $this->quoteStringLiteral((string) $level1Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level1Name)
. (
$level2Type !== null || $level2Name !== null
? ', N' . $this->quoteStringLiteral((string) $level2Type)
. ', ' . $this->quoteSingleIdentifierAsStringLiteral((string) $level2Name)
: ''
);
}

#[\Override]
protected function getCommentOnTableSQL(string $tableName, ?string $comment): string
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName, 2);
} else {
$schemaName = 'dbo';
}

return $this->getAddExtendedPropertySQL(
'MS_Description',
(string) $comment,
'SCHEMA',
$schemaName,
'TABLE',
$tableName
$this->quoteStringLiteral($this->unquoteSingleIdentifier($columnName)),
);
}
}
Loading