Developing a class that's passed an arbitrary SelectQuery in its constructor, I have a need to identify the DatabaseConnection for the query - but, there's no API to retrieve the connection. Easily solved...

CommentFileSizeAuthor
query-getConnection.patch709 bytesmikeryan

Comments

Crell’s picture

Can you be more specific about what you're doing and what the use case is? This sounds like there's some overlap with #802514: changeConnection from hook_query_alter.

mikeryan’s picture

Status: Needs review » Closed (duplicate)

This is in the context of working on #802264: Refactor map table support. I'd like to be able to automatically determine whether I can add a map table join to a source query, by comparing connections between the two (i.e., if they're on the same connection, or if both connections are MySQL and have matching host/credentials, they're good to go).

I don't have a need to change the connection, but the patch in #802514: changeConnection from hook_query_alter will suit me just fine. Setting this as a dupe of that.

Thanks.

mikeryan’s picture

Status: Closed (duplicate) » Needs review

Reopening, since #802514: changeConnection from hook_query_alter seems to be in limbo, and Crell has suggested an approach to that which does not address my issue here.

mikeryan’s picture

query-getConnection.patch queued for re-testing.

mikeryan’s picture

query-getConnection.patch queued for re-testing.

mikeryan’s picture

Bump... It still would be really helpful in migration scenarios (dealing with arbitrary queries, which might be on any connection) to be able to retrieve the connection.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Looks completely harmless. Was postponed in hopes of #802514: changeConnection from hook_query_alter but thats in limbo. This one is simpler.

webchick’s picture

Status: Reviewed & tested by the community » Needs review

This needs sign-off from Larry if we're going to do it. Is there really no workaround other than to change DBTNG? I get that this method would be handy, but I'm trying very hard to enforce a "no new features" rule in D7 since we're on the cusp of RC now. Need more information on how this is a bug and not merely a 'convenience' thing.

mikeryan’s picture

Is there another means to retrieve the connection for a query? I don't see how else to do it.

This is a clear (to my mind, anyway) hole in the API, that can be patched cleanly and safely.

@Crell, opinion?

Crell’s picture

Status: Needs review » Needs work

There is currently no way to determine the connection that a given query object will use. That is, sort of, by design as we tried very hard to avoid modules needing to care what DB they're connecting to for portability reasons. I don't fully understand mikeryan's use case here since if you're dealing with multiple connections you should have the connection objects on hand anyway via Database::getConnection(), but I am not adverse to adding such a getter method.

The interweaving issue here is *modifying* the connection object post-query-creation, which would, by extension, make it possible to determine the connection object. That is not possible either and I am less inclined to make it possible, since that could let a module change a query object to point to an incompatible connection.

Either way, the patch above is incomplete because all it does is add a method to SelectQueryExtender. That doesn't actually work. :-) Such a patch would need to add a method to SelectQueryInterface, implement it in SelectQuery, and then implement it as a pass-through (like all of the other pass-through methods) on SelectQueryExtender.

mikeryan’s picture

Version: 7.x-dev » 8.x-dev

Huh, I wonder how I managed to post a null-op patch? Could've sworn I had tested it... Unfortunately, I don't think I'll be able to reroll by Thanksgiving.

There are a couple of use cases in the Migrate module. First, the big picture - Migrate defines base classes to do the housekeeping for data migration - for a particular application, you would implement a module that extends those classes. When migrating from a dbtng-compatible database, the implementing module builds a query (usually against a non-default connection) and passes that query to Migrate, which executes it, manages the result sets, etc. Therefore, all the Migrate module knows about the query is the query object itself - it didn't build the query, so it doesn't know the connection.

So, why would the Migrate module want to know the connection? Two use cases:

1. #802264: Refactor map table support: Migrate does housekeeping in related map and message tables. In the general case, it needs to go through each row returned by the source query and check it against those tables to see if the row needs to be (re-)migrated. If the source query is on the same connection as the housekeeping tables (or if the connections are "compatible" - i.e., MySQL, same server, same username/password), the map and message tables can be joined directly into the source query, which is a major performance enhancment. It would be, yes, a convenience for the migrate module to automatically determine if this optimization is possible and apply it - but to do so, it needs the connection. In the absence of this, we've got a mapJoinable boolean on the migration object that can be set to TRUE by the implementor when they know the tables can be joined, so this isn't a huge issue.

2. #941440: Added column autodetect for sql source: Another convenience for the migration implementor would be for the migrate module to automatically identify all the fields in the query. This does work today when the implementor has explicitly added the fields to the query, by calling the query's getFields() method, but if all fields for a table are included (->fields('n')) getFields() returns nothing for that table. The alternative I came up with involves finding the actual database from the connection and querying information_schema. Now that I look back at that code, what I should've asked for was to have getFields() return all fields when there's no explicit list...

So, I'll back off on this for now... My anal side wants to see getConnection() for the sake of a clean and complete API, but I understand it's not a priority at this stage of the game. I'll pursue that getFields() enhancement instead...

ericduran’s picture

sub

q0rban’s picture

subscribe

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

voleger’s picture

Issue summary: View changes

Looks like this issue outdated. If I'm wrong, that definitely requires update IS.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.