Failed Conditions
Pull Request — develop (#3367)
by Benjamin
61: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 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 2778
    public function exec(string $statement) : int
40
    {
41
        try {
42 2778
            return $this->connection->exec($statement);
43 1737
        } catch (PDOException $exception) {
44 1737
            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 2157
    public function prepare(string $sql) : Statement
60
    {
61
        try {
62 2157
            return $this->createStatement(
63 2157
                $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 138
    public function quote(string $input) : string
88
    {
89 138
        return $this->connection->quote($input);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 82
    public function lastInsertId(?string $name = null) : string
96
    {
97 82
        try {
98
            $lastInsertId = $this->connection->lastInsertId($name);
99
        } catch (PDOException $e) {
100
            throw self::exceptionFromPDOException($e);
101
        }
102
103 382
        // pdo_mysql and others return '0', pdo_sqlsrv returns ''
104
        if ($lastInsertId === '0' || $lastInsertId === '') {
105 382
            throw DriverException::noInsertId();
106
        }
107
108
        return $lastInsertId;
109
    }
110
111 3671
    /**
112
     * {@inheritdoc}
113 3671
     */
114
    public function requiresQueryForServerVersion()
115
    {
116
        return false;
117
    }
118
119 237
    /**
120
     * Creates a wrapped statement
121 237
     */
122
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
123
    {
124
        return new PDOStatement($stmt);
125
    }
126
127 88
    /**
128
     * {@inheritDoc}
129 88
     */
130
    public function beginTransaction() : void
131
    {
132
        $this->connection->beginTransaction();
133
    }
134
135 139
    /**
136
     * {@inheritDoc}
137 139
     */
138
    public function commit() : void
139
    {
140
        $this->connection->commit();
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function rollBack() : void
147
    {
148
        $this->connection->rollBack();
149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154
    public function errorCode()
155
    {
156 716
        return $this->connection->errorCode();
157
    }
158 716
159
    /**
160
     * {@inheritDoc}
161
     */
162
    public function errorInfo()
163
    {
164
        return $this->connection->errorInfo();
165
    }
166
167
    public function getWrappedConnection() : PDO
168
    {
169
        return $this->connection;
170
    }
171
172
    /**
173
     * Creates a DriverException from a PDOException.
174
     */
175
    public static function exceptionFromPDOException(PDOException $exception) : DriverException
176
    {
177
        return new DriverException(
178
            $exception->getMessage(),
179
            $exception->errorInfo[0] ?? $exception->getCode(),
180
            $exception->errorInfo[1] ?? $exception->getCode(),
181
            $exception
182
        );
183
    }
184
}
185