SQLSrvException.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Doctrine\DBAL\Driver\SQLSrv;
  3. use Doctrine\DBAL\Driver\AbstractDriverException;
  4. use const SQLSRV_ERR_ERRORS;
  5. use function rtrim;
  6. use function sqlsrv_errors;
  7. class SQLSrvException extends AbstractDriverException
  8. {
  9. /**
  10. * Helper method to turn sql server errors into exception.
  11. *
  12. * @return \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException
  13. */
  14. public static function fromSqlSrvErrors()
  15. {
  16. $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
  17. $message = '';
  18. $sqlState = null;
  19. $errorCode = null;
  20. foreach ($errors as $error) {
  21. $message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
  22. if ($sqlState === null) {
  23. $sqlState = $error['SQLSTATE'];
  24. }
  25. if ($errorCode !== null) {
  26. continue;
  27. }
  28. $errorCode = $error['code'];
  29. }
  30. if (! $message) {
  31. $message = 'SQL Server error occurred but no error message was retrieved from driver.';
  32. }
  33. return new self(rtrim($message), $sqlState, $errorCode);
  34. }
  35. }