Skip to content

Commit

Permalink
feature: add support for converting exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bendavies committed Jul 28, 2023
1 parent a9ff7d7 commit c53e3e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
28 changes: 24 additions & 4 deletions src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace DAMA\DoctrineTestBundle\Doctrine\DBAL;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query;

/**
* Wraps a real connection and makes sure the initial nested transaction is using a savepoint.
Expand All @@ -13,6 +15,11 @@ class StaticConnection extends AbstractConnectionMiddleware
{
private const SAVEPOINT_NAME = 'DAMA_TEST';

/**
* @var Driver
*/
protected $driver;

/**
* @var Connection
*/
Expand All @@ -28,9 +35,10 @@ class StaticConnection extends AbstractConnectionMiddleware
*/
private $nested = false;

public function __construct(Connection $connection, AbstractPlatform $platform)
public function __construct(Driver $driver, Connection $connection, AbstractPlatform $platform)
{
parent::__construct($connection);
$this->driver = $driver;
$this->connection = $connection;
$this->platform = $platform;
}
Expand All @@ -41,7 +49,11 @@ public function beginTransaction(): bool
throw new \BadMethodCallException(sprintf('Bad call to "%s". A savepoint is already in use for a nested transaction.', __METHOD__));
}

$this->exec($this->platform->createSavePoint(self::SAVEPOINT_NAME));
try {
$this->exec($sql = $this->platform->createSavePoint(self::SAVEPOINT_NAME));
} catch (Driver\Exception $e) {
throw $this->driver->getExceptionConverter()->convert($e, new Query($sql, [], []));
}

$this->nested = true;

Expand All @@ -54,7 +66,11 @@ public function commit(): bool
throw new \BadMethodCallException(sprintf('Bad call to "%s". There is no savepoint for a nested transaction.', __METHOD__));
}

$this->exec($this->platform->releaseSavePoint(self::SAVEPOINT_NAME));
try {
$this->exec($sql = $this->platform->releaseSavePoint(self::SAVEPOINT_NAME));
} catch (Driver\Exception $e) {
throw $this->driver->getExceptionConverter()->convert($e, new Query($sql, [], []));
}

$this->nested = false;

Expand All @@ -67,7 +83,11 @@ public function rollBack(): bool
throw new \BadMethodCallException(sprintf('Bad call to "%s". There is no savepoint for a nested transaction.', __METHOD__));
}

$this->exec($this->platform->rollbackSavePoint(self::SAVEPOINT_NAME));
try {
$this->exec($sql = $this->platform->rollbackSavePoint(self::SAVEPOINT_NAME));
} catch (Driver\Exception $e) {
throw $this->driver->getExceptionConverter()->convert($e, new Query($sql, [], []));
}

$this->nested = false;

Expand Down
2 changes: 1 addition & 1 deletion src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function connect(array $params): DriverConnection
throw new \RuntimeException('This bundle only works for database platforms that support savepoints.');
}

return new StaticConnection($connection, $platform);
return new StaticConnection($this->underlyingDriver, $connection, $platform);
}

public static function setKeepStaticConnections(bool $keepStaticConnections): void
Expand Down

0 comments on commit c53e3e0

Please sign in to comment.