From dd92dec2e9a60658fb44bd1050f9437265b8f910 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 22 Dec 2024 17:24:14 -0800 Subject: [PATCH 1/2] Introduce Name::toSQL() --- src/Schema/Name.php | 7 +++++++ src/Schema/Name/AbstractName.php | 17 +++++++++++++---- src/Schema/Name/Identifier.php | 12 ++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Schema/Name.php b/src/Schema/Name.php index 92e7721b61..d5169775b8 100644 --- a/src/Schema/Name.php +++ b/src/Schema/Name.php @@ -4,11 +4,18 @@ namespace Doctrine\DBAL\Schema; +use Doctrine\DBAL\Platforms\AbstractPlatform; + /** * Represents a database object name. */ interface Name { + /** + * Returns the SQL representation of the name for the given platform. + */ + public function toSQL(AbstractPlatform $platform): string; + /** * Returns the string representation of the name. * diff --git a/src/Schema/Name/AbstractName.php b/src/Schema/Name/AbstractName.php index 30c79d307c..95d3c56dbf 100644 --- a/src/Schema/Name/AbstractName.php +++ b/src/Schema/Name/AbstractName.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Schema\Name; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\Name; use function array_map; @@ -25,11 +26,19 @@ public function __construct(Identifier $firstIdentifier, Identifier ...$otherIde $this->identifiers = array_merge([$firstIdentifier], array_values($otherIdentifiers)); } + public function toSQL(AbstractPlatform $platform): string + { + return $this->joinIdentifiers(static fn (Identifier $identifier): string => $identifier->toSql($platform)); + } + public function toString(): string { - return implode('.', array_map( - static fn (Identifier $identifier): string => $identifier->toString(), - $this->identifiers, - )); + return $this->joinIdentifiers(static fn (Identifier $identifier): string => $identifier->toString()); + } + + /** @param callable(Identifier): string $mapper */ + private function joinIdentifiers(callable $mapper): string + { + return implode('.', array_map($mapper, $this->identifiers)); } } diff --git a/src/Schema/Name/Identifier.php b/src/Schema/Name/Identifier.php index 1ae4ad6778..aa591de4cb 100644 --- a/src/Schema/Name/Identifier.php +++ b/src/Schema/Name/Identifier.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Schema\Name; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\Exception\InvalidIdentifier; use function sprintf; @@ -34,6 +35,17 @@ public function isQuoted(): bool return $this->isQuoted; } + public function toSql(AbstractPlatform $platform): string + { + if (! $this->isQuoted) { + $value = $platform->normalizeUnquotedIdentifier($this->value); + } else { + $value = $this->value; + } + + return $platform->quoteSingleIdentifier($value); + } + public function toString(): string { if (! $this->isQuoted) { From cd8c7f0538ed92f82b856a7ccc74417d4eede2cd Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Thu, 26 Dec 2024 19:01:39 -0800 Subject: [PATCH 2/2] Deprecate AbstractAsset::getQuotedName() --- UPGRADE.md | 5 +++++ psalm.xml.dist | 6 ++++++ src/Schema/AbstractAsset.php | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/UPGRADE.md b/UPGRADE.md index b44a3a0c77..58391d5361 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,11 @@ awareness about deprecated code. # Upgrade to 4.3 +## Deprecated `AbstractAsset::getQuotedName()` + +The `AbstractAsset::getQuotedName()` method has been deprecated. Use `NamedObject::getObjectName()` or +`OptionallyQualifiedName::getObjectName()` followed by `Name::toSQL()` instead. + ## Deprecated `AbstractAsset` namespace-related methods and property The following namespace-related methods and property have been deprecated: diff --git a/psalm.xml.dist b/psalm.xml.dist index a9d9c0a170..a735ef8e32 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -141,6 +141,12 @@ --> + + + diff --git a/src/Schema/AbstractAsset.php b/src/Schema/AbstractAsset.php index 811ccb309f..eeb589bf1e 100644 --- a/src/Schema/AbstractAsset.php +++ b/src/Schema/AbstractAsset.php @@ -330,9 +330,19 @@ public function getName(): string /** * Gets the quoted representation of this asset but only if it was defined with one. Otherwise * return the plain unquoted value as inserted. + * + * @deprecated Use {@see NamedObject::getObjectName()} or {@see OptionallyQualifiedName::getObjectName()} followed + * by {@see Name::toSQL()} instead. */ public function getQuotedName(AbstractPlatform $platform): string { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/6674', + '%s is deprecated and will be removed in 5.0.', + __METHOD__, + ); + $keywords = $platform->getReservedKeywordsList(); $parts = $normalizedParts = [];