return $this->pdo->lastInsertId();
}
catch (\PDOException $e) {
- throw new DatabaseException("Cannot fetch last insert id", $this);
+ throw new DatabaseException("Cannot fetch last insert id: " . $e->getMessage(), $this);
}
}
return $result;
}
catch (\PDOException $e) {
- throw new DatabaseException("Cannot begin transaction", $this);
+ throw new DatabaseException("Cannot begin transaction: " . $e->getMessage(), $this);
}
}
return $result;
}
catch (\PDOException $e) {
- throw new DatabaseException("Cannot commit transaction", $this);
+ throw new DatabaseException("Cannot commit transaction: " . $e->getMessage(), $this);
}
}
return $result;
}
catch (\PDOException $e) {
- throw new DatabaseException("Cannot rollback transaction", $this);
+ throw new DatabaseException("Cannot rollback transaction: " . $e->getMessage(), $this);
}
}
try {
$pdoStatement = $this->pdo->prepare($statement);
- if ($pdoStatement instanceof \PDOStatement) {
- return new $this->preparedStatementClassName($this, $pdoStatement, $statement);
- }
- throw new DatabaseException("Cannot prepare statement: ".$statement, $this);
+
+ return new $this->preparedStatementClassName($this, $pdoStatement, $statement);
}
catch (\PDOException $e) {
- throw new DatabaseException("Cannot prepare statement: ".$statement, $this);
+ throw new DatabaseException($e->getMessage(), $this, null, $statement);
}
}
*/
protected $preparedStatement = null;
+ /**
+ * SQL query if prepare() failed
+ * @var string
+ */
+ protected $sqlQuery = null;
+
/**
* Creates a new DatabaseException.
*
* @param string $message error message
* @param \wcf\system\database\Database $db affected db object
- * @param \wcf\system\database\statement\PreparedStatement $preparedStatement affected prepared statement
+ * @param \wcf\system\database\statement\PreparedStatement $preparedStatement affected prepared statement
+ * @param string $sqlQuery SQL query if prepare() failed
*/
- public function __construct($message, Database $db, PreparedStatement $preparedStatement = null) {
+ public function __construct($message, Database $db, PreparedStatement $preparedStatement = null, $sqlQuery = null) {
$this->db = $db;
$this->DBType = $db->getDBType();
$this->preparedStatement = $preparedStatement;
+ $this->sqlQuery = $sqlQuery;
// prefer errors from prepared statement
if ($this->preparedStatement !== null && $this->preparedStatement->getErrorNumber()) {
}
}
}
+ else if ($this->sqlQuery !== null) {
+ $this->information .= '<b>sql query:</b> ' . StringUtil::encodeHTML($this->sqlQuery) . '<br />';
+ }
parent::show();
}
// disable prepared statement emulation since MySQL 5.1.17 is the minimum required version
$driverOptions[\PDO::ATTR_EMULATE_PREPARES] = false;
+ // throw PDOException instead of dumb false return values
+ $driverOptions[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
+
$this->pdo = new \PDO('mysql:host='.$this->host.';port='.$this->port.';dbname='.$this->database, $this->user, $this->password, $driverOptions);
$this->setAttributes();
}