Skip to content

[DoctrineBridge] Fix DBAL 4 compatibility #52035

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

Merged
merged 1 commit into from
Oct 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function guessType(string $class, string $property)
}

switch ($metadata->getTypeOfField($property)) {
case Types::ARRAY:
case 'array': // DBAL < 4
case Types::SIMPLE_ARRAY:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
case Types::BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Doctrine\Messenger;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
Expand All @@ -33,19 +34,28 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
return $stack->next()->handle($envelope, $stack);
}

private function pingConnection(EntityManagerInterface $entityManager)
private function pingConnection(EntityManagerInterface $entityManager): void
{
$connection = $entityManager->getConnection();

try {
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
$this->executeDummySql($connection);
} catch (DBALException $e) {
$connection->close();
$connection->connect();
// Attempt to reestablish the lazy connection by sending another query.
$this->executeDummySql($connection);
}

if (!$entityManager->isOpen()) {
$this->managerRegistry->resetManager($this->entityManagerName);
}
}

/**
* @throws DBALException
*/
private function executeDummySql(Connection $connection): void
{
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
}
}
135 changes: 37 additions & 98 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,26 @@
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
* @author Alexander M. Turek <me@derrabus.de>
*
* @internal
*/
final class Connection extends AbstractConnectionMiddleware
{
private $nestingLevel = 0;
private $debugDataHolder;
private $stopwatch;
private $connectionName;

public function __construct(ConnectionInterface $connection, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName)
{
public function __construct(
ConnectionInterface $connection,
private DebugDataHolder $debugDataHolder,
private ?Stopwatch $stopwatch,
private string $connectionName,
) {
parent::__construct($connection);

$this->debugDataHolder = $debugDataHolder;
$this->stopwatch = $stopwatch;
$this->connectionName = $connectionName;
}

public function prepare(string $sql): DriverStatement
public function prepare(string $sql): Statement
{
return new Statement(
parent::prepare($sql),
Expand All @@ -53,135 +48,79 @@ public function query(string $sql): Result
{
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

$this->stopwatch?->start('doctrine', 'doctrine');
$query->start();

try {
$result = parent::query($sql);
return parent::query($sql);
} finally {
$query->stop();

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$this->stopwatch?->stop('doctrine');
}

return $result;
}

public function exec(string $sql): int
{
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql));

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

$this->stopwatch?->start('doctrine', 'doctrine');
$query->start();

try {
$affectedRows = parent::exec($sql);
} finally {
$query->stop();

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$this->stopwatch?->stop('doctrine');
}

return $affectedRows;
}

public function beginTransaction(): bool
public function beginTransaction(): void
{
$query = null;
if (1 === ++$this->nestingLevel) {
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"START TRANSACTION"'));
}

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}
$query = new Query('"START TRANSACTION"');
$this->debugDataHolder->addQuery($this->connectionName, $query);

if (null !== $query) {
$query->start();
}
$this->stopwatch?->start('doctrine', 'doctrine');
$query->start();

try {
$ret = parent::beginTransaction();
parent::beginTransaction();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$query->stop();
$this->stopwatch?->stop('doctrine');
}

return $ret;
}

public function commit(): bool
public function commit(): void
{
$query = null;
if (1 === $this->nestingLevel--) {
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"COMMIT"'));
}
$query = new Query('"COMMIT"');
$this->debugDataHolder->addQuery($this->connectionName, $query);

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}

if (null !== $query) {
$query->start();
}
$this->stopwatch?->start('doctrine', 'doctrine');
$query->start();

try {
$ret = parent::commit();
parent::commit();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$query->stop();
$this->stopwatch?->stop('doctrine');
}

return $ret;
}

public function rollBack(): bool
public function rollBack(): void
{
$query = null;
if (1 === $this->nestingLevel--) {
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"ROLLBACK"'));
}

if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine', 'doctrine');
}
$query = new Query('"ROLLBACK"');
$this->debugDataHolder->addQuery($this->connectionName, $query);

if (null !== $query) {
$query->start();
}
$this->stopwatch?->start('doctrine', 'doctrine');
$query->start();

try {
$ret = parent::rollBack();
parent::rollBack();
} finally {
if (null !== $query) {
$query->stop();
}

if (null !== $this->stopwatch) {
$this->stopwatch->stop('doctrine');
}
$query->stop();
$this->stopwatch?->stop('doctrine');
}

return $ret;
}
}
Loading