Allow retrieving the driver specific error code from DatabaseQueryExecutionException
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 31 Jul 2020 08:50:32 +0000 (10:50 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 31 Jul 2020 08:50:32 +0000 (10:50 +0200)
A single ANSI SQLSTATE can indicate several distinct error conditions. The
driver code appears to be unique for MySQL.

wcfsetup/install/files/lib/system/database/exception/DatabaseQueryExecutionException.class.php

index c04043f836c6f21018c9bc52ee627bc24a03ec63..16bf66151febbdc7f6bd134518c383778bbeade8 100644 (file)
@@ -19,6 +19,18 @@ class DatabaseQueryExecutionException extends DatabaseQueryException implements
         */
        protected $parameters = [];
        
+       /**
+        * @var string|null
+        * @since 5.3
+        */
+       protected $sqlState;
+       
+       /**
+        * @var string|null
+        * @since 5.3
+        */
+       protected $driverCode;
+       
        /** @noinspection PhpMissingParentConstructorInspection */
        /**
         * @inheritDoc
@@ -27,6 +39,31 @@ class DatabaseQueryExecutionException extends DatabaseQueryException implements
                parent::__construct($message, $previous);
                
                $this->parameters = $parameters;
+               if ($previous) {
+                       $errorInfo = $previous->errorInfo;
+                       $this->sqlState = $errorInfo[0] ?? null;
+                       $this->driverCode = $errorInfo[1] ?? null;
+               }
+       }
+       
+       /**
+        * Returns the ANSI SQLSTATE or null.
+        * 
+        * @return string|null
+        * @since 5.3
+        */
+       public function getSqlState() {
+               return $this->sqlState;
+       }
+
+       /**
+        * Returns the driver specific error code or null.
+        * 
+        * @return string|null
+        * @since 5.3
+        */
+       public function getDriverCode() {
+               return $this->driverCode;
        }
        
        /**