Skip to content

Commit dd00a4f

Browse files
committed
ok
1 parent 413698c commit dd00a4f

File tree

2 files changed

+79
-34
lines changed

2 files changed

+79
-34
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and have not dependencies (may be used independently, file src/ClichHouseAPI.php
1010
* **setServerUrl**($url) - set ClickHouse server parameters by url (host, port, etc.)
1111
* **getQuery**($h_query [, $sess]) - send GET request
1212
* **postQuery**($h_query, $post_data [, $sess]) - send POST request
13+
* **query**($sql [,$post_data]) - object-oriented style SQL-query (return $this, throw exceptions)
1314
#### Special functions
1415
* **getVersion**() - return version of ClickHouse server (side effect - detect server features)
1516
* **isSupported**(feature-name) - true or false depending on the server support features.
@@ -49,8 +50,8 @@ contains functions for simple operations with ClickHouse.
4950
* **createDatabase**($db) - create new database with specified name
5051
* **dropDatabase**($db) - drop specified database and remove all tables inside
5152
* **getDatabasesList**() - returns array contained names of existing Databases
52-
* **setCurrentDatabase**($db [, $sess]) - set current database by 'USE db' request
53-
* **getCurrentDatabase**([$sess]) - return results of 'SELECT currentDatabase()'
53+
* **setCurrentDatabase**($db [, $sess]) - set current database by 'USE db' request or by option
54+
* **getCurrentDatabase**([$sess]) - return results of 'SELECT currentDatabase()' or from option
5455
* **getUptime**() - return server uptime in seconds
5556
* **getSystemSettings**() - get information from system.settings as array [name=>value]
5657

src/ClickHouseAPI.php

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* - >setServerUrl($url) - set ClickHouse server parameters by url (host, port, etc.)
1212
* - >getQuery($h_query [, $sess]) - send GET request
1313
* - >postQuery($h_query, $post_data [, $sess]) - send POST request
14+
* - >query($sql [,$post_data]) - object-oriented style SQL-query (return $this)
1415
* Special functions:
1516
* - >getVersion() - return version of ClickHouse server (and detect server features)
1617
* - >isSupported(feature-name) - return true or false depending on server features.
@@ -107,14 +108,14 @@ class ClickHouseAPI
107108
*
108109
* @var integer 3 sec. by default
109110
*/
110-
public $curl_conn_timeout = 3;
111+
public $curl_conn_timeout = 7;
111112

112113
/**
113114
* CURL option CURLOPT_TIMEOUT
114115
*
115116
* @var integer 60 sec. by default
116117
*/
117-
public $curl_timeout = 60;
118+
public $curl_timeout = 77;
118119

119120
/**
120121
* Last error reported by CURL or empty string if none
@@ -158,20 +159,6 @@ class ClickHouseAPI
158159
*/
159160
public $hook_before_api_call = false;
160161

161-
/**
162-
* Version of ClickHouse server
163-
*
164-
* @var string|null
165-
*/
166-
public $server_version;
167-
168-
/**
169-
* list of support features array
170-
*
171-
* @var array
172-
*/
173-
public $support_fe = [];
174-
175162
/**
176163
* Auto-create session_id and send it with each request
177164
*
@@ -186,6 +173,14 @@ class ClickHouseAPI
186173
*/
187174
public $last_used_session_id;
188175

176+
/**
177+
* Results of last ->query(sql) request
178+
* Contains string with error description or data response if no errors.
179+
*
180+
* @var string
181+
*/
182+
public $results;
183+
189184
/**
190185
* Two formats supported for set server parameters when creating object:
191186
*
@@ -260,6 +255,42 @@ public function setServerUrl($full_server_url = null)
260255
. (empty($this->path) ? '/' : $this->path);
261256
}
262257

258+
/**
259+
* Object-style ->query($sql [,$post_data])->query(...)
260+
*
261+
* Sends SQL-query to server (always in POST-mode)
262+
* - If server response not empty, places results to $this->results.
263+
* - Note that there is an empty string at the end of the response line \n
264+
* - Note that many queries return an empty result and the value $this->results does not change
265+
* - Note that requests are sent only if isSupported('query') is true
266+
*
267+
* Throws an exception if there is an error, or return $this-object, if not error.
268+
*
269+
* @param string $sql SQL-query
270+
* @param array|string|null $post_data Parameters send in request body
271+
* @param string|null $sess session_id
272+
* @return $this
273+
* @throws \Exception
274+
*/
275+
public function query($sql, $post_data = null, $sess = null)
276+
{
277+
if (!$this->isSupported('query')) {
278+
throw new \Exception("Server does not accept ClickHouse-requests");
279+
}
280+
$ans = $this->postQuery($sql, $post_data, $sess);
281+
if (!empty($ans['curl_error'])) {
282+
$this->results = $ans['curl_error'];
283+
throw new \Exception($this->results);
284+
}
285+
if (!empty($ans['response'])) {
286+
$this->results = $ans['response'];
287+
}
288+
if ($ans['code'] != 200) {
289+
throw new \Exception(\substr($ans['response'], 0, 2048));
290+
}
291+
return $this;
292+
}
293+
263294
/**
264295
* Send Get query if $post_data is empty, otherwise send Post query
265296
*
@@ -323,7 +354,7 @@ public function doQuery(
323354
$session_id = null,
324355
$file = null
325356
) {
326-
if (is_null($query)) {
357+
if (\is_null($query)) {
327358
$query = $this->query;
328359
} else {
329360
$this->query = $query;
@@ -513,18 +544,29 @@ public function delOption($key)
513544
*
514545
* @param string $fe_key feature name
515546
* @param boolean $re_check set true for check again
547+
* @param boolean|null $set for set is_feature value
516548
* @return boolean true = supported, false = unsupported
517549
*/
518-
public function isSupported($fe_key = 'session_id', $re_check = false)
550+
public function isSupported($fe_key = 'session_id', $re_check = false, $set = null)
519551
{
520-
if (!isset($this->support_fe[$fe_key]) || $re_check) {
521-
if ($fe_key == 'query' || $fe_key == 'session_id') {
522-
$this->getVersion($re_check);
523-
} else {
524-
return false;
552+
static $support_fe = [];
553+
$s_key = $this->server_url;
554+
if (!isset($support_fe[$s_key])) {
555+
// First connect of server $s_key
556+
$support_fe[$s_key] = [];
557+
}
558+
if (\is_null($set)) {
559+
if (!isset($support_fe[$s_key][$fe_key]) || $re_check) {
560+
if ($fe_key == 'query' || $fe_key == 'session_id') {
561+
$this->getVersion($re_check);
562+
} else {
563+
return false;
564+
}
525565
}
566+
} else {
567+
$support_fe[$s_key][$fe_key] = $set;
526568
}
527-
return $this->support_fe[$fe_key];
569+
return isset($support_fe[$s_key][$fe_key]) ? $support_fe[$s_key][$fe_key] : false;
528570
}
529571

530572
/**
@@ -540,26 +582,28 @@ public function isSupported($fe_key = 'session_id', $re_check = false)
540582
*/
541583
public function getVersion($re_check = false)
542584
{
543-
if (\is_null($this->server_version) || $re_check) {
585+
static $server_version = [];
586+
$s_key = $this->server_url;
587+
if (empty($server_version[$s_key]) || $re_check) {
544588
$old_sess = $this->setSession(null, false);
545589
$query = 'SELECT version()';
546590
$ans = $this->doGet($query, ['session_id' => $this->getSession()]);
547-
if (!($this->support_fe['session_id'] = $ans['code'] != 404)) {
591+
if (!($this->isSupported('session_id', false, $ans['code'] != 404))) {
548592
$ans = $this->doGet($query);
549593
}
550594
$ver = explode("\n", $ans['response']);
551595
$ver = (count($ver) == 2 && strlen($ver[0]) < 32) ? $ver[0] : "Unknown";
552-
$this->support_fe['query'] = \is_string($ver) && (\count(\explode(".", $ver)) > 2);
553-
if (!$this->support_fe['query']) {
554-
$this->support_fe['session_id'] = false;
596+
$this->isSupported('query', false, \is_string($ver) && (\count(\explode(".", $ver)) > 2));
597+
if (!$this->isSupported('query')) {
598+
$this->isSupported('session_id', false, false);
555599
}
556-
if (!$this->support_fe['session_id']) {
600+
if (!$this->isSupported('session_id')) {
557601
$this->session_autocreate = false;
558602
}
559-
$this->server_version = $ver;
603+
$server_version[$s_key] = $ver;
560604
$this->setOption('session_id', $old_sess);
561605
}
562-
return $this->server_version;
606+
return $server_version[$s_key];
563607
}
564608

565609
/**

0 commit comments

Comments
 (0)