Skip to content

Commit

Permalink
Provide abstract middleware classes
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Nov 27, 2021
1 parent bb13b5f commit 09a087d
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 262 deletions.
97 changes: 97 additions & 0 deletions src/Driver/Middleware/AbstractConnectionMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Doctrine\DBAL\Driver\Middleware;

use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\ParameterType;
use Doctrine\Deprecations\Deprecation;
use LogicException;

abstract class AbstractConnectionMiddleware implements ServerInfoAwareConnection
{
/** @var Connection */
private $wrappedConnection;

public function __construct(Connection $wrappedConnection)
{
$this->wrappedConnection = $wrappedConnection;
}

public function prepare(string $sql): Statement
{
return $this->wrappedConnection->prepare($sql);
}

public function query(string $sql): Result
{
return $this->wrappedConnection->query($sql);
}

/**
* {@inheritdoc}
*/
public function quote($value, $type = ParameterType::STRING)
{
return $this->wrappedConnection->quote($value, $type);
}

public function exec(string $sql): int
{
return $this->wrappedConnection->exec($sql);
}

/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
{
if ($name !== null) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);
}

return $this->wrappedConnection->lastInsertId($name);
}

/**
* {@inheritdoc}
*/
public function beginTransaction()
{
return $this->wrappedConnection->beginTransaction();
}

/**
* {@inheritdoc}
*/
public function commit()
{
return $this->wrappedConnection->commit();
}

/**
* {@inheritdoc}
*/
public function rollBack()
{
return $this->wrappedConnection->rollBack();
}

/**
* {@inheritdoc}
*/
public function getServerVersion()
{
if (! $this->wrappedConnection instanceof ServerInfoAwareConnection) {
throw new LogicException('The underlying connection is not a ServerInfoAwareConnection');
}

return $this->wrappedConnection->getServerVersion();
}
}
61 changes: 61 additions & 0 deletions src/Driver/Middleware/AbstractDriverMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Doctrine\DBAL\Driver\Middleware;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\VersionAwarePlatformDriver;

abstract class AbstractDriverMiddleware implements VersionAwarePlatformDriver
{
/** @var Driver */
private $wrappedDriver;

public function __construct(Driver $wrappedDriver)
{
$this->wrappedDriver = $wrappedDriver;
}

/**
* {@inheritdoc}
*/
public function connect(array $params)
{
return $this->wrappedDriver->connect($params);
}

/**
* {@inheritdoc}
*/
public function getDatabasePlatform()
{
return $this->wrappedDriver->getDatabasePlatform();
}

/**
* {@inheritdoc}
*/
public function getSchemaManager(Connection $conn, AbstractPlatform $platform)
{
return $this->wrappedDriver->getSchemaManager($conn, $platform);
}

public function getExceptionConverter(): ExceptionConverter
{
return $this->wrappedDriver->getExceptionConverter();
}

/**
* {@inheritdoc}
*/
public function createDatabasePlatformForVersion($version)
{
if ($this->wrappedDriver instanceof VersionAwarePlatformDriver) {
return $this->wrappedDriver->createDatabasePlatformForVersion($version);
}

return $this->wrappedDriver->getDatabasePlatform();
}
}
79 changes: 79 additions & 0 deletions src/Driver/Middleware/AbstractResultMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Doctrine\DBAL\Driver\Middleware;

use Doctrine\DBAL\Driver\Result;

abstract class AbstractResultMiddleware implements Result
{
/** @var Result */
private $wrappedResult;

public function __construct(Result $result)
{
$this->wrappedResult = $result;
}

/**
* {@inheritdoc}
*/
public function fetchNumeric()
{
return $this->wrappedResult->fetchNumeric();
}

/**
* {@inheritdoc}
*/
public function fetchAssociative()
{
return $this->wrappedResult->fetchAssociative();
}

/**
* {@inheritdoc}
*/
public function fetchOne()
{
return $this->wrappedResult->fetchOne();
}

/**
* {@inheritdoc}
*/
public function fetchAllNumeric(): array
{
return $this->wrappedResult->fetchAllNumeric();
}

/**
* {@inheritdoc}
*/
public function fetchAllAssociative(): array
{
return $this->wrappedResult->fetchAllAssociative();
}

/**
* {@inheritdoc}
*/
public function fetchFirstColumn(): array
{
return $this->wrappedResult->fetchFirstColumn();
}

public function rowCount(): int
{
return $this->wrappedResult->rowCount();
}

public function columnCount(): int
{
return $this->wrappedResult->columnCount();
}

public function free(): void
{
$this->wrappedResult->free();
}
}
42 changes: 42 additions & 0 deletions src/Driver/Middleware/AbstractStatementMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Doctrine\DBAL\Driver\Middleware;

use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\ParameterType;

abstract class AbstractStatementMiddleware implements Statement
{
/** @var Statement */
private $wrappedStatement;

public function __construct(Statement $wrappedStatement)
{
$this->wrappedStatement = $wrappedStatement;
}

/**
* {@inheritdoc}
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
return $this->wrappedStatement->bindValue($param, $value, $type);
}

/**
* {@inheritdoc}
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
{
return $this->wrappedStatement->bindParam($param, $variable, $type, $length);
}

/**
* {@inheritdoc}
*/
public function execute($params = null): Result
{
return $this->wrappedStatement->execute($params);
}
}

0 comments on commit 09a087d

Please sign in to comment.