Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

[Refactoring] NullLogger integration for driver #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 27 additions & 46 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PDOStatement;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Spiral\Database\Exception\DriverException;
use Spiral\Database\Exception\StatementException;
use Spiral\Database\Injection\ParameterInterface;
Expand Down Expand Up @@ -102,8 +103,9 @@ public function __construct(
CompilerInterface $queryCompiler,
BuilderInterface $queryBuilder
) {
$this->logger = new NullLogger();
$this->schemaHandler = $schemaHandler->withDriver($this);
$this->queryBuilder = $queryBuilder->withDriver($this);
$this->queryBuilder = $queryBuilder->withDriver($this);
$this->queryCompiler = $queryCompiler;

$options['options'] = array_replace(
Expand Down Expand Up @@ -274,9 +276,7 @@ public function disconnect(): void
$this->pdo = null;
} catch (Throwable $e) {
// disconnect error
if ($this->logger !== null) {
$this->logger->error($e->getMessage());
}
$this->logger->error($e->getMessage());
}

$this->transactionLevel = 0;
Expand Down Expand Up @@ -332,9 +332,8 @@ public function execute(string $query, array $parameters = []): int
public function lastInsertID(string $sequence = null)
{
$result = $this->getPDO()->lastInsertId();
if ($this->logger !== null) {
$this->logger->debug("Insert ID: {$result}");
}

$this->logger->debug("Insert ID: {$result}");

return $result;
}
Expand All @@ -358,9 +357,7 @@ public function beginTransaction(string $isolationLevel = null): bool
$this->setIsolationLevel($isolationLevel);
}

if ($this->logger !== null) {
$this->logger->info('Begin transaction');
}
$this->logger->info('Begin transaction');

try {
return $this->getPDO()->beginTransaction();
Expand Down Expand Up @@ -402,14 +399,12 @@ public function commitTransaction(): bool
{
// Check active transaction
if (!$this->getPDO()->inTransaction()) {
if ($this->logger !== null) {
$this->logger->warning(
sprintf(
'Attempt to commit a transaction that has not yet begun. Transaction level: %d',
$this->transactionLevel
)
);
}
$this->logger->warning(
sprintf(
'Attempt to commit a transaction that has not yet begun. Transaction level: %d',
$this->transactionLevel
)
);

if ($this->transactionLevel === 0) {
return false;
Expand All @@ -422,9 +417,7 @@ public function commitTransaction(): bool
--$this->transactionLevel;

if ($this->transactionLevel === 0) {
if ($this->logger !== null) {
$this->logger->info('Commit transaction');
}
$this->logger->info('Commit transaction');

try {
return $this->getPDO()->commit();
Expand All @@ -449,14 +442,12 @@ public function rollbackTransaction(): bool
{
// Check active transaction
if (!$this->getPDO()->inTransaction()) {
if ($this->logger !== null) {
$this->logger->warning(
sprintf(
'Attempt to rollback a transaction that has not yet begun. Transaction level: %d',
$this->transactionLevel
)
);
}
$this->logger->warning(
sprintf(
'Attempt to rollback a transaction that has not yet begun. Transaction level: %d',
$this->transactionLevel
)
);

$this->transactionLevel = 0;
return false;
Expand All @@ -465,9 +456,7 @@ public function rollbackTransaction(): bool
--$this->transactionLevel;

if ($this->transactionLevel === 0) {
if ($this->logger !== null) {
$this->logger->info('Rollback transaction');
}
$this->logger->info('Rollback transaction');

try {
return $this->getPDO()->rollBack();
Expand Down Expand Up @@ -527,9 +516,9 @@ protected function statement(

throw $e;
} finally {
if ($this->logger !== null) {
root-aza marked this conversation as resolved.
Show resolved Hide resolved
if (!$this->logger instanceof NullLogger) {
$queryString = Interpolator::interpolate($query, $parameters);
$context = $this->defineLoggerContext($queryStart, $statement ?? null);
$context = $this->defineLoggerContext($queryStart, $statement ?? null);

if (isset($e)) {
$this->logger->error($queryString, $context);
Expand Down Expand Up @@ -634,9 +623,7 @@ abstract protected function mapException(
*/
protected function setIsolationLevel(string $level): void
{
if ($this->logger !== null) {
$this->logger->info("Transaction isolation level '{$level}'");
}
$this->logger->info("Transaction isolation level '{$level}'");

$this->execute("SET TRANSACTION ISOLATION LEVEL {$level}");
}
Expand All @@ -650,9 +637,7 @@ protected function setIsolationLevel(string $level): void
*/
protected function createSavepoint(int $level): void
{
if ($this->logger !== null) {
$this->logger->info("Transaction: new savepoint 'SVP{$level}'");
}
$this->logger->info("Transaction: new savepoint 'SVP{$level}'");

$this->execute('SAVEPOINT ' . $this->identifier("SVP{$level}"));
}
Expand All @@ -666,9 +651,7 @@ protected function createSavepoint(int $level): void
*/
protected function releaseSavepoint(int $level): void
{
if ($this->logger !== null) {
$this->logger->info("Transaction: release savepoint 'SVP{$level}'");
}
$this->logger->info("Transaction: release savepoint 'SVP{$level}'");

$this->execute('RELEASE SAVEPOINT ' . $this->identifier("SVP{$level}"));
}
Expand All @@ -682,9 +665,7 @@ protected function releaseSavepoint(int $level): void
*/
protected function rollbackSavepoint(int $level): void
{
if ($this->logger !== null) {
$this->logger->info("Transaction: rollback savepoint 'SVP{$level}'");
}
$this->logger->info("Transaction: rollback savepoint 'SVP{$level}'");

$this->execute('ROLLBACK TO SAVEPOINT ' . $this->identifier("SVP{$level}"));
}
Expand Down