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

db_select, db_insert, db_delete, db_update broken on non-default databases with patch 7.78

$
0
0

Problem/Motivation

Hello, we are using db_select to join data from our default drupal database with data from a secondary user database.
With Drupal 7.78 they overwritten the default setPrefix function in src/includes/database/mysql/database.inc:line385, which apparently adds some better escaping support for mysql. But it breaks all support for addressing another database on the same server.

Example:

before 7.78

SELECT g.pgid AS pgid
FROM 
{persondb.persons} p
INNER JOIN {persondb.person_groups} g ON p.pid = g.person
WHERE  (g.`group` = :db_condition_placeholder_0) 

after 7.78

SELECT g.pgid AS pgid
FROM 
`persondb.persons` p
INNER JOIN `persondb.person_groups` g ON p.pid = g.person
WHERE  (g.`group` = :db_condition_placeholder_0) 

While the statement before 7.78 gets processed into a working query the statement after 7.78 fails since `persondb.persons`can't be adressed (proper sql would be persondb.`persons` i suppose).

I've managed to fix a few issues with db_set_active('yourdatabasename'), but the problem goes deeper.
While this fix works with a few db_select, db_insert, db_delete cases i've had, there is no solution when it comes to queries joining data from the default drupal database with another database on my server.

Or to put it simpler, its no longer possible to work with multiple databases when it comes to db_select, db_insert and db_delete.

I can't use db_query (which is unaffected of this problem) since our db_select query is processed dynamically and is sent through different methods / classes which process / add statements to it.

Steps to reproduce

You can reproduce the problem easily writing a query adressing a non-default database (if you have one) like this one:

$select_query = db_select('persondb.person_groups', 'pg')
					->fields('pg', array('pgid'))
					->condition('person', $pid,'=')
					->execute();

For the code above a temporary fix, a user can already use, can be:

db_set_active('persondb');
$select_query = db_select('person_groups', 'pg')
			->fields('pg', array('pgid'))
			->condition('person', $pid,'=')
			->execute();
db_set_active();

But more complex queries where u join queries fail no matter what:

$query = .. some original db_select query on a default db
$inner = db_select('persondb.contacts', 'c');
$inner->join('persondb.addresses', 'a', 'a.contact  = c.cid');
....
$query->leftJoin($inner, "field_$this->name", "field_$this->name.person = elem.id");

Proposed resolution

The SQL statment processing needs to be able to detect database names before the table name and add / shift the escaping propperly:
`persondb.persons` -> persondb.`persons`


Viewing all articles
Browse latest Browse all 292190

Trending Articles



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