SQLAnywhereException.php 2.68 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Driver\SQLAnywhere;

5
use Doctrine\DBAL\Driver\AbstractDriverException;
6
use InvalidArgumentException;
7 8 9 10 11 12
use function is_resource;
use function sasql_error;
use function sasql_errorcode;
use function sasql_sqlstate;
use function sasql_stmt_errno;
use function sasql_stmt_error;
13 14 15 16

/**
 * SAP Sybase SQL Anywhere driver exception.
 */
17
class SQLAnywhereException extends AbstractDriverException
18 19 20 21 22 23 24 25 26
{
    /**
     * Helper method to turn SQL Anywhere error into exception.
     *
     * @param resource|null $conn The SQL Anywhere connection resource to retrieve the last error from.
     * @param resource|null $stmt The SQL Anywhere statement resource to retrieve the last error from.
     *
     * @return SQLAnywhereException
     *
27
     * @throws InvalidArgumentException
28 29 30
     */
    public static function fromSQLAnywhereError($conn = null, $stmt = null)
    {
31 32
        if ($conn !== null && ! is_resource($conn)) {
            throw new InvalidArgumentException('Invalid SQL Anywhere connection resource given: ' . $conn);
33 34
        }

35 36
        if ($stmt !== null && ! is_resource($stmt)) {
            throw new InvalidArgumentException('Invalid SQL Anywhere statement resource given: ' . $stmt);
37 38
        }

Steve Müller's avatar
Steve Müller committed
39 40
        $state   = $conn ? sasql_sqlstate($conn) : sasql_sqlstate();
        $code    = null;
41 42 43 44 45 46
        $message = null;

        /**
         * Try retrieving the last error from statement resource if given
         */
        if ($stmt) {
Steve Müller's avatar
Steve Müller committed
47
            $code    = sasql_stmt_errno($stmt);
48 49 50 51 52 53 54 55 56 57 58 59
            $message = sasql_stmt_error($stmt);
        }

        /**
         * Try retrieving the last error from the connection resource
         * if either the statement resource is not given or the statement
         * resource is given but the last error could not be retrieved from it (fallback).
         * Depending on the type of error, it is sometimes necessary to retrieve
         * it from the connection resource even though it occurred during
         * a prepared statement.
         */
        if ($conn && ! $code) {
Steve Müller's avatar
Steve Müller committed
60
            $code    = sasql_errorcode($conn);
61 62 63 64 65 66 67 68
            $message = sasql_error($conn);
        }

        /**
         * Fallback mode if either no connection resource is given
         * or the last error could not be retrieved from the given
         * connection / statement resource.
         */
69
        if (! $conn || ! $code) {
Steve Müller's avatar
Steve Müller committed
70
            $code    = sasql_errorcode();
71 72 73 74
            $message = sasql_error();
        }

        if ($message) {
75
            return new self('SQLSTATE [' . $state . '] [' . $code . '] ' . $message, $state, $code);
76 77
        }

78
        return new self('SQL Anywhere error occurred but no error message was retrieved from driver.', $state, $code);
79 80
    }
}