1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SQL databases adapters implementation. |
5
|
|
|
* |
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace ServiceBus\Storage\Sql\AmpPosgreSQL; |
14
|
|
|
|
15
|
|
|
use Amp\Postgres\QueryExecutionError; |
16
|
|
|
use Amp\Sql\ConnectionException; |
17
|
|
|
use ServiceBus\Storage\Common\Exceptions as InternalExceptions; |
18
|
|
|
use ServiceBus\Storage\Common\StorageConfiguration; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Convert AmPHP exceptions. |
22
|
|
|
* |
23
|
|
|
* @internal |
24
|
|
|
* |
25
|
|
|
* @return InternalExceptions\ConnectionFailed|InternalExceptions\StorageInteractingFailed|InternalExceptions\UniqueConstraintViolationCheckFailed |
26
|
|
|
*/ |
27
|
|
|
function adaptAmpThrowable(\Throwable $throwable): \Throwable |
28
|
|
|
{ |
29
|
|
|
if ( |
30
|
4 |
|
$throwable instanceof QueryExecutionError && |
31
|
4 |
|
\in_array((int) $throwable->getDiagnostics()['sqlstate'], [23503, 23505], true) === true |
32
|
|
|
) { |
33
|
2 |
|
return new InternalExceptions\UniqueConstraintViolationCheckFailed( |
34
|
2 |
|
$throwable->getMessage(), |
35
|
2 |
|
(int) $throwable->getCode(), |
36
|
|
|
$throwable |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
if ($throwable instanceof ConnectionException) |
41
|
|
|
{ |
42
|
1 |
|
return new InternalExceptions\ConnectionFailed( |
43
|
1 |
|
$throwable->getMessage(), |
44
|
1 |
|
(int) $throwable->getCode(), |
45
|
|
|
$throwable |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return new InternalExceptions\StorageInteractingFailed( |
50
|
1 |
|
$throwable->getMessage(), |
51
|
1 |
|
(int) $throwable->getCode(), |
52
|
|
|
$throwable |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @internal |
58
|
|
|
* |
59
|
|
|
* @throws InternalExceptions\InvalidConfigurationOptions |
60
|
|
|
*/ |
61
|
|
|
function postgreSqlAdapterFactory(string $connectionDsn): AmpPostgreSQLAdapter |
62
|
|
|
{ |
63
|
|
|
return new AmpPostgreSQLAdapter( |
64
|
|
|
new StorageConfiguration($connectionDsn) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|