Passed
Pull Request — develop (#3453)
by Evgeniy
19:01
created

PDOConnection::requiresQueryForServerVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Driver;
4
5
use Doctrine\DBAL\ParameterType;
6
use PDO;
7
use function assert;
8
9
/**
10
 * PDO implementation of the Connection interface.
11
 *
12
 * Used by all PDO-based drivers.
13
 */
14
class PDOConnection implements Connection, ServerInfoAwareConnection
15
{
16
    /** @var PDO */
17
    private $connection;
18
19
    /**
20
     * @param string       $dsn
21
     * @param string|null  $user
22
     * @param string|null  $password
23
     * @param mixed[]|null $options
24
     *
25
     * @throws PDOException In case of an error.
26
     */
27 1184
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
28
    {
29
        try {
30 1184
            $this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options);
31 1146
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32 73
        } catch (\PDOException $exception) {
33 73
            throw new PDOException($exception);
34
        }
35 1146
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 3683
    public function exec(string $statement) : int
41
    {
42
        try {
43 3683
            return $this->connection->exec($statement);
44 2093
        } catch (\PDOException $exception) {
45 2093
            throw new PDOException($exception);
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 478
    public function getServerVersion()
53
    {
54 478
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2793
    public function prepare(string $sql) : Statement
61
    {
62
        try {
63 2793
            return $this->createStatement(
64 2793
                $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

64
                /** @scrutinizer ignore-type */ $this->connection->prepare($sql)
Loading history...
65
            );
66 38
        } catch (\PDOException $exception) {
67 38
            throw new PDOException($exception);
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 3830
    public function query(string $sql) : ResultStatement
75
    {
76
        try {
77 3830
            $stmt = $this->connection->query($sql);
78 3706
            assert($stmt instanceof \PDOStatement);
79
80 3706
            return $this->createStatement($stmt);
81 124
        } catch (\PDOException $exception) {
82 124
            throw new PDOException($exception);
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 171
    public function quote($input, $type = ParameterType::STRING)
90
    {
91 171
        return $this->connection->quote($input, $type);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 108
    public function lastInsertId($name = null)
98
    {
99
        try {
100 108
            if ($name === null) {
101 20
                return $this->connection->lastInsertId();
102
            }
103
104 88
            return $this->connection->lastInsertId($name);
105
        } catch (\PDOException $exception) {
106
            throw new PDOException($exception);
107
        }
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 496
    public function requiresQueryForServerVersion()
114
    {
115 496
        return false;
116
    }
117
118
    /**
119
     * Creates a wrapped statement
120
     */
121 4640
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
122
    {
123 4640
        return new PDOStatement($stmt);
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129 304
    public function beginTransaction()
130
    {
131 304
        return $this->connection->beginTransaction();
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137 113
    public function commit()
138
    {
139 113
        return $this->connection->commit();
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145 179
    public function rollBack()
146
    {
147 179
        return $this->connection->rollBack();
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function errorCode()
154
    {
155
        return $this->connection->errorCode();
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function errorInfo()
162
    {
163
        return $this->connection->errorInfo();
164
    }
165
166 916
    public function getWrappedConnection() : PDO
167
    {
168 916
        return $this->connection;
169
    }
170
}
171