From 14a4af9823922a338364ee6d1e86ae1cda2eae52 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 10 Feb 2022 20:16:55 +0300 Subject: [PATCH 1/2] Fixed issue with non-empty raw data processing during init() on every fetchRow() and fetchOne() call --- src/Statement.php | 34 ++++++++++++++++++---------------- tests/FetchTest.php | 13 +++++++++++-- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Statement.php b/src/Statement.php index f9a3030..8b4b302 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -84,7 +84,7 @@ class Statement /** * @var int */ - public $iterator=0; + public $iterator = 0; public function __construct(CurlerRequest $request) @@ -214,6 +214,14 @@ private function check() : bool return true; } + /** + * @return bool + */ + public function isInited() + { + return $this->_init; + } + /** * @return bool * @throws Exception\TransportException @@ -224,33 +232,29 @@ 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); @@ -258,12 +262,9 @@ private function init() 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']]; } @@ -272,6 +273,7 @@ private function init() $this->array_data[] = $r; } + $this->_init = true; return true; } diff --git a/tests/FetchTest.php b/tests/FetchTest.php index 4ba6aad..6a48a1a 100644 --- a/tests/FetchTest.php +++ b/tests/FetchTest.php @@ -2,7 +2,6 @@ namespace ClickHouseDB\Tests; -use ClickHouseDB\Exception\QueryException; use PHPUnit\Framework\TestCase; /** @@ -15,7 +14,6 @@ final class FetchTest extends TestCase use WithClient; - public function testFetchRowKeys() { $result = $this->client->select( @@ -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( @@ -52,4 +51,14 @@ public function testFetchOne() $this->assertEquals(1,$result->fetchRow('number')); $this->assertEquals(2,$result->fetchRow('number')); } + + public function testCorrentInitOnFetchRow() + { + $result = $this->client->select( + 'SELECT number FROM system.numbers LIMIT 5' + ); + + $result->fetchRow(); + $this->assertTrue($result->isInited()); + } } From 25dc5dba444f990018f62fec4d31243ceaf35902 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 11 Feb 2022 15:50:42 +0300 Subject: [PATCH 2/2] Fixed typo on test method name --- tests/FetchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/FetchTest.php b/tests/FetchTest.php index 6a48a1a..b7fb56d 100644 --- a/tests/FetchTest.php +++ b/tests/FetchTest.php @@ -52,7 +52,7 @@ public function testFetchOne() $this->assertEquals(2,$result->fetchRow('number')); } - public function testCorrentInitOnFetchRow() + public function testCorrectInitOnFetchRow() { $result = $this->client->select( 'SELECT number FROM system.numbers LIMIT 5'