Skip to content

Commit

Permalink
decorators WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 5, 2024
1 parent 9f82d9d commit cdb8832
Show file tree
Hide file tree
Showing 21 changed files with 639 additions and 212 deletions.
19 changes: 11 additions & 8 deletions src/Bridges/DatabaseTracy/ConnectionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
namespace Nette\Bridges\DatabaseTracy;

use Nette;
use Nette\Database\Drivers;
use Nette\Database\DriverException;
use Nette\Database\Explorer;
use Nette\Database\Helpers;
use Nette\Database\Result;
use Nette\Database\SqlLiteral;
use Tracy;


Expand Down Expand Up @@ -64,13 +66,14 @@ public function __construct(Explorer $explorer, Tracy\BlueScreen $blueScreen)
}


private function logQuery(Explorer $connection, $result): void
private function logQuery(Explorer $connection, mixed $result, SqlLiteral $query, ?float $time): void
{
if ($this->disabled) {
return;
}

$this->count++;
$this->totalTime += $time;

$source = null;
$trace = $result instanceof DriverException
Expand All @@ -89,13 +92,13 @@ private function logQuery(Explorer $connection, $result): void
}
}

if ($result instanceof Result) {
$this->totalTime += $result->getTime();
if ($this->count < $this->maxQueries) {
$this->events[] = [$connection, $result->getQuery(), $source, $result->getTime(), $result->getRowCount(), null];
}
} elseif ($result instanceof DriverException && $this->count < $this->maxQueries) {
$this->events[] = [$connection, $result->getQuery(), $source, null, null, $result->getMessage()];
if ($this->count >= $this->maxQueries) {
// nothing
} elseif ($result instanceof DriverException) {
$this->events[] = [$connection, $query, $source, null, null, $result->getMessage()];
} else {
$rowCount = $result instanceof Drivers\Result ? $result->getRowCount() : $result;
$this->events[] = [$connection, $query, $source, $time, $rowCount, null];
}
}

Expand Down
98 changes: 98 additions & 0 deletions src/Database/Drivers/Accessory/ExceptionHandlingConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Drivers\Accessory;

use Nette\Database\Drivers;
use Nette\Database\Drivers\Decorator;
use Nette\Database\SqlLiteral;


