Unverified Commit d078d025 authored by Benjamin Morel's avatar Benjamin Morel Committed by Sergei Morozov

Introduce a null SQL logger

parent 463f5fac
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL;
use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Logging\NullLogger;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\Schema\AbstractAsset;
use function preg_match;
......@@ -35,12 +36,14 @@ class Configuration
/**
* Gets the SQL logger that is used.
*
* @return SQLLogger|null
*/
public function getSQLLogger()
public function getSQLLogger() : SQLLogger
{
return $this->_attributes['sqlLogger'] ?? null;
if (! isset($this->_attributes['sqlLogger'])) {
$this->_attributes['sqlLogger'] = new NullLogger();
}
return $this->_attributes['sqlLogger'];
}
/**
......
......@@ -890,9 +890,7 @@ class Connection implements DriverConnection
$connection = $this->getWrappedConnection();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($query, $params, $types);
}
$logger->startQuery($query, $params, $types);
try {
if ($params) {
......@@ -914,9 +912,7 @@ class Connection implements DriverConnection
$stmt->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
return $stmt;
}
......@@ -1007,9 +1003,7 @@ class Connection implements DriverConnection
$args = func_get_args();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]);
}
$logger->startQuery($args[0]);
try {
$statement = $connection->query(...$args);
......@@ -1019,9 +1013,7 @@ class Connection implements DriverConnection
$statement->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
return $statement;
}
......@@ -1045,9 +1037,7 @@ class Connection implements DriverConnection
$connection = $this->getWrappedConnection();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($query, $params, $types);
}
$logger->startQuery($query, $params, $types);
try {
if ($params) {
......@@ -1069,9 +1059,7 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
}
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
return $result;
}
......@@ -1090,9 +1078,7 @@ class Connection implements DriverConnection
$connection = $this->getWrappedConnection();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($statement);
}
$logger->startQuery($statement);
try {
$result = $connection->exec($statement);
......@@ -1100,9 +1086,7 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
}
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
return $result;
}
......@@ -1239,23 +1223,13 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"START TRANSACTION"');
}
$logger->startQuery('"START TRANSACTION"');
$connection->beginTransaction();
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
} elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"SAVEPOINT"');
}
$logger->startQuery('"SAVEPOINT"');
$this->createSavepoint($this->_getNestedTransactionSavePointName());
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
}
return true;
......@@ -1283,23 +1257,13 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"COMMIT"');
}
$logger->startQuery('"COMMIT"');
$result = $connection->commit();
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
} elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"RELEASE SAVEPOINT"');
}
$logger->startQuery('"RELEASE SAVEPOINT"');
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
}
--$this->transactionNestingLevel;
......@@ -1347,28 +1311,20 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"ROLLBACK"');
}
$logger->startQuery('"ROLLBACK"');
$this->transactionNestingLevel = 0;
$connection->rollBack();
$this->isRollbackOnly = false;
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
if ($this->autoCommit === false) {
$this->beginTransaction();
}
} elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"ROLLBACK TO SAVEPOINT"');
}
$logger->startQuery('"ROLLBACK TO SAVEPOINT"');
$this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
--$this->transactionNestingLevel;
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
} else {
$this->isRollbackOnly = true;
--$this->transactionNestingLevel;
......
......@@ -350,17 +350,13 @@ class MasterSlaveConnection extends Connection
$args = func_get_args();
$logger = $this->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]);
}
$logger->startQuery($args[0]);
$statement = $this->_conn->query(...$args);
$statement->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
return $statement;
}
......
<?php
namespace Doctrine\DBAL\Logging;
/**
* A SQL logger that does nothing.
*/
class NullLogger implements SQLLogger
{
/**
* {@inheritdoc}
*/
public function startQuery($sql, ?array $params = null, ?array $types = null)
{
}
/**
* {@inheritdoc}
*/
public function stopQuery()
{
}
}
......@@ -144,16 +144,12 @@ class Statement implements IteratorAggregate, DriverStatement
}
$logger = $this->conn->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($this->sql, $this->params, $this->types);
}
$logger->startQuery($this->sql, $this->params, $this->types);
try {
$stmt = $this->stmt->execute($params);
} catch (Throwable $ex) {
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
throw DBALException::driverExceptionDuringQuery(
$this->conn->getDriver(),
$ex,
......@@ -162,9 +158,7 @@ class Statement implements IteratorAggregate, DriverStatement
);
}
if ($logger) {
$logger->stopQuery();
}
$logger->stopQuery();
$this->params = [];
$this->types = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment