Skip to content

Commit cdb8832

Browse files
committed
decorators WIP
1 parent 9f82d9d commit cdb8832

21 files changed

+639
-212
lines changed

src/Bridges/DatabaseTracy/ConnectionPanel.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
namespace Nette\Bridges\DatabaseTracy;
1111

1212
use Nette;
13+
use Nette\Database\Drivers;
1314
use Nette\Database\DriverException;
1415
use Nette\Database\Explorer;
1516
use Nette\Database\Helpers;
1617
use Nette\Database\Result;
18+
use Nette\Database\SqlLiteral;
1719
use Tracy;
1820

1921

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

6668

67-
private function logQuery(Explorer $connection, $result): void
69+
private function logQuery(Explorer $connection, mixed $result, SqlLiteral $query, ?float $time): void
6870
{
6971
if ($this->disabled) {
7072
return;
7173
}
7274

7375
$this->count++;
76+
$this->totalTime += $time;
7477

7578
$source = null;
7679
$trace = $result instanceof DriverException
@@ -89,13 +92,13 @@ private function logQuery(Explorer $connection, $result): void
8992
}
9093
}
9194

92-
if ($result instanceof Result) {
93-
$this->totalTime += $result->getTime();
94-
if ($this->count < $this->maxQueries) {
95-
$this->events[] = [$connection, $result->getQuery(), $source, $result->getTime(), $result->getRowCount(), null];
96-
}
97-
} elseif ($result instanceof DriverException && $this->count < $this->maxQueries) {
98-
$this->events[] = [$connection, $result->getQuery(), $source, null, null, $result->getMessage()];
95+
if ($this->count >= $this->maxQueries) {
96+
// nothing
97+
} elseif ($result instanceof DriverException) {
98+
$this->events[] = [$connection, $query, $source, null, null, $result->getMessage()];
99+
} else {
100+
$rowCount = $result instanceof Drivers\Result ? $result->getRowCount() : $result;
101+
$this->events[] = [$connection, $query, $source, $time, $rowCount, null];
99102
}
100103
}
101104

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Database\Drivers\Accessory;
11+
12+
use Nette\Database\Drivers;
13+
use Nette\Database\Drivers\Decorator;
14+
use Nette\Database\SqlLiteral;
15+
16+
17+
class ExceptionHandlingConnection extends Decorator\Connection
18+
{
19+
public function __construct(
20+
protected Drivers\Connection $innerConnection,
21+
/** @var \Closure(\Throwable): \Throwable */
22+
private readonly \Closure $translator,
23+
) {
24+
}
25+
26+
27+
public function query(string $sql, array $params = []): Drivers\Result
28+
{
29+
try {
30+
return new ExceptionHandlingResult(
31+
$this->innerConnection->query($sql, $params),
32+
$this->translator,
33+
);
34+
} catch (\Throwable $e) {
35+
throw ($this->translator)($e, new SqlLiteral($sql), $params);
36+
}
37+
}
38+
39+
40+
public function execute(string $sql): int
41+
{
42+
try {
43+
return $this->innerConnection->execute($sql);
44+
} catch (\Throwable $e) {
45+
throw ($this->translator)($e, new SqlLiteral($sql));
46+
}
47+
}
48+
49+
50+
public function beginTransaction(): void
51+
{
52+
try {
53+
$this->innerConnection->beginTransaction();
54+
} catch (\Throwable $e) {
55+
throw ($this->translator)($e);
56+
}
57+
}
58+
59+
60+
public function commit(): void
61+
{
62+
try {
63+
$this->innerConnection->commit();
64+
} catch (\Throwable $e) {
65+
throw ($this->translator)($e);
66+
}
67+
}
68+
69+
70+
public function rollBack(): void
71+
{
72+
try {
73+
$this->innerConnection->rollBack();
74+
} catch (\Throwable $e) {
75+
throw ($this->translator)($e);
76+
}
77+
}
78+
79+
80+
public function getInsertId(?string $sequence = null): int|string
81+
{
82+
try {
83+
return $this->innerConnection->getInsertId($sequence);
84+
} catch (\Throwable $e) {
85+
throw ($this->translator)($e);
86+
}
87+
}
88+
89+
90+
public function getServerVersion(): string
91+
{
92+
try {
93+
return $this->innerConnection->getServerVersion();
94+
} catch (\Throwable $e) {
95+
throw ($this->translator)($e);
96+
}
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Database\Drivers\Accessory;
11+
12+
use Nette\Database\Drivers;
13+
use Nette\Database\Drivers\Decorator;
14+
15+
16+
class ExceptionHandlingResult extends Decorator\Result
17+
{
18+
public function __construct(
19+
protected Drivers\Result $innerResult,
20+
/** @var \Closure(\Throwable): \Throwable */
21+
private readonly \Closure $translator,
22+
) {
23+
}
24+
25+
26+
public function fetch(): ?array
27+
{
28+
try {
29+
return $this->innerResult->fetch();
30+
} catch (\Throwable $e) {
31+
throw ($this->translator)($e);
32+
}
33+
}
34+
35+
36+
public function fetchList(): ?array
37+
{
38+
try {
39+
return $this->innerResult->fetchList();
40+
} catch (\Throwable $e) {
41+
throw ($this->translator)($e);
42+
}
43+
}
44+
45+
46+
public function getColumnsInfo(): array
47+
{
48+
try {
49+
return $this->innerResult->getColumnsInfo();
50+
} catch (\Throwable $e) {
51+
throw ($this->translator)($e);
52+
}
53+
}
54+
}

src/Database/Drivers/Accessory/LazyConnection.php

+17-54
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,35 @@
1010
namespace Nette\Database\Drivers\Accessory;
1111

1212
use Nette\Database\Drivers;
13+
use Nette\Database\Drivers\Decorator;
1314

1415

15-
final class LazyConnection implements Drivers\Connection
16+
final class LazyConnection extends Decorator\Connection
1617
{
1718
public function __construct(
18-
private \Closure $callback,
19+
/** @var \Closure(self): void */
20+
private \Closure $factory,
1921
) {
22+
unset($this->innerConnection);
2023
}
2124

2225

23-
private function getConnection(): Drivers\Connection
26+
public function setInnerConnection(?Drivers\Connection $connection): static
2427
{
25-
return ($this->callback)();
28+
if ($connection) {
29+
$this->innerConnection = $connection;
30+
} else {
31+
unset($this->innerConnection);
32+
}
33+
return $this;
2634
}
2735

2836

29-
public function query(string $sql, array $params = []): Drivers\Result
37+
public function __get(string $name): mixed
3038
{
31-
return $this->getConnection()->query($sql, $params);
32-
}
33-
34-
35-
public function execute(string $sql): int
36-
{
37-
return $this->getConnection()->execute($sql);
38-
}
39-
40-
41-
public function getNativeConnection(): mixed
42-
{
43-
return $this->getConnection()->getNativeConnection();
44-
}
45-
46-
47-
public function beginTransaction(): void
48-
{
49-
$this->getConnection()->beginTransaction();
50-
}
51-
52-
53-
public function commit(): void
54-
{
55-
$this->getConnection()->commit();
56-
}
57-
58-
59-
public function rollBack(): void
60-
{
61-
$this->getConnection()->rollBack();
62-
}
63-
64-
65-
public function getInsertId(?string $sequence = null): int|string
66-
{
67-
return $this->getConnection()->getInsertId($sequence);
68-
}
69-
70-
71-
public function quote(string $string): string
72-
{
73-
return $this->getConnection()->quote($string);
74-
}
75-
76-
77-
public function getServerVersion(): string
78-
{
79-
return $this->getConnection()->getServerVersion();
39+
if ($name === 'innerConnection') {
40+
($this->factory)($this);
41+
return $this->innerConnection ?? null;
42+
}
8043
}
8144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Database\Drivers\Accessory;
11+
12+
use Nette\Database\Drivers;
13+
use Nette\Database\Drivers\Decorator;
14+
use Nette\Database\SqlLiteral;
15+
16+
17+
class LoggingConnection extends Decorator\Connection
18+
{
19+
public function __construct(
20+
protected Drivers\Connection $innerConnection,
21+
/** @var \Closure(mixed, SqlLiteral, ?float): void */
22+
private \Closure $logger,
23+
) {
24+
}
25+
26+
27+
public function query(string $sql, array $params = []): Drivers\Result
28+
{
29+
return $this->logOperation(
30+
fn() => $this->innerConnection->query($sql, $params),
31+
new SqlLiteral($sql, $params),
32+
);
33+
}
34+
35+
36+
public function execute(string $sql): int
37+
{
38+
return $this->logOperation(
39+
fn() => $this->innerConnection->execute($sql),
40+
new SqlLiteral($sql),
41+
);
42+
}
43+
44+
45+
public function beginTransaction(): void
46+
{
47+
$this->logOperation(
48+
fn() => $this->innerConnection->beginTransaction(),
49+
new SqlLiteral('BEGIN TRANSACTION'),
50+
);
51+
}
52+
53+
54+
public function commit(): void
55+
{
56+
$this->logOperation(
57+
fn() => $this->innerConnection->commit(),
58+
new SqlLiteral('COMMIT'),
59+
);
60+
}
61+
62+
63+
public function rollBack(): void
64+
{
65+
$this->logOperation(
66+
fn() => $this->innerConnection->rollBack(),
67+
new SqlLiteral('ROLLBACK'),
68+
);
69+
}
70+
71+
72+
private function logOperation(callable $operation, SqlLiteral $query): mixed
73+
{
74+
$time = microtime(true);
75+
try {
76+
$result = $operation();
77+
} catch (\Throwable $e) {
78+
($this->logger)($e, $query, null);
79+
throw $e;
80+
}
81+
82+
$time = microtime(true) - $time;
83+
($this->logger)($result, $query, $time);
84+
return $result;
85+
}
86+
}

0 commit comments

Comments
 (0)