Skip to content

Commit

Permalink
Add Statement Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
igor committed Jun 9, 2022
1 parent d4a2569 commit c8fa3e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ print_r($statement->info());
// if clickhouse-server version >= 54011
$db->settings()->set('output_format_write_statistics',true);
print_r($statement->statistics());


// Statement Iterator
$state=$this->client->select('SELECT (number+1) as nnums FROM system.numbers LIMIT 5');
foreach ($state as $key=>$value) {
echo $value['nnums'];
}

```

Select result as tree:
Expand Down
35 changes: 26 additions & 9 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use ClickHouseDB\Transport\CurlerRequest;
use ClickHouseDB\Transport\CurlerResponse;

class Statement
class Statement implements \Iterator
{
/**
* @var string|mixed
Expand Down Expand Up @@ -339,14 +339,6 @@ public function totals()
return $this->totals;
}

/**
*
*/
public function dumpRaw()
{
print_r($this->_rawData);
}

/**
*
*/
Expand Down Expand Up @@ -580,4 +572,29 @@ private function array_to_tree($arr, $path = null)
}
return $tree;
}


public function rewind(): void {
$this->iterator = 0;
}

public function current() {
if (!isset($this->array_data[$this->iterator])) {
return null;
}
return $this->array_data[$this->iterator];
}

public function key(): int {
return $this->iterator;
}

public function next(): void {
++$this->iterator;
}

public function valid(): bool {
$this->init();
return isset($this->array_data[$this->iterator]);
}
}
12 changes: 12 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ public function testInsertNestedArray()

}


public function testStatementIterator()
{
$calc=0;
$state=$this->client->select('SELECT (number+1) as nnums FROM system.numbers LIMIT 5');
foreach ($state as $key=>$value) {
$calc+=$value['nnums'];
}
$this->assertEquals(15,$calc);
}


public function testRFCCSVAndTSVWrite()
{

Expand Down
4 changes: 0 additions & 4 deletions tests/ConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,8 @@ public function testSqlConditions()
'|ZERO|| FORMAT JSON',
$this->client->selectAsync('{if FALSE}FALSE{/if}|{if ZERO}ZERO{/if}|{if NULL}NULL{/if}| ' ,$isset)->sql()
);



}


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

0 comments on commit c8fa3e3

Please sign in to comment.