Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide abstract middleware classes #5035

Merged
merged 1 commit into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Contributor

@mvorisek mvorisek Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this property be protected or getter to be added?

Otherwise how are supposed the public/middleware methods to be overriden?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it shouldn't.

You'll likely need to override the constructor anyway, so you can create your own private property for the wrapped driver if you really need it. Delegating a call to the wrapped driver can be done my calling the parent method, for example:

public function connect(array $params)
{
$this->logger->info('Connecting with parameters {params}', ['params' => $this->maskPassword($params)]);
return new Connection(
parent::connect($params),
$this->logger
);
}


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