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 DriverException In case of an error. |
24
|
|
|
*/ |
25
|
922 |
|
public function __construct($dsn, $user = null, $password = null, ?array $options = null) |
26
|
|
|
{ |
27
|
|
|
try { |
28
|
922 |
|
$this->connection = new PDO($dsn, $user, $password, $options); |
29
|
891 |
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
30
|
58 |
|
} catch (\PDOException $exception) { |
31
|
58 |
|
throw self::exceptionFromPDOException($exception); |
32
|
|
|
} |
33
|
891 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
2812 |
|
public function exec(string $statement) : int |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
2812 |
|
return $this->connection->exec($statement); |
42
|
1771 |
|
} catch (\PDOException $exception) { |
43
|
1771 |
|
throw self::exceptionFromPDOException($exception); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
368 |
|
public function getServerVersion() |
51
|
|
|
{ |
52
|
368 |
|
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
2164 |
|
public function prepare(string $sql) : Statement |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
2164 |
|
return $this->createStatement( |
62
|
2164 |
|
$this->connection->prepare($sql) |
|
|
|
|
63
|
|
|
); |
64
|
31 |
|
} catch (\PDOException $exception) { |
65
|
31 |
|
throw self::exceptionFromPDOException($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) |
|
|
|
|
77
|
|
|
); |
78
|
99 |
|
} catch (\PDOException $exception) { |
79
|
99 |
|
throw self::exceptionFromPDOException($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
|
114 |
|
public function lastInsertId(?string $name = null) : string |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
114 |
|
$lastInsertId = $this->connection->lastInsertId($name); |
98
|
12 |
|
} catch (\PDOException $e) { |
99
|
12 |
|
throw self::exceptionFromPDOException($e); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// pdo_mysql and others return '0', pdo_sqlsrv returns '' |
103
|
102 |
|
if ($lastInsertId === '0' || $lastInsertId === '') { |
104
|
14 |
|
throw DriverException::noInsertId(); |
105
|
|
|
} |
106
|
|
|
|
107
|
88 |
|
return $lastInsertId; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
382 |
|
public function requiresQueryForServerVersion() |
114
|
|
|
{ |
115
|
382 |
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Creates a wrapped statement |
120
|
|
|
*/ |
121
|
3677 |
|
protected function createStatement(\PDOStatement $stmt) : PDOStatement |
122
|
|
|
{ |
123
|
3677 |
|
return new PDOStatement($stmt); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
237 |
|
public function beginTransaction() : void |
130
|
|
|
{ |
131
|
237 |
|
$this->connection->beginTransaction(); |
132
|
237 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
88 |
|
public function commit() : void |
138
|
|
|
{ |
139
|
88 |
|
$this->connection->commit(); |
140
|
88 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
139 |
|
public function rollBack() : void |
146
|
|
|
{ |
147
|
139 |
|
$this->connection->rollBack(); |
148
|
139 |
|
} |
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
|
716 |
|
public function getWrappedConnection() : PDO |
167
|
|
|
{ |
168
|
716 |
|
return $this->connection; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Creates a DriverException from a PDOException. |
173
|
|
|
* |
174
|
|
|
* @param \PDOException $exception The PDO exception to wrap. |
175
|
|
|
* |
176
|
|
|
* @return DriverException |
177
|
|
|
*/ |
178
|
2050 |
|
public static function exceptionFromPDOException(\PDOException $exception) : DriverException |
179
|
|
|
{ |
180
|
2050 |
|
return new DriverException( |
181
|
2050 |
|
$exception->getMessage(), |
182
|
2050 |
|
$exception->errorInfo[0] ?? $exception->getCode(), |
183
|
2050 |
|
$exception->errorInfo[1] ?? $exception->getCode(), |
184
|
2050 |
|
$exception |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|