diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 66a4722..c170814 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -23,7 +23,7 @@ * * @see http://php.net/manual/book.pdo.php */ -abstract class Connection { +abstract class Connection implements \Serializable { /** * The database target this connection is for. @@ -1209,7 +1209,8 @@ public function commit() { * attribute values for the PDOStatement object that this method returns. * You would most commonly use this to set the \PDO::ATTR_CURSOR value to * \PDO::CURSOR_SCROLL to request a scrollable cursor. Some drivers have - * driver specific options that may be set at prepare-time. + * driver specific options that may be set at prepare-time. Defaults to an + * empty array. * * @return \PDOStatement|false * If the database server successfully prepares the statement, returns a @@ -1221,7 +1222,7 @@ public function commit() { * * @see \PDO::prepare() */ - public function prepare($statement, array $driver_options = NULL) { + public function prepare($statement, array $driver_options = array()) { return $this->connection->prepare($statement, $driver_options); } @@ -1232,7 +1233,7 @@ public function prepare($statement, array $driver_options = NULL) { * The string to be quoted. * @param int $parameter_type * (optional) Provides a data type hint for drivers that have alternate - * quoting styles. + * quoting styles. Defaults to \PDO::PARAM_STR. * * @return string|bool * A quoted string that is theoretically safe to pass into an SQL statement. @@ -1244,4 +1245,21 @@ public function quote($string, $parameter_type = \PDO::PARAM_STR) { return $this->connection->quote($string, $parameter_type); } + /** + * {@inheritdoc} + */ + public function serialize() { + return serialize(array($this->target, $this->key)); + } + + /** + * {@inheritdoc} + */ + public function unserialize($serialized) { + list($target, $key) = unserialize($serialized); + foreach (get_object_vars(Database::getConnection($target, $key)) as $name => $value) { + $this->{$name} = $value; + } + } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php index 8bdc45e..983a539 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionTest.php @@ -123,4 +123,16 @@ function testConnectionOptions() { $connectionOptions = $db->getConnectionOptions(); $this->assertNotEqual($connection_info['default']['database'], $connectionOptions['database'], 'The test connection info database does not match the current connection options database.'); } + + /** + * Tests the serialization and deserialization of a database connection. + */ + function testConnectionSerialization() { + $db = Database::getConnection('default', 'default'); + + $serialized = serialize($db); + $deserialized = unserialize($serialized); + + $this->assertIdenticalObject($db, $deserialized, 'Serializing and deserializing a connection returns an identical object.'); + } }