Problem/Motivation

In Drupal\Core\Database\Query\Select constructor, $alias parameter has a null default value but next parameter Connection $connection is required.

public function __construct($table, $alias = NULL, Connection $connection, $options = array()) {
    // ...
  }

As $connection is required, $alias is always setted, even with NULL.

Proposed resolution

There is two ways to fix this:

  1. Remove the default value
  2. Switch $alias and $connection.
  3. Make the connection parameter the first parameter

2nd force to change all database drivers. As $connection is required, default value can safely be removed.

So proposed solution is 1/.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

GoZ created an issue. See original summary.

GoZ’s picture

Status: Active » Needs review
FileSize
1.15 KB
GoZ’s picture

Issue tags: +Quick fix, +Quickfix
daffie’s picture

Priority: Normal » Minor
Status: Needs review » Reviewed & tested by the community

We can remove the default value for the parameter $alias, because he parameter $alias must always be set. That is so because the third parameter must also be set. This is an API-change, but one that does not require any changes to D8 core or D8 contrib.
The other proposed solution is one we cannot do because it is an api change, that will require changes to D8 core and D8 contrib.

daffie’s picture

Version: 8.3.x-dev » 9.x-dev
Status: Reviewed & tested by the community » Active
Issue tags: +API change

I thought about it so more and the best solution is to make the \Drupal\Core\Database\Connection $connection parameter the first parameter. This would be an API change and we can only do that in a new major version. So moving this to D9.

@GoZ: If you do not agree, please speak up.

daffie’s picture

Title: Select Query Builder use alias default value as parameter before non-optional parameter » Make the Connection parameter the first parameter in Select Query Builder.
Issue summary: View changes

Updating title and summary.

catch’s picture

Version: 9.x-dev » 8.3.x-dev

Constructors are considered @internal, and none of the core database drivers override __construct, so this could potentially be done in a minor release.

GoZ’s picture

Status: Active » Needs review
daffie’s picture

@GoZ: The problem that I have with your solution is that you will always have to set the value for $alias. Even when $alias has no value. The other thing is that constructors from classes in the namespace Drupal\Core\Database that need the connection parameter have it as their first parameter. Not always, I am sad to say. So for me the best solution would be number #3 and make the connection the first parameter.

GoZ’s picture

@daffie

The problem that I have with your solution is that you will always have to set the value for $alias. Even when $alias has no value.

You are right, and it's already the case since the following $connection argument is required.

The other thing is that constructors from classes in the namespace Drupal\Core\Database that need the connection parameter have it as their first parameter. Not always, I am sad to say. So for me the best solution would be number #3 and make the connection the first parameter.

There is no big issue to make #3, for core drivers. But in case custom drivers are made by people in projects, this will break. So can we assume in 8.3.x that a change can break existing project with custom drivers ?

daffie’s picture

@catch was very clear in comment #7:

Constructors are considered @internal, and none of the core database drivers override __construct, so this could potentially be done in a minor release.

So I do not see any problems for #3. We have the signoff from a Drupal core maintainer/committer.

GoZ’s picture

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

alexpott’s picture

Version: 8.3.x-dev » 9.x-dev

Discussed with @catch we agreed that the disruption vs benefit of this means we should wait to Drupal 9 for this. Code quality is not a good enough reason to break people's code in a minor release.

Crell’s picture

This makes Select consistent with the other query builder objects, so I'm fine with it. Thanks.

Version: 9.x-dev » 9.0.x-dev

The 9.0.x branch will open for development soon, and the placeholder 9.x branch should no longer be used. Only issues that require a new major version should be filed against 9.0.x (for example, removing deprecated code or updating dependency major versions). New developments and disruptive changes that are allowed in a minor version should be filed against 8.9.x, and significant new features will be moved to 9.1.x at committer discretion. For more information see the Allowed changes during the Drupal 8 and 9 release cycles and the Drupal 9.0.0 release plan.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

No longer applies.

sokru’s picture

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC.

catch’s picture

Issue tags: +Needs change record

This needs a change record just in case it does affect a contrib driver somewhere.

daffie’s picture

Issue tags: -Needs change record

Added a change record.

alexpott’s picture

Note neither sqlsrv nor oracle will have to make a change - see:

I couldn't find any code that will have to change using that search.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Quick fix, -Quickfix

Committed 258f88a and pushed to 9.0.x. Thanks!

  • alexpott committed 258f88a on 9.0.x
    Issue #2799911 by GoZ, sokru, daffie, catch: Make the Connection...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.