Commit 54655d3a authored by webchick's avatar webchick
Browse files

#977460 by dmitrig01, bfroehle, Josh The Geek: Fixed Cannot serialize or...

#977460 by dmitrig01, bfroehle, Josh The Geek: Fixed Cannot serialize or unserialize PDO instances error
parent de5b89a0
......@@ -193,6 +193,17 @@ abstract class DatabaseConnection extends PDO {
*/
protected $target = NULL;
/**
* The key representing this connection.
*
* The key is a unique string which identifies a database connection. A
* connection can be a single server or a cluster of master and slaves (use
* target to pick between master and slave).
*
* @var string
*/
protected $key = NULL;
/**
* The current database logging object for this connection.
*
......@@ -469,6 +480,28 @@ public function getTarget() {
return $this->target;
}
/**
* Tells this connection object what its key is.
*
* @param $target
* The key this connection is for.
*/
public function setKey($key) {
if (!isset($this->key)) {
$this->key = $key;
}
}
/**
* Returns the key this connection is associated with.
*
* @return
* The key of this connection.
*/
public function getKey() {
return $this->key;
}
/**
* Associates a logging object with this connection.
*
......@@ -1539,6 +1572,7 @@ final protected static function openConnection($key, $target) {
require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
$new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
$new_connection->setTarget($target);
$new_connection->setKey($key);
// If we have any active logging objects for this connection key, we need
// to associate them with the connection we just opened.
......
......@@ -241,6 +241,20 @@ abstract class Query implements QueryPlaceholderInterface {
*/
protected $connection;
/**
* The target of the connection object.
*
* @var string
*/
protected $connectionTarget;
/**
* The key of the connection object.
*
* @var string
*/
protected $connectionKey;
/**
* The query options to pass on to the connection object.
*
......@@ -270,9 +284,28 @@ abstract class Query implements QueryPlaceholderInterface {
*/
public function __construct(DatabaseConnection $connection, $options) {
$this->connection = $connection;
$this->connectionKey = $this->connection->getKey();
$this->connectionTarget = $this->connection->getTarget();
$this->queryOptions = $options;
}
/**
* Implements the magic __sleep function to disconnect from the database.
*/
public function __sleep() {
$keys = get_object_vars($this);
unset($keys['connection']);
return array_keys($keys);
}
/**
* Implements the magic __wakeup function to reconnect to the database.
*/
public function __wakeup() {
$this->connection = Database::getConnection($this->connectionTarget, $this->connectionKey);
}
/**
* Runs the query against the database.
*/
......
......@@ -2750,6 +2750,33 @@ class DatabaseLoggingTestCase extends DatabaseTestCase {
}
}
/**
* Query serialization tests.
*/
class DatabaseSerializeQueryTestCase extends DatabaseTestCase {
public static function getInfo() {
return array(
'name' => 'Serialize query',
'description' => 'Test serializing and unserializing a query.',
'group' => 'Database',
);
}
/**
* Confirm that a query can be serialized and unserialized.
*/
function testSerializeQuery() {
$query = db_select('test');
$query->addField('test', 'age');
$query->condition('name', 'Ringo');
// If this doesn't work, it will throw an exception, so no need for an
// assertion.
$query = unserialize(serialize($query));
$results = $query->execute()->fetchCol();
$this->assertEqual($results[0], 28, t('Query properly executed after unserialization.'));
}
}
/**
* Range query tests.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment