SQLAnywhereException.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Doctrine\DBAL\Driver\SQLAnywhere;
  3. use Doctrine\DBAL\Driver\AbstractDriverException;
  4. use InvalidArgumentException;
  5. use function sasql_error;
  6. use function sasql_errorcode;
  7. use function sasql_sqlstate;
  8. use function sasql_stmt_errno;
  9. use function sasql_stmt_error;
  10. /**
  11. * SAP Sybase SQL Anywhere driver exception.
  12. *
  13. * @psalm-immutable
  14. */
  15. class SQLAnywhereException extends AbstractDriverException
  16. {
  17. /**
  18. * Helper method to turn SQL Anywhere error into exception.
  19. *
  20. * @param resource|null $conn The SQL Anywhere connection resource to retrieve the last error from.
  21. * @param resource|null $stmt The SQL Anywhere statement resource to retrieve the last error from.
  22. *
  23. * @return SQLAnywhereException
  24. *
  25. * @throws InvalidArgumentException
  26. */
  27. public static function fromSQLAnywhereError($conn = null, $stmt = null)
  28. {
  29. $state = $conn ? sasql_sqlstate($conn) : sasql_sqlstate();
  30. $code = null;
  31. $message = null;
  32. /**
  33. * Try retrieving the last error from statement resource if given
  34. */
  35. if ($stmt) {
  36. $code = sasql_stmt_errno($stmt);
  37. $message = sasql_stmt_error($stmt);
  38. }
  39. /**
  40. * Try retrieving the last error from the connection resource
  41. * if either the statement resource is not given or the statement
  42. * resource is given but the last error could not be retrieved from it (fallback).
  43. * Depending on the type of error, it is sometimes necessary to retrieve
  44. * it from the connection resource even though it occurred during
  45. * a prepared statement.
  46. */
  47. if ($conn && ! $code) {
  48. $code = sasql_errorcode($conn);
  49. $message = sasql_error($conn);
  50. }
  51. /**
  52. * Fallback mode if either no connection resource is given
  53. * or the last error could not be retrieved from the given
  54. * connection / statement resource.
  55. */
  56. if (! $conn || ! $code) {
  57. $code = sasql_errorcode();
  58. $message = sasql_error();
  59. }
  60. if ($message) {
  61. return new self('SQLSTATE [' . $state . '] [' . $code . '] ' . $message, $state, $code);
  62. }
  63. return new self('SQL Anywhere error occurred but no error message was retrieved from driver.', $state, $code);
  64. }
  65. }