Skip to content

Commit f527cac

Browse files
committed
try/catch checking and converting of PDOException moved to drivers
1 parent 6965ac6 commit f527cac

27 files changed

Lines changed: 185 additions & 193 deletions

src/Bridges/DatabaseTracy/ConnectionPanel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private function logQuery(Connection $connection, $result): void
8080
$this->count++;
8181

8282
$source = null;
83-
$trace = $result instanceof \PDOException
83+
$trace = $result instanceof \Exception
8484
? $result->getTrace()
8585
: debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
8686
foreach ($trace as $row) {
@@ -101,15 +101,15 @@ private function logQuery(Connection $connection, $result): void
101101
if ($this->count < $this->maxQueries) {
102102
$this->queries[] = [$connection, $result->getQueryString(), $result->getParameters(), $source, $result->getTime(), $result->getRowCount(), null];
103103
}
104-
} elseif ($result instanceof \PDOException && $this->count < $this->maxQueries) {
104+
} elseif ($result instanceof \Exception && $this->count < $this->maxQueries) {
105105
$this->queries[] = [$connection, $result->queryString, null, $source, null, null, $result->getMessage()];
106106
}
107107
}
108108

109109

110110
public static function renderException(?\Throwable $e): ?array
111111
{
112-
if (!$e instanceof \PDOException) {
112+
if (!$e instanceof \Exception) {
113113
return null;
114114
}
115115

src/Database/Connection.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use JetBrains\PhpStorm\Language;
1313
use Nette\Utils\Arrays;
14-
use PDOException;
1514

1615

1716
/**
@@ -57,18 +56,11 @@ public function __construct(
5756

5857
public function connect(): void
5958
{
60-
if ($this->connection) {
61-
return;
62-
}
63-
64-
try {
59+
if (!$this->connection) {
6560
$this->connection = $this->driver->connect();
6661
$this->engine = $this->driver->createDatabaseEngine($this->connection);
67-
} catch (PDOException $e) {
68-
throw ConnectionException::from($e);
62+
Arrays::invoke($this->onConnect, $this);
6963
}
70-
71-
Arrays::invoke($this->onConnect, $this);
7264
}
7365

7466

@@ -148,11 +140,7 @@ public function setRowNormalizer(?callable $normalizer): static
148140

149141
public function getInsertId(?string $sequence = null): string
150142
{
151-
try {
152-
return $this->getConnectionDriver()->getInsertId($sequence);
153-
} catch (PDOException $e) {
154-
throw $this->engine->convertException($e);
155-
}
143+
return $this->getConnectionDriver()->getInsertId($sequence);
156144
}
157145

158146

@@ -228,7 +216,7 @@ public function query(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
228216
[$this->sql, $params] = $this->preprocess($sql, ...$params);
229217
try {
230218
$result = new Result($this, $this->sql, $params, $this->rowNormalizer);
231-
} catch (PDOException $e) {
219+
} catch (DriverException $e) {
232220
Arrays::invoke($this->onQuery, $this, $e);
233221
throw $e;
234222
}

src/Database/DriverException.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,40 @@
1313
/**
1414
* Base class for all errors in the driver or SQL server.
1515
*/
16-
class DriverException extends \PDOException
16+
class DriverException extends \Exception
1717
{
18-
public ?string $queryString = null;
19-
public ?array $params = null;
20-
21-
22-
public static function from(\PDOException $src): static
23-
{
24-
$e = new static($src->message, 0, $src);
25-
$e->file = $src->file;
26-
$e->line = $src->line;
27-
if (!$src->errorInfo && preg_match('#SQLSTATE\[(.*?)\] \[(.*?)\] (.*)#A', $src->message, $m)) {
28-
$m[2] = (int) $m[2];
29-
$e->errorInfo = array_slice($m, 1);
30-
$e->code = $m[1];
31-
} else {
32-
$e->errorInfo = $src->errorInfo;
33-
$e->code = $src->code;
34-
$e->code = $e->errorInfo[0] ?? $src->code;
35-
}
36-
37-
return $e;
18+
public function __construct(
19+
string $message,
20+
private readonly ?string $sqlState = null,
21+
private int $driverCode = 0,
22+
private readonly ?SqlLiteral $query = null,
23+
?\Throwable $previous = null,
24+
) {
25+
parent::__construct($message, 0, $previous);
26+
$this->code = $sqlState ?: null;
3827
}
3928

4029

4130
public function getDriverCode(): int|string|null
4231
{
43-
return $this->errorInfo[1] ?? null;
32+
return $this->driverCode ?: null;
4433
}
4534

4635

4736
public function getSqlState(): ?string
4837
{
49-
return $this->errorInfo[0] ?? null;
38+
return $this->sqlState;
5039
}
5140

5241

5342
public function getQueryString(): ?string
5443
{
55-
return $this->queryString;
44+
return $this->query?->getSql();
5645
}
5746

5847

5948
public function getParameters(): ?array
6049
{
61-
return $this->params;
50+
return $this->query?->getParameters();
6251
}
6352
}

src/Database/Drivers/Engine.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace Nette\Database\Drivers;
1111

12-
use Nette\Database;
1312
use Nette\Database\TypeConverter;
1413

1514

@@ -27,9 +26,9 @@ interface Engine
2726
SupportSchema = 'schema';
2827

2928
/**
30-
* Converts PDOException to DriverException or its descendant.
29+
* Suggests an appropriate class for the exception.
3130
*/
32-
function convertException(\PDOException $e): Database\DriverException;
31+
static function determineExceptionClass(int $code, ?string $sqlState, string $message): ?string;
3332

3433
/**
3534
* Delimites identifier for use in a SQL statement.

src/Database/Drivers/Engines/MSSQLEngine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function __construct(
2626
}
2727

2828

29-
public function convertException(\PDOException $e): Nette\Database\DriverException
29+
public static function determineExceptionClass(int $code, ?string $sqlState, string $message): ?string
3030
{
31-
return Nette\Database\DriverException::from($e);
31+
return null;
3232
}
3333

3434

src/Database/Drivers/Engines/MySQLEngine.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,15 @@ public function __construct(
2626
}
2727

2828

29-
public function convertException(\PDOException $e): Nette\Database\DriverException
29+
public static function determineExceptionClass(int $code, ?string $sqlState, string $message): ?string
3030
{
31-
$code = $e->errorInfo[1] ?? null;
32-
if (in_array($code, [1216, 1217, 1451, 1452, 1701], strict: true)) {
33-
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
34-
35-
} elseif (in_array($code, [1062, 1557, 1569, 1586], strict: true)) {
36-
return Nette\Database\UniqueConstraintViolationException::from($e);
37-
38-
} elseif ($code >= 2001 && $code <= 2028) {
39-
return Nette\Database\ConnectionException::from($e);
40-
41-
} elseif (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], strict: true)) {
42-
return Nette\Database\NotNullConstraintViolationException::from($e);
43-
44-
} else {
45-
return Nette\Database\DriverException::from($e);
46-
}
31+
return match (true) {
32+
in_array($code, [1216, 1217, 1451, 1452, 1701], strict: true) => Nette\Database\ForeignKeyConstraintViolationException::class,
33+
in_array($code, [1062, 1557, 1569, 1586], strict: true) => Nette\Database\UniqueConstraintViolationException::class,
34+
$code >= 2001 && $code <= 2028 => Nette\Database\ConnectionException::class,
35+
in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], strict: true) => Nette\Database\NotNullConstraintViolationException::class,
36+
default => null,
37+
};
4738
}
4839

4940

src/Database/Drivers/Engines/ODBCEngine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
*/
2020
class ODBCEngine implements Engine
2121
{
22-
public function convertException(\PDOException $e): Nette\Database\DriverException
22+
public static function determineExceptionClass(int $code, ?string $sqlState, string $message): ?string
2323
{
24-
return Nette\Database\DriverException::from($e);
24+
return null;
2525
}
2626

2727

src/Database/Drivers/Engines/OracleEngine.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,14 @@ public function __construct(
2929
}
3030

3131

32-
public function convertException(\PDOException $e): Nette\Database\DriverException
32+
public static function determineExceptionClass(int $code, ?string $sqlState, string $message): ?string
3333
{
34-
$code = $e->errorInfo[1] ?? null;
35-
if (in_array($code, [1, 2299, 38911], strict: true)) {
36-
return Nette\Database\UniqueConstraintViolationException::from($e);
37-
38-
} elseif (in_array($code, [1400], strict: true)) {
39-
return Nette\Database\NotNullConstraintViolationException::from($e);
40-
41-
} elseif (in_array($code, [2266, 2291, 2292], strict: true)) {
42-
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
43-
44-
} else {
45-
return Nette\Database\DriverException::from($e);
46-
}
34+
return match (true) {
35+
in_array($code, [1, 2299, 38911], strict: true) => Nette\Database\UniqueConstraintViolationException::class,
36+
in_array($code, [1400], strict: true) => Nette\Database\NotNullConstraintViolationException::class,
37+
in_array($code, [2266, 2291, 2292], strict: true) => Nette\Database\ForeignKeyConstraintViolationException::class,
38+
default => null,
39+
};
4740
}
4841

4942

src/Database/Drivers/Engines/PostgreSQLEngine.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,16 @@ public function __construct(
2626
}
2727

2828

29-
public function convertException(\PDOException $e): Nette\Database\DriverException
29+
public static function determineExceptionClass(int $code, ?string $sqlState, string $message): ?string
3030
{
31-
$code = $e->errorInfo[0] ?? null;
32-
if ($code === '0A000' && str_contains($e->getMessage(), 'truncate')) {
33-
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
34-
35-
} elseif ($code === '23502') {
36-
return Nette\Database\NotNullConstraintViolationException::from($e);
37-
38-
} elseif ($code === '23503') {
39-
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
40-
41-
} elseif ($code === '23505') {
42-
return Nette\Database\UniqueConstraintViolationException::from($e);
43-
44-
} elseif ($code === '08006') {
45-
return Nette\Database\ConnectionException::from($e);
46-
47-
} else {
48-
return Nette\Database\DriverException::from($e);
49-
}
31+
return match ($sqlState) {
32+
'0A000' => str_contains($message, 'truncate') ? Nette\Database\ForeignKeyConstraintViolationException::class : null,
33+
'23502' => Nette\Database\NotNullConstraintViolationException::class,
34+
'23503' => Nette\Database\ForeignKeyConstraintViolationException::class,
35+
'23505' => Nette\Database\UniqueConstraintViolationException::class,
36+
'08006' => Nette\Database\ConnectionException::class,
37+
default => null,
38+
};
5039
}
5140

5241

src/Database/Drivers/Engines/SQLServerEngine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function __construct(
2626
}
2727

2828

29-
public function convertException(\PDOException $e): Nette\Database\DriverException
29+
public static function determineExceptionClass(int $code, ?string $sqlState, string $message): ?string
3030
{
31-
return Nette\Database\DriverException::from($e);
31+
return null;
3232
}
3333

3434

0 commit comments

Comments
 (0)