-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
639 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Database/Drivers/Accessory/ExceptionHandlingConnection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
||
public function execute(string $sql): int | ||
{ | ||
try { | ||
return $this->innerConnection->execute($sql); | ||
} catch (\Throwable $e) { | ||
throw ($this->translator)($e, new SqlLiteral($sql)); | ||
} | ||
} | ||
|
||
|
||
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
54
src/Database/Drivers/Accessory/ExceptionHandlingResult.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.