Introduce value objects representing database object names#6646
Merged
morozov merged 3 commits intodoctrine:4.3.xfrom Dec 13, 2024
Merged
Introduce value objects representing database object names#6646morozov merged 3 commits intodoctrine:4.3.xfrom
morozov merged 3 commits intodoctrine:4.3.xfrom
Conversation
5ab759d to
22ab33a
Compare
greg0ire
reviewed
Dec 13, 2024
| $this->identifiers = []; | ||
| try { | ||
| $this->setName($parsedName); | ||
| } catch (Throwable $e) { |
Member
There was a problem hiding this comment.
Should this be narrowed down to something more precise, like NotImplemented?
Member
Author
There was a problem hiding this comment.
It can also throw InvalidName. Since this is just an upgrade path, that doesn't add any new functionality, the idea is to prefer safety over purity. No matter what happens in setName(), I'd rather it to result in a false-positive deprecation message than an uncaught exception.
ec7a8f5 to
3349cdd
Compare
greg0ire
approved these changes
Dec 13, 2024
This was referenced Dec 16, 2024
This was referenced Aug 10, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a continuation of the effort to improve handling database object names. In this PR, in addition to object names expressed as strings, I'm introducing value objects that represent names.
Design Overview
Names
The interface for all names is
Name. Currently, it has a single methodtoString(): string. There are various implementations ofName:UnqualifiedName– to be used by the objects that exist within a table and thus cannot have a qualifier (e.g., column name).OptionallyQualifiedName– to be used by the objects that exist within a database and optionally may exist in a different schema than the current one (e.g., table name).GenericName– to be used for building ad-hoc SQL queries and eventually replace theIdentifierclass.Named Objects
All database objects (the subclasses of
AbstractAsset) will implement one of following interfaces:NamedObject<N extends Name>– the ones that have a name of typeN,OptionallyNamedObject<N extends Name>– similar but the name is optional (implemented by indexes and constraints).The interfaces conflict by design, since each class will have either a
Nameor an optional?Name, not both.Parsing Names
The
Parserclass introduced earlier has been broken down into aParser<N extends Name>interface and one implementation perNametype.GenericNameParserparses the string into a generic name which may contain an arbitrary number of identifiers, and other parsers ensure that the name contains exactly as many identifiers as supported by the name.Deprecations
Using the names that cannot be parsed is deprecated. Each database object will try to parse its name using the corresponding parser in
_setName(), and if that fails, a deprecation will trigger. In DBAL 5.0, all this logic will move to the class constructors (see #6614).As part of the implementation, the PR introduces two deprecated methods:
AbstractAsset::createNameParser()andAbstractAsset::setObjectName()– they will be removed in DBAL 5.0 but aren't marked as internal because someone may want to use them to migrate their own implementation ofAbstractAsset.The chances that there are implementations of
AbstractAssetoutside of DBAL are very unlikely. Furthermore, the class itself is used in method signatures literally in a couple of places (it's more like a trait). Most likely, it will be removed in DBAL 5.0. With that said, I'm marking it as@internalfor now.Technical Challenges
NamedObject::getObjectName()andOptionallyNamedObject::getObjectName()This method name sucks. A proper name would be
getName(), but it's already taken and will be eventually deprecated. Unlike changing the parameter type in a method, I don't know of an easy way to change the return type w/o introducing a new method.Next Steps
Name::toSQL(): stringas a replacement forAbstractAsset::getQuotedName(). I could have done it right now but it wouldn't be used anywhere and would be untested.Schema\Identifierin favor of the correspondingNameon a per-case basis – this is whereName::toSQL()will be used."id"andIDcould co-exist for Oracle and IBM DB2 (reference).