1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\ParameterType; |
23
|
|
|
use PDO; |
24
|
|
|
use function func_get_args; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* PDO implementation of the Connection interface. |
28
|
|
|
* Used by all PDO-based drivers. |
29
|
|
|
* |
30
|
|
|
* @since 2.0 |
31
|
|
|
*/ |
32
|
|
|
class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection |
33
|
|
|
{ |
34
|
|
|
/** @var LastInsertId */ |
35
|
|
|
private $lastInsertId; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $dsn |
39
|
|
|
* @param string|null $user |
40
|
|
|
* @param string|null $password |
41
|
|
|
* @param array|null $options |
42
|
|
|
* |
43
|
47 |
|
* @throws PDOException in case of an error. |
44
|
|
|
*/ |
45
|
|
|
public function __construct($dsn, $user = null, $password = null, array $options = null) |
46
|
47 |
|
{ |
47
|
46 |
|
try { |
48
|
46 |
|
parent::__construct($dsn, $user, $password, $options); |
49
|
2 |
|
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['Doctrine\DBAL\Driver\PDOStatement', [$this]]); |
50
|
2 |
|
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
51
|
|
|
} catch (\PDOException $exception) { |
52
|
46 |
|
throw new PDOException($exception); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->lastInsertId = new LastInsertId(); |
56
|
|
|
} |
57
|
158 |
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
158 |
|
*/ |
61
|
93 |
|
public function exec($statement) |
62
|
93 |
|
{ |
63
|
|
|
try { |
64
|
|
|
$result = parent::exec($statement); |
65
|
|
|
} catch (\PDOException $exception) { |
66
|
|
|
throw new PDOException($exception); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->trackLastInsertId(); |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
132 |
|
public function getServerVersion() |
78
|
|
|
{ |
79
|
|
|
return PDO::getAttribute(PDO::ATTR_SERVER_VERSION); |
|
|
|
|
80
|
132 |
|
} |
81
|
3 |
|
|
82
|
3 |
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function prepare($prepareString, $driverOptions = []) |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
return parent::prepare($prepareString, $driverOptions); |
|
|
|
|
89
|
173 |
|
} catch (\PDOException $exception) { |
90
|
|
|
throw new PDOException($exception); |
91
|
173 |
|
} |
92
|
173 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
173 |
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function query() |
98
|
|
|
{ |
99
|
173 |
|
try { |
100
|
|
|
/** @var PDOStatement $stmt */ |
101
|
|
|
$stmt = parent::query(...func_get_args()); |
|
|
|
|
102
|
|
|
|
103
|
173 |
|
$this->trackLastInsertId(); |
104
|
|
|
|
105
|
|
|
return $stmt; |
106
|
|
|
} catch (\PDOException $exception) { |
107
|
173 |
|
throw new PDOException($exception); |
108
|
6 |
|
} |
109
|
6 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function quote($input, $type = ParameterType::STRING) |
115
|
|
|
{ |
116
|
4 |
|
return parent::quote($input, $type); |
117
|
|
|
} |
118
|
4 |
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function lastInsertId($name = null) |
123
|
|
|
{ |
124
|
2 |
|
if ($name === null) { |
125
|
|
|
return $this->lastInsertId->get(); |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
return $this->fetchLastInsertId($name); |
130
|
|
|
} catch (\PDOException $exception) { |
131
|
|
|
return '0'; |
132
|
1 |
|
} |
133
|
|
|
} |
134
|
1 |
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function requiresQueryForServerVersion() |
139
|
|
|
{ |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Tracks the last insert ID at the current state. |
145
|
|
|
* |
146
|
|
|
* If this PDO driver is not able to fetch the last insert ID for identity columns |
147
|
|
|
* without influencing connection state or transaction state, this is a noop method. |
148
|
|
|
* |
149
|
|
|
* @internal this method is only supposed to be used in DBAL internals |
150
|
|
|
* |
151
|
|
|
* @throws \PDOException |
152
|
|
|
*/ |
153
|
|
|
public function trackLastInsertId() : void |
154
|
|
|
{ |
155
|
|
|
// We need to avoid unnecessary exception generation for drivers not supporting this feature, |
156
|
|
|
// by temporarily disabling exception mode. |
157
|
|
|
$originalErrorMode = $this->getAttribute(\PDO::ATTR_ERRMODE); |
158
|
|
|
|
159
|
|
|
$this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); |
160
|
|
|
|
161
|
|
|
try { |
162
|
|
|
$lastInsertId = $this->fetchLastInsertId(null); |
163
|
|
|
} finally { |
164
|
|
|
// Reactivate exception mode. |
165
|
|
|
$this->setAttribute(\PDO::ATTR_ERRMODE, $originalErrorMode); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($lastInsertId === null) { |
|
|
|
|
169
|
|
|
// In case this driver implementation does not support this feature |
170
|
|
|
// or an error occurred while retrieving the last insert ID, there is nothing to track here. |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->lastInsertId->register($lastInsertId); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Fetches the last insert ID generated by this connection. |
179
|
|
|
* |
180
|
|
|
* This method queries the database connection for the last insert ID. |
181
|
|
|
* |
182
|
|
|
* @param string|null $sequenceName The name of the sequence to retrieve the last insert ID from, |
183
|
|
|
* if not given the overall last insert ID is returned. |
184
|
|
|
* |
185
|
|
|
* @return string The last insert ID or '0' in case the last insert ID generated on this connection is unknown. |
186
|
|
|
* |
187
|
|
|
* @throws \PDOException |
188
|
|
|
*/ |
189
|
|
|
protected function fetchLastInsertId(?string $sequenceName) : string |
190
|
|
|
{ |
191
|
|
|
return parent::lastInsertId($sequenceName); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|