SQLAnywhereException.php 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Driver\SQLAnywhere;

22
use Doctrine\DBAL\Driver\AbstractDriverException;
23 24 25 26 27 28 29 30

/**
 * SAP Sybase SQL Anywhere driver exception.
 *
 * @author Steve Müller <st.mueller@dzh-online.de>
 * @link   www.doctrine-project.org
 * @since  2.5
 */
31
class SQLAnywhereException extends AbstractDriverException
32 33 34 35 36 37 38 39 40 41 42 43 44
{
    /**
     * 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
     *
     * @throws \InvalidArgumentException
     */
    public static function fromSQLAnywhereError($conn = null, $stmt = null)
    {
45
        if (null !== $conn && ! (is_resource($conn))) {
46 47 48
            throw new \InvalidArgumentException('Invalid SQL Anywhere connection resource given: ' . $conn);
        }

49
        if (null !== $stmt && ! (is_resource($stmt))) {
50 51 52
            throw new \InvalidArgumentException('Invalid SQL Anywhere statement resource given: ' . $stmt);
        }

Steve Müller's avatar
Steve Müller committed
53 54
        $state   = $conn ? sasql_sqlstate($conn) : sasql_sqlstate();
        $code    = null;
55 56 57 58 59 60
        $message = null;

        /**
         * Try retrieving the last error from statement resource if given
         */
        if ($stmt) {
Steve Müller's avatar
Steve Müller committed
61
            $code    = sasql_stmt_errno($stmt);
62 63 64 65 66 67 68 69 70 71 72 73
            $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
74
            $code    = sasql_errorcode($conn);
75 76 77 78 79 80 81 82 83
            $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.
         */
        if ( ! $conn || ! $code) {
Steve Müller's avatar
Steve Müller committed
84
            $code    = sasql_errorcode();
85 86 87 88
            $message = sasql_error();
        }

        if ($message) {
89
            return new self('SQLSTATE [' . $state . '] [' . $code . '] ' . $message, $state, $code);
90 91
        }

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