merged branch vicb/session_pdo_cleanup (PR #4246)

Commits
-------

51b753a [Session] cleanup of the PDO storage

Discussion
----------

[Session] cleanup of the PDO storage

and adding what was missing from #4244

---------------------------------------------------------------------------

by travisbot at 2012-05-10T10:40:33Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1293901) (merged 51b753a6 into 62594291).
This commit is contained in:
Fabien Potencier 2012-05-10 13:54:53 +02:00
commit fae4523f3b

View File

@ -20,23 +20,24 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
class PdoSessionHandler implements \SessionHandlerInterface
{
/**
* PDO instance.
*
* @var \PDO
* @var \PDO PDO instance.
*/
private $pdo;
/**
* Database options.
*
*
* @var array
* @var array Database options.
*/
private $dbOptions;
/**
* Constructor.
*
* List of available options:
* * db_table: The name of the table [required]
* * db_id_col: The column where to store the session id [default: sess_id]
* * db_data_col: The column where to store the session data [default: sess_data]
* * db_time_col: The column where to store the timestamp [default: sess_time]
*
* @param \PDO $pdo A \PDO instance
* @param array $dbOptions An associative array of DB options
*
@ -166,20 +167,20 @@ class PdoSessionHandler implements \SessionHandlerInterface
//session data can contain non binary safe characters so we need to encode it
$encoded = base64_encode($data);
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
// MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
// it could result in calling createNewSession() whereas the session already exists in
// the DB which would fail as the id is unique
$stmt = $this->pdo->prepare(
"INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time) " .
"ON DUPLICATE KEY UPDATE $dbDataCol = VALUES($dbDataCol), $dbTimeCol = VALUES($dbTimeCol)"
);
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
$stmt->execute();
} else {
try {
try {
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
// MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
// it could result in calling createNewSession() whereas the session already exists in
// the DB which would fail as the id is unique
$stmt = $this->pdo->prepare(
"INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time) " .
"ON DUPLICATE KEY UPDATE $dbDataCol = VALUES($dbDataCol), $dbTimeCol = VALUES($dbTimeCol)"
);
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
$stmt->execute();
} else {
$stmt = $this->pdo->prepare("UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id");
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
@ -191,9 +192,9 @@ class PdoSessionHandler implements \SessionHandlerInterface
// session_regenerate_id()
$this->createNewSession($id, $data);
}
} catch (\PDOException $e) {
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
}
} catch (\PDOException $e) {
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
}
return true;