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