11
11
* - >setServerUrl($url) - set ClickHouse server parameters by url (host, port, etc.)
12
12
* - >getQuery($h_query [, $sess]) - send GET request
13
13
* - >postQuery($h_query, $post_data [, $sess]) - send POST request
14
+ * - >query($sql [,$post_data]) - object-oriented style SQL-query (return $this)
14
15
* Special functions:
15
16
* - >getVersion() - return version of ClickHouse server (and detect server features)
16
17
* - >isSupported(feature-name) - return true or false depending on server features.
@@ -107,14 +108,14 @@ class ClickHouseAPI
107
108
*
108
109
* @var integer 3 sec. by default
109
110
*/
110
- public $ curl_conn_timeout = 3 ;
111
+ public $ curl_conn_timeout = 7 ;
111
112
112
113
/**
113
114
* CURL option CURLOPT_TIMEOUT
114
115
*
115
116
* @var integer 60 sec. by default
116
117
*/
117
- public $ curl_timeout = 60 ;
118
+ public $ curl_timeout = 77 ;
118
119
119
120
/**
120
121
* Last error reported by CURL or empty string if none
@@ -158,20 +159,6 @@ class ClickHouseAPI
158
159
*/
159
160
public $ hook_before_api_call = false ;
160
161
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
-
175
162
/**
176
163
* Auto-create session_id and send it with each request
177
164
*
@@ -186,6 +173,14 @@ class ClickHouseAPI
186
173
*/
187
174
public $ last_used_session_id ;
188
175
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
+
189
184
/**
190
185
* Two formats supported for set server parameters when creating object:
191
186
*
@@ -260,6 +255,42 @@ public function setServerUrl($full_server_url = null)
260
255
. (empty ($ this ->path ) ? '/ ' : $ this ->path );
261
256
}
262
257
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
+
263
294
/**
264
295
* Send Get query if $post_data is empty, otherwise send Post query
265
296
*
@@ -323,7 +354,7 @@ public function doQuery(
323
354
$ session_id = null ,
324
355
$ file = null
325
356
) {
326
- if (is_null ($ query )) {
357
+ if (\ is_null ($ query )) {
327
358
$ query = $ this ->query ;
328
359
} else {
329
360
$ this ->query = $ query ;
@@ -513,18 +544,29 @@ public function delOption($key)
513
544
*
514
545
* @param string $fe_key feature name
515
546
* @param boolean $re_check set true for check again
547
+ * @param boolean|null $set for set is_feature value
516
548
* @return boolean true = supported, false = unsupported
517
549
*/
518
- public function isSupported ($ fe_key = 'session_id ' , $ re_check = false )
550
+ public function isSupported ($ fe_key = 'session_id ' , $ re_check = false , $ set = null )
519
551
{
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
+ }
525
565
}
566
+ } else {
567
+ $ support_fe [$ s_key ][$ fe_key ] = $ set ;
526
568
}
527
- return $ this -> support_fe [$ fe_key ];
569
+ return isset ( $ support_fe [$ s_key ][ $ fe_key ]) ? $ support_fe [ $ s_key ][ $ fe_key ] : false ;
528
570
}
529
571
530
572
/**
@@ -540,26 +582,28 @@ public function isSupported($fe_key = 'session_id', $re_check = false)
540
582
*/
541
583
public function getVersion ($ re_check = false )
542
584
{
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 ) {
544
588
$ old_sess = $ this ->setSession (null , false );
545
589
$ query = 'SELECT version() ' ;
546
590
$ 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 ) )) {
548
592
$ ans = $ this ->doGet ($ query );
549
593
}
550
594
$ ver = explode ("\n" , $ ans ['response ' ]);
551
595
$ 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 ) ;
555
599
}
556
- if (!$ this ->support_fe [ 'session_id ' ] ) {
600
+ if (!$ this ->isSupported ( 'session_id ' ) ) {
557
601
$ this ->session_autocreate = false ;
558
602
}
559
- $ this -> server_version = $ ver ;
603
+ $ server_version[ $ s_key ] = $ ver ;
560
604
$ this ->setOption ('session_id ' , $ old_sess );
561
605
}
562
- return $ this -> server_version ;
606
+ return $ server_version[ $ s_key ] ;
563
607
}
564
608
565
609
/**
0 commit comments