SQLAnywhereException.php 2.7 KB

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