Skip to content

Commit

Permalink
Merge pull request #161 from anathex/master
Browse files Browse the repository at this point in the history
Fixed issue with non-empty raw data processing during init() on every fetchRow() and fetchOne() call
  • Loading branch information
isublimity committed Feb 11, 2022
2 parents 990354d + 25dc5db commit be18663
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
34 changes: 18 additions & 16 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Statement
/**
* @var int
*/
public $iterator=0;
public $iterator = 0;


public function __construct(CurlerRequest $request)
Expand Down Expand Up @@ -214,6 +214,14 @@ private function check() : bool
return true;
}

/**
* @return bool
*/
public function isInited()
{
return $this->_init;
}

/**
* @return bool
* @throws Exception\TransportException
Expand All @@ -224,46 +232,39 @@ private function init()
return false;
}


$this->check();


$this->_rawData = $this->response()->rawDataOrJson($this->format);

if (!$this->_rawData) {
$this->_init = true;
return false;
}
$data=[];

$data = [];
foreach (['meta', 'data', 'totals', 'extremes', 'rows', 'rows_before_limit_at_least', 'statistics'] as $key) {

if (isset($this->_rawData[$key])) {
if ($key=='data')
{
if ($key=='data') {
$data=$this->_rawData[$key];
}
else{
} else {
$this->{$key} = $this->_rawData[$key];
}

}
}

if (empty($this->meta)) {
throw new QueryException('Can`t find meta');
throw new QueryException('Can`t find meta');
}

$isJSONCompact=(stripos($this->format,'JSONCompact')!==false?true:false);
$this->array_data = [];
foreach ($data as $rows) {
$r = [];


if ($isJSONCompact)
{
$r[]=$rows;
}
else {
if ($isJSONCompact) {
$r[] = $rows;
} else {
foreach ($this->meta as $meta) {
$r[$meta['name']] = $rows[$meta['name']];
}
Expand All @@ -272,6 +273,7 @@ private function init()
$this->array_data[] = $r;
}

$this->_init = true;

return true;
}
Expand Down
13 changes: 11 additions & 2 deletions tests/FetchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ClickHouseDB\Tests;

use ClickHouseDB\Exception\QueryException;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -15,7 +14,6 @@ final class FetchTest extends TestCase
use WithClient;



public function testFetchRowKeys()
{
$result = $this->client->select(
Expand All @@ -33,6 +31,7 @@ public function testFetchRowKeys()
$this->assertEquals(null,$result->fetchOne('q'));
$this->assertEquals(0,$result->fetchOne('number'));
}

public function testFetchOne()
{
$result = $this->client->select(
Expand All @@ -52,4 +51,14 @@ public function testFetchOne()
$this->assertEquals(1,$result->fetchRow('number'));
$this->assertEquals(2,$result->fetchRow('number'));
}

public function testCorrectInitOnFetchRow()
{
$result = $this->client->select(
'SELECT number FROM system.numbers LIMIT 5'
);

$result->fetchRow();
$this->assertTrue($result->isInited());
}
}

0 comments on commit be18663

Please sign in to comment.