Failed Conditions
Pull Request — develop (#3335)
by
unknown
12:27
created

PDOConnection::exec()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 908
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
26
    {
27
        try {
28 908
            $this->connection = new PDO($dsn, $user, $password, $options);
29 877
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30 58
        } catch (\PDOException $exception) {
31 58
            throw new PDOException($exception);
32
        }
33 877
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2778
    public function exec(string $statement) : int
39
    {
40
        try {
41 2778
            return $this->connection->exec($statement);
42 1737
        } catch (\PDOException $exception) {
43 1737
            throw new PDOException($exception);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 355
    public function getServerVersion()
51
    {
52 355
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2157
    public function prepare(string $sql) : Statement
59
    {
60
        try {
61 2157
            return $this->createStatement(
62 2157
                $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 82
    public function lastInsertId(?string $name = null) : string
95
    {
96 82
        return $this->connection->lastInsertId($name);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 369
    public function requiresQueryForServerVersion()
103
    {
104 369
        return false;
105
    }
106
107
    /**
108
     * Creates a wrapped statement
109
     */
110 3671
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
111
    {
112 3671
        return new PDOStatement($stmt);
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 237
    public function beginTransaction() : void
119
    {
120 237
        $this->connection->beginTransaction();
121 237
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126 88
    public function commit() : void
127
    {
128 88
        $this->connection->commit();
129 88
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 139
    public function rollBack() : void
135
    {
136 139
        $this->connection->rollBack();
137 139
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142
    public function errorCode()
143
    {
144
        return $this->connection->errorCode();
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150
    public function errorInfo()
151
    {
152
        return $this->connection->errorInfo();
153
    }
154
155 709
    public function getWrappedConnection() : PDO
156
    {
157 709
        return $this->connection;
158
    }
159
}
160