Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions src/Adapter/Driver/Pdo/AbstractPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Override;
use PDO;
use PDOStatement;
use PhpDb\Adapter\Driver\ConnectionInterface;
use PhpDb\Adapter\Driver\Feature\DriverFeatureProviderInterface;
use PhpDb\Adapter\Driver\PdoConnectionInterface;
use PhpDb\Adapter\Driver\PdoDriverAwareInterface;
use PhpDb\Adapter\Driver\PdoDriverInterface;
use PhpDb\Adapter\Driver\ResultInterface;
Expand All @@ -27,26 +26,14 @@

abstract class AbstractPdo implements PdoDriverInterface, ProfilerAwareInterface
{
/** @internal */
protected ?ProfilerInterface $profiler;
protected (PdoConnectionInterface&PdoDriverAwareInterface)|PDO $connection;

public function __construct(
protected AbstractPdoConnection|PDO $connection,
protected StatementInterface&PdoDriverAwareInterface $statementPrototype,
protected ResultInterface $resultPrototype,
array $features = [],
) {
if ($this->connection instanceof PdoDriverAwareInterface) {
$this->connection->setDriver($this);
}
protected StatementInterface&PdoDriverAwareInterface $statementPrototype;

$this->statementPrototype->setDriver($this);
protected ResultInterface $resultPrototype;

// $features is not constructor promoted because $this->features is defined in the trait
if ($features !== [] && $this instanceof DriverFeatureProviderInterface) {
$this->addFeatures($features);
}
}
/** @internal */
protected ?ProfilerInterface $profiler;

#[Override]
public function setProfiler(ProfilerInterface $profiler): ProfilerAwareInterface
Expand Down Expand Up @@ -81,7 +68,7 @@ public function checkEnvironment(): bool
}

#[Override]
public function getConnection(): ConnectionInterface
public function getConnection(): PdoConnectionInterface
{
return $this->connection;
}
Expand Down
16 changes: 0 additions & 16 deletions src/Adapter/Driver/Pdo/AbstractPdoConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PhpDb\Adapter\Exception;
use PhpDb\Adapter\Exception\RuntimeException;

use function is_array;
use function strtolower;

abstract class AbstractPdoConnection extends AbstractConnection implements
Expand All @@ -30,21 +29,6 @@ abstract class AbstractPdoConnection extends AbstractConnection implements
/** @var ?PDO $resource */
protected $resource;

/**
* Constructor
*
* @throws Exception\InvalidArgumentException
*/
public function __construct(
PDO|array $connectionParameters
) {
if (is_array($connectionParameters)) {
$this->setConnectionParameters($connectionParameters);
} elseif ($connectionParameters instanceof PDO) {
$this->setResource($connectionParameters);
}
}

