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 function Amp\call; |
16
|
|
|
use Amp\Postgres\PgSqlCommandResult; |
17
|
|
|
use Amp\Postgres\PooledResultSet; |
18
|
|
|
use Amp\Postgres\PqCommandResult; |
19
|
|
|
use Amp\Promise; |
20
|
|
|
use Amp\Sql\ResultSet as AmpResultSet; |
21
|
|
|
use Amp\Success; |
22
|
|
|
use ServiceBus\Storage\Common\Exceptions\ResultSetIterationFailed; |
23
|
|
|
use ServiceBus\Storage\Common\ResultSet; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
class AmpPostgreSQLResultSet implements ResultSet |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var AmpResultSet|PgSqlCommandResult|PooledResultSet|PqCommandResult |
32
|
|
|
*/ |
33
|
|
|
private $originalResultSet; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $advanceCalled = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @noinspection PhpDocSignatureInspection |
42
|
|
|
* |
43
|
|
|
* @psalm-suppress TypeCoercion Assume a different data type |
44
|
|
|
* |
45
|
|
|
* @param AmpResultSet|PgSqlCommandResult|PooledResultSet|PqCommandResult $originalResultSet |
46
|
|
|
*/ |
47
|
21 |
|
public function __construct(object $originalResultSet) |
48
|
|
|
{ |
49
|
21 |
|
$this->originalResultSet = $originalResultSet; |
50
|
21 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
15 |
|
public function advance(): Promise |
56
|
|
|
{ |
57
|
15 |
|
$this->advanceCalled = true; |
58
|
|
|
|
59
|
|
|
try |
60
|
|
|
{ |
61
|
15 |
|
if ($this->originalResultSet instanceof AmpResultSet) |
62
|
|
|
{ |
63
|
14 |
|
return $this->originalResultSet->advance(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return new Success(false); |
67
|
|
|
} |
68
|
|
|
// @codeCoverageIgnoreStart |
69
|
|
|
catch (\Throwable $throwable) |
70
|
|
|
{ |
71
|
|
|
throw new ResultSetIterationFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable); |
72
|
|
|
} |
73
|
|
|
// @codeCoverageIgnoreEnd |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
10 |
|
public function getCurrent(): ?array |
80
|
|
|
{ |
81
|
|
|
try |
82
|
|
|
{ |
83
|
|
|
if ( |
84
|
10 |
|
$this->originalResultSet instanceof PgSqlCommandResult || |
85
|
10 |
|
$this->originalResultSet instanceof PqCommandResult |
86
|
|
|
) { |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @var array<string, float|int|resource|string|null>|null $data */ |
91
|
10 |
|
$data = $this->originalResultSet->getCurrent(); |
92
|
|
|
|
93
|
10 |
|
return $data; |
94
|
|
|
} |
95
|
|
|
// @codeCoverageIgnoreStart |
96
|
|
|
catch (\Throwable $throwable) |
97
|
|
|
{ |
98
|
|
|
throw new ResultSetIterationFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable); |
99
|
|
|
} |
100
|
|
|
// @codeCoverageIgnoreEnd |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
* |
106
|
|
|
* @psalm-suppress MixedReturnTypeCoercion |
107
|
|
|
*/ |
108
|
2 |
|
public function lastInsertId(?string $sequence = null): Promise |
109
|
|
|
{ |
110
|
2 |
|
return call( |
111
|
|
|
function (): \Generator |
112
|
|
|
{ |
113
|
|
|
try |
114
|
|
|
{ |
115
|
2 |
|
if ($this->originalResultSet instanceof PooledResultSet) |
116
|
|
|
{ |
117
|
1 |
|
if ($this->advanceCalled === false) |
118
|
|
|
{ |
119
|
1 |
|
yield $this->originalResultSet->advance(); |
120
|
|
|
|
121
|
1 |
|
$this->advanceCalled = true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** @var array<string, mixed> $result */ |
125
|
1 |
|
$result = $this->originalResultSet->getCurrent(); |
126
|
|
|
|
127
|
1 |
|
if (0 !== \count($result)) |
128
|
|
|
{ |
129
|
|
|
/** @var bool|int|string $value */ |
130
|
1 |
|
$value = \reset($result); |
131
|
|
|
|
132
|
1 |
|
if (false !== $value) |
133
|
|
|
{ |
134
|
2 |
|
return (string) $value; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
// @codeCoverageIgnoreStart |
140
|
|
|
catch (\Throwable $throwable) |
141
|
|
|
{ |
142
|
|
|
throw new ResultSetIterationFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable); |
143
|
|
|
} |
144
|
|
|
// @codeCoverageIgnoreEnd |
145
|
2 |
|
} |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
1 |
|
public function affectedRows(): int |
153
|
|
|
{ |
154
|
|
|
try |
155
|
|
|
{ |
156
|
|
|
if ( |
157
|
1 |
|
$this->originalResultSet instanceof PgSqlCommandResult || |
158
|
1 |
|
$this->originalResultSet instanceof PqCommandResult |
159
|
|
|
) { |
160
|
1 |
|
return $this->originalResultSet->getAffectedRowCount(); |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
return 0; |
164
|
|
|
} |
165
|
|
|
// @codeCoverageIgnoreStart |
166
|
|
|
catch (\Throwable $throwable) |
167
|
|
|
{ |
168
|
|
|
throw new ResultSetIterationFailed($throwable->getMessage(), (int) $throwable->getCode(), $throwable); |
169
|
|
|
} |
170
|
|
|
// @codeCoverageIgnoreEnd |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|