Failed Conditions
Pull Request — develop (#3367)
by Benjamin
10:59
created

DriverException::noInsertId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Exception;
6
use Throwable;
7
8
/**
9
 * A driver exception.
10
 *
11
 * Driver exceptions provide the SQLSTATE of the driver
12
 * and the driver specific error code at the time the error occurred.
13
 */
14
class DriverException extends Exception
15
{
16
    /**
17
     * The driver specific error code.
18
     *
19
     * @var int|string|null
20
     */
21
    private $errorCode;
22
23
    /**
24
     * The SQLSTATE of the driver.
25
     *
26
     * @var string|null
27
     */
28
    private $sqlState;
29
30
    /**
31
     * @param string          $message   The driver error message.
32
     * @param string|null     $sqlState  The SQLSTATE the driver is in at the time the error occurred, if any.
33
     * @param int|string|null $errorCode The driver specific error code if any.
34
     * @param Throwable|null  $previous  The previous throwable used for the exception chaining.
35
     */
36 3672
    public function __construct(string $message, ?string $sqlState = null, $errorCode = null, ?Throwable $previous = null)
37
    {
38 3672
        parent::__construct($message, 0, $previous);
39
40 3672
        $this->errorCode = $errorCode;
41 3672
        $this->sqlState  = $sqlState;
42 3672
    }
43
44
    /**
45
     * @return DriverException
46
     */
47 28
    public static function noInsertId() : self
48
    {
49 28
        return new self('No identity value was generated by the last statement.');
50
    }
51
52
    /**
53
     * @param string $name The sequence name.
54
     *
55
     * @return DriverException
56
     */
57 2
    public static function noSuchSequence(string $name) : self
58
    {
59 2
        return new self('No sequence with name "' . $name . '" found.');
60
    }
61
62
    /**
63
     * Returns the driver specific error code if available.
64
     *
65
     * Returns null if no driver specific error code is available
66
     * for the error raised by the driver.
67
     *
68
     * @return int|string|null The error code.
69
     */
70 2026
    public function getErrorCode()
71
    {
72 2026
        return $this->errorCode;
73
    }
74
75
    /**
76
     * Returns the SQLSTATE the driver was in at the time the error occurred.
77
     *
78
     * Returns null if the driver does not provide a SQLSTATE for the error occurred.
79
     *
80
     * @return string|null The SQLSTATE, or null if not available.
81
     */
82 1315
    public function getSQLState() : ?string
83
    {
84 1315
        return $this->sqlState;
85
    }
86
}
87