mirror of
https://github.com/silverstripe/silverstripe-mssql
synced 2024-10-22 08:05:53 +02:00
Tidying up error reporting in databaseError() and adding getLastError()
Whenever databaseError() is called when a database problem occurred then the last error is automatically appended to the end of the general error message.
This commit is contained in:
parent
f34996ae1c
commit
f64c662124
@ -174,6 +174,23 @@ class MSSQLDatabase extends SS_Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the last database error.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getLastError() {
|
||||||
|
$error = '';
|
||||||
|
if($this->mssql) {
|
||||||
|
$error = mssql_get_last_message();
|
||||||
|
} else {
|
||||||
|
$errors = sqlsrv_errors();
|
||||||
|
if($errors) foreach($errors as $info) {
|
||||||
|
$error .= implode(', ', array($info['SQLSTATE'], $info['code'], $info['message']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $error;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the current SQL Server version has full-text
|
* Checks whether the current SQL Server version has full-text
|
||||||
* support installed and full-text is enabled for this database.
|
* support installed and full-text is enabled for this database.
|
||||||
@ -197,15 +214,7 @@ class MSSQLDatabase extends SS_Database {
|
|||||||
* Throw a database error
|
* Throw a database error
|
||||||
*/
|
*/
|
||||||
function databaseError($message, $errorLevel = E_USER_ERROR) {
|
function databaseError($message, $errorLevel = E_USER_ERROR) {
|
||||||
if(!$this->mssql) {
|
$message .= "\n" . $this->getLastError();
|
||||||
$errorMessages = array();
|
|
||||||
$errors = sqlsrv_errors();
|
|
||||||
if ($errors) foreach($errors as $error) {
|
|
||||||
$errorMessages[] = $error['message'];
|
|
||||||
}
|
|
||||||
$message .= ": \n" . implode("; ",$errorMessages);
|
|
||||||
}
|
|
||||||
|
|
||||||
return parent::databaseError($message, $errorLevel);
|
return parent::databaseError($message, $errorLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,18 +309,9 @@ class MSSQLDatabase extends SS_Database {
|
|||||||
$error = '';
|
$error = '';
|
||||||
if($this->mssql) {
|
if($this->mssql) {
|
||||||
$handle = mssql_query($sql, $this->dbConn);
|
$handle = mssql_query($sql, $this->dbConn);
|
||||||
$error = mssql_get_last_message();
|
|
||||||
} else {
|
} else {
|
||||||
$handle = sqlsrv_query($this->dbConn, $sql);
|
$handle = sqlsrv_query($this->dbConn, $sql);
|
||||||
if($handle) $this->lastAffectedRows = sqlsrv_rows_affected($handle);
|
if($handle) $this->lastAffectedRows = sqlsrv_rows_affected($handle);
|
||||||
if(function_exists('sqlsrv_errors')) {
|
|
||||||
$errInfo = sqlsrv_errors();
|
|
||||||
if($errInfo) {
|
|
||||||
foreach($errInfo as $info) {
|
|
||||||
$error .= implode(', ', array($info['SQLSTATE'], $info['code'], $info['message']));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($_REQUEST['showqueries'])) {
|
if(isset($_REQUEST['showqueries'])) {
|
||||||
@ -319,7 +319,7 @@ class MSSQLDatabase extends SS_Database {
|
|||||||
Debug::message("\n$sql\n{$endtime}ms\n", false);
|
Debug::message("\n$sql\n{$endtime}ms\n", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$handle && $errorLevel) $this->databaseError("Couldn't run query ($error): $sql", $errorLevel);
|
if(!$handle && $errorLevel) $this->databaseError("Couldn't run query: $sql", $errorLevel);
|
||||||
return new MSSQLQuery($this, $handle, $this->mssql);
|
return new MSSQLQuery($this, $handle, $this->mssql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user