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

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