Skip to content

Commit 79f8850

Browse files
committed
doGet added
1 parent 56772e3 commit 79f8850

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

src/ClickHouseAPI.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,10 @@ public function isSupported($fe_key = 'session_id', $re_check = false)
527527
/**
528528
* Return version of ClickHouse server by function SELECT version()
529529
*
530-
* The response is cached
530+
* Do SELECT version() + session_id to see if a session is supported or not
531+
* if session_id not supported, request is send again, but without session_id.
532+
* - Depending on result, the isSupported('session_id') is set true or false.
533+
* - If server version response unrecognized, isSupported('query') set false.
531534
*
532535
* @param boolean $re_check Set true for re-send query to server
533536
* @return string|boolean String version or false if error
@@ -536,32 +539,40 @@ public function getVersion($re_check = false)
536539
{
537540
if (\is_null($this->server_version) || $re_check) {
538541
$old_sess = $this->setSession(null, false);
539-
$session_id = $this->getSession();
540542
$query = 'SELECT version()';
541-
$user = $this->user;
542-
$password = $this->pass;
543-
$h_opt_arr = \compact('query', 'user', 'password', 'session_id');
544-
$ans = $this->doApiCall($this->server_url, $h_opt_arr);
545-
if ($ans['code'] == 200) {
546-
$this->support_fe['session_id'] = true;
547-
} else {
548-
$this->support_fe['session_id'] = false;
549-
$this->session_autocreate = false;
550-
unset($h_opt_arr['session_id']);
551-
// if session_id unsupported send request again
552-
if ($ans['code'] == 404) {
553-
$ans = $this->doApiCall($this->server_url, $h_opt_arr);
554-
}
543+
$ans = $this->doGet($query, ['session_id' => $this->getSession()]);
544+
if (!($this->support_fe['session_id'] = $ans['code'] != 404)) {
545+
$ans = $this->doGet($query);
555546
}
556547
$ver = explode("\n", $ans['response']);
557548
$ver = (count($ver) == 2 && strlen($ver[0]) < 32) ? $ver[0] : "Unknown";
558549
$this->support_fe['query'] = \is_string($ver) && (\count(\explode(".", $ver)) > 2);
559550
if (!$this->support_fe['query']) {
560551
$this->support_fe['session_id'] = false;
561552
}
553+
if (!$this->support_fe['session_id']) {
554+
$this->session_autocreate = false;
555+
}
562556
$this->server_version = $ver;
563557
$this->setOption('session_id', $old_sess);
564558
}
565559
return $this->server_version;
566560
}
561+
562+
/**
563+
* Do GET-request with base options (query, user, password) + specified options
564+
*
565+
* The result array is the same as for the function doApiCall
566+
*
567+
* @param string $query will be set as an option ?query=...
568+
* @param array $h_opt Other in-url-options for make GET-request
569+
* @return array
570+
*/
571+
public function doGet($query, $h_opt = [])
572+
{
573+
$user = $this->user;
574+
$password = $this->pass;
575+
$h_opt_arr = \array_merge(\compact('query', 'user', 'password'), $h_opt);
576+
return $this->doApiCall($this->server_url, $h_opt_arr);
577+
}
567578
}

tests/src/ClickHouseAPITest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ public function testGetQuery()
151151
}
152152
}
153153

154+
/**
155+
* @covers Ierusalim\ClickHouse\ClickHouseAPI::doGet
156+
* @todo Implement testDoGet().
157+
*/
158+
public function testDoGet()
159+
{
160+
$ch = $this->object;
161+
if ($ch->isSupported('query')) {
162+
$ans = $ch->doGet("SELECT 567", ['database' => 'default']);
163+
$this->assertEquals(200, ($ans['code']));
164+
$this->assertTrue(isset($ans['response']));
165+
$this->assertEquals(trim($ans['response']), '567');
166+
} else {
167+
echo '-';
168+
}
169+
}
170+
154171
/**
155172
* @covers Ierusalim\ClickHouse\ClickHouseAPI::postQuery
156173
* @todo Implement testPostQuery().

tests/src/ClickHouseQueryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ public function testQueryTableSubstract()
404404
$arr = $ch->queryTableSubstract($tbl);
405405
$this->assertArrayHasKey('columns_arr', $arr);
406406
$arr = $arr['columns_arr'];
407+
$arr = $ch->queryTableSubstract("notfound'\nthistable");
408+
$this->assertFalse(\is_array($arr));
407409
}
408-
$arr = $ch->queryTableSubstract("notfound'\nthistable");
409-
$this->assertFalse(\is_array($arr));
410410
}
411411

412412
/**

0 commit comments

Comments
 (0)