From 5e247487b6c139a397d32a5e6b0bf8c89ba4019f Mon Sep 17 00:00:00 2001
From: Martin Krisell <martin.krisell@gmail.com>
Date: Tue, 2 Jul 2019 08:54:19 +0200
Subject: [PATCH] Handle duplicate insertion also for SQL Server (#1134)

* Handle duplicate insertion also for SQL Server
* Improve 'update or insert' handling for different SQL drivers
---
 lib/SimpleSAML/Store/SQL.php | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/SimpleSAML/Store/SQL.php b/lib/SimpleSAML/Store/SQL.php
index 44599b9f9..a9223ddf9 100644
--- a/lib/SimpleSAML/Store/SQL.php
+++ b/lib/SimpleSAML/Store/SQL.php
@@ -242,13 +242,14 @@ class SQL extends Store
             $insertQuery->execute($data);
             return;
         } catch (PDOException $e) {
-            $ecode = (string) $e->getCode();
-            switch ($ecode) {
-                case '23505': // PostgreSQL
-                    break;
-                default:
-                    Logger::error('Error while saving data: '.$e->getMessage());
-                    throw $e;
+            $duplicateInsertErrorCodes = [
+                'pgsql' => '23505',
+                'sqlsrv' => '23000'
+            ];
+
+            if (!array_key_exists($this->driver, $duplicateInsertErrorCodes) || $duplicateInsertErrorCodes[$this->driver] !== (string) $e->getCode()) {
+                Logger::error('Error while saving data: '.$e->getMessage());
+                throw $e;
             }
         }
 
-- 
GitLab