[Session] cleanup of the PDO storage

This commit is contained in:
Victor Berchet 2012-05-10 12:33:43 +02:00
parent 62594291e4
commit 51b753a6b8

View File

@ -20,23 +20,24 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
class PdoSessionHandler implements \SessionHandlerInterface class PdoSessionHandler implements \SessionHandlerInterface
{ {
/** /**
* PDO instance. * @var \PDO PDO instance.
*
* @var \PDO
*/ */
private $pdo; private $pdo;
/** /**
* Database options. * @var array Database options.
*
*
* @var array
*/ */
private $dbOptions; private $dbOptions;
/** /**
* Constructor. * 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 \PDO $pdo A \PDO instance
* @param array $dbOptions An associative array of DB options * @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 //session data can contain non binary safe characters so we need to encode it
$encoded = base64_encode($data); $encoded = base64_encode($data);
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) { try {
// MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
// it could result in calling createNewSession() whereas the session already exists in // MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
// the DB which would fail as the id is unique // it could result in calling createNewSession() whereas the session already exists in
$stmt = $this->pdo->prepare( // the DB which would fail as the id is unique
"INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time) " . $stmt = $this->pdo->prepare(
"ON DUPLICATE KEY UPDATE $dbDataCol = VALUES($dbDataCol), $dbTimeCol = VALUES($dbTimeCol)" "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->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindValue(':time', time(), \PDO::PARAM_INT); $stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
$stmt->execute(); $stmt->bindValue(':time', time(), \PDO::PARAM_INT);
} else { $stmt->execute();
try { } else {
$stmt = $this->pdo->prepare("UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id"); $stmt = $this->pdo->prepare("UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id");
$stmt->bindParam(':id', $id, \PDO::PARAM_STR); $stmt->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR); $stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
@ -191,9 +192,9 @@ class PdoSessionHandler implements \SessionHandlerInterface
// session_regenerate_id() // session_regenerate_id()
$this->createNewSession($id, $data); $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; return true;