vendor/symfony/doctrine-bridge/Middleware/Debug/Connection.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Doctrine\Middleware\Debug;
  11. use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
  12. use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
  13. use Doctrine\DBAL\Driver\Result;
  14. use Doctrine\DBAL\Driver\Statement as DriverStatement;
  15. use Symfony\Component\Stopwatch\Stopwatch;
  16. /**
  17.  * @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
  18.  *
  19.  * @internal
  20.  */
  21. final class Connection extends AbstractConnectionMiddleware
  22. {
  23.     private int $nestingLevel 0;
  24.     public function __construct(
  25.         ConnectionInterface $connection,
  26.         private DebugDataHolder $debugDataHolder,
  27.         private ?Stopwatch $stopwatch,
  28.         private string $connectionName,
  29.     ) {
  30.         parent::__construct($connection);
  31.     }
  32.     public function prepare(string $sql): DriverStatement
  33.     {
  34.         return new Statement(
  35.             parent::prepare($sql),
  36.             $this->debugDataHolder,
  37.             $this->connectionName,
  38.             $sql,
  39.         );
  40.     }
  41.     public function query(string $sql): Result
  42.     {
  43.         $this->debugDataHolder->addQuery($this->connectionName$query = new Query($sql));
  44.         $this->stopwatch?->start('doctrine''doctrine');
  45.         $query->start();
  46.         try {
  47.             $result parent::query($sql);
  48.         } finally {
  49.             $query->stop();
  50.             $this->stopwatch?->stop('doctrine');
  51.         }
  52.         return $result;
  53.     }
  54.     public function exec(string $sql): int
  55.     {
  56.         $this->debugDataHolder->addQuery($this->connectionName$query = new Query($sql));
  57.         $this->stopwatch?->start('doctrine''doctrine');
  58.         $query->start();
  59.         try {
  60.             $affectedRows parent::exec($sql);
  61.         } finally {
  62.             $query->stop();
  63.             $this->stopwatch?->stop('doctrine');
  64.         }
  65.         return $affectedRows;
  66.     }
  67.     public function beginTransaction(): bool
  68.     {
  69.         $query null;
  70.         if (=== ++$this->nestingLevel) {
  71.             $this->debugDataHolder->addQuery($this->connectionName$query = new Query('"START TRANSACTION"'));
  72.         }
  73.         $this->stopwatch?->start('doctrine''doctrine');
  74.         $query?->start();
  75.         try {
  76.             $ret parent::beginTransaction();
  77.         } finally {
  78.             $query?->stop();
  79.             $this->stopwatch?->stop('doctrine');
  80.         }
  81.         return $ret;
  82.     }
  83.     public function commit(): bool
  84.     {
  85.         $query null;
  86.         if (=== $this->nestingLevel--) {
  87.             $this->debugDataHolder->addQuery($this->connectionName$query = new Query('"COMMIT"'));
  88.         }
  89.         $this->stopwatch?->start('doctrine''doctrine');
  90.         $query?->start();
  91.         try {
  92.             $ret parent::commit();
  93.         } finally {
  94.             $query?->stop();
  95.             $this->stopwatch?->stop('doctrine');
  96.         }
  97.         return $ret;
  98.     }
  99.     public function rollBack(): bool
  100.     {
  101.         $query null;
  102.         if (=== $this->nestingLevel--) {
  103.             $this->debugDataHolder->addQuery($this->connectionName$query = new Query('"ROLLBACK"'));
  104.         }
  105.         $this->stopwatch?->start('doctrine''doctrine');
  106.         $query?->start();
  107.         try {
  108.             $ret parent::rollBack();
  109.         } finally {
  110.             $query?->stop();
  111.             $this->stopwatch?->stop('doctrine');
  112.         }
  113.         return $ret;
  114.     }
  115. }