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

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\DBAL3;
  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 StatementInterface;
  15. use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
  16. use Symfony\Bridge\Doctrine\Middleware\Debug\Query;
  17. use Symfony\Component\Stopwatch\Stopwatch;
  18. /**
  19.  * @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
  20.  *
  21.  * @internal
  22.  */
  23. final class Connection extends AbstractConnectionMiddleware
  24. {
  25.     /** @var int */
  26.     private $nestingLevel 0;
  27.     private $debugDataHolder;
  28.     private $stopwatch;
  29.     private $connectionName;
  30.     public function __construct(
  31.         ConnectionInterface $connection,
  32.         DebugDataHolder $debugDataHolder,
  33.         ?Stopwatch $stopwatch,
  34.         string $connectionName
  35.     ) {
  36.         $this->connectionName $connectionName;
  37.         $this->stopwatch $stopwatch;
  38.         $this->debugDataHolder $debugDataHolder;
  39.         parent::__construct($connection);
  40.     }
  41.     public function prepare(string $sql): StatementInterface
  42.     {
  43.         return new Statement(
  44.             parent::prepare($sql),
  45.             $this->debugDataHolder,
  46.             $this->connectionName,
  47.             $sql,
  48.             $this->stopwatch,
  49.         );
  50.     }
  51.     public function query(string $sql): Result
  52.     {
  53.         $this->debugDataHolder->addQuery($this->connectionName$query = new Query($sql));
  54.         if ($this->stopwatch) {
  55.             $this->stopwatch->start('doctrine''doctrine');
  56.         }
  57.         $query->start();
  58.         try {
  59.             return parent::query($sql);
  60.         } finally {
  61.             $query->stop();
  62.             if ($this->stopwatch) {
  63.                 $this->stopwatch->stop('doctrine');
  64.             }
  65.         }
  66.     }
  67.     public function exec(string $sql): int
  68.     {
  69.         $this->debugDataHolder->addQuery($this->connectionName$query = new Query($sql));
  70.         if ($this->stopwatch) {
  71.             $this->stopwatch->start('doctrine''doctrine');
  72.         }
  73.         $query->start();
  74.         try {
  75.             return parent::exec($sql);
  76.         } finally {
  77.             $query->stop();
  78.             if ($this->stopwatch) {
  79.                 $this->stopwatch->stop('doctrine');
  80.             }
  81.         }
  82.     }
  83.     public function beginTransaction(): bool
  84.     {
  85.         $query null;
  86.         if (=== ++$this->nestingLevel) {
  87.             $this->debugDataHolder->addQuery($this->connectionName$query = new Query('"START TRANSACTION"'));
  88.         }
  89.         if ($this->stopwatch) {
  90.             $this->stopwatch->start('doctrine''doctrine');
  91.         }
  92.         if ($query) {
  93.             $query->start();
  94.         }
  95.         try {
  96.             return parent::beginTransaction();
  97.         } finally {
  98.             if ($query) {
  99.                 $query->stop();
  100.             }
  101.             if ($this->stopwatch) {
  102.                 $this->stopwatch->stop('doctrine');
  103.             }
  104.         }
  105.     }
  106.     public function commit(): bool
  107.     {
  108.         $query null;
  109.         if (=== $this->nestingLevel--) {
  110.             $this->debugDataHolder->addQuery($this->connectionName$query = new Query('"COMMIT"'));
  111.         }
  112.         if ($this->stopwatch) {
  113.             $this->stopwatch->start('doctrine''doctrine');
  114.         }
  115.         if ($query) {
  116.             $query->start();
  117.         }
  118.         try {
  119.             return parent::commit();
  120.         } finally {
  121.             if ($query) {
  122.                 $query->stop();
  123.             }
  124.             if ($this->stopwatch) {
  125.                 $this->stopwatch->stop('doctrine');
  126.             }
  127.         }
  128.     }
  129.     public function rollBack(): bool
  130.     {
  131.         $query null;
  132.         if (=== $this->nestingLevel--) {
  133.             $this->debugDataHolder->addQuery($this->connectionName$query = new Query('"ROLLBACK"'));
  134.         }
  135.         if ($this->stopwatch) {
  136.             $this->stopwatch->start('doctrine''doctrine');
  137.         }
  138.         if ($query) {
  139.             $query->start();
  140.         }
  141.         try {
  142.             return parent::rollBack();
  143.         } finally {
  144.             if ($query) {
  145.                 $query->stop();
  146.             }
  147.             if ($this->stopwatch) {
  148.                 $this->stopwatch->stop('doctrine');
  149.             }
  150.         }
  151.     }
  152. }