-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge] Allow to use a middleware instead of DbalLogger #45491
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
<?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; | ||
|
||
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> | ||
* | ||
* @internal | ||
*/ | ||
final class Connection extends AbstractConnectionMiddleware | ||
{ | ||
private $nestingLevel = 0; | ||
private $debugDataHolder; | ||
private $stopwatch; | ||
private $connectionName; | ||
|
||
public function __construct(ConnectionInterface $connection, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName) | ||
{ | ||
parent::__construct($connection); | ||
|
||
$this->debugDataHolder = $debugDataHolder; | ||
$this->stopwatch = $stopwatch; | ||
$this->connectionName = $connectionName; | ||
} | ||
|
||
public function prepare(string $sql): DriverStatement | ||
{ | ||
return new Statement( | ||
parent::prepare($sql), | ||
$this->debugDataHolder, | ||
$this->connectionName, | ||
$sql | ||
); | ||
} | ||
|
||
public function query(string $sql): Result | ||
{ | ||
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql)); | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->start('doctrine', 'doctrine'); | ||
} | ||
|
||
$query->start(); | ||
|
||
try { | ||
$result = parent::query($sql); | ||
} finally { | ||
$query->stop(); | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->stop('doctrine'); | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function exec(string $sql): int | ||
{ | ||
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query($sql)); | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->start('doctrine', 'doctrine'); | ||
} | ||
|
||
$query->start(); | ||
|
||
try { | ||
$affectedRows = parent::exec($sql); | ||
} finally { | ||
$query->stop(); | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->stop('doctrine'); | ||
} | ||
} | ||
|
||
return $affectedRows; | ||
} | ||
|
||
public function beginTransaction(): bool | ||
{ | ||
$query = null; | ||
if (1 === ++$this->nestingLevel) { | ||
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"START TRANSACTION"')); | ||
} | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->start('doctrine', 'doctrine'); | ||
} | ||
|
||
if (null !== $query) { | ||
$query->start(); | ||
} | ||
|
||
try { | ||
$ret = parent::beginTransaction(); | ||
} finally { | ||
if (null !== $query) { | ||
$query->stop(); | ||
} | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->stop('doctrine'); | ||
} | ||
} | ||
|
||
return $ret; | ||
} | ||
|
||
public function commit(): bool | ||
{ | ||
$query = null; | ||
if (1 === $this->nestingLevel--) { | ||
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"COMMIT"')); | ||
} | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->start('doctrine', 'doctrine'); | ||
} | ||
|
||
if (null !== $query) { | ||
$query->start(); | ||
} | ||
|
||
try { | ||
$ret = parent::commit(); | ||
} finally { | ||
if (null !== $query) { | ||
$query->stop(); | ||
} | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->stop('doctrine'); | ||
} | ||
} | ||
|
||
return $ret; | ||
} | ||
|
||
public function rollBack(): bool | ||
{ | ||
$query = null; | ||
if (1 === $this->nestingLevel--) { | ||
$this->debugDataHolder->addQuery($this->connectionName, $query = new Query('"ROLLBACK"')); | ||
} | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->start('doctrine', 'doctrine'); | ||
} | ||
|
||
if (null !== $query) { | ||
$query->start(); | ||
} | ||
|
||
try { | ||
$ret = parent::rollBack(); | ||
} finally { | ||
if (null !== $query) { | ||
$query->stop(); | ||
} | ||
|
||
if (null !== $this->stopwatch) { | ||
$this->stopwatch->stop('doctrine'); | ||
} | ||
} | ||
|
||
return $ret; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Bridge/Doctrine/Middleware/Debug/DebugDataHolder.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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; | ||
|
||
/** | ||
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com> | ||
*/ | ||
class DebugDataHolder | ||
{ | ||
private $data = []; | ||
|
||
public function addQuery(string $connectionName, Query $query): void | ||
{ | ||
$this->data[$connectionName][] = [ | ||
'sql' => $query->getSql(), | ||
'params' => $query->getParams(), | ||
'types' => $query->getTypes(), | ||
'executionMS' => [$query, 'getDuration'], // stop() may not be called at this point | ||
]; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
foreach ($this->data as $connectionName => $dataForConn) { | ||
foreach ($dataForConn as $idx => $data) { | ||
if (\is_callable($data['executionMS'])) { | ||
$this->data[$connectionName][$idx]['executionMS'] = $data['executionMS'](); | ||
} | ||
} | ||
} | ||
|
||
return $this->data; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->data = []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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; | ||
|
||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class Driver extends AbstractDriverMiddleware | ||
{ | ||
private $debugDataHolder; | ||
private $stopwatch; | ||
private $connectionName; | ||
|
||
public function __construct(DriverInterface $driver, DebugDataHolder $debugDataHolder, ?Stopwatch $stopwatch, string $connectionName) | ||
{ | ||
parent::__construct($driver); | ||
|
||
$this->debugDataHolder = $debugDataHolder; | ||
$this->stopwatch = $stopwatch; | ||
$this->connectionName = $connectionName; | ||
} | ||
|
||
public function connect(array $params): Connection | ||
{ | ||
return new Connection( | ||
parent::connect($params), | ||
$this->debugDataHolder, | ||
$this->stopwatch, | ||
$this->connectionName | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the double quotes ? They are not part of the SQL query starting a transaction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for COMMIT and ROLLBACK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the format that is currently used in the profiler: https://github.com/doctrine/dbal/blob/2afc6f00e8ff145ab386e9d25af293db98418bf5/src/Connection.php#L1297; should I change it anyway ?