|
| 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 | +} |
0 commit comments