Skip to content

Commit d695c12

Browse files
tyrssonsimon-mundy
andcommitted
Completes code move to this satellite. (does not include working unit/integration testing yet)
Co-authored-by: Simon Mundy <simon.mundy@peptolab.com> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent d3ac706 commit d695c12

40 files changed

+3390
-795
lines changed

src/Adapter.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace Laminas\Db\Adapter\Mysql;
44

5-
use Laminas\Db\Adapter\Driver\DriverInterface;
6-
use Laminas\Db\Adapter\Platform\PlatformInterface;
75
use Laminas\Db\Adapter\AbstractAdapter;
6+
use Laminas\Db\Adapter\Driver\DriverInterface;
87
use Laminas\Db\Adapter\Exception;
8+
use Laminas\Db\Adapter\Platform\PlatformInterface;
99

1010
use function is_string;
1111
use function str_starts_with;
1212
use function strtolower;
1313

1414
/**
15-
* @property Driver\DriverInterface $driver
16-
* @property Platform\PlatformInterface $platform
15+
* @property DriverInterface $driver
16+
* @property PlatformInterface $platform
1717
*/
1818
class Adapter extends AbstractAdapter
1919
{
@@ -30,13 +30,16 @@ protected function createDriver(array $parameters): DriverInterface
3030
);
3131
}
3232

33-
if ($parameters['driver'] instanceof DriverInterface) {
33+
if ($parameters['driver'] instanceof Driver\Mysqli\Mysqli || $parameters['driver'] instanceof Driver\Pdo\Pdo) {
3434
return $parameters['driver'];
3535
}
3636

3737
if (! is_string($parameters['driver'])) {
3838
throw new Exception\InvalidArgumentException(
39-
__FUNCTION__ . ' expects a "driver" to be a string or instance of DriverInterface'
39+
__FUNCTION__
40+
. ' expects a "driver" to be a string or instance of '
41+
. Driver\Mysqli\Mysqli::class
42+
. ' or ' . Driver\Pdo\Pdo::class
4043
);
4144
}
4245

@@ -67,19 +70,10 @@ protected function createDriver(array $parameters): DriverInterface
6770

6871
protected function createPlatform(array $parameters): PlatformInterface
6972
{
70-
if (isset($parameters['platform'])) {
71-
$platformName = $parameters['platform'];
72-
} elseif ($this->driver instanceof DriverInterface) {
73-
$platformName = $this->driver->getDatabasePlatformName();
74-
} else {
75-
throw new Exception\InvalidArgumentException(
76-
'A platform could not be determined from the provided configuration'
77-
);
78-
}
79-
73+
$platformName = $parameters['platform'] ?? $this->driver->getDatabasePlatformName();
8074
// currently only supported by the IbmDb2 & Oracle concrete implementations
81-
//$options = $parameters['platform_options'] ?? [];
82-
75+
// todo: check recent versions of mysqli and pdo to see if they support this
76+
$options = $parameters['platform_options'] ?? [];
8377
// mysqli or pdo_mysql driver
8478
if ($this->driver instanceof Driver\Mysqli\Mysqli || $this->driver instanceof Driver\Pdo\Pdo) {
8579
$driver = $this->driver;

src/Driver/Mysqli/Connection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Laminas\Db\Adapter\Mysql\Driver\Mysqli;
46

57
use Exception as GenericException;

src/Driver/Mysqli/Mysqli.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Laminas\Db\Adapter\Mysql\Driver\Mysqli;
46

57
use Laminas\Db\Adapter\Driver\DriverInterface;

src/Driver/Mysqli/Result.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Laminas\Db\Adapter\Mysql\Driver\Mysqli;
46

57
use Iterator;

src/Driver/Mysqli/Statement.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Laminas\Db\Adapter\Mysql\Driver\Mysqli;
46

57
use Laminas\Db\Adapter\Driver\StatementInterface;

src/Driver/Pdo/Connection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Laminas\Db\Adapter\Mysql\Driver\Pdo;
46

57
use Laminas\Db\Adapter\Driver\AbstractConnection;

src/Driver/Pdo/Pdo.php

Lines changed: 17 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,21 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Laminas\Db\Adapter\Mysql\Driver\Pdo;
46

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;
610
use Laminas\Db\Adapter\Driver\Feature\AbstractFeature;
7-
use Laminas\Db\Adapter\Driver\Feature\DriverFeatureInterface;
811
use Laminas\Db\Adapter\Exception;
912
use Laminas\Db\Adapter\Profiler;
10-
use PDOStatement;
1113

12-
use function extension_loaded;
1314
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;
1915
use function ucfirst;
2016

21-
class Pdo implements PdoDriverInterface, DriverFeatureInterface, Profiler\ProfilerAwareInterface
17+
class Pdo extends AbstractPdo
2218
{
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-
4719
/**
4820
* @param array|Connection|\PDO $connection
4921
* @param string $features
@@ -107,40 +79,6 @@ public function registerConnection(Connection $connection)
10779
return $this;
10880
}
10981

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-
14482
/**
14583
* Setup the default features for Pdo
14684
*
@@ -171,150 +109,23 @@ public function getFeature($name)
171109
* @param string $nameFormat
172110
* @return string
173111
*/
174-
public function getDatabasePlatformName($nameFormat = self::NAME_FORMAT_CAMELCASE)
112+
public function getDatabasePlatformName($nameFormat = self::NAME_FORMAT_CAMELCASE): string
175113
{
176114
$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-
}
210115

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);
236118
}
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-
}
279119

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+
};
307125
}
308126

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+
);
319130
}
320131
}

0 commit comments

Comments
 (0)