Skip to content

Commit 6c61bbb

Browse files
committed
isSupported added
1 parent cabc042 commit 6c61bbb

File tree

4 files changed

+134
-35
lines changed

4 files changed

+134
-35
lines changed

src/ClickHouseAPI.php

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ class ClickHouseAPI
148148
*/
149149
public $hook_before_api_call = false;
150150

151+
/**
152+
* Version of ClickHouse server
153+
*
154+
* @var string|null
155+
*/
156+
public $server_version;
157+
/**
158+
* list of support features array
159+
*
160+
* @var array
161+
*/
162+
public $support_fe=[];
163+
151164
/**
152165
* Auto-create session_id and send it with each request
153166
*
@@ -320,6 +333,18 @@ public function doQuery(
320333
}
321334
}
322335

336+
// If need session, check this feature
337+
if(!empty($this->options['session_id'])) {
338+
if (isset($this->support_fe['session_id'])) {
339+
$sess_sup = $this->support_fe['session_id'];
340+
} else {
341+
$sess_sup = $this->isSupported('session_id');
342+
}
343+
if (!$sess_sup) {
344+
unset($this->options['session_id']);
345+
}
346+
}
347+
323348
$h_parameters = \array_merge(
324349
\compact('user', 'password', 'query'),
325350
$this->options
@@ -364,7 +389,7 @@ public function doApiCall(
364389
$api_url .= "?" . \http_build_query($h_params);
365390

366391
if ($this->hook_before_api_call) {
367-
$api_url = call_user_func($this->hook_before_api_call, $api_url);
392+
$api_url = call_user_func($this->hook_before_api_call, $api_url, $this);
368393
}
369394

370395
if ($this->debug) {
@@ -452,7 +477,7 @@ public function getOption($key)
452477
*/
453478
public function setSession($session_id = null, $overwrite = true)
454479
{
455-
if (is_null($session_id)) {
480+
if (\is_null($session_id)) {
456481
$session_id = \md5(\uniqid(\mt_rand(0, \PHP_INT_MAX), true));
457482
}
458483
return $this->setOption('session_id', $session_id, $overwrite);
@@ -480,4 +505,55 @@ public function delOption($key)
480505
unset($this->options[$key]);
481506
return $old_value;
482507
}
508+
509+
/**
510+
* Check supported feature by string name
511+
*
512+
* @param string $fe_key feature name
513+
* @param boolean $re_check set true for check again
514+
* @return boolean|null true = supported, false = unsupported, null = unknown feature
515+
*/
516+
public function isSupported($fe_key = 'session_id', $re_check = false)
517+
{
518+
if (!isset($this->support_fe[$fe_key]) || $re_check) {
519+
switch ($fe_key) {
520+
case 'session_id':
521+
$this->getVersion($re_check);
522+
break;
523+
default:
524+
return null;
525+
}
526+
}
527+
return $this->support_fe[$fe_key];
528+
}
529+
530+
/**
531+
* Return version of ClickHouse server by function SELECT version()
532+
*
533+
* The response is cached
534+
*
535+
* @param boolean $re_check Set true for re-send query to server
536+
* @return string|boolean String version or false if error
537+
*/
538+
public function getVersion($re_check = false)
539+
{
540+
if (\is_null($this->server_version) || $re_check) {
541+
$old_sess = $this->setSession(null, false);
542+
$session_id = $this->getSession();
543+
$query = 'SELECT version()';
544+
$ans = $this->doApiCall($this->server_url, compact('query', 'session_id'));
545+
if($ans['code'] == 200) {
546+
$this->support_fe['session_id'] = true;
547+
} else {
548+
// if session_id unsupported send request again
549+
$this->support_fe['session_id'] = false;
550+
$this->session_autocreate = false;
551+
$ans = $this->doApiCall($this->server_url, compact('query'));
552+
}
553+
$ver = explode("\n", $ans['response']);
554+
$this->server_version = (count($ver) == 2) ? $ver[0] : "Unknown";
555+
$this->setSession($old_sess);
556+
}
557+
return $this->server_version;
558+
}
483559
}

src/ClickHouseFunctions.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,6 @@ public function getSystemSettings()
381381
return $this->queryKeyValues('system.settings', 'name, value');
382382
}
383383

