Added original sql query to database exception output
authorMarcel Werk <burntime@woltlab.com>
Sun, 23 Oct 2011 21:52:25 +0000 (23:52 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sun, 23 Oct 2011 21:52:25 +0000 (23:52 +0200)
wcfsetup/install/files/lib/system/database/Database.class.php
wcfsetup/install/files/lib/system/database/DatabaseException.class.php
wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php

index b99be3abe16b921e48e61f37db2bb7b0362e9bb8..e5fc0982174074d78a026d293f34e81bd9affcf3 100644 (file)
@@ -186,7 +186,7 @@ abstract class Database {
                try {
                        $pdoStatement = $this->pdo->prepare($statement);
                        if ($pdoStatement instanceof \PDOStatement) {
-                               return new $this->preparedStatementClassName($this, $pdoStatement);
+                               return new $this->preparedStatementClassName($this, $pdoStatement, $statement);
                        }
                        throw new DatabaseException("Can not prepare statement: ".$statement, $this);
                }
index aac52273749b930e0fb558293fec1bb48e2fa17d..9cf8bd604e4b17cdb21e2918f3ae1a0a36ffc8d2 100644 (file)
@@ -64,7 +64,7 @@ class DatabaseException extends SystemException {
                $this->preparedStatement = $preparedStatement;
                
                // prefer errors from prepared statement
-               if (($this->preparedStatement instanceof PreparedStatement) && $this->preparedStatement->getErrorNumber()) {
+               if ($this->preparedStatement !== null && $this->preparedStatement->getErrorNumber()) {
                        $this->errorNumber = $this->preparedStatement->getErrorNumber();
                        $this->errorDesc = $this->preparedStatement->getErrorDesc();
                }
@@ -129,6 +129,7 @@ class DatabaseException extends SystemException {
                $this->information .= '<b>sql error:</b> ' . StringUtil::encodeHTML($this->getErrorDesc()) . '<br />';
                $this->information .= '<b>sql error number:</b> ' . StringUtil::encodeHTML($this->getErrorNumber()) . '<br />';
                $this->information .= '<b>sql version:</b> ' . StringUtil::encodeHTML($this->getSQLVersion()) . '<br />';
+               if ($this->preparedStatement !== null) $this->information .= '<b>sql query:</b> ' . StringUtil::encodeHTML($this->preparedStatement->getSQLQuery()) . '<br />';
                
                $this->information .= "\n<!-- db error: #".$this->db->getErrorNumber().': '.$this->db->getErrorDesc()." -->\n";
                if ($this->preparedStatement !== null) {
index 432b18fa3da4402412403798274ae02aa776f38b..4a18ffa8d055d22ccc4ea8ea4c7771ed58f140a5 100644 (file)
@@ -29,15 +29,23 @@ class PreparedStatement {
         */
        protected $pdoStatement = null;
        
+       /**
+        * SQL query
+        * @var string
+        */
+       protected $query = '';
+       
        /**
         * Creates a new PreparedStatement object.
         *
         * @param       wcf\system\database\Database    $database
         * @param       \PDOStatement                   $pdoStatement
+        * @param       string                          $query          SQL query
         */
-       public function __construct(Database $database, \PDOStatement $pdoStatement) {
+       public function __construct(Database $database, \PDOStatement $pdoStatement, $query = '') {
                $this->database = $database;
                $this->pdoStatement = $pdoStatement;
+               $this->query = $query;
        }
        
        /**
@@ -157,4 +165,13 @@ class PreparedStatement {
                }
                return '';
        }
+       
+       /**
+        * Returns the SQL query of this statement.
+        * 
+        * @return string
+        */
+       public function getSQLQuery() {
+               return $this->query;
+       }
 }