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

PDOConnection::exceptionFromPDOException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use PDO;
6
use PDOException;
7
8
/**
9
 * PDO implementation of the Connection interface.
10
 *
11
 * Used by all PDO-based drivers.
12
 */
13
class PDOConnection implements Connection, ServerInfoAwareConnection
14
{
15
    /** @var PDO */
16
    private $connection;
17
18
    /**
19
     * @param string       $dsn
20
     * @param string|null  $user
21
     * @param string|null  $password
22
     * @param mixed[]|null $options
23
     *
24
     * @throws DriverException In case of an error.
25
     */
26 977
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
27
    {
28
        try {
29 977
            $this->connection = new PDO($dsn, $user, $password, $options);
30 944
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
31 62
        } catch (PDOException $exception) {
32 62
            throw self::exceptionFromPDOException($exception);
33
        }
34 944
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3025
    public function exec(string $statement) : int
40
    {
41
        try {
42 3025
            return $this->connection->exec($statement);
43 1908
        } catch (PDOException $exception) {
44 1908
            throw self::exceptionFromPDOException($exception);
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 395
    public function getServerVersion()
52
    {
53 395
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2316
    public function prepare(string $sql) : Statement
60
    {
61
        try {
62 2316
            return $this->createStatement(
63 2316
                $this->connection->prepare($sql)
0 ignored issues
show
Bug introduced by
It seems like $this->connection->prepare($sql) can also be of type boolean; however, parameter $stmt of Doctrine\DBAL\Driver\PDO...tion::createStatement() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
                /** @scrutinizer ignore-type */ $this->connection->prepare($sql)
Loading history...
64
            );
65 32
        } catch (PDOException $exception) {
66 32
            throw self::exceptionFromPDOException($exception);
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 3125
    public function query(string $sql) : ResultStatement
74
    {
75
        try {
76 3125
            return $this->createStatement(
77 3125
                $this->connection->query($sql)
0 ignored issues
show
Bug introduced by
It seems like $this->connection->query($sql) can also be of type boolean; however, parameter $stmt of Doctrine\DBAL\Driver\PDO...tion::createStatement() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
                /** @scrutinizer ignore-type */ $this->connection->query($sql)
Loading history...
78
            );
79 105
        } catch (PDOException $exception) {
80 105
            throw self::exceptionFromPDOException($exception);
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 129
    public function quote(string $input) : string
88
    {
89 129
        return $this->connection->quote($input);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 128
    public function lastInsertId(?string $name = null) : string
96
    {
97
        try {
98 128
            $lastInsertId = $this->connection->lastInsertId($name);
99 14
        } catch (PDOException $e) {
100 14
            throw self::exceptionFromPDOException($e);
101
        }
102
103
        // pdo_mysql and others return '0', pdo_sqlsrv returns ''
104 114
        if ($lastInsertId === '0' || $lastInsertId === '') {
105 14
            throw DriverException::noInsertId();
106
        }
107
108 100
        return $lastInsertId;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 410
    public function requiresQueryForServerVersion()
115
    {
116 410
        return false;
117
    }
118
119
    /**
120
     * Creates a wrapped statement
121
     */
122 3971
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
123
    {
124 3971
        return new PDOStatement($stmt);
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130 253
    public function beginTransaction() : void
131
    {
132 253
        $this->connection->beginTransaction();
133 253
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138 94
    public function commit() : void
139
    {
140 94
        $this->connection->commit();
141 94
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146 149
    public function rollBack() : void
147
    {
148 149
        $this->connection->rollBack();
149 149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154
    public function errorCode()
155
    {
156
        return $this->connection->errorCode();
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162
    public function errorInfo()
163
    {
164
        return $this->connection->errorInfo();
165
    }
166
167 778
    public function getWrappedConnection() : PDO
168
    {
169 778
        return $this->connection;
170
    }
171
172
    /**
173
     * Creates a DriverException from a PDOException.
174
     */
175 2205
    public static function exceptionFromPDOException(PDOException $exception) : DriverException
176
    {
177 2205
        return new DriverException(
178 2205
            $exception->getMessage(),
179 2205
            $exception->errorInfo[0] ?? $exception->getCode(),
180 2205
            $exception->errorInfo[1] ?? $exception->getCode(),
181 2205
            $exception
182
        );
183
    }
184
}
185