Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with non-empty raw data processing during init() on every fetchRow() and fetchOne() call #161

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}