1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plasma Core component |
4
|
|
|
* Copyright 2018-2019 PlasmaPHP, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://github.com/PlasmaPHP |
7
|
|
|
* License: https://github.com/PlasmaPHP/core/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Plasma; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A query result stream. Used to get rows row by row, as sent by the DBMS. |
14
|
|
|
* To adhere to the `react/stream` standard,a `close` event |
15
|
|
|
* will be emitted after the `end` or `error` event. |
16
|
|
|
*/ |
17
|
|
|
class StreamQueryResult implements StreamQueryResultInterface { |
18
|
|
|
use \Evenement\EventEmitterTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Plasma\CommandInterface |
22
|
|
|
*/ |
23
|
|
|
protected $command; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $affectedRows; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $warningsCount; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int|null |
37
|
|
|
*/ |
38
|
|
|
protected $insertID; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Plasma\ColumnDefinitionInterface[]|null |
42
|
|
|
*/ |
43
|
|
|
protected $columns; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array|null |
47
|
|
|
*/ |
48
|
|
|
protected $rows; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor. |
52
|
|
|
* @param \Plasma\CommandInterface $command |
53
|
|
|
* @param int $affectedRows |
54
|
|
|
* @param int $warningsCount |
55
|
|
|
* @param int|null $insertID |
56
|
|
|
* @param \Plasma\ColumnDefinitionInterface[]|null $columns |
57
|
|
|
*/ |
58
|
7 |
|
function __construct(\Plasma\CommandInterface $command, int $affectedRows = 0, int $warningsCount = 0, ?int $insertID = null, ?array $columns = null) { |
59
|
7 |
|
$this->command = $command; |
60
|
|
|
|
61
|
7 |
|
$this->affectedRows = $affectedRows; |
62
|
7 |
|
$this->warningsCount = $warningsCount; |
63
|
|
|
|
64
|
7 |
|
$this->insertID = $insertID; |
65
|
7 |
|
$this->columns = $columns; |
66
|
|
|
|
67
|
|
|
$buffer = function ($row) { |
68
|
1 |
|
$this->emit('data', array($row)); |
69
|
7 |
|
}; |
70
|
|
|
|
71
|
7 |
|
$command->on('data', $buffer); |
72
|
|
|
|
73
|
|
|
$command->once('end', function () use ($buffer) { |
74
|
2 |
|
$this->removeListener('data', $buffer); |
75
|
|
|
|
76
|
2 |
|
$this->emit('end'); |
77
|
2 |
|
$this->emit('close'); |
78
|
7 |
|
}); |
79
|
|
|
|
80
|
|
|
$command->once('error', function (\Throwable $error) use ($buffer) { |
81
|
1 |
|
$this->removeListener('data', $buffer); |
82
|
|
|
|
83
|
1 |
|
$this->emit('error', array($error)); |
84
|
1 |
|
$this->emit('close'); |
85
|
7 |
|
}); |
86
|
7 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the number of affected rows (for UPDATE, DELETE, etc.). |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
1 |
|
function getAffectedRows(): int { |
93
|
1 |
|
return $this->affectedRows; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the number of warnings sent by the server. |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
1 |
|
function getWarningsCount(): int { |
101
|
1 |
|
return $this->warningsCount; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the used insert ID for the row, if any. `INSERT` statements only. |
106
|
|
|
* @return int|null |
107
|
|
|
*/ |
108
|
1 |
|
function getInsertID(): ?int { |
109
|
1 |
|
return $this->insertID; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the field definitions, if any. `SELECT` statements only. |
114
|
|
|
* @return \Plasma\ColumnDefinitionInterface[]|null |
115
|
|
|
*/ |
116
|
1 |
|
function getFieldDefinitions(): ?array { |
117
|
1 |
|
return $this->columns; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the rows, if any. Returns always `null`. |
122
|
|
|
* @return array|null |
123
|
|
|
*/ |
124
|
1 |
|
function getRows(): ?array { |
125
|
1 |
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Buffers all rows and returns a promise which resolves with an instance of `QueryResultInterface`. |
130
|
|
|
* This method does not guarantee that all rows get returned, as the buffering depends on when this |
131
|
|
|
* method gets invoked. There's no automatic buffering, as such rows may be missing if invoked too late. |
132
|
|
|
* @return \React\Promise\PromiseInterface |
133
|
|
|
*/ |
134
|
1 |
|
function all(): \React\Promise\PromiseInterface { |
135
|
|
|
return \React\Promise\Stream\all($this)->then(function (array $rows) { |
136
|
1 |
|
return (new \Plasma\QueryResult($this->affectedRows, $this->warningsCount, $this->insertID, $this->columns, $rows)); |
137
|
1 |
|
}); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|