Failed Conditions
Pull Request — develop (#3367)
by Benjamin
12:25
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 922
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
27
    {
28
        try {
29 922
            $this->connection = new PDO($dsn, $user, $password, $options);
30 891
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
31 58
        } catch (PDOException $exception) {
32 58
            throw self::exceptionFromPDOException($exception);
33
        }
34 891
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2812
    public function exec(string $statement) : int
40
    {
41
        try {
42 2812
            return $this->connection->exec($statement);
43 1771
        } catch (PDOException $exception) {
44 1771
            throw self::exceptionFromPDOException($exception);
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 368
    public function getServerVersion()
52
    {
53 368
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2164
    public function prepare(string $sql) : Statement
60
    {
61
        try {
62 2164
            return $this->createStatement(
63 2164
                $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 31
        } catch (PDOException $exception) {
66 31
            throw self::exceptionFromPDOException($exception);
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2909
    public function query(string $sql) : ResultStatement
74
    {
75
        try {
76 2909
            return $this->createStatement(
77 2909
                $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 99
        } catch (PDOException $exception) {
80 99
            throw self::exceptionFromPDOException($exception);
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 124
    public function quote(string $input) : string
88
    {
89 124
        return $this->connection->quote($input);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 114
    public function lastInsertId(?string $name = null) : string
96
    {
97
        try {
98 114
            $lastInsertId = $this->connection->lastInsertId($name);
99 12
        } catch (PDOException $e) {
100 12
            throw self::exceptionFromPDOException($e);
101
        }
102
103
        // pdo_mysql and others return '0', pdo_sqlsrv returns ''
104 102
        if ($lastInsertId === '0' || $lastInsertId === '') {
105 14
            throw DriverException::noInsertId();
106
        }
107
108 88
        return $lastInsertId;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 382
    public function requiresQueryForServerVersion()
115
    {
116 382
        return false;
117
    }
118
119
    /**
120
     * Creates a wrapped statement
121
     */
122 3677
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
123
    {
124 3677
        return new PDOStatement($stmt);
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130 237
    public function beginTransaction() : void
131
    {
132 237
        $this->connection->beginTransaction();
133 237
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138 88
    public function commit() : void
139
    {
140 88
        $this->connection->commit();
141 88
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146 139
    public function rollBack() : void
147
    {
148 139
        $this->connection->rollBack();
149 139
    }
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 716
    public function getWrappedConnection() : PDO
168
    {
169 716
        return $this->connection;
170
    }
171
172
    /**
173
     * Creates a DriverException from a PDOException.
174
     */
175 2050
    public static function exceptionFromPDOException(PDOException $exception) : DriverException
176
    {
177 2050
        return new DriverException(
178 2050
            $exception->getMessage(),
179 2050
            $exception->errorInfo[0] ?? $exception->getCode(),
180 2050
            $exception->errorInfo[1] ?? $exception->getCode(),
181 2050
            $exception
182
        );
183
    }
184
}
185