Completed
Push — master ( cc3868...bfc8bb )
by Marco
21s queued 15s
created

PDOConnection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 18
eloc 34
dl 0
loc 128
ccs 42
cts 44
cp 0.9545
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getServerVersion() 0 3 1
A exec() 0 6 2
A beginTransaction() 0 3 1
A commit() 0 3 1
A rollBack() 0 3 1
A createStatement() 0 3 1
A quote() 0 3 1
A prepare() 0 8 2
A __construct() 0 7 2
A lastInsertId() 0 10 3
A query() 0 9 2
A getWrappedConnection() 0 3 1
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
     */
25 933
    public function __construct(string $dsn, string $username = '', string $password = '', array $options = [])
26
    {
27
        try {
28 933
            $this->connection = new PDO($dsn, $username, $password, $options);
29 878
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30 69
        } catch (\PDOException $exception) {
31 69
            throw new PDOException($exception);
32
        }
33 878
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3414
    public function exec(string $statement) : int
39
    {
40
        try {
41 3414
            return $this->connection->exec($statement);
42 1707
        } catch (\PDOException $exception) {
43 1707
            throw new PDOException($exception);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 325
    public function getServerVersion() : string
51
    {
52 325
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3482
    public function prepare(string $sql) : Statement
59
    {
60
        try {
61 3482
            return $this->createStatement(
62 3482
                $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 37
        } catch (\PDOException $exception) {
65 37
            throw new PDOException($exception);
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4069
    public function query(string $sql) : ResultStatement
73
    {
74
        try {
75 4069
            $stmt = $this->connection->query($sql);
76 3952
            assert($stmt instanceof \PDOStatement);
77
78 3952
            return $this->createStatement($stmt);
79 117
        } catch (\PDOException $exception) {
80 117
            throw new PDOException($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 77
    public function lastInsertId(?string $name = null) : string
96
    {
97
        try {
98 77
            if ($name === null) {
99 22
                return $this->connection->lastInsertId();
100
            }
101
102 55
            return $this->connection->lastInsertId($name);
103
        } catch (\PDOException $exception) {
104
            throw new PDOException($exception);
105
        }
106
    }
107
108
    /**
109
     * Creates a wrapped statement
110
     */
111 5713
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
112
    {
113 5713
        return new PDOStatement($stmt);
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119 279
    public function beginTransaction() : void
120
    {
121 279
        $this->connection->beginTransaction();
122 279
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127 110
    public function commit() : void
128
    {
129 110
        $this->connection->commit();
130 110
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 185
    public function rollBack() : void
136
    {
137 185
        $this->connection->rollBack();
138 185
    }
139
140 555
    public function getWrappedConnection() : PDO
141
    {
142 555
        return $this->connection;
143
    }
144
}
145