@@ -180,7 +180,7 @@ public function write($sessionId, $data)
180
180
$ updateStmt ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
181
181
$ updateStmt ->execute ();
182
182
183
- // When MERGE is not supported, like in Postgres, we have to use this approach that can result in
183
+ // When MERGE is not supported, like in Postgres < 9.5 , we have to use this approach that can result in
184
184
// duplicate key errors when the same session is written simultaneously. We can just catch such an
185
185
// error and re-execute the update. This is similar to a serializable transaction with retry logic
186
186
// on serialization failures but without the overhead and without possible false positives due to
@@ -224,11 +224,11 @@ private function getMergeSql()
224
224
{
225
225
$ platform = $ this ->con ->getDatabasePlatform ()->getName ();
226
226
227
- switch ($ platform ) {
228
- case 'mysql ' :
227
+ switch (true ) {
228
+ case 'mysql ' === $ platform :
229
229
return "INSERT INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " .
230
230
"ON DUPLICATE KEY UPDATE $ this ->dataCol = VALUES( $ this ->dataCol ), $ this ->timeCol = VALUES( $ this ->timeCol ) " ;
231
- case 'oracle ' :
231
+ case 'oracle ' === $ platform :
232
232
// DUAL is Oracle specific dummy table
233
233
return "MERGE INTO $ this ->table USING DUAL ON ( $ this ->idCol = :id) " .
234
234
"WHEN NOT MATCHED THEN INSERT ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " .
@@ -239,8 +239,11 @@ private function getMergeSql()
239
239
return "MERGE INTO $ this ->table WITH (HOLDLOCK) USING (SELECT 1 AS dummy) AS src ON ( $ this ->idCol = :id) " .
240
240
"WHEN NOT MATCHED THEN INSERT ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " .
241
241
"WHEN MATCHED THEN UPDATE SET $ this ->dataCol = :data, $ this ->timeCol = :time; " ;
242
- case 'sqlite ' :
242
+ case 'sqlite ' === $ platform :
243
243
return "INSERT OR REPLACE INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " ;
244
+ case 'postgresql ' === $ platform && version_compare ($ this ->con ->getServerVersion (), '9.5 ' , '>= ' ):
245
+ return "INSERT INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " .
246
+ "ON CONFLICT ( $ this ->idCol ) DO UPDATE SET ( $ this ->dataCol , $ this ->timeCol ) = (:data, :time) WHERE $ this ->idCol = :id " ;
244
247
}
245
248
}
246
249
}
0 commit comments