|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Laminas\Db\Adapter\Mysql\Driver\Pdo; |
4 | 6 |
|
5 | | -use Laminas\Db\Adapter\Driver\PdoDriverInterface; |
| 7 | +use Laminas\Db\Adapter\Driver\Pdo\AbstractPdo; |
| 8 | +use Laminas\Db\Adapter\Driver\Pdo\Result; |
| 9 | +use Laminas\Db\Adapter\Driver\Pdo\Statement; |
6 | 10 | use Laminas\Db\Adapter\Driver\Feature\AbstractFeature; |
7 | | -use Laminas\Db\Adapter\Driver\Feature\DriverFeatureInterface; |
8 | 11 | use Laminas\Db\Adapter\Exception; |
9 | 12 | use Laminas\Db\Adapter\Profiler; |
10 | | -use PDOStatement; |
11 | 13 |
|
12 | | -use function extension_loaded; |
13 | 14 | use function is_array; |
14 | | -use function is_numeric; |
15 | | -use function is_string; |
16 | | -use function ltrim; |
17 | | -use function preg_match; |
18 | | -use function sprintf; |
19 | 15 | use function ucfirst; |
20 | 16 |
|
21 | | -class Pdo implements PdoDriverInterface, DriverFeatureInterface, Profiler\ProfilerAwareInterface |
| 17 | +class Pdo extends AbstractPdo |
22 | 18 | { |
23 | | - /** |
24 | | - * @const |
25 | | - */ |
26 | | - public const FEATURES_DEFAULT = 'default'; |
27 | | - |
28 | | - /** @var Connection */ |
29 | | - protected $connection; |
30 | | - |
31 | | - /** @var Statement */ |
32 | | - protected $statementPrototype; |
33 | | - |
34 | | - /** @var Result */ |
35 | | - protected $resultPrototype; |
36 | | - |
37 | | - /** @var array */ |
38 | | - protected $features = []; |
39 | | - |
40 | | - /** |
41 | | - * @internal |
42 | | - * |
43 | | - * @var Profiler\ProfilerInterface |
44 | | - */ |
45 | | - public $profiler; |
46 | | - |
47 | 19 | /** |
48 | 20 | * @param array|Connection|\PDO $connection |
49 | 21 | * @param string $features |
@@ -107,40 +79,6 @@ public function registerConnection(Connection $connection) |
107 | 79 | return $this; |
108 | 80 | } |
109 | 81 |
|
110 | | - /** |
111 | | - * Register statement prototype |
112 | | - */ |
113 | | - public function registerStatementPrototype(Statement $statementPrototype) |
114 | | - { |
115 | | - $this->statementPrototype = $statementPrototype; |
116 | | - $this->statementPrototype->setDriver($this); |
117 | | - } |
118 | | - |
119 | | - /** |
120 | | - * Register result prototype |
121 | | - */ |
122 | | - public function registerResultPrototype(Result $resultPrototype) |
123 | | - { |
124 | | - $this->resultPrototype = $resultPrototype; |
125 | | - } |
126 | | - |
127 | | - /** |
128 | | - * Add feature |
129 | | - * |
130 | | - * @param string $name |
131 | | - * @param AbstractFeature $feature |
132 | | - * @return $this Provides a fluent interface |
133 | | - */ |
134 | | - public function addFeature($name, $feature) |
135 | | - { |
136 | | - if ($feature instanceof AbstractFeature) { |
137 | | - $name = $feature->getName(); // overwrite the name, just in case |
138 | | - $feature->setDriver($this); |
139 | | - } |
140 | | - $this->features[$name] = $feature; |
141 | | - return $this; |
142 | | - } |
143 | | - |
144 | 82 | /** |
145 | 83 | * Setup the default features for Pdo |
146 | 84 | * |
@@ -171,150 +109,23 @@ public function getFeature($name) |
171 | 109 | * @param string $nameFormat |
172 | 110 | * @return string |
173 | 111 | */ |
174 | | - public function getDatabasePlatformName($nameFormat = self::NAME_FORMAT_CAMELCASE) |
| 112 | + public function getDatabasePlatformName($nameFormat = self::NAME_FORMAT_CAMELCASE): string |
175 | 113 | { |
176 | 114 | $name = $this->getConnection()->getDriverName(); |
177 | | - if ($nameFormat === self::NAME_FORMAT_CAMELCASE) { |
178 | | - switch ($name) { |
179 | | - case 'pgsql': |
180 | | - return 'Postgresql'; |
181 | | - case 'oci': |
182 | | - return 'Oracle'; |
183 | | - case 'dblib': |
184 | | - case 'sqlsrv': |
185 | | - return 'SqlServer'; |
186 | | - default: |
187 | | - return ucfirst($name); |
188 | | - } |
189 | | - } else { |
190 | | - switch ($name) { |
191 | | - case 'mysql': |
192 | | - return 'MySQL'; |
193 | | - default: |
194 | | - return ucfirst($name); |
195 | | - } |
196 | | - } |
197 | | - } |
198 | | - |
199 | | - /** |
200 | | - * Check environment |
201 | | - */ |
202 | | - public function checkEnvironment() |
203 | | - { |
204 | | - if (! extension_loaded('PDO')) { |
205 | | - throw new Exception\RuntimeException( |
206 | | - 'The PDO extension is required for this adapter but the extension is not loaded' |
207 | | - ); |
208 | | - } |
209 | | - } |
210 | 115 |
|
211 | | - /** |
212 | | - * @return Connection |
213 | | - */ |
214 | | - public function getConnection() |
215 | | - { |
216 | | - return $this->connection; |
217 | | - } |
218 | | - |
219 | | - /** |
220 | | - * @param string|PDOStatement $sqlOrResource |
221 | | - * @return Statement |
222 | | - */ |
223 | | - public function createStatement($sqlOrResource = null) |
224 | | - { |
225 | | - $statement = clone $this->statementPrototype; |
226 | | - if ($sqlOrResource instanceof PDOStatement) { |
227 | | - $statement->setResource($sqlOrResource); |
228 | | - } else { |
229 | | - if (is_string($sqlOrResource)) { |
230 | | - $statement->setSql($sqlOrResource); |
231 | | - } |
232 | | - if (! $this->connection->isConnected()) { |
233 | | - $this->connection->connect(); |
234 | | - } |
235 | | - $statement->initialize($this->connection->getResource()); |
| 116 | + if ($nameFormat === self::NAME_FORMAT_CAMELCASE) { |
| 117 | + return ucfirst($name); |
236 | 118 | } |
237 | | - return $statement; |
238 | | - } |
239 | | - |
240 | | - /** |
241 | | - * @param resource $resource |
242 | | - * @param mixed $context |
243 | | - * @return Result |
244 | | - */ |
245 | | - public function createResult($resource, $context = null) |
246 | | - { |
247 | | - $result = clone $this->resultPrototype; |
248 | | - $rowCount = null; |
249 | | - |
250 | | - // special feature, sqlite PDO counter |
251 | | - // if ( |
252 | | - // $this->connection->getDriverName() === 'sqlite' |
253 | | - // && ($sqliteRowCounter = $this->getFeature('SqliteRowCounter')) |
254 | | - // && $resource->columnCount() > 0 |
255 | | - // ) { |
256 | | - // $rowCount = $sqliteRowCounter->getRowCountClosure($context); |
257 | | - // } |
258 | | - |
259 | | - // // special feature, oracle PDO counter |
260 | | - // if ( |
261 | | - // $this->connection->getDriverName() === 'oci' |
262 | | - // && ($oracleRowCounter = $this->getFeature('OracleRowCounter')) |
263 | | - // && $resource->columnCount() > 0 |
264 | | - // ) { |
265 | | - // $rowCount = $oracleRowCounter->getRowCountClosure($context); |
266 | | - // } |
267 | | - |
268 | | - $result->initialize($resource, $this->connection->getLastGeneratedValue(), $rowCount); |
269 | | - return $result; |
270 | | - } |
271 | | - |
272 | | - /** |
273 | | - * @return Result |
274 | | - */ |
275 | | - public function getResultPrototype() |
276 | | - { |
277 | | - return $this->resultPrototype; |
278 | | - } |
279 | 119 |
|
280 | | - /** |
281 | | - * @return string |
282 | | - */ |
283 | | - public function getPrepareType() |
284 | | - { |
285 | | - return self::PARAMETERIZATION_NAMED; |
286 | | - } |
287 | | - |
288 | | - /** |
289 | | - * @param string $name |
290 | | - * @param string|null $type |
291 | | - * @return string |
292 | | - */ |
293 | | - public function formatParameterName($name, $type = null) |
294 | | - { |
295 | | - if ($type === null && ! is_numeric($name) || $type === self::PARAMETERIZATION_NAMED) { |
296 | | - $name = ltrim($name, ':'); |
297 | | - // @see https://bugs.php.net/bug.php?id=43130 |
298 | | - if (preg_match('/[^a-zA-Z0-9_]/', $name)) { |
299 | | - throw new Exception\RuntimeException(sprintf( |
300 | | - 'The PDO param %s contains invalid characters.' |
301 | | - . ' Only alphabetic characters, digits, and underscores (_)' |
302 | | - . ' are allowed.', |
303 | | - $name |
304 | | - )); |
305 | | - } |
306 | | - return ':' . $name; |
| 120 | + if ($nameFormat === self::NAME_FORMAT_NATURAL) { |
| 121 | + return match ($name) { |
| 122 | + 'mysql' => 'MySQL', |
| 123 | + default => ucfirst($name), |
| 124 | + }; |
307 | 125 | } |
308 | 126 |
|
309 | | - return '?'; |
310 | | - } |
311 | | - |
312 | | - /** |
313 | | - * @param string|null $name |
314 | | - * @return string|null|false |
315 | | - */ |
316 | | - public function getLastGeneratedValue($name = null) |
317 | | - { |
318 | | - return $this->connection->getLastGeneratedValue($name); |
| 127 | + throw new Exception\InvalidArgumentException( |
| 128 | + 'Invalid name format provided. Must be one of: ' . self::NAME_FORMAT_CAMELCASE . ', ' . self::NAME_FORMAT_NATURAL |
| 129 | + ); |
319 | 130 | } |
320 | 131 | } |
0 commit comments