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

Introduce a null SQL logger

parent 2a20c16d
......@@ -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'];
}
/**
......
......@@ -889,9 +889,7 @@ class Connection implements DriverConnection
$this->connect();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($query, $params, $types);
}
try {
if ($params) {
......@@ -913,9 +911,7 @@ class Connection implements DriverConnection
$stmt->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery();
}
return $stmt;
}
......@@ -1002,9 +998,7 @@ class Connection implements DriverConnection
$args = func_get_args();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]);
}
try {
$statement = $this->_conn->query(...$args);
......@@ -1014,9 +1008,7 @@ class Connection implements DriverConnection
$statement->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery();
}
return $statement;
}
......@@ -1040,9 +1032,7 @@ class Connection implements DriverConnection
$this->connect();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($query, $params, $types);
}
try {
if ($params) {
......@@ -1063,9 +1053,7 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $query, $this->resolveParams($params, $types));
}
if ($logger) {
$logger->stopQuery();
}
return $result;
}
......@@ -1084,9 +1072,7 @@ class Connection implements DriverConnection
$this->connect();
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($statement);
}
try {
$result = $this->_conn->exec($statement);
......@@ -1094,9 +1080,7 @@ class Connection implements DriverConnection
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
}
if ($logger) {
$logger->stopQuery();
}
return $result;
}
......@@ -1240,23 +1224,15 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"START TRANSACTION"');
}
$this->_conn->beginTransaction();
if ($logger) {
$logger->stopQuery();
}
} elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"SAVEPOINT"');
}
$this->createSavepoint($this->_getNestedTransactionSavePointName());
if ($logger) {
$logger->stopQuery();
}
}
}
/**
* Commits the current transaction.
......@@ -1280,22 +1256,14 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"COMMIT"');
}
$this->_conn->commit();
if ($logger) {
$logger->stopQuery();
}
} elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"RELEASE SAVEPOINT"');
}
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
if ($logger) {
$logger->stopQuery();
}
}
--$this->transactionNestingLevel;
......@@ -1340,28 +1308,20 @@ class Connection implements DriverConnection
$logger = $this->_config->getSQLLogger();
if ($this->transactionNestingLevel === 1) {
if ($logger) {
$logger->startQuery('"ROLLBACK"');
}
$this->transactionNestingLevel = 0;
$this->_conn->rollBack();
$this->isRollbackOnly = false;
if ($logger) {
$logger->stopQuery();
}
if ($this->autoCommit === false) {
$this->beginTransaction();
}
} elseif ($this->nestTransactionsWithSavepoints) {
if ($logger) {
$logger->startQuery('"ROLLBACK TO SAVEPOINT"');
}
$this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
--$this->transactionNestingLevel;
if ($logger) {
$logger->stopQuery();
}
} else {
$this->isRollbackOnly = true;
--$this->transactionNestingLevel;
......
......@@ -348,15 +348,11 @@ class MasterSlaveConnection extends Connection
$args = func_get_args();
$logger = $this->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($args[0]);
}
$statement = $this->_conn->query(...$args);
if ($logger) {
$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);
}
try {
$stmt = $this->stmt->execute($params);
} catch (Throwable $ex) {
if ($logger) {
$logger->stopQuery();
}
throw DBALException::driverExceptionDuringQuery(
$this->conn->getDriver(),
$ex,
......@@ -162,9 +158,7 @@ class Statement implements IteratorAggregate, DriverStatement
);
}
if ($logger) {
$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