10000 [Doctrine][DoctrineBridge] Compatibility with ORM 3 and DBAL 4 by derrabus · Pull Request #51997 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Doctrine][DoctrineBridge] Compatibility with ORM 3 and DBAL 4 #51997

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Run tests with ORM 3 and DBAL 4
  • Loading branch information
derrabus committed Oct 12, 2023
commit e1808dacbb0906a37fa6b64c45315864feb67d72
2 changes: 2 additions & 0 deletions .github/patch-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
}
// no break;
case false !== strpos($file, '/vendor/'):
case false !== strpos($file, '/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php'):
case false !== strpos($file, '/src/Symfony/Bridge/Doctrine/Middleware/Debug/DBAL3'):
10000 case false !== strpos($file, '/src/Symfony/Bridge/PhpUnit/'):
case false !== strpos($file, '/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Article.php'):
case false !== strpos($file, '/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php'):
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@
"doctrine/annotations": "^1.13.1|^2",
"doctrine/collections": "^1.0|^2.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^2.13.1|^3.0",
"doctrine/orm": "^2.12",
"doctrine/dbal": "^2.13.1|^3",
"doctrine/orm": "^2.12|^3",
"dragonmantank/cron-expression": "^3.1",
"egulias/email-validator": "^2.1.10|^3.1|^4",
"guzzlehttp/promises": "^1.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function guessType(string $class, string $property): ?TypeGuess
}

return match ($metadata->getTypeOfField($property)) {
Types::ARRAY,
'array',
Types::SIMPLE_ARRAY => new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE),
Types::BOOLEAN => new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE),
Types::DATETIME_MUTABLE,
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 Down Expand Up @@ -38,14 +39,23 @@ private function pingConnection(EntityManagerInterface $entityManager): void
$connection = $entityManager->getConnection();

try {
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
$this->executeDummySql($connection);
} catch (DBALException) {
$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());
}
}
58 changes: 21 additions & 37 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
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 int $nestingLevel = 0;

public function __construct(
ConnectionInterface $connection,
private DebugDataHolder $debugDataHolder,
Expand All @@ -35,7 +33,7 @@ public function __construct(
parent::__construct($connection);
}

public function prepare(string $sql): DriverStatement
public function prepare(string $sql): Statement
{
return new Statement(
parent::prepare($sql),
Expand All @@ -54,13 +52,11 @@ public function query(string $sql): Result
$query->start();

try {
$result = parent::query($sql);
return parent::query($sql);
} finally {
$query->stop();
$this->stopwatch?->stop('doctrine');
}

return $result;
}

public function exec(string $sql): int
Expand All @@ -80,63 +76,51 @@ public function exec(string $sql): int
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"'));
}
$query = new Query('"START TRANSACTION"');
$this->debugDataHolder->addQuery($this->connectionName, $query);

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

try {
$ret = parent::beginTransaction();
parent::beginTransaction();
} finally {
$query?->stop();
$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);

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

try {
$ret = parent::commit();
parent::commit();
} finally {
$query?->stop();
$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"'));
}
$query = new Query('"ROLLBACK"');
$this->debugDataHolder->addQuery($this->connectionName, $query);

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

try {
$ret = parent::rollBack();
parent::rollBack();
} finally {
$query?->stop();
$query->stop();
$this->stopwatch?->stop('doctrine');
}

return $ret;
}
}
133 changes: 133 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/DBAL3/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Middleware\Debug\DBAL3;

use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Symfony\Bridge\Doctrine\Middleware\Debug\Query;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*
* @internal
*/
final class Connection extends AbstractConnectionMiddleware
{
private int $nestingLevel = 0;

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

public function prepare(string $sql): Statement
{
return new Statement(
parent::prepare($sql),
$this->debugDataHolder,
$this->connectionName,
$sql,
$this->stopwatch,
);
}

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

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

try {
return parent::query($sql);
} finally {
$query->stop();
$this->stopwatch?->stop('doctrine');
}
}

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

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

try {
return parent::exec($sql);
} finally {
$query->stop();
$this->stopwatch?->stop('doctrine');
}
}

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

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

try {
return parent::beginTransaction();
} finally {
$query?->stop();
$this->stopwatch?->stop('doctrine');
}
}

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

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

try {
return parent::commit();
} finally {
$query?->stop();
$this->stopwatch?->stop('doctrine');
}
}

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

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

try {
return parent::rollBack();
} finally {
$query?->stop();
$this->stopwatch?->stop('doctrine');
}
}
}
76 changes: 76 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/DBAL3/Statement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Middleware\Debug\DBAL3;

use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware;
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Symfony\Bridge\Doctrine\Middleware\Debug\Query;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*
* @internal
*/
final class Statement extends AbstractStatementMiddleware
{
private Query $query;

public function __construct(
StatementInterface $statement,
private DebugDataHolder $debugDataHolder,
private string $connectionName,
string $sql,
private ?Stopwatch $stopwatch = null,
) {
parent::__construct($statement);

$this->query = new Query($sql);
}

public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
{
$this->query->setParam($param, $variable, $type);

return parent::bindParam($param, $variable, $type, ...\array_slice(\func_get_args(), 3));
}

public function bindValue($param, $value, $type = ParameterType::STRING): bool
{
$this->query->setValue($param, $value, $type);

return parent::bindValue($param, $value, $type);
}

public function execute($params = null): ResultInterface
{
if (null !== $params) {
$this->query->setValues($params);
}

// clone to prevent variables by reference to change
$this->debugDataHolder->addQuery($this->connectionName, $query = clone $this->query);

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

try {
return parent::execute($params);
} finally {
$query->stop();
$this->stopwatch?->stop('doctrine');
}
}
}
Loading
0