Failed Conditions
Pull Request — develop (#3335)
by
unknown
15:06
created

PDOConnection.php$0 ➔ rollBack()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use PDO;
6
7
/**
8
 * PDO implementation of the Connection interface.
9
 *
10
 * Used by all PDO-based drivers.
11
 */
12
class PDOConnection implements Connection, ServerInfoAwareConnection
13
{
14
    /** @var PDO */
15
    private $connection;
16
17
    /**
18
     * @param string       $dsn
19
     * @param string|null  $user
20
     * @param string|null  $password
21
     * @param mixed[]|null $options
22
     *
23
     * @throws PDOException In case of an error.
24
     */
25 922
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
26
    {
27
        try {
28 922
            $this->connection = new PDO($dsn, $user, $password, $options);
29 891
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30 58
        } catch (\PDOException $exception) {
31 58
            throw new PDOException($exception);
32
        }
33 891
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2812
    public function exec(string $statement) : int
39
    {
40
        try {
41 2812
            return $this->connection->exec($statement);
42 1771
        } catch (\PDOException $exception) {
43 1771
            throw new PDOException($exception);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 368
    public function getServerVersion()
51
    {
52 368
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2164
    public function prepare(string $sql) : Statement
59
    {
60
        try {
61 2164
            return $this->createStatement(
62 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

62
                /** @scrutinizer ignore-type */ $this->connection->prepare($sql)
Loading history...
63
            );
64 31
        } catch (\PDOException $exception) {
65 31
            throw new PDOException($exception);
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 2909
    public function query(string $sql) : ResultStatement
73
    {
74
        try {
75 2909
            return $this->createStatement(
76 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

76
                /** @scrutinizer ignore-type */ $this->connection->query($sql)
Loading history...
77
            );
78 99
        } catch (\PDOException $exception) {
79 99
            throw new PDOException($exception);
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 124
    public function quote(string $input) : string
87
    {
88 124
        return $this->connection->quote($input);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 114
    public function lastInsertId(?string $name = null) : string
95
    {
96
        try {
97 114
            $lastInsertId = $this->connection->lastInsertId($name);
98 12
        } catch (\PDOException $e) {
99 12
            throw new PDOException($e);
100
        }
101
102
        // pdo_mysql and others return '0', pdo_sqlsrv returns ''
103 102
        if ($lastInsertId === '0' || $lastInsertId === '') {
104
            // WIP regarding exceptions, see:
105
            // https://github.com/doctrine/dbal/pull/3335#discussion_r234381175
106
            throw new class ('The last statement did not return an insert ID.') extends AbstractDriverException {
107
            };
108
        }
109
110 88
        return $lastInsertId;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 382
    public function requiresQueryForServerVersion()
117
    {
118 382
        return false;
119
    }
120
121
    /**
122
     * Creates a wrapped statement
123
     */
124 3677
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
125
    {
126 3677
        return new PDOStatement($stmt);
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132 237
    public function beginTransaction() : void
133
    {
134 237
        $this->connection->beginTransaction();
135 237
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140 88
    public function commit() : void
141
    {
142 88
        $this->connection->commit();
143 88
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148 139
    public function rollBack() : void
149
    {
150 139
        $this->connection->rollBack();
151 139
    }
152
153
    /**
154
     * {@inheritDoc}
155
     */
156
    public function errorCode()
157
    {
158
        return $this->connection->errorCode();
159
    }
160
161
    /**
162
     * {@inheritDoc}
163
     */
164
    public function errorInfo()
165
    {
166
        return $this->connection->errorInfo();
167
    }
168
169 716
    public function getWrappedConnection() : PDO
170
    {
171 716
        return $this->connection;
172
    }
173
}
174