Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 291718

Database::openConnection

$
0
0

This is half bug/half feature request.

I am creating a module for fine tuning master/slave database interaction. Basically writes and reads go through different connections as long as there is not a transaction happening. I know the pitfalls that ensue, we are carefully considering those in the process.

Now to the issue: In /includes/database/database.inc, class Database, the method Database::openConnection attempts to open a DatabaseConnection file in order to load said class and then instantiate it, Here's the current code (please bear with me):

...
$driver_class = 'DatabaseConnection_' . $driver;
require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
$new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
...

Regardless of my ultimate goal, I propose for us to change the code to this:

...
$driver_class = 'DatabaseConnection_' . $driver;

if (file_exists(DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc')) {
  require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
} else if (!class_exists($driver_class)) {
  /* issue warning, or error message and stop loading the page */
}

$new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
...

I have two reasons for doing this:

  1. I think it is better practice to stop loading the page instead of failing when the settings.php database configuration contains a reference to a db driver that is invalid (i.e. doesn't have a corresponding DatabaseConnection_{driver_name}.ini file in the /includes/database/ directory
  2. It allows for people who want to provide new database drivers to do so through a module, loading their sources in hook_boot and adding the connection info programmatically (through Database::addConnectionInfo(...) for example). It seems a bit of a waste that a class might be defined and not accessible in openConnection. My sample code is pretty simple and I hope it can serve as a starting point for this issue.

If we like this approach I can go and propose the patch for it, along with a sample module to test it and show how to exploit the possibilities brought by it.


Viewing all articles
Browse latest Browse all 291718

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>