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