#[Override]
public function setDriver(PdoDriverInterface $driver): PdoDriverAwareInterface
{
Expand Down
20 changes: 7 additions & 13 deletions src/Adapter/Driver/Pdo/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Statement implements StatementInterface, PdoDriverAwareInterface, Profiler
protected bool $isPrepared = false;

public function __construct(
protected ?ParameterContainer $parameterContainer = null,
protected ParameterContainer $parameterContainer = new ParameterContainer(),
protected array $options = [],
) {
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public function setParameterContainer(ParameterContainer $parameterContainer): S
#[Override]
public function getParameterContainer(): ParameterContainer
{
return $this->parameterContainer ??= new ParameterContainer();
return $this->parameterContainer;
}

/** @throws Exception\RuntimeException */
Expand Down Expand Up @@ -156,13 +156,9 @@ public function execute(null|array|ParameterContainer $parameters = null): ?Resu
}

/** START Standard ParameterContainer Merging Block */
if (! $this->parameterContainer instanceof ParameterContainer) {
if ($parameters instanceof ParameterContainer) {
if ($parameters instanceof ParameterContainer) {
$this->parameterContainer = $parameters;
$parameters = null;
} else {
$this->parameterContainer = new ParameterContainer();
}
}

if (is_array($parameters)) {
Expand Down Expand Up @@ -247,11 +243,9 @@ protected function bindParametersFromContainer(): void
/** Perform a deep clone */
public function __clone(): void
{
$this->isPrepared = false;
$this->parametersBound = false;
$this->resource = null;
if ($this->parameterContainer) {
$this->parameterContainer = clone $this->parameterContainer;
}
$this->isPrepared = false;
$this->parametersBound = false;
$this->resource = null;
$this->parameterContainer = clone $this->parameterContainer;
}
}
2 changes: 1 addition & 1 deletion src/Adapter/Driver/PdoConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PhpDb\Adapter\Driver;

interface PdoConnectionInterface
interface PdoConnectionInterface extends ConnectionInterface
{
public function getDsn(): string;
}
10 changes: 10 additions & 0 deletions test/unit/Adapter/Driver/Pdo/TestAsset/TestConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@
use PhpDb\Adapter\Driver\ConnectionInterface;
use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection;

use function is_array;
use function sprintf;

/**
* Test asset for AbstractPdoConnection - provides a concrete implementation for testing
*/
final class TestConnection extends AbstractPdoConnection
{
public function __construct(PDO|array $connectionParameters)
{
if (is_array($connectionParameters)) {
$this->setConnectionParameters($connectionParameters);
} elseif ($connectionParameters instanceof PDO) {
$this->setResource($connectionParameters);
}
}

#[Override]
public function connect(): ConnectionInterface
{
Expand Down
27 changes: 20 additions & 7 deletions test/unit/Adapter/Driver/Pdo/TestAsset/TestPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Override;
use PDO;
use PhpDb\Adapter\Driver\Feature\DriverFeatureProviderInterface;
use PhpDb\Adapter\Driver\Feature\DriverFeatureProviderTrait;
use PhpDb\Adapter\Driver\Pdo\AbstractPdo;
use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection;
use PhpDb\Adapter\Driver\Pdo\Result;
Expand All @@ -16,8 +18,10 @@
/**
* Test asset for AbstractPdo - provides a concrete implementation for testing
*/
final class TestPdo extends AbstractPdo
final class TestPdo extends AbstractPdo implements DriverFeatureProviderInterface
{
use DriverFeatureProviderTrait;

public function __construct(
array|AbstractPdoConnection|PDO $connection,
?Statement $statement = null,
Expand All @@ -28,12 +32,20 @@ public function __construct(
$connection = new TestConnection($connection);
}

parent::__construct(
$connection,
$statement ?? new Statement(),
$result ?? new Result(),
$features
);
$this->connection = $connection;
$this->statementPrototype = $statement ?? new Statement();
$this->resultPrototype = $result ?? new Result();

if (! $this->connection instanceof PDO) {
$this->connection->setDriver($this);
}

$this->statementPrototype->setDriver($this);

// $features is not constructor promoted because $this->features is defined in the trait
if ($features !== []) {
$this->addFeatures($features);
}
}

/**
Expand Down Expand Up @@ -65,6 +77,7 @@ public function getDatabasePlatformName(string $nameFormat = self::NAME_FORMAT_C
$pdoDriver = $this->connection->getResource()->getAttribute(PDO::ATTR_DRIVER_NAME);
}

// todo: None of this belongs here
Comment thread
tyrsson marked this conversation as resolved.
Outdated
return match ($nameFormat) {
self::NAME_FORMAT_CAMELCASE => match ($pdoDriver) {
'sqlsrv', 'dblib', 'mssql' => 'SqlServer',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/TestAsset/ConnectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ConnectionWrapper extends AbstractPdoConnection
public function __construct(
PDO $connectionParameters = new PdoStubDriver()
) {
parent::__construct($connectionParameters);
$this->setResource($connectionParameters);
}

public function connect(): ConnectionInterface
Expand Down