class ExceptionHandlingConnection extends Decorator\Connection
{
public function __construct(
protected Drivers\Connection $innerConnection,
/** @var \Closure(\Throwable): \Throwable */
private readonly \Closure $translator,
) {
}


public function query(string $sql, array $params = []): Drivers\Result
{
try {
return new ExceptionHandlingResult(
$this->innerConnection->query($sql, $params),
$this->translator,
);
} catch (\Throwable $e) {
throw ($this->translator)($e, new SqlLiteral($sql), $params);

Check failure on line 35 in src/Database/Drivers/Accessory/ExceptionHandlingConnection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Closure invoked with 3 parameters, 1 required.
}
}


public function execute(string $sql): int
{
try {
return $this->innerConnection->execute($sql);
} catch (\Throwable $e) {
throw ($this->translator)($e, new SqlLiteral($sql));

Check failure on line 45 in src/Database/Drivers/Accessory/ExceptionHandlingConnection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Closure invoked with 2 parameters, 1 required.
}
}


public function beginTransaction(): void
{
try {
$this->innerConnection->beginTransaction();
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}


public function commit(): void
{
try {
$this->innerConnection->commit();
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}


public function rollBack(): void
{
try {
$this->innerConnection->rollBack();
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}


public function getInsertId(?string $sequence = null): int|string
{
try {
return $this->innerConnection->getInsertId($sequence);
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}


public function getServerVersion(): string
{
try {
return $this->innerConnection->getServerVersion();
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}
}
54 changes: 54 additions & 0 deletions src/Database/Drivers/Accessory/ExceptionHandlingResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Drivers\Accessory;

use Nette\Database\Drivers;
use Nette\Database\Drivers\Decorator;


class ExceptionHandlingResult extends Decorator\Result
{
public function __construct(
protected Drivers\Result $innerResult,
/** @var \Closure(\Throwable): \Throwable */
private readonly \Closure $translator,
) {
}


public function fetch(): ?array
{
try {
return $this->innerResult->fetch();
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}


public function fetchList(): ?array
{
try {
return $this->innerResult->fetchList();
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}


public function getColumnsInfo(): array
{
try {
return $this->innerResult->getColumnsInfo();
} catch (\Throwable $e) {
throw ($this->translator)($e);
}
}
}
71 changes: 17 additions & 54 deletions src/Database/Drivers/Accessory/LazyConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,35 @@
namespace Nette\Database\Drivers\Accessory;

use Nette\Database\Drivers;
use Nette\Database\Drivers\Decorator;


final class LazyConnection implements Drivers\Connection
final class LazyConnection extends Decorator\Connection
{
public function __construct(
private \Closure $callback,
/** @var \Closure(self): void */
private \Closure $factory,
) {
unset($this->innerConnection);
}


private function getConnection(): Drivers\Connection
public function setInnerConnection(?Drivers\Connection $connection): static
{
return ($this->callback)();
if ($connection) {
$this->innerConnection = $connection;
} else {
unset($this->innerConnection);
}
return $this;
}


public function query(string $sql, array $params = []): Drivers\Result
public function __get(string $name): mixed
{
return $this->getConnection()->query($sql, $params);
}


public function execute(string $sql): int
{
return $this->getConnection()->execute($sql);
}


public function getNativeConnection(): mixed
{
return $this->getConnection()->getNativeConnection();
}


public function beginTransaction(): void
{
$this->getConnection()->beginTransaction();
}


public function commit(): void
{
$this->getConnection()->commit();
}


public function rollBack(): void
{
$this->getConnection()->rollBack();
}


public function getInsertId(?string $sequence = null): int|string
{
return $this->getConnection()->getInsertId($sequence);
}


public function quote(string $string): string
{
return $this->getConnection()->quote($string);
}


public function getServerVersion(): string
{
return $this->getConnection()->getServerVersion();
if ($name === 'innerConnection') {

Check failure on line 39 in src/Database/Drivers/Accessory/LazyConnection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Nette\Database\Drivers\Accessory\LazyConnection::__get() should return mixed but return statement is missing.
($this->factory)($this);
return $this->innerConnection ?? null;
}
}
}
86 changes: 86 additions & 0 deletions src/Database/Drivers/Accessory/LoggingConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Drivers\Accessory;

use Nette\Database\Drivers;
use Nette\Database\Drivers\Decorator;
use Nette\Database\SqlLiteral;


class LoggingConnection extends Decorator\Connection
{
public function __construct(
protected Drivers\Connection $innerConnection,
/** @var \Closure(mixed, SqlLiteral, ?float): void */
private \Closure $logger,
) {
}


public function query(string $sql, array $params = []): Drivers\Result
{
return $this->logOperation(
fn() => $this->innerConnection->query($sql, $params),
new SqlLiteral($sql, $params),
);
}


public function execute(string $sql): int
{
return $this->logOperation(
fn() => $this->innerConnection->execute($sql),
new SqlLiteral($sql),
);
}


public function beginTransaction(): void
{
$this->logOperation(
fn() => $this->innerConnection->beginTransaction(),
new SqlLiteral('BEGIN TRANSACTION'),
);
}


public function commit(): void
{
$this->logOperation(
fn() => $this->innerConnection->commit(),
new SqlLiteral('COMMIT'),
);
}


public function rollBack(): void
{
$this->logOperation(
fn() => $this->innerConnection->rollBack(),
new SqlLiteral('ROLLBACK'),
);
}


private function logOperation(callable $operation, SqlLiteral $query): mixed
{
$time = microtime(true);
try {
$result = $operation();
} catch (\Throwable $e) {
($this->logger)($e, $query, null);
throw $e;
}

$time = microtime(true) - $time;
($this->logger)($result, $query, $time);
return $result;
}
}
Loading

0 comments on commit cdb8832

Please sign in to comment.