Failed Conditions
Pull Request — develop (#3368)
by Benjamin
42:38 queued 39:25
created

PDOConnection::getSequenceNumber()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
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 96
    public function lastInsertId() : string
96
    {
97
        try {
98 96
            $lastInsertId = $this->connection->lastInsertId();
99 6
        } catch (PDOException $e) {
100 6
            throw self::exceptionFromPDOException($e);
101
        }
102
103
        // pdo_mysql and others return '0', pdo_sqlsrv returns ''
104 90
        if ($lastInsertId === '0' || $lastInsertId === '') {
105 8
            throw DriverException::noInsertId();
106
        }
107
108 82
        return $lastInsertId;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 18
    public function getSequenceNumber(string $name) : string
115
    {
116
        try {
117 18
            $lastInsertId = $this->connection->lastInsertId($name);
118 6
        } catch (\PDOException $e) {
119 6
            throw self::exceptionFromPDOException($e);
120
        }
121
122
        // pdo_mysql and others return '0', pdo_sqlsrv returns ''
123 12
        if ($lastInsertId === '0' || $lastInsertId === '') {
124 6
            throw DriverException::noSuchSequence($name);
125
        }
126
127 6
        return $lastInsertId;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 382
    public function requiresQueryForServerVersion()
134
    {
135 382
        return false;
136
    }
137
138
    /**
139
     * Creates a wrapped statement
140
     */
141 3677
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
142
    {
143 3677
        return new PDOStatement($stmt);
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 237
    public function beginTransaction() : void
150
    {
151 237
        $this->connection->beginTransaction();
152 237
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157 88
    public function commit() : void
158
    {
159 88
        $this->connection->commit();
160 88
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 139
    public function rollBack() : void
166
    {
167 139
        $this->connection->rollBack();
168 139
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173
    public function errorCode()
174
    {
175
        return $this->connection->errorCode();
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181
    public function errorInfo()
182
    {
183
        return $this->connection->errorInfo();
184
    }
185
186 716
    public function getWrappedConnection() : PDO
187
    {
188 716
        return $this->connection;
189
    }
190
191
    /**
192
     * Creates a DriverException from a PDOException.
193
     */
194 2050
    public static function exceptionFromPDOException(PDOException $exception) : DriverException
195
    {
196 2050
        return new DriverException(
197 2050
            $exception->getMessage(),
198 2050
            $exception->errorInfo[0] ?? $exception->getCode(),
199 2050
            $exception->errorInfo[1] ?? $exception->getCode(),
200 2050
            $exception
201
        );
202
    }
203
}
204