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 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Logging\NullLogger;
use Doctrine\DBAL\Logging\SQLLogger; use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\DBAL\Schema\AbstractAsset; use Doctrine\DBAL\Schema\AbstractAsset;
use function preg_match; use function preg_match;
...@@ -35,12 +36,14 @@ class Configuration ...@@ -35,12 +36,14 @@ class Configuration
/** /**
* Gets the SQL logger that is used. * 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 ...@@ -890,9 +890,7 @@ class Connection implements DriverConnection
$connection = $this->getWrappedConnection(); $connection = $this->getWrappedConnection();
$logger = $this->_config->getSQLLogger(); $logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($query, $params, $types); $logger->startQuery($query, $params, $types);
}
try { try {
if ($params) { if ($params) {
...@@ -914,9 +912,7 @@ class Connection implements DriverConnection ...@@ -914,9 +912,7 @@ class Connection implements DriverConnection
$stmt->setFetchMode($this->defaultFetchMode); $stmt->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
return $stmt; return $stmt;
} }
...@@ -1007,9 +1003,7 @@ class Connection implements DriverConnection ...@@ -1007,9 +1003,7 @@ class Connection implements DriverConnection
$args = func_get_args(); $args = func_get_args();
$logger = $this->_config->getSQLLogger(); $logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]); $logger->startQuery($args[0]);
}
try { try {
$statement = $connection->query(...$args); $statement = $connection->query(...$args);
...@@ -1019,9 +1013,7 @@ class Connection implements DriverConnection ...@@ -1019,9 +1013,7 @@ class Connection implements DriverConnection
$statement->setFetchMode($this->defaultFetchMode); $statement->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
return $statement; return $statement;
} }
...@@ -1045,9 +1037,7 @@ class Connection implements DriverConnection ...@@ -1045,9 +1037,7 @@ class Connection implements DriverConnection
$connection = $this->getWrappedConnection(); $connection = $this->getWrappedConnection();
$logger = $this->_config->getSQLLogger(); $logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($query, $params, $types); $logger->startQuery($query, $params, $types);
}
try { try {
if ($params) { if ($params) {
...@@ -1069,9 +1059,7 @@ class Connection implements DriverConnection ...@@ -1069,9 +1059,7 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types)); throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
} }
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
return $result; return $result;
} }
...@@ -1090,9 +1078,7 @@ class Connection implements DriverConnection ...@@ -1090,9 +1078,7 @@ class Connection implements DriverConnection
$connection = $this->getWrappedConnection(); $connection = $this->getWrappedConnection();
$logger = $this->_config->getSQLLogger(); $logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($statement); $logger->startQuery($statement);
}
try { try {
$result = $connection->exec($statement); $result = $connection->exec($statement);
...@@ -1100,9 +1086,7 @@ class Connection implements DriverConnection ...@@ -1100,9 +1086,7 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement); throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
} }
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
return $result; return $result;
} }
...@@ -1239,24 +1223,14 @@ class Connection implements DriverConnection ...@@ -1239,24 +1223,14 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger(); $logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) { if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"START TRANSACTION"'); $logger->startQuery('"START TRANSACTION"');
}
$connection->beginTransaction(); $connection->beginTransaction();
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
} elseif ($this->nestTransactionsWithSavepoints) { } elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"SAVEPOINT"'); $logger->startQuery('"SAVEPOINT"');
}
$this->createSavepoint($this->_getNestedTransactionSavePointName()); $this->createSavepoint($this->_getNestedTransactionSavePointName());
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
} }
}
return true; return true;
} }
...@@ -1283,24 +1257,14 @@ class Connection implements DriverConnection ...@@ -1283,24 +1257,14 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger(); $logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) { if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"COMMIT"'); $logger->startQuery('"COMMIT"');
}
$result = $connection->commit(); $result = $connection->commit();
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
} elseif ($this->nestTransactionsWithSavepoints) { } elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"RELEASE SAVEPOINT"'); $logger->startQuery('"RELEASE SAVEPOINT"');
}
$this->releaseSavepoint($this->_getNestedTransactionSavePointName()); $this->releaseSavepoint($this->_getNestedTransactionSavePointName());
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
} }
}
--$this->transactionNestingLevel; --$this->transactionNestingLevel;
...@@ -1347,28 +1311,20 @@ class Connection implements DriverConnection ...@@ -1347,28 +1311,20 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger(); $logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) { if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"ROLLBACK"'); $logger->startQuery('"ROLLBACK"');
}
$this->transactionNestingLevel = 0; $this->transactionNestingLevel = 0;
$connection->rollBack(); $connection->rollBack();
$this->isRollbackOnly = false; $this->isRollbackOnly = false;
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
if ($this->autoCommit === false) { if ($this->autoCommit === false) {
$this->beginTransaction(); $this->beginTransaction();
} }
} elseif ($this->nestTransactionsWithSavepoints) { } elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"ROLLBACK TO SAVEPOINT"'); $logger->startQuery('"ROLLBACK TO SAVEPOINT"');
}
$this->rollbackSavepoint($this->_getNestedTransactionSavePointName()); $this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
--$this->transactionNestingLevel; --$this->transactionNestingLevel;
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
} else { } else {
$this->isRollbackOnly = true; $this->isRollbackOnly = true;
--$this->transactionNestingLevel; --$this->transactionNestingLevel;
......
...@@ -350,17 +350,13 @@ class MasterSlaveConnection extends Connection ...@@ -350,17 +350,13 @@ class MasterSlaveConnection extends Connection
$args = func_get_args(); $args = func_get_args();
$logger = $this->getConfiguration()->getSQLLogger(); $logger = $this->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]); $logger->startQuery($args[0]);
}
$statement = $this->_conn->query(...$args); $statement = $this->_conn->query(...$args);
$statement->setFetchMode($this->defaultFetchMode); $statement->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
return $statement; 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 ...@@ -144,16 +144,12 @@ class Statement implements IteratorAggregate, DriverStatement
} }
$logger = $this->conn->getConfiguration()->getSQLLogger(); $logger = $this->conn->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($this->sql, $this->params, $this->types); $logger->startQuery($this->sql, $this->params, $this->types);
}
try { try {
$stmt = $this->stmt->execute($params); $stmt = $this->stmt->execute($params);
} catch (Throwable $ex) { } catch (Throwable $ex) {
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
throw DBALException::driverExceptionDuringQuery( throw DBALException::driverExceptionDuringQuery(
$this->conn->getDriver(), $this->conn->getDriver(),
$ex, $ex,
...@@ -162,9 +158,7 @@ class Statement implements IteratorAggregate, DriverStatement ...@@ -162,9 +158,7 @@ class Statement implements IteratorAggregate, DriverStatement
); );
} }
if ($logger) {
$logger->stopQuery(); $logger->stopQuery();
}
$this->params = []; $this->params = [];
$this->types = []; $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