Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 534 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple concurrent database connections in drupal 7

#1
I'm writing a wrapper class for my drupal 7 site which lets me connect to and query my phpbb database.

When connecting to an external data source (as per drupal documentation) you have set the active db, run the query, then set the active db back to the default.

e.g.

db_set_active('phpbb');
$result = db_query($sql,$args,$opts);
db_set_active();//back to default

But is there any way to use drupal's database wrapper to create a brand new connection which can be permanently set to the new database without having to do this switching back-and-forth nonsense? surely we can handle connections to multiple databases concurrently.

I have done some googling but haven't found anybody trying to do this as yet.
Reply

#2
Typical. 5 minutes after posting i figure it out... so, for future googlers:

Basically, you don't use db_query, instead you run the query on your connection without setting the active link.

you can figure this out by looking at how db_query works:

[To see links please register here]


So it looks like this:

$target='default';
$key = 'phpbb';
$phpbb = Database::getConnection($target,$key);
$result = $phpbb->query($sql,$args,$opts);

This assumes you have a database configured in your settings.php like the following:



$databases['phpbb']['default'] = array(
'driver' => 'mysql',
'database' => 'forum',
'username' => 'username',
'password' => 'password',
'host' => 'mysql.host.com',
'prefix' => 'phpbb3_'
);
Reply

#3
[Database::addConnectionInfo()][1] perhaps?

> This method allows the addition of new connection credentials at
> runtime. Under normal circumstances the preferred way to specify
> database credentials is via settings.php. However, this method allows
> them to be added at arbitrary times, such as during unit tests, when
> connecting to admin-defined third party databases, etc.
>
> If the given key/target pair already exists, this method will be
> ignored.


[1]:

[To see links please register here]

Reply

#4
The definition for getConnection cites a different order for arguments than used above.

function getConnection($target = 'default', $key = NULL)


This is sadly different from Database::addConnectionInfo() which is

public static function addConnectionInfo($key, $target, $info)

Also, on DB_select, the $key is not a parameter, though it is in the options array:

function db_select($table, $alias = NULL, array $options = array()) {
if (empty($options['target'])) {
$options['target'] = 'default';
}
return Database::getConnection($options['target'])->select($table, $alias, $options);
}

while

final public static function getConnection($target = 'default', $key = NULL) {

so this implies that the 'master' or 'slave' or 'default' is always used as set, but not the key to the alternative database/schema, requiring the db_set_active('...'); and db_set_active(); around the db_select.

Since calls to other dbs can easily be required within the processing of the db_select (such as devel calls or calls in alters), this is inflexible design. Changing this call:

return Database::getConnection($options['target'])->select($table, $alias, $options);

to add the Key parameter (it is already spec'd as an argument!!) is needed but insufficient so far as I can now see.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through