diff --git a/src/Logging/Connection.php b/src/Logging/Connection.php new file mode 100644 index 0000000..f402629 --- /dev/null +++ b/src/Logging/Connection.php @@ -0,0 +1,71 @@ +queries, $sql); + } + + public function exec(string $sql): int + { + $start = microtime(true); + try { + return parent::exec($sql); + } finally { + $this->queries->addQuery(new Query($sql, [], microtime(true) - $start)); + } + } + + public function query(string $sql): Result + { + $start = microtime(true); + try { + return parent::query($sql); + } finally { + $this->queries->addQuery(new Query($sql, [], microtime(true) - $start)); + } + } + + public function beginTransaction(): void + { + $start = microtime(true); + try { + parent::beginTransaction(); + } finally { + $this->queries->addQuery(new Query('"BEGIN TRANSACTION"', [], microtime(true) - $start)); + } + } + + public function commit(): void + { + $start = microtime(true); + try { + parent::commit(); + } finally { + $this->queries->addQuery(new Query('"COMMIT"', [], microtime(true) - $start)); + } + } + + public function rollBack(): void + { + $start = microtime(true); + try { + parent::rollBack(); + } finally { + $this->queries->addQuery(new Query('"ROLLBACK"', [], microtime(true) - $start)); + } + } +} \ No newline at end of file diff --git a/src/Logging/Driver.php b/src/Logging/Driver.php new file mode 100644 index 0000000..060226e --- /dev/null +++ b/src/Logging/Driver.php @@ -0,0 +1,20 @@ +queries); + } +} \ No newline at end of file diff --git a/src/Logging/Middleware.php b/src/Logging/Middleware.php new file mode 100644 index 0000000..4469483 --- /dev/null +++ b/src/Logging/Middleware.php @@ -0,0 +1,38 @@ +queries = new QueryCollection(); + } + + public function wrap(DriverInterface $driver): DriverInterface + { + return new Driver($driver, $this->queries); + } + + /** + * @return Query[] + */ + public function getQueries(): array + { + return $this->queries->queries; + } + + public function isEnabled(): bool + { + return $this->queries->enabled; + } + + public function setEnabled(bool $enabled): void + { + $this->queries->enabled = $enabled; + } +} \ No newline at end of file diff --git a/src/ORMTestInfrastructure/Query.php b/src/Logging/Query.php similarity index 57% rename from src/ORMTestInfrastructure/Query.php rename to src/Logging/Query.php index 3da999e..5d35bff 100644 --- a/src/ORMTestInfrastructure/Query.php +++ b/src/Logging/Query.php @@ -7,60 +7,44 @@ * file that was distributed with this source code. */ -namespace Webfactory\Doctrine\ORMTestInfrastructure; +namespace Webfactory\Doctrine\Logging; /** * Represents a query that has been executed. */ class Query { - /** - * The SQL query. - * - * @var string - */ - protected $sql = null; - - /** - * The assigned parameters. - * - * @var mixed[] - */ - protected $params = null; - - /** - * Currently not used: - * - types - * - * @param string $sql - sql - * @param mixed[] $params - params - */ - public function __construct($sql, array $params) - { - $this->sql = $sql; - $this->params = $params; + public function __construct( + protected readonly string $sql, + protected readonly array $params, + protected readonly float $executionTimeInSeconds, + ) { } /** * Returns the SQL of the query. - * - * @return string */ - public function getSql() + public function getSql(): string { return $this->sql; } /** * Returns a list of parameters that have been assigned to the statement. - * - * @return mixed[] */ - public function getParams() + public function getParams(): array { return $this->params; } + /** + * Returns the execution time of the query in seconds. + */ + public function getExecutionTimeInSeconds(): float + { + return $this->executionTimeInSeconds; + } + /** * Returns a string representation of the query and its params. * diff --git a/src/Logging/QueryCollection.php b/src/Logging/QueryCollection.php new file mode 100644 index 0000000..68573b7 --- /dev/null +++ b/src/Logging/QueryCollection.php @@ -0,0 +1,18 @@ + */ + public array $queries = []; + + public bool $enabled = true; + + public function addQuery(Query $query): void + { + if ($this->enabled) { + $this->queries[] = $query; + } + } +} \ No newline at end of file diff --git a/src/Logging/Statement.php b/src/Logging/Statement.php new file mode 100644 index 0000000..1e7dc3c --- /dev/null +++ b/src/Logging/Statement.php @@ -0,0 +1,38 @@ +params[$param] = $value; + + parent::bindValue($param, $value, $type); + } + + public function execute($params = []): Result + { + $start = microtime(true); + try { + return parent::execute(); + } finally { + $this->queries->addQuery(new Query($this->sql, $this->params, microtime(true) - $start)); + } + } +} \ No newline at end of file diff --git a/src/ORMTestInfrastructure/ORMInfrastructure.php b/src/ORMTestInfrastructure/ORMInfrastructure.php index 77f6e79..cc10afd 100644 --- a/src/ORMTestInfrastructure/ORMInfrastructure.php +++ b/src/ORMTestInfrastructure/ORMInfrastructure.php @@ -15,15 +15,14 @@ use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Doctrine\Persistence\ObjectRepository; use Doctrine\ORM\EntityManager; -use Doctrine\ORM\Events; -use Doctrine\ORM\Mapping\ClassMetadataFactory; use Doctrine\ORM\Mapping\DefaultNamingStrategy; use Doctrine\ORM\Mapping\NamingStrategy; use Doctrine\ORM\Tools\ResolveTargetEntityListener; use Doctrine\ORM\Tools\SchemaTool; -use Doctrine\Common\EventSubscriber; use Webfactory\Doctrine\Config\ConnectionConfiguration; use Webfactory\Doctrine\Config\ExistingConnectionConfiguration; +use Webfactory\Doctrine\Logging\Middleware; +use Webfactory\Doctrine\Logging\Query; /** * Helper class that creates the database infrastructure for a defined set of entity classes. @@ -92,7 +91,7 @@ class ORMInfrastructure /** * The query logger that is used. * - * @var DebugStack + * @var Middleware */ protected $queryLogger = null; @@ -197,7 +196,7 @@ private function __construct($entityClasses, ?ConnectionConfiguration $connectio } $this->entityClasses = $entityClasses; $this->connectionConfiguration = $connectionConfiguration; - $this->queryLogger = new QueryLogger(); + $this->queryLogger = new Middleware(); $this->namingStrategy = new DefaultNamingStrategy(); $this->mappingDriver = $mappingDriver; $this->resolveTargetListener = new ResolveTargetEntityListener(); @@ -255,11 +254,11 @@ public function getQueries() */ public function import($dataSource) { - $loggerWasEnabled = $this->queryLogger->enabled; - $this->queryLogger->enabled = false; + $loggerWasEnabled = $this->queryLogger->isEnabled(); + $this->queryLogger->setEnabled(false); $importer = new Importer($this->copyEntityManager()); $importer->import($dataSource); - $this->queryLogger->enabled = $loggerWasEnabled; + $this->queryLogger->setEnabled($loggerWasEnabled); } /** @@ -270,14 +269,14 @@ public function import($dataSource) public function getEntityManager() { if ($this->entityManager === null) { - $loggerWasEnabled = $this->queryLogger->enabled; - $this->queryLogger->enabled = false; + $loggerWasEnabled = $this->queryLogger->isEnabled(); + $this->queryLogger->setEnabled(false); $this->entityManager = $this->createEntityManager(); $this->setupEventSubscribers(); if ($this->createSchema) { $this->createSchemaForSupportedEntities(); } - $this->queryLogger->enabled = $loggerWasEnabled; + $this->queryLogger->setEnabled($loggerWasEnabled); } return $this->entityManager; }