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\DoctrineDBAL; |
14
|
|
|
|
15
|
|
|
use Amp\Promise; |
16
|
|
|
use Amp\Success; |
17
|
|
|
use Doctrine\DBAL\Connection; |
18
|
|
|
use Doctrine\DBAL\Driver\Statement; |
19
|
|
|
use ServiceBus\Storage\Common\ResultSet; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
final class DoctrineDBALResultSet implements ResultSet |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Last row emitted. |
28
|
|
|
* |
29
|
|
|
* @var array|null |
30
|
|
|
*/ |
31
|
|
|
private $currentRow; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Pdo fetch result. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $fetchResult; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Results count. |
42
|
|
|
* |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $resultsCount; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Current iterator position. |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
private $currentPosition = 0; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Connection instance. |
56
|
|
|
* |
57
|
|
|
* @var Connection |
58
|
|
|
*/ |
59
|
|
|
private $connection; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Number of rows affected by the last DELETE, INSERT, or UPDATE statement. |
63
|
|
|
* |
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
private $affectedRows; |
67
|
|
|
|
68
|
25 |
|
public function __construct(Connection $connection, Statement $wrappedStmt) |
69
|
|
|
{ |
70
|
25 |
|
$rows = $wrappedStmt->fetchAll(); |
71
|
|
|
|
72
|
25 |
|
$this->connection = $connection; |
73
|
25 |
|
$this->fetchResult = $rows; |
74
|
25 |
|
$this->affectedRows = $wrappedStmt->rowCount(); |
75
|
25 |
|
$this->resultsCount = \count($this->fetchResult); |
76
|
25 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* |
81
|
|
|
* @psalm-suppress MixedReturnTypeCoercion |
82
|
|
|
*/ |
83
|
18 |
|
public function advance(): Promise |
84
|
|
|
{ |
85
|
18 |
|
$this->currentRow = null; |
86
|
|
|
|
87
|
18 |
|
if (++$this->currentPosition > $this->resultsCount) |
88
|
|
|
{ |
89
|
18 |
|
return new Success(false); |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
return new Success(true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
14 |
|
public function getCurrent(): ?array |
99
|
|
|
{ |
100
|
14 |
|
if (null !== $this->currentRow) |
101
|
|
|
{ |
102
|
|
|
/** |
103
|
|
|
* @psalm-var array<string, float|int|resource|string|null>|null $row |
104
|
|
|
* |
105
|
|
|
* @var array $row |
106
|
|
|
*/ |
107
|
1 |
|
$row = $this->currentRow; |
108
|
|
|
|
109
|
1 |
|
return $row; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @psalm-var array<string, float|int|resource|string|null>|null $data |
114
|
|
|
* |
115
|
|
|
* @var array $row |
116
|
|
|
*/ |
117
|
14 |
|
$data = $this->fetchResult[$this->currentPosition - 1] ?? null; |
118
|
|
|
|
119
|
14 |
|
if (\is_array($data) === true && \count($data) === 0) |
120
|
|
|
{ |
121
|
|
|
$data = null; |
122
|
|
|
} |
123
|
|
|
|
124
|
14 |
|
return $this->currentRow = $data; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
* |
130
|
|
|
* @psalm-suppress MixedReturnTypeCoercion |
131
|
|
|
*/ |
132
|
1 |
|
public function lastInsertId(?string $sequence = null): Promise |
133
|
|
|
{ |
134
|
1 |
|
return new Success($this->connection->lastInsertId($sequence)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
1 |
|
public function affectedRows(): int |
141
|
|
|
{ |
142
|
1 |
|
return $this->affectedRows; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|