1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Ytake\PrestoClient; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class QueryResult |
8
|
|
|
*/ |
9
|
|
|
final class QueryResult |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $id; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $infoUri; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $partialCancelUri; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $nextUri; |
22
|
|
|
|
23
|
|
|
/** @var \stdClass[] */ |
24
|
|
|
private $columns = []; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
private $data = []; |
28
|
|
|
|
29
|
|
|
/** @var StatementStats|null */ |
30
|
|
|
private $stats; |
31
|
|
|
|
32
|
|
|
/** @var QueryError|null */ |
33
|
|
|
private $error; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* QueryResult constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $content |
39
|
|
|
*/ |
40
|
|
|
public function set(string $content) |
41
|
|
|
{ |
42
|
|
|
$parsed = $this->parseContent($content); |
43
|
|
|
$this->id = $parsed->id; |
44
|
|
|
$this->infoUri = $parsed->infoUri; |
45
|
|
|
$this->partialCancelUri = $parsed->partialCancelUri ?? null; |
46
|
|
|
$this->nextUri = $parsed->nextUri ?? null; |
47
|
|
|
$this->columns = []; |
48
|
|
|
if (isset($parsed->columns)) { |
49
|
|
|
$this->columnTransfer($parsed->columns); |
50
|
|
|
} |
51
|
|
|
$this->data = $parsed->data ?? []; |
52
|
|
|
$this->stats = isset($parsed->stats) ? $this->statsTransfer($parsed->stats) : null; |
53
|
|
|
$this->error = isset($parsed->error) ? $this->errorTransfer($parsed->error) : null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string|null |
58
|
|
|
*/ |
59
|
|
|
public function getId() |
60
|
|
|
{ |
61
|
|
|
return $this->id; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string|null |
66
|
|
|
*/ |
67
|
|
|
public function getInfoUri() |
68
|
|
|
{ |
69
|
|
|
return $this->infoUri; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
|
|
public function getNextUri() |
76
|
|
|
{ |
77
|
|
|
return $this->nextUri; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return QueryError|null |
82
|
|
|
*/ |
83
|
|
|
public function getError() |
84
|
|
|
{ |
85
|
|
|
return $this->error; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
|
|
public function getPartialCancelUri() |
92
|
|
|
{ |
93
|
|
|
return $this->partialCancelUri; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return \Generator |
98
|
|
|
*/ |
99
|
|
|
public function getData(): \Generator |
100
|
|
|
{ |
101
|
|
|
if (!count($this->data)) { |
102
|
|
|
yield; |
103
|
|
|
} |
104
|
|
|
foreach ($this->data as $data) { |
105
|
|
|
$fixData = new FixData(); |
106
|
|
|
$column = $this->getColumns(); |
107
|
|
|
for ($i = 0; $i < count($column); $i++) { |
|
|
|
|
108
|
|
|
$fixData->add($column[$i]->getName(), $data[$i]); |
109
|
|
|
} |
110
|
|
|
yield $fixData; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $content |
116
|
|
|
* |
117
|
|
|
* @return \stdClass |
118
|
|
|
*/ |
119
|
|
|
private function parseContent(string $content): \stdClass |
120
|
|
|
{ |
121
|
|
|
$parsed = json_decode($content); |
122
|
|
|
if ($parsed === null && json_last_error() !== JSON_ERROR_NONE) { |
123
|
|
|
throw new \RuntimeException; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $parsed; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param \stdClass $jsonContent |
131
|
|
|
* |
132
|
|
|
* @return StatementStats |
133
|
|
|
*/ |
134
|
|
|
private function statsTransfer(\stdClass $jsonContent): StatementStats |
135
|
|
|
{ |
136
|
|
|
return new StatementStats($jsonContent); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param \stdClass $jsonContent |
141
|
|
|
* |
142
|
|
|
* @return QueryError |
143
|
|
|
*/ |
144
|
|
|
private function errorTransfer(\stdClass $jsonContent): QueryError |
145
|
|
|
{ |
146
|
|
|
return new QueryError($jsonContent); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $columns |
151
|
|
|
*/ |
152
|
|
|
private function columnTransfer(array $columns) |
153
|
|
|
{ |
154
|
|
|
foreach ($columns as $column) { |
155
|
|
|
$this->columns[] = new Column($column); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return StatementStats|null |
161
|
|
|
*/ |
162
|
|
|
public function getStats() |
163
|
|
|
{ |
164
|
|
|
return $this->stats; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return Column[] |
169
|
|
|
*/ |
170
|
|
|
public function getColumns(): array |
171
|
|
|
{ |
172
|
|
|
return $this->columns; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: