Skip to content

Commit c8fa3e3

Browse files
author
igor
committed
Add Statement Iterator
1 parent d4a2569 commit c8fa3e3

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ print_r($statement->info());
158158
// if clickhouse-server version >= 54011
159159
$db->settings()->set('output_format_write_statistics',true);
160160
print_r($statement->statistics());
161+
162+
163+
// Statement Iterator
164+
$state=$this->client->select('SELECT (number+1) as nnums FROM system.numbers LIMIT 5');
165+
foreach ($state as $key=>$value) {
166+
echo $value['nnums'];
167+
}
168+
161169
```
162170

163171
Select result as tree:

src/Statement.php

+26-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use ClickHouseDB\Transport\CurlerRequest;
1010
use ClickHouseDB\Transport\CurlerResponse;
1111

12-
class Statement
12+
class Statement implements \Iterator
1313
{
1414
/**
1515
* @var string|mixed
@@ -339,14 +339,6 @@ public function totals()
339339
return $this->totals;
340340
}
341341

342-
/**
343-
*
344-
*/
345-
public function dumpRaw()
346-
{
347-
print_r($this->_rawData);
348-
}
349-
350342
/**
351343
*
352344
*/
@@ -580,4 +572,29 @@ private function array_to_tree($arr, $path = null)
580572
}
581573
return $tree;
582574
}
575+
576+
577+
public function rewind(): void {
578+
$this->iterator = 0;
579+
}
580+
581+
public function current() {
582+
if (!isset($this->array_data[$this->iterator])) {
583+
return null;
584+
}
585+
return $this->array_data[$this->iterator];
586+
}
587+
588+
public function key(): int {
589+
return $this->iterator;
590+
}
591+
592+
public function next(): void {
593+
++$this->iterator;
594+
}
595+
596+
public function valid(): bool {
597+
$this->init();
598+
return isset($this->array_data[$this->iterator]);
599+
}
583600
}

tests/ClientTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,18 @@ public function testInsertNestedArray()
273273

274274
}
275275

276+
277+
public function testStatementIterator()
278+
{
279+
$calc=0;
280+
$state=$this->client->select('SELECT (number+1) as nnums FROM system.numbers LIMIT 5');
281+
foreach ($state as $key=>$value) {
282+
$calc+=$value['nnums'];
283+
}
284+
$this->assertEquals(15,$calc);
285+
}
286+
287+
276288
public function testRFCCSVAndTSVWrite()
277289
{
278290

tests/ConditionsTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,8 @@ public function testSqlConditions()
224224
'|ZERO|| FORMAT JSON',
225225
$this->client->selectAsync('{if FALSE}FALSE{/if}|{if ZERO}ZERO{/if}|{if NULL}NULL{/if}| ' ,$isset)->sql()
226226
);
227-
228-
229-
230227
}
231228

232-
233229
public function testSqlDisableConditions()
234230
{
235231
$this->assertEquals('SELECT * FROM ZZZ {if limit}LIMIT {limit}{/if} FORMAT JSON', $this->client->selectAsync('SELECT * FROM ZZZ {if limit}LIMIT {limit}{/if}', [])->sql());

0 commit comments

Comments
 (0)