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
|
4982 |
|
* |
25
|
|
|
* @throws PDOException In case of an error. |
26
|
|
|
*/ |
27
|
4982 |
|
public function __construct($dsn, $user = null, $password = null, ?array $options = null) |
28
|
4979 |
|
{ |
29
|
4979 |
|
try { |
30
|
4312 |
|
$this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options); |
31
|
4312 |
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
32
|
|
|
} catch (\PDOException $exception) { |
33
|
4979 |
|
throw new PDOException($exception); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
5098 |
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function exec(string $statement) : int |
41
|
5098 |
|
{ |
42
|
5050 |
|
try { |
43
|
5050 |
|
return $this->connection->exec($statement); |
44
|
|
|
} catch (\PDOException $exception) { |
45
|
|
|
throw new PDOException($exception); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
4456 |
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
4456 |
|
public function getServerVersion() |
53
|
|
|
{ |
54
|
|
|
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
4952 |
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function prepare(string $sql) : Statement |
61
|
4952 |
|
{ |
62
|
4818 |
|
try { |
63
|
4818 |
|
return $this->createStatement( |
64
|
|
|
$this->connection->prepare($sql) |
|
|
|
|
65
|
|
|
); |
66
|
|
|
} catch (\PDOException $exception) { |
67
|
|
|
throw new PDOException($exception); |
68
|
|
|
} |
69
|
|
|
} |
70
|
5112 |
|
|
71
|
|
|
/** |
72
|
5112 |
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function query(string $sql) : ResultStatement |
75
|
5112 |
|
{ |
76
|
4960 |
|
try { |
77
|
|
|
$stmt = $this->connection->query($sql); |
78
|
4960 |
|
assert($stmt instanceof \PDOStatement); |
79
|
4899 |
|
|
80
|
4899 |
|
return $this->createStatement($stmt); |
81
|
|
|
} catch (\PDOException $exception) { |
82
|
|
|
throw new PDOException($exception); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
4490 |
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
4490 |
|
public function quote($input, $type = ParameterType::STRING) |
90
|
|
|
{ |
91
|
|
|
return $this->connection->quote($input, $type); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
487 |
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function lastInsertId($name = null) |
98
|
487 |
|
{ |
99
|
145 |
|
try { |
100
|
|
|
if ($name === null) { |
101
|
|
|
return $this->connection->lastInsertId(); |
102
|
342 |
|
} |
103
|
|
|
|
104
|
|
|
return $this->connection->lastInsertId($name); |
105
|
|
|
} catch (\PDOException $exception) { |
106
|
|
|
throw new PDOException($exception); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
4638 |
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
4638 |
|
public function requiresQueryForServerVersion() |
114
|
|
|
{ |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Creates a wrapped statement |
120
|
|
|
*/ |
121
|
|
|
protected function createStatement(\PDOStatement $stmt) : PDOStatement |
122
|
|
|
{ |
123
|
|
|
return new PDOStatement($stmt); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
|
|
public function beginTransaction() |
130
|
|
|
{ |
131
|
|
|
return $this->connection->beginTransaction(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
|
|
public function commit() |
138
|
|
|
{ |
139
|
|
|
return $this->connection->commit(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
|
|
public function rollBack() |
146
|
|
|
{ |
147
|
|
|
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
|
|
|
public function getWrappedConnection() : PDO |
167
|
|
|
{ |
168
|
|
|
return $this->connection; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|