3
3
namespace Symfony \Bridge \Doctrine \Security \SessionRegistry ;
4
4
5
5
use Doctrine \DBAL \Connection ;
6
+ use Doctrine \DBAL \DBALException ;
6
7
use Symfony \Component \Security \Http \Session \SessionInformation ;
7
8
use Symfony \Component \Security \Http \Session \SessionRegistryStorageInterface ;
8
9
@@ -66,6 +67,8 @@ public function getSessionInformations($username, $includeExpiredSessions = fals
66
67
$ sessionInformations [] = $ this ->instantiateSessionInformationFromResultSet ($ data );
67
68
}
68
69
70
+ $ statement ->closeCursor ();
71
+
69
72
return $ sessionInformations ;
70
73
}
71
74
@@ -79,23 +82,30 @@ public function setSessionInformation(SessionInformation $sessionInformation)
79
82
$ mergeSql = $ this ->getMergeSql ();
80
83
81
84
if (null !== $ mergeSql ) {
82
- $ mergeStmt = $ this ->pdo ->prepare ($ mergeSql );
83
- $ mergeStmt ->bindValue ('session_id ' , $ sessionInformation ->getSessionId ());
84
- $ mergeStmt ->bindValue ('username ' , $ sessionInformation ->getUsername ());
85
- $ mergeStmt ->bindValue ('last_request ' , $ sessionInformation ->getLastRequest (), 'datetime ' );
86
- $ mergeStmt ->bindValue ('expired ' , $ sessionInformation ->getExpired (), 'datetime ' );
87
- $ mergeStmt ->execute ();
85
+ $ this ->connection ->executeQuery (
86
+ $ mergeSql ,
87
+ array (
88
+ 'session_id ' => $ sessionInformation ->getSessionId (),
89
+ 'username ' => $ sessionInformation ->getUsername (),
90
+ 'last_request ' => $ sessionInformation ->getLastRequest (),
91
+ 'expired ' => $ sessionInformation ->getExpired ()
92
+ ),
93
+ array (
94
+ 'last_request ' => 'datetime ' ,
95
+ 'expired ' => 'datetime '
96
+ )
97
+ );
88
98
89
99
return true ;
90
100
}
91
101
92
- $ updateStmt = $ this ->pdo ->prepare (
102
+ $ updateStmt = $ this ->connection ->prepare (
93
103
"UPDATE $ this ->table SET username=:username, last_request=:last_request, expired=:expired WHERE session_id = :session_id "
94
104
);
95
- $ mergeStmt ->bindValue ('session_id ' , $ sessionInformation ->getSessionId ());
96
- $ mergeStmt ->bindValue ('username ' , $ sessionInformation ->getUsername ());
97
- $ mergeStmt ->bindValue ('last_request ' , $ sessionInformation ->getLastRequest (), 'datetime ' );
98
- $ mergeStmt ->bindValue ('expired ' , $ sessionInformation ->getExpired (), 'datetime ' );
105
+ $ updateStmt ->bindValue ('session_id ' , $ sessionInformation ->getSessionId ());
106
+ $ updateStmt ->bindValue ('username ' , $ sessionInformation ->getUsername ());
107
+ $ updateStmt ->bindValue ('last_request ' , $ sessionInformation ->getLastRequest (), 'datetime ' );
108
+ $ updateStmt ->bindValue ('expired ' , $ sessionInformation ->getExpired (), 'datetime ' );
99
109
$ updateStmt ->execute ();
100
110
101
111
// When MERGE is not supported, like in Postgres, we have to use this approach that can result in
@@ -105,17 +115,22 @@ public function setSessionInformation(SessionInformation $sessionInformation)
105
115
// longer gap locking.
106
116
if (!$ updateStmt ->rowCount ()) {
107
117
try {
108
- $ insertStmt = $ this ->pdo ->prepare (
109
- "INTO $ this ->table (session_id, username, last_request, expired) VALUES (:session_id, :username, :last_request, :expired) "
118
+ $ this ->connection ->insert (
119
+ $ this ->table ,
120
+ array (
121
+ 'session_id ' => $ sessionInformation ->getSessionId (),
122
+ 'username ' => $ sessionInformation ->getUsername (),
123
+ 'last_request ' => $ sessionInformation ->getLastRequest (),
124
+ 'expired ' => $ sessionInformation ->getExpired ()
125
+ ),
126
+ array (
127
+ 'last_request ' => 'datetime ' ,
128
+ 'expired ' => 'datetime '
129
+ )
110
130
);
111
- $ insertStmt ->bindValue ('session_id ' , $ sessionInformation ->getSessionId ());
112
- $ insertStmt ->bindValue ('username ' , $ sessionInformation ->getUsername ());
113
- $ insertStmt ->bindValue ('last_request ' , $ sessionInformation ->getLastRequest (), 'datetime ' );
114
- $ insertStmt ->bindValue ('expired ' , $ sessionInformation ->getExpired (), 'datetime ' );
115
- $ insertStmt ->execute ();
116
- } catch (\PDOException $ e ) {
131
+ } catch (DBALException $ e ) {
117
132
// Handle integrity violation SQLSTATE 23000 (or a subclass like 23505 in Postgres) for duplicate keys
118
- if (0 === strpos ($ e ->getCode (), '23 ' )) {
133
+ if ($ e -> getPrevious () instanceof \PDOException && 0 === strpos ($ e-> getPrevious () ->getCode (), '23 ' )) {
119
134
$ updateStmt ->execute ();
120
135
} else {
121
136
throw $ e ;
@@ -151,24 +166,24 @@ private function instantiateSessionInformationFromResultSet($data)
151
166
*/
152
167
private function getMergeSql ()
153
168
{
154
- switch ($ this ->connection ->getDriver ()->getName ()) {
155
- case 'pdo_mysql ' :
169
+ switch ($ this ->connection ->getDatabasePlatform ()->getName ()) {
170
+ case 'mysql ' :
156
171
return "INSERT INTO $ this ->table (session_id, username, last_request, expired) VALUES (:session_id, :username, :last_request, :expired) " .
157
172
"ON DUPLICATE KEY UPDATE username = VALUES(username), last_request = VALUES(last_request), expired = VALUES(expired) " ;
158
- case 'pdo_oracle ' :
173
+ case 'oracle ' :
159
174
// DUAL is Oracle specific dummy table
160
175
return "MERGE INTO $ this ->table USING DUAL ON (session_id= :session_id) " .
161
176
"WHEN NOT MATCHED THEN INSERT (session_id, username, last_request, expired) VALUES (:session_id, :username, :last_request, :expired) " .
162
177
"WHEN MATCHED THEN UPDATE SET username = :username, last_request = :last_request, expired = :expired " ;
163
- case 'pdo_sqlsrv ' :
164
- if (version_compare ( $ this ->connection ->getWrappedConnection ()-> getAttribute (\ PDO :: ATTR_SERVER_VERSION ), ' 10 ' , ' >= ' ) ) {
178
+ case 'mssql ' :
179
+ if ($ this ->connection ->getDatabasePlatform () instanceof \ Doctrine \ DBAL \ Platforms \SQLServer2008Platform || $ this -> connection -> getDatabasePlatform () instanceof \ Doctrine \ DBAL \ Platforms \SQLServer2012Platform ) {
165
180
// MERGE is only available since SQL Server 2008 and must be terminated by semicolon
166
181
// It also requires HOLDLOCK according to http://weblogs.sqlteam.com/dang/archive/2009/01/31/UPSERT-Race-Condition-With-MERGE.aspx
167
182
return "MERGE INTO $ this ->table WITH (HOLDLOCK) USING (SELECT 1 AS dummy) AS src ON (session_id = :session_id) " .
168
183
"WHEN NOT MATCHED THEN INSERT (session_id, username, last_request, expired) VALUES (:session_id, :username, :last_request, :expired) " .
169
184
"WHEN MATCHED THEN UPDATE SET username = :username, last_request = :last_request, expired = :expired; " ;
170
185
}
171
- case 'pdo_sqlite ' :
186
+ case 'sqlite ' :
172
187
return "INSERT OR REPLACE INTO $ this ->table (session_id, username, last_request, expired) VALUES (:session_id, :username, :last_request, :expired) " ;
173
188
}
174
189
}
0 commit comments