Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

DBAL/Driver/SQLAnywhere/SQLAnywhereException.php (2 issues)

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
20
namespace Doctrine\DBAL\Driver\SQLAnywhere;
21
22
use Doctrine\DBAL\Driver\AbstractDriverException;
23
24
/**
25
 * SAP Sybase SQL Anywhere driver exception.
26
 *
27
 * @author Steve Müller <[email protected]>
28
 * @link   www.doctrine-project.org
29
 * @since  2.5
30
 */
31
class SQLAnywhereException extends AbstractDriverException
32
{
33
    /**
34
     * Helper method to turn SQL Anywhere error into exception.
35
     *
36
     * @param resource|null $conn The SQL Anywhere connection resource to retrieve the last error from.
37
     * @param resource|null $stmt The SQL Anywhere statement resource to retrieve the last error from.
38
     *
39
     * @return SQLAnywhereException
40
     *
41
     * @throws \InvalidArgumentException
42
     */
43
    public static function fromSQLAnywhereError($conn = null, $stmt = null)
44
    {
45 View Code Duplication
        if (null !== $conn && ! (is_resource($conn))) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            throw new \InvalidArgumentException('Invalid SQL Anywhere connection resource given: ' . $conn);
47
        }
48
49 View Code Duplication
        if (null !== $stmt && ! (is_resource($stmt))) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            throw new \InvalidArgumentException('Invalid SQL Anywhere statement resource given: ' . $stmt);
51
        }
52
53
        $state   = $conn ? sasql_sqlstate($conn) : sasql_sqlstate();
54
        $code    = null;
55
        $message = null;
56
57
        /**
58
         * Try retrieving the last error from statement resource if given
59
         */
60
        if ($stmt) {
61
            $code    = sasql_stmt_errno($stmt);
62
            $message = sasql_stmt_error($stmt);
63
        }
64
65
        /**
66
         * Try retrieving the last error from the connection resource
67
         * if either the statement resource is not given or the statement
68
         * resource is given but the last error could not be retrieved from it (fallback).
69
         * Depending on the type of error, it is sometimes necessary to retrieve
70
         * it from the connection resource even though it occurred during
71
         * a prepared statement.
72
         */
73
        if ($conn && ! $code) {
74
            $code    = sasql_errorcode($conn);
75
            $message = sasql_error($conn);
76
        }
77
78
        /**
79
         * Fallback mode if either no connection resource is given
80
         * or the last error could not be retrieved from the given
81
         * connection / statement resource.
82
         */
83
        if ( ! $conn || ! $code) {
84
            $code    = sasql_errorcode();
85
            $message = sasql_error();
86
        }
87
88
        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
}
95