384-
/**
385-
* Return version of ClickHouse server
386-
*
387-
* @return string|boolean String version or false if error
388-
*/
389-
public function getVersion()
390-
{
391-
return $this->queryValue('SELECT version()');
392-
}
393-
394384
/**
395385
* Return server uptime in seconds
396386
*

tests/src/ClickHouseAPITest.php

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,69 @@ public function testDoQuery()
135135
$this->assertEquals(\trim($ans['response']), 22);
136136

137137
$session_id = $ch->getSession();
138-
$this->assertEquals(32, strlen($session_id));
138+
if($ch->isSupported('session_id')) {
139+
$this->assertEquals(32, strlen($session_id));
140+
} else {
141+
$this->assertNull($session_id);
142+
}
139143

140144
// test previous query SELECT 22
141145
$ans = $ch->doQuery();
142146
$this->assertEquals(\trim($ans['response']), 22);
143147

144-
$sess = $ch->getSession();
145-
$this->assertEquals($sess, $session_id);
148+
if ($ch->isSupported('session_id')) {
149+
$sess = $ch->getSession();
150+
$this->assertEquals($sess, $session_id);
151+
152+
// test temporary session
153+
$sess_tmp = md5(microtime());
154+
// use temporary session
155+
$ans = $ch->doQuery("SELECT 123", false, [], $sess_tmp);
146156

147-
// test temporary session
148-
$sess_tmp = md5(microtime());
149-
// use temporary session
150-
$ans = $ch->doQuery("SELECT 123", false, [], $sess_tmp);
157+
// session_id must not changed
158+
$this->assertEquals($session_id, $ch->getSession());
159+
160+
// but last last_used_session_id must be sess_tmp
161+
$this->assertEquals($sess_tmp, $ch->last_used_session_id);
162+
}
163+
}
151164

152-
// session_id must not changed
153-
$this->assertEquals($session_id, $ch->getSession());
154165

155-
// but last last_used_session_id must be sess_tmp
156-
$this->assertEquals($sess_tmp, $ch->last_used_session_id);
166+
/**
167+
* @covers ierusalim\ClickHouse\ClickHouseAPI::getVersion
168+
*/
169+
public function testGetVersion()
170+
{
171+
$ch = $this->object;
172+
$version = $ch->getVersion();
173+
$this->assertTrue(strpos($version, '.') > 0);
174+
echo "Version of ClickHouse server: $version\n";
175+
$this->assertEquals($version, $fake_version = $ch->server_version);
176+
177+
// test get cached version
178+
$fake_version = $ch->server_version = 'fake_version';
179+
$this->assertEquals($fake_version, $ch->getVersion());
180+
181+
$ch->session_autocreate = true;
182+
// set fake server for emulate session unsupported
183+
$ch->hook_before_api_call = function($s, $obj) {
184+
return 'http://google.com/';
185+
};
186+
$version = $ch->getVersion(true);
187+
$this->assertFalse($ch->session_autocreate);
188+
$this->assertEquals("Unknown", $version);
189+
}
190+
191+
/**
192+
* @covers ierusalim\ClickHouse\ClickHouseAPI::isSupported
193+
*/
194+
public function testIsSupported()
195+
{
196+
$ch = $this->object;
197+
$sess_sup = $ch->isSupported('session_id');
198+
echo "Sessions " .($sess_sup ? '':'not ') . "supported\n";
199+
200+
$this->assertNull($ch->isSupported('unknown'));
157201
}
158202

159203
/**
@@ -164,7 +208,7 @@ public function testDoApiCall()
164208
{
165209
$ch = $this->object;
166210
$ch->debug = true;
167-
$ch->hook_before_api_call = function ($url) {
211+
$ch->hook_before_api_call = function ($url, $obj) {
168212
return "https://ierusalim.github.io";
169213
};
170214

tests/src/ClickHouseFunctionsTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,6 @@ public function testGetSystemSettings()
255255
$this->assertTrue(count($arr) > 10);
256256
}
257257

258-
/**
259-
* @covers ierusalim\ClickHouse\ClickHouseFunctions::getVersion
260-
*/
261-
public function testGetVersion()
262-
{
263-
$ch = $this->object;
264-
$version = $ch->getVersion();
265-
$this->assertTrue(strpos($version, '.') > 0);
266-
echo "Version of ClickHouse server: $version\n";
267-
}
268-
269258
/**
270259
* @covers ierusalim\ClickHouse\ClickHouseFunctions::getUptime
271260
*/

0 commit comments

Comments
 (0)