Skip to content

Commit 12bd2a6

Browse files
committed
Simple refactor + testFetchRowKeys
1 parent fdf013a commit 12bd2a6

File tree

3 files changed

+91
-49
lines changed

3 files changed

+91
-49
lines changed

src/Statement.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,19 @@ public function countAll()
359359
public function statistics($key = false)
360360
{
361361
$this->init();
362-
if ($key)
363-
{
364-
if (!is_array($this->statistics)) {
365-
return null;
366-
}
367-
if (!isset($this->statistics[$key])) {
368-
return null;
369-
}
370-
return $this->statistics[$key];
362+
363+
if (!is_array($this->statistics)) {
364+
return null;
365+
}
366+
367+
if (!$key) return $this->statistics;
368+
369+
if (!isset($this->statistics[$key])) {
370+
return null;
371371
}
372-
return $this->statistics;
372+
373+
return $this->statistics[$key];
374+
373375
}
374376

375377
/**
@@ -410,19 +412,22 @@ public function fetchRow($key = null)
410412
$this->init();
411413

412414
$position=$this->iterator;
413-
if (isset($this->array_data[$position])) {
414-
$this->iterator++;
415-
if ($key) {
416-
if (isset($this->array_data[$position][$key])) {
417-
return $this->array_data[$position][$key];
418-
} else {
419-
return null;
420-
}
421-
}
415+
416+
if (!isset($this->array_data[$position])) {
417+
return null;
418+
}
419+
420+
$this->iterator++;
421+
422+
if (!$key) {
422423
return $this->array_data[$position];
423424
}
425+
if (!isset($this->array_data[$position][$key])) {
426+
return null;
427+
}
428+
429+
return $this->array_data[$position][$key];
424430

425-
return null;
426431
}
427432
/**
428433
* @param string $key
@@ -432,17 +437,19 @@ public function fetchRow($key = null)
432437
public function fetchOne($key = null)
433438
{
434439
$this->init();
435-
if (isset($this->array_data[0])) {
436-
if ($key) {
437-
if (isset($this->array_data[0][$key])) {
438-
return $this->array_data[0][$key];
439-
} else {
440-
return null;
441-
}
442-
}
440+
if (!isset($this->array_data[0])) {
441+
return null;
442+
}
443+
444+
if (!$key) {
443445
return $this->array_data[0];
444446
}
445-
return null;
447+
448+
if (!isset($this->array_data[0][$key])) {
449+
return null;
450+
}
451+
452+
return $this->array_data[0][$key];
446453
}
447454

448455
/**

tests/ClientTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,24 +1008,4 @@ public function testStreamInsertFormatJSONEachRow()
10081008
$this->assertEquals(count(file($file_name)), $statement->count());
10091009
}
10101010

1011-
1012-
public function testFetchOne()
1013-
{
1014-
$result = $this->client->select(
1015-
'SELECT number FROM system.numbers LIMIT 5'
1016-
);
1017-
// fetchOne
1018-
$this->assertEquals(0,$result->fetchOne('number'));
1019-
$this->assertEquals(0,$result->fetchOne('number'));
1020-
$this->assertEquals(0,$result->fetchOne('number'));
1021-
1022-
// fetchRow
1023-
$this->assertEquals(0,$result->fetchRow('number'));
1024-
$this->assertEquals(1,$result->fetchRow('number'));
1025-
$this->assertEquals(2,$result->fetchRow('number'));
1026-
$result->resetIterator();
1027-
$this->assertEquals(0,$result->fetchRow('number'));
1028-
$this->assertEquals(1,$result->fetchRow('number'));
1029-
$this->assertEquals(2,$result->fetchRow('number'));
1030-
}
10311011
}

tests/FetchTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace ClickHouseDB\Tests;
4+
5+
use ClickHouseDB\Exception\QueryException;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Class FetchTest
10+
* @group FetchTest
11+
* @package ClickHouseDB\Tests
12+
*/
13+
final class FetchTest extends TestCase
14+
{
15+
use WithClient;
16+
17+
18+
19+
public function testFetchRowKeys()
20+
{
21+
$result = $this->client->select(
22+
'SELECT number FROM system.numbers LIMIT 5'
23+
);
24+
$this->assertEquals(null,$result->fetchRow('x'));
25+
$this->assertEquals(null,$result->fetchRow('y'));
26+
$this->assertEquals(2,$result->fetchRow('number'));
27+
$result->resetIterator();
28+
$this->assertEquals(null,$result->fetchRow('x'));
29+
$this->assertEquals(1,$result->fetchRow('number'));
30+
31+
32+
$this->assertEquals(null,$result->fetchOne('w'));
33+
$this->assertEquals(null,$result->fetchOne('q'));
34+
$this->assertEquals(0,$result->fetchOne('number'));
35+
}
36+
public function testFetchOne()
37+
{
38+
$result = $this->client->select(
39+
'SELECT number FROM system.numbers LIMIT 5'
40+
);
41+
// fetchOne
42+
$this->assertEquals(0,$result->fetchOne('number'));
43+
$this->assertEquals(0,$result->fetchOne('number'));
44+
$this->assertEquals(0,$result->fetchOne('number'));
45+
46+
// fetchRow
47+
$this->assertEquals(0,$result->fetchRow('number'));
48+
$this->assertEquals(1,$result->fetchRow('number'));
49+
$this->assertEquals(2,$result->fetchRow('number'));
50+
$result->resetIterator();
51+
$this->assertEquals(0,$result->fetchRow('number'));
52+
$this->assertEquals(1,$result->fetchRow('number'));
53+
$this->assertEquals(2,$result->fetchRow('number'));
54+
}
55+
}

0 commit comments

Comments
 (0)