Completed
Pull Request — master (#3759)
by Benjamin
63:45
created

PDOConnection::beginTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
use PDO;
8
use function assert;
9
10
/**
11
 * PDO implementation of the Connection interface.
12
 *
13
 * Used by all PDO-based drivers.
14
 */
15
class PDOConnection implements ServerInfoAwareConnection
16
{
17
    /** @var PDO */
18
    private $connection;
19
20
    /**
21
     * @param array<int, mixed> $options
22
     *
23
     * @throws PDOException In case of an error.
24 5025
     */
25
    public function __construct(string $dsn, string $username = '', string $password = '', array $options = [])
26
    {
27 5025
        try {
28 5022
            $this->connection = new PDO($dsn, $username, $password, $options);
29 5022
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30 4355
        } catch (\PDOException $exception) {
31 4355
            throw PDOException::fromNativePDOException($exception);
32
        }
33 5022
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 5141
    public function exec(string $statement) : int
39
    {
40
        try {
41 5141
            return $this->connection->exec($statement);
42 5093
        } catch (\PDOException $exception) {
43 5093
            throw PDOException::fromNativePDOException($exception);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4502
    public function getServerVersion() : string
51
    {
52 4502
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 4995
    public function prepare(string $sql) : Statement
59
    {
60
        try {
61 4995
            return $this->createStatement(
62 4861
                $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 4861
            );
64
        } catch (\PDOException $exception) {
65
            throw PDOException::fromNativePDOException($exception);
66
        }
67
    }
68
69
    /**
70 5155
     * {@inheritdoc}
71
     */
72 5155
    public function query(string $sql) : ResultStatement
73
    {
74
        try {
75 5155
            $stmt = $this->connection->query($sql);
76 5003
            assert($stmt instanceof \PDOStatement);
77
78 5003
            return $this->createStatement($stmt);
79 4942
        } catch (\PDOException $exception) {
80 4942
            throw PDOException::fromNativePDOException($exception);
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 4533
    public function quote(string $input) : string
88
    {
89 4533
        return $this->connection->quote($input);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 486
    public function lastInsertId() : string
96
    {
97
        try {
98 486
            $lastInsertId = (string) $this->connection->lastInsertId();
99 145
        } catch (\PDOException $exception) {
100
            throw PDOException::fromNativePDOException($exception);
101
        }
102 341
103
        if (! $this->isValidLastInsertIdOrSequenceNumber($lastInsertId)) {
104
            throw new PDOException('No identity value was generated by the last statement.');
105
        }
106
107
        return $lastInsertId;
108
    }
109
110
    /**
111 4681
     * {@inheritdoc}
112
     */
113 4681
    public function getSequenceNumber(string $name) : string
114
    {
115
        try {
116
            $sequenceNumber = (string) $this->connection->lastInsertId($name);
117
        } catch (\PDOException $exception) {
118
            throw PDOException::fromNativePDOException($exception);
119
        }
120
121
        if (! $this->isValidLastInsertIdOrSequenceNumber($sequenceNumber)) {
122
            throw new PDOException('No sequence with name "' . $name . '" found.');
123
        }
124
125
        return $sequenceNumber;
126
    }
127
128
    private function isValidLastInsertIdOrSequenceNumber(string $value) : bool
129
    {
130
        // pdo_mysql & pdo_sqlite return '0', pdo_sqlsrv returns ''
131
        return $value !== '0' && $value !== '';
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function requiresQueryForServerVersion() : bool
138
    {
139
        return false;
140
    }
141
142
    /**
143
     * Creates a wrapped statement
144
     */
145
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
146
    {
147
        return new PDOStatement($stmt);
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function beginTransaction() : void
154
    {
155
        $this->connection->beginTransaction();
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function commit() : void
162
    {
163
        $this->connection->commit();
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169
    public function rollBack() : void
170
    {
171
        $this->connection->rollBack();
172
    }
173
174
    public function getWrappedConnection() : PDO
175
    {
176
        return $this->connection;
177
    }
178
}
179