Skip to content

Introduce value objects representing database object names#6646

Merged
morozov merged 3 commits intodoctrine:4.3.xfrom
morozov:object-names
Dec 13, 2024
Merged

Introduce value objects representing database object names#6646
morozov merged 3 commits intodoctrine:4.3.xfrom
morozov:object-names

Conversation

@morozov
Copy link
Copy Markdown
Member

@morozov morozov commented Dec 12, 2024

Q A
Type improvement

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 method toString(): string. There are various implementations of Name:

  1. UnqualifiedName – to be used by the objects that exist within a table and thus cannot have a qualifier (e.g., column name).
  2. 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).
  3. GenericName – to be used for building ad-hoc SQL queries and eventually replace the Identifier class.

Named Objects

All database objects (the subclasses of AbstractAsset) will implement one of following interfaces:

  1. NamedObject<N extends Name> – the ones that have a name of type N,
  2. 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 Name or an optional ?Name, not both.

Parsing Names

The Parser class introduced earlier has been broken down into a Parser<N extends Name> interface and one implementation per Name type. GenericNameParser parses 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() and AbstractAsset::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 of AbstractAsset.

The chances that there are implementations of AbstractAsset outside 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 @internal for now.

Technical Challenges

NamedObject::getObjectName() and OptionallyNamedObject::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

  1. Add Name::toSQL(): string as a replacement for AbstractAsset::getQuotedName(). I could have done it right now but it wouldn't be used anywhere and would be untested.
  2. Try to get rid of Schema\Identifier in favor of the corresponding Name on a per-case basis – this is where Name::toSQL() will be used.
  3. Instead of using lowercased old names as object keys (e.g. for columns in the table) use the new keys and apply the normalization logic of the target platform. Thus, "id" and ID could co-exist for Oracle and IBM DB2 (reference).

@morozov morozov marked this pull request as ready for review December 12, 2024 03:22
@morozov morozov requested review from derrabus and greg0ire December 12, 2024 03:22
@morozov morozov added this to the 4.3.0 milestone Dec 12, 2024
$this->identifiers = [];
try {
$this->setName($parsedName);
} catch (Throwable $e) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be narrowed down to something more precise, like NotImplemented?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@morozov morozov merged commit 3b9bc11 into doctrine:4.3.x Dec 13, 2024
@morozov morozov deleted the object-names branch December 13, 2024 22:15
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants