Skip to content

Commit f275dd2

Browse files
committed
added executed query to result instance
1 parent 01f28f9 commit f275dd2

File tree

4 files changed

+76
-36
lines changed

4 files changed

+76
-36
lines changed

src/Query/Result.php

+55-24
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace Tinderbox\Clickhouse\Query;
44

55
use Tinderbox\Clickhouse\Exceptions\ResultException;
6+
use Tinderbox\Clickhouse\Query;
67

78
/**
89
* Query result.
910
*
1011
* Container for request results and statistic
1112
*
13+
* @property Query $query
1214
* @property array $rows
1315
* @property QueryStatistic $statistic
1416
*/
@@ -20,33 +22,52 @@ class Result implements \ArrayAccess, \Iterator, \Countable
2022
* @var \Tinderbox\Clickhouse\Query\QueryStatistic
2123
*/
2224
protected $statistic;
23-
25+
2426
/**
2527
* Result of query execution.
2628
*
2729
* @var array
2830
*/
2931
protected $rows;
30-
32+
33+
/**
34+
* Query which was executed to get this result.
35+
*
36+
* @var Query
37+
*/
38+
protected $query;
39+
3140
/**
3241
* Current index for Iterator interface.
3342
*
3443
* @var int
3544
*/
3645
protected $current = 0;
37-
46+
3847
/**
3948
* Result constructor.
4049
*
50+
* @param Query $query
4151
* @param array $rows
4252
* @param \Tinderbox\Clickhouse\Query\QueryStatistic $statistic
4353
*/
44-
public function __construct(array $rows, QueryStatistic $statistic)
54+
public function __construct(Query $query, array $rows, QueryStatistic $statistic)
4555
{
56+
$this->setQuery($query);
4657
$this->setRows($rows);
4758
$this->setStatistic($statistic);
4859
}
49-
60+
61+
/**
62+
* Sets query.
63+
*
64+
* @param Query $query
65+
*/
66+
protected function setQuery(Query $query)
67+
{
68+
$this->query = $query;
69+
}
70+
5071
/**
5172
* Sets statistic.
5273
*
@@ -56,7 +77,7 @@ protected function setStatistic(QueryStatistic $statistic)
5677
{
5778
$this->statistic = $statistic;
5879
}
59-
80+
6081
/**
6182
* Sets rows.
6283
*
@@ -66,7 +87,7 @@ protected function setRows(array $rows)
6687
{
6788
$this->rows = $rows;
6889
}
69-
90+
7091
/**
7192
* Returns rows.
7293
*
@@ -76,7 +97,17 @@ public function getRows(): array
7697
{
7798
return $this->rows;
7899
}
79-
100+
101+
/**
102+
* Returns query.
103+
*
104+
* @return Query
105+
*/
106+
public function getQuery(): Query
107+
{
108+
return $this->query;
109+
}
110+
80111
/**
81112
* Returns statistic.
82113
*
@@ -86,7 +117,7 @@ public function getStatistic(): QueryStatistic
86117
{
87118
return $this->statistic;
88119
}
89-
120+
90121
/**
91122
* Getter to simplify access to rows and statistic.
92123
*
@@ -99,71 +130,71 @@ public function getStatistic(): QueryStatistic
99130
public function __get($name)
100131
{
101132
$method = 'get'.ucfirst($name);
102-
133+
103134
if (method_exists($this, $method)) {
104135
return call_user_func([$this, $method]);
105136
}
106-
137+
107138
throw ResultException::propertyNotExists($name);
108139
}
109-
140+
110141
/*
111142
* ArrayAccess
112143
*/
113-
144+
114145
public function offsetExists($offset)
115146
{
116147
return isset($this->rows[$offset]);
117148
}
118-
149+
119150
public function offsetGet($offset)
120151
{
121152
return $this->rows[$offset];
122153
}
123-
154+
124155
public function offsetSet($offset, $value)
125156
{
126157
throw ResultException::isReadonly();
127158
}
128-
159+
129160
public function offsetUnset($offset)
130161
{
131162
throw ResultException::isReadonly();
132163
}
133-
164+
134165
/*
135166
* Iterator
136167
*/
137-
168+
138169
public function current()
139170
{
140171
return $this->rows[$this->current];
141172
}
142-
173+
143174
public function next()
144175
{
145176
++$this->current;
146177
}
147-
178+
148179
public function key()
149180
{
150181
return $this->current;
151182
}
152-
183+
153184
public function valid()
154185
{
155186
return isset($this->rows[$this->current]);
156187
}
157-
188+
158189
public function rewind()
159190
{
160191
$this->current = 0;
161192
}
162-
193+
163194
/*
164195
* Countable
165196
*/
166-
197+
167198
public function count()
168199
{
169200
return count($this->rows);

src/Transport/HttpTransport.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function read(array $queries, int $concurrency = 5) : array
215215
$this->httpClient, $requests($queries), [
216216
'concurrency' => $concurrency,
217217
'fulfilled' => function ($response, $index) use (&$result, $queries) {
218-
$result[$index] = $this->assembleResult($response);
218+
$result[$index] = $this->assembleResult($queries[$index], $response);
219219
},
220220
'rejected' => function ($response, $index) use ($queries) {
221221
$query = $queries[$index];
@@ -320,11 +320,12 @@ protected function parseReason(Query $query)
320320
/**
321321
* Assembles Result instance from server response.
322322
*
323+
* @param Query $query
323324
* @param \Psr\Http\Message\ResponseInterface $response
324325
*
325326
* @return \Tinderbox\Clickhouse\Query\Result
326327
*/
327-
protected function assembleResult(ResponseInterface $response): Result
328+
protected function assembleResult(Query $query, ResponseInterface $response): Result
328329
{
329330
$response = $response->getBody()->getContents();
330331

@@ -338,7 +339,7 @@ protected function assembleResult(ResponseInterface $response): Result
338339
$result['rows_before_limit_at_least'] ?? null
339340
);
340341

341-
return new Result($result['data'] ?? [], $statistic);
342+
return new Result($query, $result['data'] ?? [], $statistic);
342343
} catch (\Exception $e) {
343344
throw TransportException::malformedResponseFromServer($response);
344345
}

tests/ClientTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testTransports()
5656

5757
$transport = $this->createMock(TransportInterface::class);
5858
$transport->method('read')->willReturn([
59-
new Result([0,1], new QueryStatistic(0,0,0,0))
59+
new Result(new Query($server, ''), [0,1], new QueryStatistic(0,0,0,0))
6060
]);
6161

6262
$client = new Client($serverProvider, null, $transport);

tests/ResultTest.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@ public function testGetters()
3333
'col' => 5,
3434
],
3535
];
36+
$query = new Query(new Server('localhost'), '');
3637
$statistic = new QueryStatistic(5, 1024, 0.122);
3738

38-
$result = new Result($rows, $statistic);
39+
$result = new Result($query, $rows, $statistic);
3940

4041
$this->assertEquals($rows, $result->getRows(), 'Returns rows passed to constructor');
4142
$this->assertEquals($statistic, $result->getStatistic(), 'Returns statistic passed to constructor');
43+
$this->assertEquals($query, $result->getQuery(), 'Returns query passed to constructor');
4244

4345
$this->assertEquals($rows, $result->rows, 'Returns rows passed to constructor via magic property');
4446
$this->assertEquals($statistic, $result->statistic, 'Returns statistic passed to constructor via magic property');
45-
47+
$this->assertEquals($query, $result->query, 'Returns query passed to constructor via magic property');
48+
4649
$e = ResultException::propertyNotExists('miss');
4750
$this->expectException(ResultException::class);
4851
$this->expectExceptionMessage($e->getMessage());
@@ -52,9 +55,10 @@ public function testGetters()
5255

5356
public function testResultCountable()
5457
{
58+
$query = new Query(new Server('localhost'), '');
5559
$statistic = new QueryStatistic(5, 1024, 0.122);
5660

57-
$result = new Result(['', '', ''], $statistic);
61+
$result = new Result($query, ['', '', ''], $statistic);
5862

5963
$this->assertEquals(3, count($result), 'Returns correct rows count via Countable interface');
6064
}
@@ -78,9 +82,10 @@ public function testResultArrayAccessSet()
7882
'col' => 5,
7983
],
8084
];
85+
$query = new Query(new Server('localhost'), '');
8186
$statistic = new QueryStatistic(5, 1024, 0.122);
8287

83-
$result = new Result($rows, $statistic);
88+
$result = new Result($query, $rows, $statistic);
8489

8590
$e = ResultException::isReadonly();
8691
$this->expectException(ResultException::class);
@@ -108,9 +113,10 @@ public function testResultArrayAccessGet()
108113
'col' => 5,
109114
],
110115
];
116+
$query = new Query(new Server('localhost'), '');
111117
$statistic = new QueryStatistic(5, 1024, 0.122);
112118

113-
$result = new Result($rows, $statistic);
119+
$result = new Result($query, $rows, $statistic);
114120
$this->assertTrue(isset($result[1]), 'Correctly determines that offset exists via ArrayAccess interface');
115121
$this->assertFalse(isset($result[10]), 'Correctly determines that offset does not exists via ArrayAccess interface');
116122
$this->assertEquals($rows[0], $result[0], 'Correctly returns offset via ArrayAccess interface');
@@ -135,9 +141,10 @@ public function testResultArrayAccessUnset()
135141
'col' => 5,
136142
],
137143
];
144+
$query = new Query(new Server('localhost'), '');
138145
$statistic = new QueryStatistic(5, 1024, 0.122);
139-
140-
$result = new Result($rows, $statistic);
146+
147+
$result = new Result($query, $rows, $statistic);
141148

142149
$e = ResultException::isReadonly();
143150
$this->expectException(ResultException::class);
@@ -167,9 +174,10 @@ public function testResultIterator()
167174
'col' => 5,
168175
],
169176
];
177+
$query = new Query(new Server('localhost'), '');
170178
$statistic = new QueryStatistic(5, 1024, 0.122);
171179

172-
$result = new Result($rows, $statistic);
180+
$result = new Result($query, $rows, $statistic);
173181

174182
$prev = null;
175183

0 commit comments

Comments
 (0)