|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Transport; |
|
4
|
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\TransportException; |
|
6
|
|
|
use ClickHouseDB\Query\Degeneration; |
|
7
|
|
|
use ClickHouseDB\Query\Query; |
|
8
|
|
|
use ClickHouseDB\Query\WhereInFile; |
|
9
|
|
|
use ClickHouseDB\Query\WriteToFile; |
|
10
|
|
|
use ClickHouseDB\Settings; |
|
11
|
|
|
use ClickHouseDB\Statement; |
|
12
|
|
|
use const PHP_EOL; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class Http |
|
15
|
|
|
{ |
|
16
|
|
|
const AUTH_METHOD_HEADER = 1; |
|
|
|
|
|
|
17
|
|
|
const AUTH_METHOD_QUERY_STRING = 2; |
|
|
|
|
|
|
18
|
|
|
const AUTH_METHOD_BASIC_AUTH = 3; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
const AUTH_METHODS_LIST = [ |
|
|
|
|
|
|
21
|
|
|
self::AUTH_METHOD_HEADER, |
|
22
|
|
|
self::AUTH_METHOD_QUERY_STRING, |
|
23
|
|
|
self::AUTH_METHOD_BASIC_AUTH, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $_username = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $_password = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The username and password can be indicated in one of three ways: |
|
38
|
|
|
* - Using HTTP Basic Authentication. |
|
39
|
|
|
* - In the ‘user’ and ‘password’ URL parameters. |
|
40
|
|
|
* - Using ‘X-ClickHouse-User’ and ‘X-ClickHouse-Key’ headers (by default) |
|
41
|
|
|
* |
|
42
|
|
|
* @see https://clickhouse.tech/docs/en/interfaces/http/ |
|
|
|
|
|
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
private $_authMethod = self::AUTH_METHOD_HEADER; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
|
|
|
|
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $_host = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
|
|
|
|
|
53
|
|
|
* @var int |
|
54
|
|
|
*/ |
|
55
|
|
|
private $_port = 0; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
|
|
|
|
|
58
|
|
|
* @var bool|int |
|
59
|
|
|
*/ |
|
60
|
|
|
private $_verbose = false; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
|
|
|
|
|
63
|
|
|
* @var CurlerRolling |
|
64
|
|
|
*/ |
|
65
|
|
|
private $_curler = null; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
|
|
|
|
|
68
|
|
|
* @var Settings |
|
69
|
|
|
*/ |
|
70
|
|
|
private $_settings = null; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
|
|
|
|
|
73
|
|
|
* @var array |
|
|
|
|
|
|
74
|
|
|
*/ |
|
75
|
|
|
private $_query_degenerations = []; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Count seconds (int) |
|
79
|
|
|
* |
|
80
|
|
|
* @var float |
|
81
|
|
|
*/ |
|
82
|
|
|
private $_connectTimeOut = 5.0; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
|
|
|
|
|
85
|
|
|
* @var callable |
|
86
|
|
|
*/ |
|
87
|
|
|
private $xClickHouseProgress = null; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
|
|
|
|
|
90
|
|
|
* @var null|string |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
private $sslCA = null; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
|
|
|
|
|
95
|
|
|
* @var null|resource |
|
|
|
|
|
|
96
|
|
|
*/ |
|
97
|
|
|
private $stdErrOut = null; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
|
|
|
|
|
100
|
|
|
* @var null|resource |
|
|
|
|
|
|
101
|
|
|
*/ |
|
102
|
68 |
|
private $handle = null; |
|
103
|
|
|
|
|
104
|
68 |
|
/** |
|
105
|
|
|
* Http constructor. |
|
|
|
|
|
|
106
|
68 |
|
* @param string $host |
|
|
|
|
|
|
107
|
68 |
|
* @param int $port |
|
|
|
|
|
|
108
|
68 |
|
* @param string $username |
|
109
|
|
|
* @param string $password |
|
110
|
|
|
* @param int $authMethod |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
68 |
|
public function __construct($host, $port, $username, $password, $authMethod = null) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
68 |
|
$this->setHost($host, $port); |
|
115
|
68 |
|
|
|
116
|
|
|
$this->_username = $username; |
|
117
|
|
|
$this->_password = $password; |
|
118
|
68 |
|
if ($authMethod) { |
|
|
|
|
|
|
119
|
|
|
$this->_authMethod = $authMethod; |
|
120
|
68 |
|
} |
|
121
|
68 |
|
|
|
122
|
|
|
$this->_settings = new Settings(); |
|
123
|
|
|
|
|
124
|
|
|
$this->setCurler(); |
|
125
|
|
|
$this->setHandle(); |
|
126
|
|
|
} |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
public function setCurler() : void |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$this->_curler = new CurlerRolling(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param CurlerRolling $curler |
|
|
|
|
|
|
136
|
|
|
*/ |
|
137
|
|
|
public function setDirtyCurler(CurlerRolling $curler) : void |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
if ($curler instanceof CurlerRolling) { |
|
|
|
|
|
|
140
|
|
|
$this->_curler = $curler; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
68 |
|
* @return CurlerRolling |
|
|
|
|
|
|
146
|
|
|
*/ |
|
147
|
68 |
|
public function getCurler(): ?CurlerRolling |
|
|
|
|
|
|
148
|
68 |
|
{ |
|
149
|
|
|
return $this->_curler; |
|
150
|
|
|
} |
|
151
|
68 |
|
|
|
152
|
68 |
|
/** |
|
153
|
|
|
* @param string $host |
|
|
|
|
|
|
154
|
|
|
* @param int $port |
|
|
|
|
|
|
155
|
|
|
*/ |
|
156
|
|
|
public function setHost(string $host, int $port = -1) : void |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
if ($port > 0) { |
|
159
|
|
|
$this->_port = $port; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->_host = $host; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Sets client SSL certificate for Yandex Cloud |
|
167
|
55 |
|
* |
|
168
|
|
|
* @param string $caPath |
|
|
|
|
|
|
169
|
55 |
|
*/ |
|
170
|
55 |
|
public function setSslCa(string $caPath) : void |
|
|
|
|
|
|
171
|
1 |
|
{ |
|
172
|
|
|
$this->sslCA = $caPath; |
|
173
|
55 |
|
} |
|
174
|
55 |
|
|
|
175
|
1 |
|
/** |
|
176
|
|
|
* @return string |
|
|
|
|
|
|
177
|
55 |
|
*/ |
|
178
|
55 |
|
public function getUri(): string |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
1 |
|
$proto = 'http'; |
|
181
|
|
|
if ($this->settings()->isHttps()) { |
|
|
|
|
|
|
182
|
|
|
$proto = 'https'; |
|
183
|
|
|
} |
|
184
|
|
|
$uri = $proto . '://' . $this->_host; |
|
185
|
|
|
if (stripos($this->_host, '/') !== false || stripos($this->_host, ':') !== false) { |
|
|
|
|
|
|
186
|
68 |
|
return $uri; |
|
187
|
|
|
} |
|
188
|
68 |
|
if (intval($this->_port) > 0) { |
|
|
|
|
|
|
189
|
|
|
return $uri . ':' . $this->_port; |
|
190
|
|
|
} |
|
191
|
|
|
return $uri; |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return Settings |
|
|
|
|
|
|
196
|
|
|
*/ |
|
197
|
|
|
public function settings(): Settings |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
|
|
return $this->_settings; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param bool $flag |
|
|
|
|
|
|
204
|
|
|
* @return bool |
|
|
|
|
|
|
205
|
45 |
|
*/ |
|
206
|
|
|
public function verbose(bool $flag): bool |
|
|
|
|
|
|
207
|
45 |
|
{ |
|
208
|
|
|
$this->_verbose = $flag; |
|
209
|
45 |
|
return $flag; |
|
|
|
|
|
|
210
|
45 |
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param array $params |
|
|
|
|
|
|
214
|
45 |
|
* @return string |
|
|
|
|
|
|
215
|
|
|
*/ |
|
216
|
|
|
private function getUrl($params = []): string |
|
|
|
|
|
|
217
|
|
|
{ |
|
218
|
|
|
$settings = $this->settings()->getSettings(); |
|
219
|
|
|
|
|
220
|
|
|
if (is_array($params) && sizeof($params)) { |
|
|
|
|
|
|
221
|
|
|
$settings = array_merge($settings, $params); |
|
|
|
|
|
|
222
|
45 |
|
} |
|
223
|
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
225
|
45 |
|
if ($this->settings()->isReadOnlyUser()) { |
|
226
|
|
|
unset($settings['extremes']); |
|
227
|
|
|
unset($settings['readonly']); |
|
228
|
|
|
unset($settings['enable_http_compression']); |
|
229
|
|
|
unset($settings['max_execution_time']); |
|
230
|
|
|
|
|
|
|
|
|
|
231
|
|
|
} |
|
232
|
45 |
|
|
|
233
|
|
|
unset($settings['https']); |
|
234
|
45 |
|
|
|
|
|
|
|
|
235
|
|
|
|
|
236
|
45 |
|
return $this->getUri() . '?' . http_build_query($settings); |
|
|
|
|
|
|
237
|
45 |
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param array $extendinfo |
|
|
|
|
|
|
241
|
|
|
* @return CurlerRequest |
|
|
|
|
|
|
242
|
|
|
*/ |
|
243
|
|
|
private function newRequest($extendinfo): CurlerRequest |
|
|
|
|
|
|
244
|
45 |
|
{ |
|
245
|
|
|
$new = new CurlerRequest(false, $this->handle); |
|
246
|
|
|
|
|
247
|
|
|
switch ($this->_authMethod) { |
|
248
|
|
|
case self::AUTH_METHOD_QUERY_STRING: |
|
249
|
45 |
|
/* @todo: Move this implementation to CurlerRequest class. Possible options: the authentication method |
|
250
|
45 |
|
* should be applied in method `CurlerRequest:prepareRequest()`. |
|
251
|
|
|
*/ |
|
252
|
|
|
$this->settings()->set('user', $this->_username); |
|
253
|
45 |
|
$this->settings()->set('password', $this->_password); |
|
254
|
|
|
break; |
|
255
|
45 |
|
case self::AUTH_METHOD_BASIC_AUTH: |
|
256
|
45 |
|
$new->authByBasicAuth($this->_username, $this->_password); |
|
257
|
|
|
break; |
|
258
|
45 |
|
default: |
|
259
|
1 |
|
// Auth with headers by default |
|
260
|
|
|
$new->authByHeaders($this->_username, $this->_password); |
|
261
|
45 |
|
break; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$new->POST()->setRequestExtendedInfo($extendinfo); |
|
265
|
45 |
|
|
|
266
|
45 |
|
$new->httpCompression($this->settings()->isEnableHttpCompression()); |
|
|
|
|
|
|
267
|
45 |
|
|
|
268
|
|
|
if ($this->settings()->getSessionId()) { |
|
|
|
|
|
|
269
|
45 |
|
$new->persistent(); |
|
270
|
|
|
} |
|
271
|
|
|
if ($this->sslCA) { |
|
272
|
|
|
$new->setSslCa($this->sslCA); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
$new->timeOut($this->settings()->getTimeOut()); |
|
276
|
|
|
$new->connectTimeOut($this->_connectTimeOut); |
|
277
|
|
|
$new->keepAlive(); |
|
278
|
|
|
$new->verbose(boolval($this->_verbose)); |
|
|
|
|
|
|
279
|
45 |
|
|
|
280
|
|
|
return $new; |
|
281
|
45 |
|
} |
|
282
|
|
|
|
|
283
|
45 |
|
/** |
|
284
|
1 |
|
* @param Query $query |
|
|
|
|
|
|
285
|
|
|
* @param array $urlParams |
|
|
|
|
|
|
286
|
|
|
* @param bool $query_as_string |
|
|
|
|
|
|
287
|
|
|
* @return CurlerRequest |
|
|
|
|
|
|
288
|
45 |
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
289
|
45 |
|
*/ |
|
290
|
45 |
|
private function makeRequest(Query $query, array $urlParams = [], bool $query_as_string = false): CurlerRequest |
|
|
|
|
|
|
291
|
|
|
{ |
|
292
|
|
|
$sql = $query->toSql(); |
|
293
|
45 |
|
|
|
294
|
|
|
if ($query_as_string) { |
|
|
|
|
|
|
295
|
|
|
$urlParams['query'] = $sql; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
$extendInfo = [ |
|
299
|
45 |
|
'sql' => $sql, |
|
300
|
45 |
|
'query' => $query, |
|
301
|
|
|
'format' => $query->getFormat() |
|
|
|
|
|
|
302
|
|
|
]; |
|
303
|
45 |
|
|
|
304
|
45 |
|
$new = $this->newRequest($extendInfo); |
|
305
|
|
|
|
|
306
|
45 |
|
/* |
|
307
|
45 |
|
* Build URL after request making, since URL may contain auth data. This will not matter after the |
|
308
|
|
|
* implantation of the todo in the `HTTP:newRequest()` method. |
|
309
|
|
|
*/ |
|
310
|
45 |
|
|
|
311
|
|
|
if ($query->isUseInUrlBindingsParams()) { |
|
312
|
|
|
$urlParams = array_replace_recursive($urlParams, $query->getUrlBindingsParams()); |
|
|
|
|
|
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$url = $this->getUrl($urlParams); |
|
316
|
|
|
$new->url($url); |
|
317
|
3 |
|
|
|
318
|
|
|
if (!$query_as_string) { |
|
|
|
|
|
|
319
|
|
|
$new->parameters_json($sql); |
|
320
|
3 |
|
} |
|
321
|
1 |
|
$new->httpCompression($this->settings()->isEnableHttpCompression()); |
|
|
|
|
|
|
322
|
|
|
|
|
323
|
2 |
|
return $new; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
3 |
|
* @param resource $stream |
|
|
|
|
|
|
328
|
3 |
|
* @return void |
|
329
|
3 |
|
*/ |
|
330
|
|
|
public function setStdErrOut($stream) |
|
|
|
|
|
|
331
|
|
|
{ |
|
332
|
3 |
|
if (is_resource($stream)) { |
|
|
|
|
|
|
333
|
|
|
$this->stdErrOut=$stream; |
|
|
|
|
|
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
} |
|
|
|
|
|
|
337
|
|
|
|
|
338
|
3 |
|
/** |
|
339
|
3 |
|
* @param string|Query $sql |
|
|
|
|
|
|
340
|
3 |
|
* @return CurlerRequest |
|
|
|
|
|
|
341
|
|
|
*/ |
|
342
|
|
|
public function writeStreamData($sql): CurlerRequest |
|
343
|
3 |
|
{ |
|
|
|
|
|
|
344
|
3 |
|
|
|
345
|
|
|
if ($sql instanceof Query) { |
|
346
|
|
|
$query = $sql; |
|
347
|
|
|
} else { |
|
348
|
|
|
$query = new Query($sql); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
$extendInfo = [ |
|
352
|
|
|
'sql' => $sql, |
|
353
|
|
|
'query' => $query, |
|
354
|
8 |
|
'format' => $query->getFormat() |
|
|
|
|
|
|
355
|
|
|
]; |
|
356
|
8 |
|
|
|
357
|
|
|
$request = $this->newRequest($extendInfo); |
|
358
|
|
|
|
|
359
|
8 |
|
/* |
|
360
|
8 |
|
* Build URL after request making, since URL may contain auth data. This will not matter after the |
|
361
|
8 |
|
* implantation of the todo in the `HTTP:newRequest()` method. |
|
362
|
|
|
*/ |
|
363
|
|
|
$url = $this->getUrl([ |
|
364
|
8 |
|
'readonly' => 0, |
|
365
|
|
|
'query' => $query->toSql() |
|
|
|
|
|
|
366
|
|
|
]); |
|
367
|
|
|
|
|
368
|
|
|
$request->url($url); |
|
369
|
|
|
return $request; |
|
|
|
|
|
|
370
|
8 |
|
} |
|
|
|
|
|
|
371
|
8 |
|
|
|
372
|
8 |
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* @param string $sql |
|
|
|
|
|
|
375
|
8 |
|
* @param string $file_name |
|
376
|
|
|
* @return Statement |
|
|
|
|
|
|
377
|
8 |
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
378
|
8 |
|
*/ |
|
379
|
8 |
|
public function writeAsyncCSV($sql, $file_name): Statement |
|
|
|
|
|
|
380
|
8 |
|
{ |
|
381
|
|
|
$query = new Query($sql); |
|
382
|
8 |
|
|
|
383
|
|
|
$extendinfo = [ |
|
384
|
8 |
|
'sql' => $sql, |
|
385
|
8 |
|
'query' => $query, |
|
386
|
|
|
'format' => $query->getFormat() |
|
|
|
|
|
|
387
|
8 |
|
]; |
|
388
|
|
|
|
|
389
|
|
|
$request = $this->newRequest($extendinfo); |
|
390
|
|
|
|
|
391
|
|
|
/* |
|
392
|
|
|
* Build URL after request making, since URL may contain auth data. This will not matter after the |
|
393
|
|
|
* implantation of the todo in the `HTTP:newRequest()` method. |
|
394
|
|
|
*/ |
|
395
|
12 |
|
$url = $this->getUrl([ |
|
396
|
|
|
'readonly' => 0, |
|
397
|
12 |
|
'query' => $query->toSql() |
|
|
|
|
|
|
398
|
|
|
]); |
|
399
|
|
|
|
|
400
|
|
|
$request->url($url); |
|
401
|
|
|
|
|
402
|
|
|
$request->setCallbackFunction(function (CurlerRequest $request) { |
|
|
|
|
|
|
403
|
|
|
$handle = $request->getInfileHandle(); |
|
404
|
|
|
if (is_resource($handle)) { |
|
|
|
|
|
|
405
|
2 |
|
fclose($handle); |
|
|
|
|
|
|
406
|
|
|
} |
|
407
|
2 |
|
}); |
|
408
|
2 |
|
|
|
409
|
|
|
$request->setInfile($file_name); |
|
|
|
|
|
|
410
|
|
|
$this->_curler->addQueLoop($request); |
|
411
|
|
|
|
|
412
|
|
|
return new Statement($request); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
38 |
|
/** |
|
416
|
|
|
* get Count Pending Query in Queue |
|
417
|
38 |
|
* |
|
418
|
|
|
* @return int |
|
|
|
|
|
|
419
|
|
|
*/ |
|
420
|
|
|
public function getCountPendingQueue(): int |
|
421
|
1 |
|
{ |
|
422
|
|
|
return $this->_curler->countPending(); |
|
423
|
1 |
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
1 |
|
* set Connect TimeOut in seconds [CURLOPT_CONNECTTIMEOUT] ( int ) |
|
427
|
1 |
|
* |
|
428
|
1 |
|
* @param float $connectTimeOut |
|
|
|
|
|
|
429
|
1 |
|
*/ |
|
430
|
|
|
public function setConnectTimeOut(float $connectTimeOut) |
|
|
|
|
|
|
431
|
|
|
{ |
|
432
|
|
|
$this->_connectTimeOut = $connectTimeOut; |
|
433
|
1 |
|
} |
|
434
|
1 |
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* get ConnectTimeOut in seconds |
|
437
|
|
|
* |
|
438
|
1 |
|
* @return float |
|
|
|
|
|
|
439
|
1 |
|
*/ |
|
440
|
|
|
public function getConnectTimeOut(): float |
|
441
|
|
|
{ |
|
442
|
|
|
return $this->_connectTimeOut; |
|
443
|
1 |
|
} |
|
|
|
|
|
|
444
|
1 |
|
|
|
445
|
|
|
|
|
446
|
1 |
|
public function __findXClickHouseProgress($handle): bool |
|
|
|
|
|
|
447
|
|
|
{ |
|
448
|
1 |
|
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
|
|
|
|
|
449
|
|
|
|
|
450
|
|
|
// Search X-ClickHouse-Progress |
|
451
|
1 |
|
if ($code == 200) { |
|
|
|
|
|
|
452
|
|
|
$response = curl_multi_getcontent($handle); |
|
|
|
|
|
|
453
|
|
|
$header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE); |
|
|
|
|
|
|
454
|
|
|
if (!$header_size) { |
|
|
|
|
|
|
455
|
|
|
return false; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
1 |
|
$header = substr($response, 0, $header_size); |
|
|
|
|
|
|
459
|
|
|
if (!$header) { |
|
|
|
|
|
|
460
|
|
|
return false; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
$match = []; |
|
464
|
|
|
if (preg_match_all('/^X-ClickHouse-(?:Progress|Summary):(.*?)$/im', $header, $match)) { |
|
|
|
|
|
|
465
|
|
|
$data = @json_decode(end($match[1]), true); |
|
|
|
|
|
|
466
|
|
|
if ($data && is_callable($this->xClickHouseProgress)) { |
|
|
|
|
|
|
467
|
|
|
|
|
468
|
40 |
|
if (is_array($this->xClickHouseProgress)) { |
|
|
|
|
|
|
469
|
|
|
call_user_func_array($this->xClickHouseProgress, [$data]); |
|
|
|
|
|
|
470
|
40 |
|
} else { |
|
471
|
40 |
|
call_user_func($this->xClickHouseProgress, $data); |
|
|
|
|
|
|
472
|
|
|
} |
|
473
|
40 |
|
|
|
|
|
|
|
|
474
|
|
|
} |
|
475
|
1 |
|
} |
|
476
|
|
|
} |
|
477
|
1 |
|
return false; |
|
|
|
|
|
|
478
|
1 |
|
} |
|
479
|
|
|
|
|
480
|
|
|
/** |
|
481
|
|
|
* @param Query $query |
|
|
|
|
|
|
482
|
40 |
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
|
|
483
|
1 |
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
|
|
484
|
1 |
|
* @return CurlerRequest |
|
|
|
|
|
|
485
|
|
|
* @throws \Exception |
|
|
|
|
|
|
486
|
|
|
*/ |
|
487
|
|
|
public function getRequestRead(Query $query, $whereInFile = null, $writeToFile = null): CurlerRequest |
|
|
|
|
|
|
488
|
40 |
|
{ |
|
489
|
|
|
$urlParams = ['readonly' => 2]; |
|
|
|
|
|
|
490
|
|
|
$query_as_string = false; |
|
|
|
|
|
|
491
|
40 |
|
// --------------------------------------------------------------------------------- |
|
492
|
1 |
|
if ($whereInFile instanceof WhereInFile && $whereInFile->size()) { |
|
|
|
|
|
|
493
|
|
|
// $request = $this->prepareSelectWhereIn($request, $whereInFile); |
|
494
|
|
|
$structure = $whereInFile->fetchUrlParams(); |
|
495
|
|
|
// $structure = []; |
|
496
|
40 |
|
$urlParams = array_merge($urlParams, $structure); |
|
|
|
|
|
|
497
|
|
|
$query_as_string = true; |
|
|
|
|
|
|
498
|
1 |
|
} |
|
499
|
1 |
|
// --------------------------------------------------------------------------------- |
|
500
|
|
|
// if result to file |
|
501
|
1 |
|
if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) { |
|
|
|
|
|
|
502
|
|
|
$query->setFormat($writeToFile->fetchFormat()); |
|
503
|
1 |
|
unset($urlParams['extremes']); |
|
504
|
|
|
} |
|
505
|
|
|
// --------------------------------------------------------------------------------- |
|
506
|
|
|
// makeRequest read |
|
507
|
|
|
$request = $this->makeRequest($query, $urlParams, $query_as_string); |
|
|
|
|
|
|
508
|
|
|
// --------------------------------------------------------------------------------- |
|
509
|
|
|
// attach files |
|
510
|
|
|
if ($whereInFile instanceof WhereInFile && $whereInFile->size()) { |
|
|
|
|
|
|
511
|
|
|
$request->attachFiles($whereInFile->fetchFiles()); |
|
512
|
|
|
} |
|
513
|
|
|
// --------------------------------------------------------------------------------- |
|
514
|
|
|
// result to file |
|
515
|
|
|
if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) { |
|
|
|
|
|
|
516
|
1 |
|
|
|
517
|
|
|
$fout = fopen($writeToFile->fetchFile(), 'w'); |
|
|
|
|
|
|
518
|
1 |
|
if (is_resource($fout)) { |
|
|
|
|
|
|
519
|
|
|
|
|
520
|
|
|
$isGz = $writeToFile->getGzip(); |
|
521
|
40 |
|
|
|
522
|
1 |
|
if ($isGz) { |
|
|
|
|
|
|
523
|
|
|
// write gzip header |
|
524
|
|
|
// "\x1f\x8b\x08\x00\x00\x00\x00\x00" |
|
525
|
40 |
|
// fwrite($fout, "\x1F\x8B\x08\x08".pack("V", time())."\0\xFF", 10); |
|
526
|
|
|
// write the original file name |
|
527
|
|
|
// $oname = str_replace("\0", "", basename($writeToFile->fetchFile())); |
|
528
|
|
|
// fwrite($fout, $oname."\0", 1+strlen($oname)); |
|
529
|
1 |
|
|
|
530
|
|
|
fwrite($fout, "\x1f\x8b\x08\x00\x00\x00\x00\x00"); |
|
|
|
|
|
|
531
|
1 |
|
|
|
|
|
|
|
|
532
|
1 |
|
} |
|
533
|
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
535
|
68 |
|
$request->setResultFileHandle($fout, $isGz)->setCallbackFunction(function (CurlerRequest $request) { |
|
|
|
|
|
|
536
|
|
|
fclose($request->getResultFileHandle()); |
|
|
|
|
|
|
537
|
68 |
|
}); |
|
538
|
68 |
|
} |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
if ($this->stdErrOut) { |
|
|
|
|
|
|
542
|
|
|
$request->setStdErrOut($this->stdErrOut); |
|
543
|
|
|
} |
|
544
|
|
|
if ($this->xClickHouseProgress) { |
|
|
|
|
|
|
545
|
|
|
$request->setFunctionProgress([$this, '__findXClickHouseProgress']); |
|
546
|
27 |
|
} |
|
547
|
|
|
// --------------------------------------------------------------------------------- |
|
548
|
27 |
|
return $request; |
|
|
|
|
|
|
549
|
27 |
|
|
|
550
|
|
|
} |
|
|
|
|
|
|
551
|
|
|
|
|
552
|
|
|
public function cleanQueryDegeneration(): bool |
|
553
|
|
|
{ |
|
554
|
|
|
$this->_query_degenerations = []; |
|
|
|
|
|
|
555
|
38 |
|
return true; |
|
|
|
|
|
|
556
|
|
|
} |
|
557
|
38 |
|
|
|
558
|
38 |
|
public function addQueryDegeneration(Degeneration $degeneration): bool |
|
559
|
38 |
|
{ |
|
560
|
|
|
$this->_query_degenerations[] = $degeneration; |
|
|
|
|
|
|
561
|
38 |
|
return true; |
|
|
|
|
|
|
562
|
|
|
} |
|
563
|
|
|
|
|
564
|
|
|
/** |
|
565
|
|
|
* @param Query $query |
|
|
|
|
|
|
566
|
|
|
* @return CurlerRequest |
|
|
|
|
|
|
567
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
568
|
|
|
*/ |
|
569
|
46 |
|
public function getRequestWrite(Query $query): CurlerRequest |
|
570
|
|
|
{ |
|
571
|
|
|
$urlParams = ['readonly' => 0]; |
|
572
|
|
|
return $this->makeRequest($query, $urlParams); |
|
|
|
|
|
|
573
|
46 |
|
} |
|
574
|
46 |
|
|
|
575
|
|
|
/** |
|
576
|
|
|
* @throws TransportException |
|
577
|
46 |
|
*/ |
|
578
|
|
|
public function ping(): bool |
|
579
|
|
|
{ |
|
580
|
|
|
$request = new CurlerRequest(false, $this->handle); |
|
581
|
|
|
$request->url($this->getUri())->verbose(false)->GET()->connectTimeOut($this->getConnectTimeOut()); |
|
582
|
|
|
$this->_curler->execOne($request); |
|
583
|
|
|
|
|
584
|
|
|
return trim($request->response()->body()) === 'Ok.'; |
|
|
|
|
|
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* @param string $sql |
|
|
|
|
|
|
589
|
39 |
|
* @param mixed[] $bindings |
|
590
|
|
|
* @return Query |
|
|
|
|
|
|
591
|
39 |
|
*/ |
|
592
|
|
|
private function prepareQuery($sql, $bindings): Query |
|
|
|
|
|
|
593
|
|
|
{ |
|
|
|
|
|
|
594
|
39 |
|
|
|
595
|
39 |
|
// add Degeneration query |
|
596
|
39 |
|
foreach ($this->_query_degenerations as $degeneration) { |
|
|
|
|
|
|
597
|
|
|
$degeneration->bindParams($bindings); |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
return new Query($sql, $this->_query_degenerations); |
|
|
|
|
|
|
601
|
|
|
} |
|
|
|
|
|
|
602
|
|
|
|
|
603
|
|
|
|
|
604
|
|
|
/** |
|
605
|
|
|
* @param Query|string $sql |
|
|
|
|
|
|
606
|
28 |
|
* @param mixed[] $bindings |
|
|
|
|
|
|
607
|
|
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
|
|
608
|
28 |
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
|
|
609
|
|
|
* @return CurlerRequest |
|
|
|
|
|
|
610
|
|
|
* @throws \Exception |
|
|
|
|
|
|
611
|
|
|
*/ |
|
612
|
28 |
|
private function prepareSelect($sql, $bindings, $whereInFile, $writeToFile = null): CurlerRequest |
|
|
|
|
|
|
613
|
27 |
|
{ |
|
614
|
|
|
if ($sql instanceof Query) { |
|
|
|
|
|
|
615
|
|
|
return $this->getRequestWrite($sql); |
|
616
|
|
|
} |
|
617
|
|
|
$query = $this->prepareQuery($sql, $bindings); |
|
618
|
|
|
$query->setFormat('JSON'); |
|
619
|
|
|
return $this->getRequestRead($query, $whereInFile, $writeToFile); |
|
|
|
|
|
|
620
|
10 |
|
} |
|
|
|
|
|
|
621
|
|
|
|
|
622
|
10 |
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* @param Query|string $sql |
|
|
|
|
|
|
625
|
|
|
* @param mixed[] $bindings |
|
|
|
|
|
|
626
|
|
|
* @return CurlerRequest |
|
|
|
|
|
|
627
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
628
|
|
|
*/ |
|
629
|
|
|
private function prepareWrite($sql, $bindings = []): CurlerRequest |
|
|
|
|
|
|
630
|
|
|
{ |
|
631
|
|
|
if ($sql instanceof Query) { |
|
632
|
|
|
return $this->getRequestWrite($sql); |
|
633
|
|
|
} |
|
634
|
32 |
|
|
|
635
|
|
|
$query = $this->prepareQuery($sql, $bindings); |
|
636
|
32 |
|
|
|
637
|
32 |
|
if (strpos($sql, 'ON CLUSTER') === false) { |
|
|
|
|
|
|
638
|
32 |
|
return $this->getRequestWrite($query); |
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
if (strpos($sql, 'CREATE') === 0 || strpos($sql, 'DROP') === 0 || strpos($sql, 'ALTER') === 0) { |
|
|
|
|
|
|
642
|
|
|
$query->setFormat('JSON'); |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
return $this->getRequestWrite($query); |
|
646
|
|
|
} |
|
647
|
|
|
|
|
648
|
|
|
/** |
|
649
|
|
|
* @return bool |
|
|
|
|
|
|
650
|
7 |
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
651
|
|
|
*/ |
|
652
|
7 |
|
public function executeAsync(): bool |
|
653
|
7 |
|
{ |
|
654
|
7 |
|
return $this->_curler->execLoopWait(); |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
/** |
|
658
|
|
|
* @param Query|string $sql |
|
|
|
|
|
|
659
|
|
|
* @param mixed[] $bindings |
|
|
|
|
|
|
660
|
1 |
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
|
|
661
|
|
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
|
|
662
|
1 |
|
* @return Statement |
|
|
|
|
|
|
663
|
1 |
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
664
|
|
|
* @throws \Exception |
|
|
|
|
|
|
665
|
|
|
*/ |
|
666
|
|
|
public function select($sql, array $bindings = [], $whereInFile = null, $writeToFile = null): Statement |
|
|
|
|
|
|
667
|
|
|
{ |
|
668
|
|
|
$request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile); |
|
669
|
|
|
$this->_curler->execOne($request); |
|
670
|
|
|
return new Statement($request); |
|
|
|
|
|
|
671
|
|
|
} |
|
672
|
28 |
|
|
|
673
|
|
|
/** |
|
674
|
28 |
|
* @param Query|string $sql |
|
|
|
|
|
|
675
|
27 |
|
* @param mixed[] $bindings |
|
|
|
|
|
|
676
|
27 |
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
|
|
677
|
27 |
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
|
|
678
|
27 |
|
* @return Statement |
|
|
|
|
|
|
679
|
3 |
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
680
|
|
|
* @throws \Exception |
|
|
|
|
|
|
681
|
|
|
*/ |
|
682
|
25 |
|
public function selectAsync($sql, array $bindings = [], $whereInFile = null, $writeToFile = null): Statement |
|
|
|
|
|
|
683
|
|
|
{ |
|
684
|
|
|
$request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile); |
|
685
|
|
|
$this->_curler->addQueLoop($request); |
|
686
|
|
|
return new Statement($request); |
|
|
|
|
|
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
/** |
|
690
|
|
|
* @param callable $callback |
|
|
|
|
|
|
691
|
2 |
|
*/ |
|
692
|
|
|
public function setProgressFunction(callable $callback) : void |
|
|
|
|
|
|
693
|
2 |
|
{ |
|
694
|
2 |
|
$this->xClickHouseProgress = $callback; |
|
695
|
|
|
} |
|
696
|
|
|
|
|
697
|
|
|
/** |
|
698
|
|
|
* @param string $sql |
|
|
|
|
|
|
699
|
|
|
* @param mixed[] $bindings |
|
700
|
2 |
|
* @param bool $exception |
|
|
|
|
|
|
701
|
|
|
* @return Statement |
|
|
|
|
|
|
702
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
703
|
|
|
*/ |
|
704
|
|
|
public function write($sql, array $bindings = [], $exception = true): Statement |
|
|
|
|
|
|
705
|
|
|
{ |
|
706
|
|
|
$request = $this->prepareWrite($sql, $bindings); |
|
707
|
|
|
$this->_curler->execOne($request); |
|
708
|
|
|
$response = new Statement($request); |
|
709
|
|
|
if ($exception) { |
|
|
|
|
|
|
710
|
|
|
if ($response->isError()) { |
|
711
|
|
|
$response->error(); |
|
712
|
|
|
} |
|
713
|
2 |
|
} |
|
714
|
|
|
return $response; |
|
|
|
|
|
|
715
|
1 |
|
} |
|
716
|
1 |
|
|
|
717
|
1 |
|
/** |
|
718
|
|
|
* @param Stream $streamRW |
|
|
|
|
|
|
719
|
|
|
* @param CurlerRequest $request |
|
|
|
|
|
|
720
|
|
|
* @return Statement |
|
|
|
|
|
|
721
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
722
|
|
|
*/ |
|
723
|
|
|
private function streaming(Stream $streamRW, CurlerRequest $request): Statement |
|
724
|
|
|
{ |
|
725
|
2 |
|
$callable = $streamRW->getClosure(); |
|
726
|
|
|
$stream = $streamRW->getStream(); |
|
|
|
|
|
|
727
|
|
|
|
|
|
|
|
|
|
728
|
2 |
|
|
|
729
|
1 |
|
try { |
|
|
|
|
|
|
730
|
|
|
|
|
|
|
|
|
|
731
|
1 |
|
|
|
732
|
|
|
if (!is_callable($callable)) { |
|
|
|
|
|
|
733
|
|
|
if ($streamRW->isWrite()) { |
|
|
|
|
|
|
734
|
|
|
|
|
735
|
|
|
$callable = function ($ch, $fd, $length) use ($stream) { |
|
|
|
|
|
|
736
|
|
|
return ($line = fread($stream, $length)) ? $line : ''; |
|
|
|
|
|
|
737
|
|
|
}; |
|
738
|
2 |
|
} else { |
|
739
|
2 |
|
$callable = function ($ch, $fd) use ($stream) { |
|
|
|
|
|
|
740
|
2 |
|
return fwrite($stream, $fd); |
|
|
|
|
|
|
741
|
|
|
}; |
|
742
|
|
|
} |
|
743
|
2 |
|
} |
|
744
|
|
|
|
|
745
|
2 |
|
if ($streamRW->isGzipHeader()) { |
|
|
|
|
|
|
746
|
2 |
|
|
|
747
|
|
|
if ($streamRW->isWrite()) { |
|
|
|
|
|
|
748
|
|
|
$request->header('Content-Encoding', 'gzip'); |
|
749
|
|
|
$request->header('Content-Type', 'application/x-www-form-urlencoded'); |
|
750
|
|
|
} else { |
|
751
|
|
|
$request->header('Accept-Encoding', 'gzip'); |
|
752
|
|
|
} |
|
753
|
|
|
|
|
|
|
|
|
|
754
|
|
|
} |
|
755
|
|
|
|
|
|
|
|
|
|
756
|
|
|
|
|
757
|
|
|
$request->header('Transfer-Encoding', 'chunked'); |
|
758
|
|
|
|
|
759
|
|
|
if ($streamRW->isWrite()) { |
|
|
|
|
|
|
760
|
1 |
|
$request->setReadFunction($callable); |
|
761
|
|
|
} else { |
|
762
|
1 |
|
$request->setWriteFunction($callable); |
|
763
|
1 |
|
|
|
764
|
1 |
|
// $request->setHeaderFunction($callableHead); |
|
765
|
|
|
} |
|
766
|
|
|
|
|
|
|
|
|
|
767
|
|
|
|
|
768
|
|
|
$this->_curler->execOne($request); |
|
769
|
|
|
$response = new Statement($request); |
|
770
|
|
|
if ($response->isError()) { |
|
|
|
|
|
|
771
|
|
|
$response->error(); |
|
772
|
|
|
} |
|
773
|
|
|
return $response; |
|
|
|
|
|
|
774
|
|
|
} finally { |
|
775
|
1 |
|
if ($streamRW->isWrite()) |
|
|
|
|
|
|
776
|
|
|
fclose($stream); |
|
|
|
|
|
|
777
|
1 |
|
} |
|
778
|
1 |
|
|
|
|
|
|
|
|
779
|
1 |
|
|
|
780
|
|
|
} |
|
|
|
|
|
|
781
|
|
|
|
|
782
|
|
|
|
|
783
|
|
|
/** |
|
784
|
|
|
* @param Stream $streamRead |
|
|
|
|
|
|
785
|
|
|
* @param string $sql |
|
|
|
|
|
|
786
|
|
|
* @param mixed[] $bindings |
|
787
|
|
|
* @return Statement |
|
|
|
|
|
|
788
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
789
|
|
|
*/ |
|
790
|
|
|
public function streamRead(Stream $streamRead, $sql, $bindings = []): Statement |
|
|
|
|
|
|
791
|
|
|
{ |
|
792
|
|
|
$sql = $this->prepareQuery($sql, $bindings); |
|
|
|
|
|
|
793
|
|
|
$request = $this->getRequestRead($sql); |
|
794
|
|
|
return $this->streaming($streamRead, $request); |
|
|
|
|
|
|
795
|
|
|
|
|
796
|
|
|
} |
|
|
|
|
|
|
797
|
|
|
|
|
798
|
|
|
/** |
|
799
|
|
|
* @param Stream $streamWrite |
|
|
|
|
|
|
800
|
|
|
* @param string $sql |
|
|
|
|
|
|
801
|
|
|
* @param mixed[] $bindings |
|
802
|
|
|
* @return Statement |
|
|
|
|
|
|
803
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
|
|
804
|
|
|
*/ |
|
805
|
|
|
public function streamWrite(Stream $streamWrite, $sql, $bindings = []): Statement |
|
|
|
|
|
|
806
|
|
|
{ |
|
807
|
|
|
$sql = $this->prepareQuery($sql, $bindings); |
|
|
|
|
|
|
808
|
|
|
$request = $this->writeStreamData($sql); |
|
809
|
|
|
return $this->streaming($streamWrite, $request); |
|
|
|
|
|
|
810
|
|
|
} |
|
811
|
|
|
|
|
812
|
|
|
public function __destruct() |
|
813
|
|
|
{ |
|
814
|
|
|
if ($this->handle) { |
|
|
|
|
|
|
815
|
|
|
curl_close($this->handle); |
|
|
|
|
|
|
816
|
|
|
} |
|
817
|
|
|
} |
|
818
|
|
|
|
|
819
|
|
|
public function setHandle() |
|
|
|
|
|
|
820
|
|
|
{ |
|
821
|
|
|
$this->handle = curl_init(); |
|
|
|
|
|
|
822
|
|
|
} |
|
|
|
|
|
|
823
|
|
|
|
|
824
|
|
|
} |
|
|
|
|
|
|
825
|
|
|
|