Completed
Push — develop ( 361a2b...a96e4b )
by Marco
20s queued 14s
created

PDOConnection::errorCode()   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 1
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 Connection, ServerInfoAwareConnection
16
{
17
    /** @var PDO */
18
    private $connection;
19
20
    /**
21
     * @param string       $dsn
22
     * @param string|null  $user
23
     * @param string|null  $password
24
     * @param mixed[]|null $options
25
     *
26 4638
     * @throws PDOException In case of an error.
27
     */
28
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
29 4638
    {
30 4638
        try {
31 4035
            $this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options);
32 4035
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
33
        } catch (\PDOException $exception) {
34 4638
            throw new PDOException($exception);
35
        }
36
    }
37
38
    /**
39 4626
     * {@inheritdoc}
40
     */
41
    public function exec(string $statement) : int
42 4626
    {
43 4624
        try {
44 4624
            return $this->connection->exec($statement);
45
        } catch (\PDOException $exception) {
46
            throw new PDOException($exception);
47
        }
48
    }
49
50
    /**
51 4156
     * {@inheritdoc}
52
     */
53 4156
    public function getServerVersion()
54
    {
55
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
56
    }
57
58
    /**
59 4529
     * {@inheritdoc}
60
     */
61
    public function prepare(string $sql) : Statement
62 4529
    {
63 4529
        try {
64
            return $this->createStatement(
65 4527
                $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

65
                /** @scrutinizer ignore-type */ $this->connection->prepare($sql)
Loading history...
66 4527
            );
67
        } catch (\PDOException $exception) {
68
            throw new PDOException($exception);
69
        }
70
    }
71
72
    /**
73 4604
     * {@inheritdoc}
74
     */
75
    public function query(string $sql) : ResultStatement
76 4604
    {
77 4466
        try {
78
            $stmt = $this->connection->query($sql);
79 4466
            assert($stmt instanceof \PDOStatement);
80 4600
81 4600
            return $this->createStatement($stmt);
82
        } catch (\PDOException $exception) {
83
            throw new PDOException($exception);
84
        }
85
    }
86
87
    /**
88 4230
     * {@inheritdoc}
89
     */
90 4230
    public function quote(string $input) : string
91
    {
92
        return $this->connection->quote($input);
93
    }
94
95
    /**
96 466
     * {@inheritdoc}
97
     */
98
    public function lastInsertId($name = null)
99 466
    {
100 154
        try {
101
            if ($name === null) {
102
                return $this->connection->lastInsertId();
103 312
            }
104
105
            return $this->connection->lastInsertId($name);
106
        } catch (\PDOException $exception) {
107
            throw new PDOException($exception);
108
        }
109
    }
110
111
    /**
112 4327
     * {@inheritdoc}
113
     */
114 4327
    public function requiresQueryForServerVersion()
115
    {
116
        return false;
117
    }
118
119
    /**
120 4234
     * Creates a wrapped statement
121
     */
122 4234
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
123
    {
124
        return new PDOStatement($stmt);
125
    }
126
127
    /**
128 4337
     * {@inheritDoc}
129
     */
130 4337
    public function beginTransaction() : void
131 4337
    {
132
        $this->connection->beginTransaction();
133
    }
134
135
    /**
136 4252
     * {@inheritDoc}
137
     */
138 4252
    public function commit() : void
139 4252
    {
140
        $this->connection->commit();
141
    }
142
143
    /**
144 4282
     * {@inheritDoc}
145
     */
146 4282
    public function rollBack() : void
147 4282
    {
148
        $this->connection->rollBack();
149
    }
150
151
    public function getWrappedConnection() : PDO
152
    {
153
        return $this->connection;
154
    }
155
}
156