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 function array_merge; |
13
|
|
|
use function fclose; |
14
|
|
|
use function http_build_query; |
15
|
|
|
use function is_callable; |
16
|
|
|
use function is_resource; |
17
|
|
|
use const PHP_EOL; |
18
|
|
|
use function stripos; |
|
|
|
|
19
|
|
|
|
20
|
|
|
class Http |
21
|
|
|
{ |
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $username; |
26
|
|
|
|
27
|
|
|
/** |
|
|
|
|
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $password; |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $host; |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $port; |
41
|
|
|
|
42
|
|
|
/** @var bool */ |
43
|
|
|
private $https; |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var bool|int |
47
|
|
|
*/ |
48
|
|
|
private $verbose = false; |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var CurlerRolling |
52
|
|
|
*/ |
53
|
|
|
private $_curler = null; |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @var Settings |
57
|
|
|
*/ |
58
|
|
|
private $settings; |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @var array |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
private $_query_degenerations = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Count seconds (float) |
67
|
|
|
* |
68
|
|
|
* @var float |
69
|
|
|
*/ |
70
|
|
|
private $timeout = 20; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Count seconds (float) |
74
|
|
|
* |
75
|
|
|
* @var float |
76
|
|
|
*/ |
77
|
|
|
private $connectTimeout = 5; |
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* @var callable|null |
81
|
|
|
*/ |
82
|
|
|
private $xClickHouseProgress; |
83
|
|
|
|
84
|
|
|
/** @var string */ |
85
|
|
|
private $database; |
86
|
|
|
|
87
|
63 |
|
public function __construct( |
|
|
|
|
88
|
|
|
string $host, |
89
|
|
|
int $port, |
90
|
|
|
string $username, |
91
|
|
|
string $password, |
92
|
|
|
string $database, |
93
|
|
|
bool $https = false |
94
|
|
|
) { |
95
|
63 |
|
$this->host = $host; |
96
|
63 |
|
$this->port = $port; |
97
|
63 |
|
$this->username = $username; |
98
|
63 |
|
$this->password = $password; |
99
|
63 |
|
$this->setDatabase($database); |
100
|
63 |
|
$this->setHttps($https); |
101
|
63 |
|
$this->settings = new Settings(); |
102
|
|
|
|
103
|
63 |
|
$this->setCurler(); |
104
|
63 |
|
} |
105
|
|
|
|
106
|
63 |
|
public function setHttps(bool $https) : void |
|
|
|
|
107
|
|
|
{ |
108
|
63 |
|
$this->https = $https; |
109
|
63 |
|
} |
110
|
|
|
|
111
|
63 |
|
public function setCurler() |
|
|
|
|
112
|
|
|
{ |
113
|
63 |
|
$this->_curler = new CurlerRolling(); |
114
|
63 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return CurlerRolling |
118
|
|
|
*/ |
119
|
|
|
public function getCurler() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
return $this->_curler; |
122
|
|
|
} |
123
|
|
|
|
124
|
63 |
|
public function setDatabase(string $database) : void |
|
|
|
|
125
|
|
|
{ |
126
|
63 |
|
$this->database = $database; |
127
|
63 |
|
} |
128
|
|
|
|
129
|
|
|
public function setHost(string $host) : void |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$this->host = $host; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
63 |
|
public function getUri() |
|
|
|
|
138
|
|
|
{ |
139
|
63 |
|
$proto = 'http'; |
140
|
63 |
|
if ($this->https) { |
141
|
1 |
|
$proto = 'https'; |
142
|
|
|
} |
|
|
|
|
143
|
63 |
|
$uri = $proto . '://' . $this->host; |
144
|
63 |
|
if (stripos($this->host, '/') !== false || stripos($this->host, ':') !== false) { |
145
|
1 |
|
return $uri; |
146
|
|
|
} |
|
|
|
|
147
|
63 |
|
if ($this->port > 0) { |
148
|
63 |
|
return $uri . ':' . $this->port; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
return $uri; |
152
|
|
|
} |
153
|
|
|
|
154
|
63 |
|
public function getSettings() : Settings |
|
|
|
|
155
|
|
|
{ |
156
|
63 |
|
return $this->settings; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function setVerbose(bool $flag) : void |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this->verbose = $flag; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param mixed[] $params |
|
|
|
|
166
|
|
|
*/ |
|
|
|
|
167
|
63 |
|
private function getUrl(array $params = []) : string |
168
|
|
|
{ |
169
|
63 |
|
$settings = $this->getSettings()->getQueryableSettings($params); |
170
|
|
|
|
171
|
63 |
|
return $this->getUri() . '?' . http_build_query(array_merge(['database' => $this->database], $settings)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array $extendinfo |
|
|
|
|
176
|
|
|
* @return CurlerRequest |
177
|
|
|
*/ |
178
|
63 |
|
private function newRequest($extendinfo) |
|
|
|
|
179
|
|
|
{ |
180
|
63 |
|
$new = new CurlerRequest(); |
181
|
63 |
|
$new->auth($this->username, $this->password) |
182
|
63 |
|
->POST() |
183
|
63 |
|
->setRequestExtendedInfo($extendinfo); |
184
|
|
|
|
185
|
63 |
|
if ($this->getSettings()->isHttpCompressionEnabled()) { |
186
|
63 |
|
$new->httpCompression(true); |
187
|
|
|
} |
188
|
|
|
|
189
|
63 |
|
if ($this->getSettings()->getSessionId()) { |
190
|
1 |
|
$new->persistent(); |
191
|
|
|
} |
192
|
|
|
|
193
|
63 |
|
$new->setTimeout($this->timeout); |
194
|
63 |
|
$new->setConnectTimeout($this->connectTimeout); // one sec |
195
|
63 |
|
$new->keepAlive(); // one sec |
196
|
63 |
|
$new->verbose((bool) $this->verbose); |
197
|
|
|
|
198
|
63 |
|
return $new; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
|
|
|
|
202
|
|
|
* @param array $urlParams |
|
|
|
|
203
|
|
|
* @param bool $query_as_string |
|
|
|
|
204
|
|
|
* @return CurlerRequest |
205
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
206
|
|
|
*/ |
207
|
63 |
|
private function makeRequest(Query $query, array $urlParams = [], $query_as_string = false) |
|
|
|
|
208
|
|
|
{ |
209
|
63 |
|
$sql = $query->toSql(); |
210
|
|
|
|
211
|
63 |
|
if ($query_as_string) { |
212
|
1 |
|
$urlParams['query'] = $sql; |
213
|
|
|
} |
214
|
|
|
|
215
|
63 |
|
$url = $this->getUrl($urlParams); |
216
|
|
|
|
217
|
|
|
$extendinfo = [ |
218
|
63 |
|
'sql' => $sql, |
|
|
|
|
219
|
63 |
|
'query' => $query, |
|
|
|
|
220
|
63 |
|
'format' => $query->getFormat(), |
|
|
|
|
221
|
|
|
]; |
|
|
|
|
222
|
|
|
|
223
|
63 |
|
$new = $this->newRequest($extendinfo); |
224
|
63 |
|
$new->url($url); |
225
|
|
|
|
226
|
63 |
|
if (! $query_as_string) { |
227
|
63 |
|
$new->parameters_json($sql); |
228
|
|
|
} |
|
|
|
|
229
|
63 |
|
if ($this->getSettings()->isHttpCompressionEnabled()) { |
230
|
63 |
|
$new->httpCompression(true); |
231
|
|
|
} |
232
|
|
|
|
233
|
63 |
|
return $new; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string|Query $sql |
|
|
|
|
238
|
|
|
* @return CurlerRequest |
239
|
|
|
*/ |
240
|
3 |
|
public function writeStreamData($sql) |
|
|
|
|
241
|
|
|
{ |
|
|
|
|
242
|
|
|
|
243
|
3 |
|
if ($sql instanceof Query) { |
244
|
1 |
|
$query = $sql; |
245
|
|
|
} else { |
246
|
2 |
|
$query = new Query($sql); |
247
|
|
|
} |
248
|
|
|
|
249
|
3 |
|
$url = $this->getUrl([ |
250
|
3 |
|
'query' => $query->toSql(), |
|
|
|
|
251
|
|
|
]); |
|
|
|
|
252
|
|
|
$extendinfo = [ |
253
|
3 |
|
'sql' => $sql, |
|
|
|
|
254
|
3 |
|
'query' => $query, |
|
|
|
|
255
|
3 |
|
'format' => $query->getFormat(), |
|
|
|
|
256
|
|
|
]; |
|
|
|
|
257
|
|
|
|
258
|
3 |
|
$request = $this->newRequest($extendinfo); |
259
|
3 |
|
$request->url($url); |
260
|
|
|
|
261
|
3 |
|
return $request; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $sql |
|
|
|
|
266
|
|
|
* @param string $file_name |
|
|
|
|
267
|
|
|
* @return Statement |
268
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
269
|
|
|
*/ |
270
|
8 |
|
public function writeAsyncCSV($sql, $file_name) |
|
|
|
|
271
|
|
|
{ |
272
|
8 |
|
$query = new Query($sql); |
273
|
|
|
|
274
|
8 |
|
$url = $this->getUrl([ |
275
|
8 |
|
'query' => $query->toSql(), |
|
|
|
|
276
|
|
|
]); |
|
|
|
|
277
|
|
|
|
278
|
|
|
$extendinfo = [ |
279
|
8 |
|
'sql' => $sql, |
|
|
|
|
280
|
8 |
|
'query' => $query, |
|
|
|
|
281
|
8 |
|
'format' => $query->getFormat(), |
|
|
|
|
282
|
|
|
]; |
|
|
|
|
283
|
|
|
|
284
|
8 |
|
$request = $this->newRequest($extendinfo); |
285
|
8 |
|
$request->url($url); |
286
|
|
|
|
287
|
|
|
$request->setCallbackFunction(function (CurlerRequest $request) { |
|
|
|
|
288
|
8 |
|
$handle = $request->getInfileHandle(); |
289
|
8 |
|
if (is_resource($handle)) { |
|
|
|
|
290
|
8 |
|
fclose($handle); |
291
|
|
|
} |
292
|
8 |
|
}); |
293
|
|
|
|
294
|
8 |
|
$request->setInfile($file_name); |
295
|
8 |
|
$this->_curler->addQueLoop($request); |
296
|
|
|
|
297
|
8 |
|
return new Statement($request); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* get Count Pending Query in Queue |
302
|
|
|
* |
303
|
|
|
* @return int |
|
|
|
|
304
|
|
|
*/ |
305
|
12 |
|
public function getCountPendingQueue() |
|
|
|
|
306
|
|
|
{ |
307
|
12 |
|
return $this->_curler->countPending(); |
308
|
|
|
} |
309
|
|
|
|
310
|
1 |
|
public function getTimeout() : float |
|
|
|
|
311
|
|
|
{ |
312
|
1 |
|
return $this->timeout; |
313
|
|
|
} |
314
|
|
|
|
315
|
2 |
|
public function setTimeout(float $timeout) : void |
|
|
|
|
316
|
|
|
{ |
317
|
2 |
|
$this->timeout = $timeout; |
318
|
2 |
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get ConnectTimeout in seconds |
322
|
|
|
*/ |
|
|
|
|
323
|
39 |
|
public function getConnectTimeout() : float |
324
|
|
|
{ |
325
|
39 |
|
return $this->connectTimeout; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
|
|
|
|
329
|
|
|
* Set Connect Timeout in seconds |
330
|
|
|
*/ |
|
|
|
|
331
|
2 |
|
public function setConnectTimeout(float $connectTimeOut) : void |
332
|
|
|
{ |
333
|
2 |
|
$this->connectTimeout = $connectTimeOut; |
334
|
2 |
|
} |
335
|
|
|
|
336
|
1 |
|
private function findXClickHouseProgress($handle) |
|
|
|
|
337
|
|
|
{ |
338
|
1 |
|
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
|
|
|
339
|
|
|
|
340
|
|
|
// Search X-ClickHouse-Progress |
341
|
1 |
|
if ($code == 200) { |
|
|
|
|
342
|
1 |
|
$response = curl_multi_getcontent($handle); |
|
|
|
|
343
|
1 |
|
$header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE); |
|
|
|
|
344
|
1 |
|
if (! $header_size) { |
345
|
|
|
return false; |
346
|
|
|
} |
347
|
|
|
|
348
|
1 |
|
$header = substr($response, 0, $header_size); |
|
|
|
|
349
|
1 |
|
if (! $header_size) { |
350
|
|
|
return false; |
351
|
|
|
} |
|
|
|
|
352
|
1 |
|
$pos = strrpos($header, 'X-ClickHouse-Progress'); |
|
|
|
|
353
|
|
|
|
354
|
1 |
|
if (! $pos) { |
355
|
|
|
return false; |
356
|
|
|
} |
357
|
|
|
|
358
|
1 |
|
$last = substr($header, $pos); |
|
|
|
|
359
|
1 |
|
$data = @json_decode(str_ireplace('X-ClickHouse-Progress:', '', $last), true); |
|
|
|
|
360
|
|
|
|
361
|
1 |
|
if ($data && $this->xClickHouseProgress !== null) { |
362
|
1 |
|
($this->xClickHouseProgress)($data); |
363
|
|
|
} |
364
|
|
|
} |
365
|
1 |
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param Query $query |
|
|
|
|
369
|
|
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
370
|
|
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
371
|
|
|
* @return CurlerRequest |
372
|
|
|
* @throws \Exception |
|
|
|
|
373
|
|
|
*/ |
374
|
35 |
|
public function getRequestRead(Query $query, $whereInFile = null, $writeToFile = null) |
|
|
|
|
375
|
|
|
{ |
376
|
35 |
|
$query_as_string = false; |
377
|
35 |
|
$urlParams = []; |
378
|
|
|
// --------------------------------------------------------------------------------- |
379
|
35 |
|
if ($whereInFile instanceof WhereInFile && $whereInFile->size()) { |
380
|
|
|
// $request = $this->prepareSelectWhereIn($request, $whereInFile); |
381
|
1 |
|
$structure = $whereInFile->fetchUrlParams(); |
382
|
1 |
|
$urlParams = $structure; |
383
|
1 |
|
$query_as_string = true; |
384
|
|
|
} |
|
|
|
|
385
|
|
|
// --------------------------------------------------------------------------------- |
386
|
|
|
// if result to file |
387
|
35 |
|
if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) { |
388
|
1 |
|
$query->setFormat($writeToFile->fetchFormat()); |
389
|
1 |
|
unset($urlParams['extremes']); |
390
|
|
|
} |
|
|
|
|
391
|
|
|
// --------------------------------------------------------------------------------- |
392
|
|
|
// makeRequest read |
393
|
35 |
|
$request = $this->makeRequest($query, $urlParams, $query_as_string); |
394
|
|
|
// --------------------------------------------------------------------------------- |
395
|
|
|
// attach files |
396
|
35 |
|
if ($whereInFile instanceof WhereInFile && $whereInFile->size()) { |
397
|
1 |
|
$request->attachFiles($whereInFile->fetchFiles()); |
398
|
|
|
} |
|
|
|
|
399
|
|
|
// --------------------------------------------------------------------------------- |
400
|
|
|
// result to file |
401
|
35 |
|
if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) { |
|
|
|
|
402
|
|
|
|
403
|
1 |
|
$fout = fopen($writeToFile->fetchFile(), 'w'); |
|
|
|
|
404
|
1 |
|
if (is_resource($fout)) { |
|
|
|
|
405
|
|
|
|
406
|
1 |
|
$isGz = $writeToFile->getGzip(); |
407
|
|
|
|
408
|
1 |
|
if ($isGz) { |
409
|
|
|
// write gzip header |
410
|
|
|
// "\x1f\x8b\x08\x00\x00\x00\x00\x00" |
411
|
|
|
// fwrite($fout, "\x1F\x8B\x08\x08".pack("V", time())."\0\xFF", 10); |
412
|
|
|
// write the original file name |
413
|
|
|
// $oname = str_replace("\0", "", basename($writeToFile->fetchFile())); |
414
|
|
|
// fwrite($fout, $oname."\0", 1+strlen($oname)); |
415
|
|
|
|
416
|
|
|
fwrite($fout, "\x1f\x8b\x08\x00\x00\x00\x00\x00"); |
|
|
|
|
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$request->setResultFileHandle($fout, $isGz)->setCallbackFunction(function (CurlerRequest $request) { |
|
|
|
|
420
|
|
|
fclose($request->getResultFileHandle()); |
421
|
1 |
|
}); |
422
|
|
|
} |
423
|
|
|
} |
|
|
|
|
424
|
35 |
|
if ($this->xClickHouseProgress !== null) { |
425
|
|
|
$request->setFunctionProgress(function ($x) { |
426
|
1 |
|
return $this->findXClickHouseProgress($x); |
427
|
1 |
|
}); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
// --------------------------------------------------------------------------------- |
431
|
35 |
|
return $request; |
432
|
|
|
} |
433
|
|
|
|
434
|
1 |
|
public function cleanQueryDegeneration() : void |
|
|
|
|
435
|
|
|
{ |
436
|
1 |
|
$this->_query_degenerations = []; |
437
|
1 |
|
} |
438
|
|
|
|
439
|
63 |
|
public function addQueryDegeneration(Degeneration $degeneration) : void |
|
|
|
|
440
|
|
|
{ |
441
|
63 |
|
$this->_query_degenerations[] = $degeneration; |
442
|
63 |
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @param Query $query |
|
|
|
|
446
|
|
|
* @return CurlerRequest |
447
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
448
|
|
|
*/ |
449
|
63 |
|
public function getRequestWrite(Query $query) |
|
|
|
|
450
|
|
|
{ |
451
|
63 |
|
return $this->makeRequest($query); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @throws TransportException |
|
|
|
|
456
|
|
|
*/ |
|
|
|
|
457
|
39 |
|
public function ping() : bool |
458
|
|
|
{ |
459
|
39 |
|
$request = new CurlerRequest(); |
460
|
39 |
|
$request->url($this->getUri())->verbose(false)->GET()->setConnectTimeout($this->getConnectTimeout()); |
461
|
39 |
|
$this->_curler->execOne($request); |
462
|
|
|
|
463
|
39 |
|
return $request->response()->body() === 'Ok.' . PHP_EOL; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @param string $sql |
|
|
|
|
468
|
|
|
* @param mixed[] $bindings |
|
|
|
|
469
|
|
|
* @return Query |
470
|
|
|
*/ |
471
|
63 |
|
private function prepareQuery($sql, $bindings) |
|
|
|
|
472
|
|
|
{ |
|
|
|
|
473
|
|
|
|
474
|
|
|
// add Degeneration query |
475
|
63 |
|
foreach ($this->_query_degenerations as $degeneration) { |
476
|
63 |
|
$degeneration->bindParams($bindings); |
477
|
|
|
} |
478
|
|
|
|
479
|
63 |
|
return new Query($sql, $this->_query_degenerations); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @param Query|string $sql |
|
|
|
|
484
|
|
|
* @param mixed[] $bindings |
|
|
|
|
485
|
|
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
486
|
|
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
487
|
|
|
* @return CurlerRequest |
488
|
|
|
* @throws \Exception |
|
|
|
|
489
|
|
|
*/ |
490
|
34 |
|
private function prepareSelect($sql, $bindings, $whereInFile, $writeToFile = null) |
|
|
|
|
491
|
|
|
{ |
492
|
34 |
|
if ($sql instanceof Query) { |
493
|
|
|
return $this->getRequestWrite($sql); |
494
|
|
|
} |
|
|
|
|
495
|
34 |
|
$query = $this->prepareQuery($sql, $bindings); |
496
|
34 |
|
$query->setFormat('JSON'); |
497
|
|
|
|
498
|
34 |
|
return $this->getRequestRead($query, $whereInFile, $writeToFile); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param Query|string $sql |
|
|
|
|
503
|
|
|
* @param mixed[] $bindings |
|
|
|
|
504
|
|
|
* @return CurlerRequest |
505
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
506
|
|
|
*/ |
507
|
63 |
|
private function prepareWrite($sql, $bindings = []) |
|
|
|
|
508
|
|
|
{ |
509
|
63 |
|
if ($sql instanceof Query) { |
510
|
|
|
return $this->getRequestWrite($sql); |
511
|
|
|
} |
512
|
|
|
|
513
|
63 |
|
$query = $this->prepareQuery($sql, $bindings); |
514
|
|
|
|
515
|
63 |
|
return $this->getRequestWrite($query); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @return bool |
|
|
|
|
520
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
521
|
|
|
*/ |
522
|
10 |
|
public function executeAsync() |
|
|
|
|
523
|
|
|
{ |
524
|
10 |
|
return $this->_curler->execLoopWait(); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* @param Query|string $sql |
|
|
|
|
529
|
|
|
* @param mixed[] $bindings |
|
|
|
|
530
|
|
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
531
|
|
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
532
|
|
|
* @return Statement |
533
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
534
|
|
|
* @throws \Exception |
|
|
|
|
535
|
|
|
*/ |
536
|
29 |
|
public function select($sql, array $bindings = [], $whereInFile = null, $writeToFile = null) |
|
|
|
|
537
|
|
|
{ |
538
|
29 |
|
$request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile); |
539
|
29 |
|
$this->_curler->execOne($request); |
540
|
|
|
|
541
|
29 |
|
return new Statement($request); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* @param Query|string $sql |
|
|
|
|
546
|
|
|
* @param mixed[] $bindings |
|
|
|
|
547
|
|
|
* @param null|WhereInFile $whereInFile |
|
|
|
|
548
|
|
|
* @param null|WriteToFile $writeToFile |
|
|
|
|
549
|
|
|
* @return Statement |
550
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
551
|
|
|
* @throws \Exception |
|
|
|
|
552
|
|
|
*/ |
553
|
5 |
|
public function selectAsync($sql, array $bindings = [], $whereInFile = null, $writeToFile = null) |
|
|
|
|
554
|
|
|
{ |
555
|
5 |
|
$request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile); |
556
|
5 |
|
$this->_curler->addQueLoop($request); |
557
|
|
|
|
558
|
5 |
|
return new Statement($request); |
559
|
|
|
} |
560
|
|
|
|
561
|
1 |
|
public function setProgressFunction(callable $callback) : void |
|
|
|
|
562
|
|
|
{ |
563
|
1 |
|
$this->xClickHouseProgress = $callback; |
564
|
1 |
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* @param string $sql |
|
|
|
|
568
|
|
|
* @param mixed[] $bindings |
|
|
|
|
569
|
|
|
* @param bool $exception |
|
|
|
|
570
|
|
|
* @return Statement |
571
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
572
|
|
|
*/ |
573
|
63 |
|
public function write($sql, array $bindings = [], $exception = true) |
|
|
|
|
574
|
|
|
{ |
575
|
63 |
|
$request = $this->prepareWrite($sql, $bindings); |
576
|
63 |
|
$this->_curler->execOne($request); |
577
|
63 |
|
$response = new Statement($request); |
578
|
63 |
|
if ($exception) { |
579
|
63 |
|
if ($response->isError()) { |
580
|
3 |
|
$response->error(); |
581
|
|
|
} |
582
|
|
|
} |
583
|
|
|
|
584
|
63 |
|
return $response; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* @param Stream $streamRW |
|
|
|
|
589
|
|
|
* @param CurlerRequest $request |
|
|
|
|
590
|
|
|
* @return Statement |
591
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
592
|
|
|
*/ |
593
|
2 |
|
private function streaming(Stream $streamRW, CurlerRequest $request) |
|
|
|
|
594
|
|
|
{ |
595
|
2 |
|
$callable = $streamRW->getClosure(); |
596
|
2 |
|
$stream = $streamRW->getStream(); |
597
|
|
|
|
598
|
|
|
try { |
|
|
|
|
599
|
|
|
|
600
|
2 |
|
if (! is_callable($callable)) { |
601
|
|
|
if ($streamRW->isWrite()) { |
|
|
|
|
602
|
|
|
|
603
|
|
|
$callable = function ($ch, $fd, $length) use ($stream) { |
|
|
|
|
604
|
|
|
return ($line = fread($stream, $length)) ? $line : ''; |
|
|
|
|
605
|
|
|
}; |
606
|
|
|
} else { |
607
|
|
|
$callable = function ($ch, $fd) use ($stream) { |
|
|
|
|
608
|
|
|
return fwrite($stream, $fd); |
|
|
|
|
609
|
|
|
}; |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
|
613
|
2 |
|
if ($streamRW->isGzipHeader()) { |
|
|
|
|
614
|
|
|
|
615
|
1 |
|
if ($streamRW->isWrite()) { |
616
|
1 |
|
$request->header('Content-Encoding', 'gzip'); |
617
|
1 |
|
$request->header('Content-Type', 'application/x-www-form-urlencoded'); |
618
|
|
|
} else { |
619
|
|
|
$request->header('Accept-Encoding', 'gzip'); |
620
|
|
|
} |
621
|
|
|
} |
622
|
|
|
|
623
|
2 |
|
$request->header('Transfer-Encoding', 'chunked'); |
624
|
|
|
|
625
|
2 |
|
if ($streamRW->isWrite()) { |
626
|
1 |
|
$request->setReadFunction($callable); |
627
|
|
|
} else { |
628
|
1 |
|
$request->setWriteFunction($callable); |
629
|
|
|
// $request->setHeaderFunction($callableHead); |
630
|
|
|
} |
631
|
|
|
|
632
|
2 |
|
$this->_curler->execOne($request, true); |
633
|
2 |
|
$response = new Statement($request); |
634
|
2 |
|
if ($response->isError()) { |
635
|
|
|
$response->error(); |
636
|
|
|
} |
637
|
|
|
|
638
|
2 |
|
return $response; |
639
|
|
|
} finally { |
640
|
2 |
|
if ($streamRW->isWrite()) |
|
|
|
|
641
|
2 |
|
fclose($stream); |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* @param Stream $streamRead |
647
|
|
|
* @param string $sql |
648
|
|
|
* @param mixed[] $bindings |
649
|
|
|
* @return Statement |
650
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
651
|
|
|
*/ |
652
|
1 |
|
public function streamRead(Stream $streamRead, $sql, $bindings = []) |
653
|
|
|
{ |
654
|
1 |
|
$sql = $this->prepareQuery($sql, $bindings); |
655
|
1 |
|
$request = $this->getRequestRead($sql); |
656
|
|
|
|
657
|
1 |
|
return $this->streaming($streamRead, $request); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* @param Stream $streamWrite |
662
|
|
|
* @param string $sql |
663
|
|
|
* @param mixed[] $bindings |
664
|
|
|
* @return Statement |
665
|
|
|
* @throws \ClickHouseDB\Exception\TransportException |
|
|
|
|
666
|
|
|
*/ |
667
|
1 |
|
public function streamWrite(Stream $streamWrite, $sql, $bindings = []) |
668
|
|
|
{ |
669
|
1 |
|
$sql = $this->prepareQuery($sql, $bindings); |
670
|
1 |
|
$request = $this->writeStreamData($sql); |
671
|
|
|
|
672
|
1 |
|
return $this->streaming($streamWrite, $request); |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
|