SQLAnywhereException.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\SQLAnywhere;
  20. use Doctrine\DBAL\Driver\AbstractDriverException;
  21. /**
  22. * SAP Sybase SQL Anywhere driver exception.
  23. *
  24. * @author Steve Müller <st.mueller@dzh-online.de>
  25. * @link www.doctrine-project.org
  26. * @since 2.5
  27. */
  28. class SQLAnywhereException extends AbstractDriverException
  29. {
  30. /**
  31. * Helper method to turn SQL Anywhere error into exception.
  32. *
  33. * @param resource|null $conn The SQL Anywhere connection resource to retrieve the last error from.
  34. * @param resource|null $stmt The SQL Anywhere statement resource to retrieve the last error from.
  35. *
  36. * @return SQLAnywhereException
  37. *
  38. * @throws \InvalidArgumentException
  39. */
  40. public static function fromSQLAnywhereError($conn = null, $stmt = null)
  41. {
  42. if (null !== $conn && ! (is_resource($conn) && get_resource_type($conn) === 'SQLAnywhere connection')) {
  43. throw new \InvalidArgumentException('Invalid SQL Anywhere connection resource given: ' . $conn);
  44. }
  45. if (null !== $stmt && ! (is_resource($stmt) && get_resource_type($stmt) === 'SQLAnywhere statement')) {
  46. throw new \InvalidArgumentException('Invalid SQL Anywhere statement resource given: ' . $stmt);
  47. }
  48. $state = $conn ? sasql_sqlstate($conn) : sasql_sqlstate();
  49. $code = null;
  50. $message = null;
  51. /**
  52. * Try retrieving the last error from statement resource if given
  53. */
  54. if ($stmt) {
  55. $code = sasql_stmt_errno($stmt);
  56. $message = sasql_stmt_error($stmt);
  57. }
  58. /**
  59. * Try retrieving the last error from the connection resource
  60. * if either the statement resource is not given or the statement
  61. * resource is given but the last error could not be retrieved from it (fallback).
  62. * Depending on the type of error, it is sometimes necessary to retrieve
  63. * it from the connection resource even though it occurred during
  64. * a prepared statement.
  65. */
  66. if ($conn && ! $code) {
  67. $code = sasql_errorcode($conn);
  68. $message = sasql_error($conn);
  69. }
  70. /**
  71. * Fallback mode if either no connection resource is given
  72. * or the last error could not be retrieved from the given
  73. * connection / statement resource.
  74. */
  75. if ( ! $conn || ! $code) {
  76. $code = sasql_errorcode();
  77. $message = sasql_error();
  78. }
  79. if ($message) {
  80. return new self('SQLSTATE [' . $state . '] [' . $code . '] ' . $message, $state, $code);
  81. }
  82. return new self('SQL Anywhere error occurred but no error message was retrieved from driver.', $state, $code);
  83. }
  84. }