Skip to content

Commit be18663

Browse files
authored
Merge pull request smi2#161 from anathex/master
Fixed issue with non-empty raw data processing during init() on every fetchRow() and fetchOne() call
2 parents 990354d + 25dc5db commit be18663

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/Statement.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Statement
8484
/**
8585
* @var int
8686
*/
87-
public $iterator=0;
87+
public $iterator = 0;
8888

8989

9090
public function __construct(CurlerRequest $request)
@@ -214,6 +214,14 @@ private function check() : bool
214214
return true;
215215
}
216216

217+
/**
218+
* @return bool
219+
*/
220+
public function isInited()
221+
{
222+
return $this->_init;
223+
}
224+
217225
/**
218226
* @return bool
219227
* @throws Exception\TransportException
@@ -224,46 +232,39 @@ private function init()
224232
return false;
225233
}
226234

227-
228235
$this->check();
229236

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

233239
if (!$this->_rawData) {
234240
$this->_init = true;
235241
return false;
236242
}
237-
$data=[];
243+
244+
$data = [];
238245
foreach (['meta', 'data', 'totals', 'extremes', 'rows', 'rows_before_limit_at_least', 'statistics'] as $key) {
239246

240247
if (isset($this->_rawData[$key])) {
241-
if ($key=='data')
242-
{
248+
if ($key=='data') {
243249
$data=$this->_rawData[$key];
244-
}
245-
else{
250+
} else {
246251
$this->{$key} = $this->_rawData[$key];
247252
}
248-
249253
}
250254
}
251255

252256
if (empty($this->meta)) {
253-
throw new QueryException('Can`t find meta');
257+
throw new QueryException('Can`t find meta');
254258
}
255259

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

261-
262-
if ($isJSONCompact)
263-
{
264-
$r[]=$rows;
265-
}
266-
else {
265+
if ($isJSONCompact) {
266+
$r[] = $rows;
267+
} else {
267268
foreach ($this->meta as $meta) {
268269
$r[$meta['name']] = $rows[$meta['name']];
269270
}
@@ -272,6 +273,7 @@ private function init()
272273
$this->array_data[] = $r;
273274
}
274275

276+
$this->_init = true;
275277

276278
return true;
277279
}

tests/FetchTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace ClickHouseDB\Tests;
44

5-
use ClickHouseDB\Exception\QueryException;
65
use PHPUnit\Framework\TestCase;
76

87
/**
@@ -15,7 +14,6 @@ final class FetchTest extends TestCase
1514
use WithClient;
1615

1716

18-
1917
public function testFetchRowKeys()
2018
{
2119
$result = $this->client->select(
@@ -33,6 +31,7 @@ public function testFetchRowKeys()
3331
$this->assertEquals(null,$result->fetchOne('q'));
3432
$this->assertEquals(0,$result->fetchOne('number'));
3533
}
34+
3635
public function testFetchOne()
3736
{
3837
$result = $this->client->select(
@@ -52,4 +51,14 @@ public function testFetchOne()
5251
$this->assertEquals(1,$result->fetchRow('number'));
5352
$this->assertEquals(2,$result->fetchRow('number'));
5453
}
54+
55+
public function testCorrectInitOnFetchRow()
56+
{
57+
$result = $this->client->select(
58+
'SELECT number FROM system.numbers LIMIT 5'
59+
);
60+
61+
$result->fetchRow();
62+
$this->assertTrue($result->isInited());
63+
}
5564
}

0 commit comments

Comments
 (0)