1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\QueryException; |
6
|
|
|
use ClickHouseDB\Query\Degeneration\Bindings; |
7
|
|
|
use ClickHouseDB\Query\WhereInFile; |
8
|
|
|
use ClickHouseDB\Query\WriteToFile; |
9
|
|
|
use ClickHouseDB\Quote\FormatLine; |
10
|
|
|
use ClickHouseDB\Transport\Http; |
11
|
|
|
use ClickHouseDB\Transport\Stream; |
12
|
|
|
use function sprintf; |
13
|
|
|
|
14
|
|
|
class Client |
15
|
|
|
{ |
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* @var Http |
18
|
|
|
*/ |
19
|
|
|
private $_transport = null; |
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $_connect_username = ''; |
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $_connect_password = ''; |
30
|
|
|
|
31
|
|
|
/** |
|
|
|
|
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $_connect_host = ''; |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $_connect_port = ''; |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $_connect_user_readonly = false; |
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var array |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
private $_support_format = ['TabSeparated', 'TabSeparatedWithNames', 'CSV', 'CSVWithNames', 'JSONEachRow']; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Client constructor. |
|
|
|
|
52
|
|
|
* @param array $connect_params |
|
|
|
|
53
|
|
|
* @param array $settings |
|
|
|
|
54
|
|
|
*/ |
55
|
58 |
|
public function __construct($connect_params, $settings = []) |
|
|
|
|
56
|
|
|
{ |
57
|
58 |
|
if (!isset($connect_params['username'])) { |
|
|
|
|
58
|
|
|
throw new \InvalidArgumentException('not set username'); |
59
|
|
|
} |
60
|
|
|
|
61
|
58 |
|
if (!isset($connect_params['password'])) { |
|
|
|
|
62
|
|
|
throw new \InvalidArgumentException('not set password'); |
63
|
|
|
} |
64
|
|
|
|
65
|
58 |
|
if (!isset($connect_params['port'])) { |
|
|
|
|
66
|
|
|
throw new \InvalidArgumentException('not set port'); |
67
|
|
|
} |
68
|
|
|
|
69
|
58 |
|
if (!isset($connect_params['host'])) { |
|
|
|
|
70
|
|
|
throw new \InvalidArgumentException('not set host'); |
71
|
|
|
} |
72
|
|
|
|
73
|
58 |
|
if (isset($connect_params['settings']) && is_array($connect_params['settings'])) { |
|
|
|
|
74
|
2 |
|
if (empty($settings)) { |
75
|
2 |
|
$settings = $connect_params['settings']; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
58 |
|
$this->_connect_username = $connect_params['username']; |
|
|
|
|
80
|
58 |
|
$this->_connect_password = $connect_params['password']; |
|
|
|
|
81
|
58 |
|
$this->_connect_port = $connect_params['port']; |
|
|
|
|
82
|
58 |
|
$this->_connect_host = $connect_params['host']; |
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
85
|
|
|
// init transport class |
86
|
58 |
|
$this->_transport = new Http( |
87
|
58 |
|
$this->_connect_host, |
88
|
58 |
|
$this->_connect_port, |
89
|
58 |
|
$this->_connect_username, |
90
|
58 |
|
$this->_connect_password |
91
|
|
|
); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
94
|
58 |
|
$this->_transport->addQueryDegeneration(new Bindings()); |
95
|
|
|
|
96
|
|
|
// apply settings to transport class |
97
|
58 |
|
$this->settings()->database('default'); |
98
|
58 |
|
if (sizeof($settings)) { |
|
|
|
|
99
|
2 |
|
$this->settings()->apply($settings); |
100
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
103
|
58 |
|
if (isset($connect_params['readonly'])) |
104
|
|
|
{ |
105
|
|
|
$this->setReadOnlyUser($connect_params['readonly']); |
106
|
|
|
} |
107
|
|
|
|
108
|
58 |
|
if (isset($connect_params['https'])) |
109
|
|
|
{ |
110
|
|
|
$this->https($connect_params['https']); |
111
|
|
|
} |
112
|
|
|
|
113
|
58 |
|
$this->enableHttpCompression(); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
116
|
58 |
|
} |
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* if the user has only read in the config file |
120
|
|
|
* |
121
|
|
|
* @param bool $flag |
122
|
|
|
*/ |
123
|
|
|
public function setReadOnlyUser($flag) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$this->_connect_user_readonly = $flag; |
126
|
|
|
$this->settings()->setReadOnlyUser($this->_connect_user_readonly); |
127
|
|
|
} |
128
|
|
|
/** |
129
|
|
|
* Clear Degeneration processing request [template ] |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
1 |
|
public function cleanQueryDegeneration() |
|
|
|
|
134
|
|
|
{ |
135
|
1 |
|
return $this->_transport->cleanQueryDegeneration(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* add Degeneration processing |
140
|
|
|
* |
141
|
|
|
* @param Query\Degeneration $degeneration |
|
|
|
|
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function addQueryDegeneration(Query\Degeneration $degeneration) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
return $this->_transport->addQueryDegeneration($degeneration); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* add Conditions in query |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
1 |
|
public function enableQueryConditions() |
|
|
|
|
155
|
|
|
{ |
156
|
1 |
|
return $this->_transport->addQueryDegeneration(new \ClickHouseDB\Query\Degeneration\Conditions()); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
/** |
159
|
|
|
* Set connection host |
160
|
|
|
* |
161
|
|
|
* @param string|array $host |
|
|
|
|
162
|
|
|
*/ |
163
|
|
|
public function setHost($host) |
|
|
|
|
164
|
|
|
{ |
|
|
|
|
165
|
|
|
|
166
|
|
|
if (is_array($host)) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$host = array_rand(array_flip($host)); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->_connect_host = $host; |
172
|
|
|
$this->transport()->setHost($host); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Таймаут |
177
|
|
|
* |
178
|
|
|
* @param int $timeout |
179
|
|
|
* @return Settings |
180
|
|
|
*/ |
181
|
2 |
|
public function setTimeout($timeout) |
|
|
|
|
182
|
|
|
{ |
183
|
2 |
|
return $this->settings()->max_execution_time($timeout); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Timeout |
188
|
|
|
* |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
1 |
|
public function getTimeout() |
192
|
|
|
{ |
193
|
1 |
|
return $this->settings()->getTimeOut(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* ConnectTimeOut in seconds ( support 1.5 = 1500ms ) |
198
|
|
|
* |
199
|
|
|
* @param int $connectTimeOut |
200
|
|
|
*/ |
201
|
2 |
|
public function setConnectTimeOut($connectTimeOut) |
|
|
|
|
202
|
|
|
{ |
203
|
2 |
|
$this->transport()->setConnectTimeOut($connectTimeOut); |
204
|
2 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* get ConnectTimeOut |
208
|
|
|
* |
209
|
|
|
* @return int |
210
|
|
|
*/ |
211
|
1 |
|
public function getConnectTimeOut() |
|
|
|
|
212
|
|
|
{ |
213
|
1 |
|
return $this->transport()->getConnectTimeOut(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* transport |
219
|
|
|
* |
220
|
|
|
* @return Http |
221
|
|
|
*/ |
222
|
58 |
|
public function transport() |
|
|
|
|
223
|
|
|
{ |
224
|
58 |
|
if (!$this->_transport) { |
|
|
|
|
225
|
|
|
throw new \InvalidArgumentException('Empty transport class'); |
226
|
|
|
} |
227
|
58 |
|
return $this->_transport; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
|
|
public function getConnectHost() |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
return $this->_connect_host; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getConnectPassword() |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
return $this->_connect_password; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getConnectPort() |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
return $this->_connect_port; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function getConnectUsername() |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
return $this->_connect_username; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* transport |
264
|
|
|
* |
265
|
|
|
* @return Http |
266
|
|
|
*/ |
267
|
|
|
public function getTransport() |
|
|
|
|
268
|
|
|
{ |
269
|
|
|
return $this->_transport; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Режим отладки CURL |
275
|
|
|
* |
276
|
|
|
* @return mixed |
277
|
|
|
*/ |
278
|
|
|
public function verbose() |
279
|
|
|
{ |
280
|
|
|
return $this->transport()->verbose(true); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return Settings |
285
|
|
|
*/ |
286
|
58 |
|
public function settings() |
|
|
|
|
287
|
|
|
{ |
288
|
58 |
|
return $this->transport()->settings(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @return $this |
293
|
|
|
*/ |
294
|
2 |
|
public function useSession($useSessionId = false) |
|
|
|
|
295
|
|
|
{ |
296
|
2 |
|
if (!$this->settings()->getSessionId()) |
|
|
|
|
297
|
|
|
{ |
298
|
2 |
|
if (!$useSessionId) |
|
|
|
|
299
|
|
|
{ |
300
|
2 |
|
$this->settings()->makeSessionId(); |
301
|
|
|
} else |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
$this->settings()->session_id($useSessionId); |
304
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
} |
307
|
2 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
/** |
310
|
|
|
* @return mixed |
311
|
|
|
*/ |
312
|
2 |
|
public function getSession() |
313
|
|
|
{ |
314
|
2 |
|
return $this->settings()->getSessionId(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Query CREATE/DROP |
319
|
|
|
* |
320
|
|
|
* @param string $sql |
321
|
|
|
* @param array $bindings |
|
|
|
|
322
|
|
|
* @param bool $exception |
|
|
|
|
323
|
|
|
* @return Statement |
324
|
|
|
* @throws Exception\TransportException |
325
|
|
|
*/ |
326
|
22 |
|
public function write($sql, $bindings = [], $exception = true) |
|
|
|
|
327
|
|
|
{ |
328
|
22 |
|
return $this->transport()->write($sql, $bindings, $exception); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* set db name |
333
|
|
|
* @param string $db |
334
|
|
|
* @return $this |
335
|
|
|
*/ |
336
|
58 |
|
public function database($db) |
|
|
|
|
337
|
|
|
{ |
338
|
58 |
|
$this->settings()->database($db); |
339
|
58 |
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Write to system.query_log |
344
|
|
|
* |
345
|
|
|
* @param bool $flag |
346
|
|
|
* @return $this |
347
|
|
|
*/ |
348
|
|
|
public function enableLogQueries($flag = true) |
|
|
|
|
349
|
|
|
{ |
350
|
|
|
$this->settings()->set('log_queries', intval($flag)); |
|
|
|
|
351
|
|
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Compress the result if the HTTP client said that it understands data compressed with gzip or deflate |
356
|
|
|
* |
357
|
|
|
* @param bool $flag |
358
|
|
|
* @return $this |
359
|
|
|
*/ |
360
|
58 |
|
public function enableHttpCompression($flag = true) |
|
|
|
|
361
|
|
|
{ |
362
|
58 |
|
$this->settings()->enableHttpCompression($flag); |
363
|
58 |
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Enable / Disable HTTPS |
368
|
|
|
* |
369
|
|
|
* @param bool $flag |
370
|
|
|
* @return $this |
371
|
|
|
*/ |
372
|
1 |
|
public function https($flag = true) |
|
|
|
|
373
|
|
|
{ |
374
|
1 |
|
$this->settings()->https($flag); |
375
|
1 |
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Read extremes of the result columns. They can be output in JSON-formats. |
380
|
|
|
* |
381
|
|
|
* @param bool $flag |
382
|
|
|
* @return $this |
383
|
|
|
*/ |
384
|
2 |
|
public function enableExtremes($flag = true) |
|
|
|
|
385
|
|
|
{ |
386
|
2 |
|
$this->settings()->set('extremes', intval($flag)); |
|
|
|
|
387
|
2 |
|
return $this; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* SELECT |
392
|
|
|
* |
393
|
|
|
* @param string $sql |
|
|
|
|
394
|
|
|
* @param array $bindings |
|
|
|
|
395
|
|
|
* @param null|WhereInFile $whereInFile |
396
|
|
|
* @param null|WriteToFile $writeToFile |
397
|
|
|
* @return Statement |
398
|
|
|
* @throws Exception\TransportException |
399
|
|
|
* @throws \Exception |
400
|
|
|
*/ |
401
|
25 |
|
public function select($sql, $bindings = [], $whereInFile = null, $writeToFile = null) |
|
|
|
|
402
|
|
|
{ |
403
|
25 |
|
return $this->transport()->select($sql, $bindings, $whereInFile, $writeToFile); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* execute run |
408
|
|
|
* |
409
|
|
|
* @return bool |
410
|
|
|
* @throws Exception\TransportException |
411
|
|
|
*/ |
412
|
10 |
|
public function executeAsync() |
|
|
|
|
413
|
|
|
{ |
414
|
10 |
|
return $this->transport()->executeAsync(); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* set progressFunction |
419
|
|
|
* |
420
|
|
|
* @param callable $callback |
421
|
|
|
*/ |
422
|
1 |
|
public function progressFunction($callback) |
|
|
|
|
423
|
|
|
{ |
424
|
1 |
|
if (!is_callable($callback)) { |
|
|
|
|
425
|
|
|
throw new \InvalidArgumentException('Not is_callable progressFunction'); |
426
|
|
|
} |
427
|
|
|
|
428
|
1 |
|
if (!$this->settings()->is('send_progress_in_http_headers')) |
|
|
|
|
429
|
|
|
{ |
430
|
1 |
|
$this->settings()->set('send_progress_in_http_headers', 1); |
431
|
|
|
} |
432
|
1 |
|
if (!$this->settings()->is('http_headers_progress_interval_ms')) |
|
|
|
|
433
|
|
|
{ |
434
|
1 |
|
$this->settings()->set('http_headers_progress_interval_ms', 100); |
435
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
438
|
1 |
|
$this->transport()->setProgressFunction($callback); |
439
|
1 |
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* prepare select |
443
|
|
|
* |
444
|
|
|
* @param string $sql |
|
|
|
|
445
|
|
|
* @param array $bindings |
|
|
|
|
446
|
|
|
* @param null|WhereInFile $whereInFile |
447
|
|
|
* @param null|WriteToFile $writeToFile |
448
|
|
|
* @return Statement |
449
|
|
|
* @throws Exception\TransportException |
450
|
|
|
* @throws \Exception |
451
|
|
|
*/ |
452
|
5 |
|
public function selectAsync($sql, $bindings = [], $whereInFile = null, $writeToFile = null) |
|
|
|
|
453
|
|
|
{ |
454
|
5 |
|
return $this->transport()->selectAsync($sql, $bindings, $whereInFile, $writeToFile); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* SHOW PROCESSLIST |
459
|
|
|
* |
460
|
|
|
* @return array |
|
|
|
|
461
|
|
|
* @throws Exception\TransportException |
462
|
|
|
* @throws \Exception |
463
|
|
|
*/ |
464
|
|
|
public function showProcesslist() |
|
|
|
|
465
|
|
|
{ |
466
|
|
|
return $this->select('SHOW PROCESSLIST')->rows(); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* show databases |
471
|
|
|
* |
472
|
|
|
* @return array |
|
|
|
|
473
|
|
|
* @throws Exception\TransportException |
474
|
|
|
* @throws \Exception |
475
|
|
|
*/ |
476
|
|
|
public function showDatabases() |
|
|
|
|
477
|
|
|
{ |
478
|
|
|
return $this->select('show databases')->rows(); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* statement = SHOW CREATE TABLE |
483
|
|
|
* |
484
|
|
|
* @param string $table |
485
|
|
|
* @return mixed |
486
|
|
|
* @throws Exception\TransportException |
487
|
|
|
* @throws \Exception |
488
|
|
|
*/ |
489
|
|
|
public function showCreateTable($table) |
|
|
|
|
490
|
|
|
{ |
491
|
|
|
return ($this->select('SHOW CREATE TABLE ' . $table)->fetchOne('statement')); |
|
|
|
|
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* SHOW TABLES |
496
|
|
|
* |
497
|
|
|
* @return array |
|
|
|
|
498
|
|
|
* @throws Exception\TransportException |
499
|
|
|
* @throws \Exception |
500
|
|
|
*/ |
501
|
1 |
|
public function showTables() |
|
|
|
|
502
|
|
|
{ |
503
|
1 |
|
return $this->select('SHOW TABLES')->rowsAsTree('name'); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Get the number of simultaneous/Pending requests |
508
|
|
|
* |
509
|
|
|
* @return int |
510
|
|
|
*/ |
511
|
12 |
|
public function getCountPendingQueue() |
|
|
|
|
512
|
|
|
{ |
513
|
12 |
|
return $this->transport()->getCountPendingQueue(); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Insert Array |
518
|
|
|
* |
519
|
|
|
* @param string $table |
520
|
|
|
* @param array $values |
|
|
|
|
521
|
|
|
* @param array $columns |
|
|
|
|
522
|
|
|
* @return Statement |
523
|
|
|
* @throws Exception\TransportException |
524
|
|
|
*/ |
525
|
5 |
|
public function insert($table, $values, $columns = []) |
|
|
|
|
526
|
|
|
{ |
527
|
5 |
|
$sql = 'INSERT INTO `' . $table . "`"; |
|
|
|
|
528
|
|
|
|
529
|
5 |
|
if (0 !== count($columns)) { |
|
|
|
|
530
|
5 |
|
$sql .= ' (`' . implode('`,`', $columns) . '`) '; |
|
|
|
|
531
|
|
|
} |
532
|
|
|
|
533
|
5 |
|
$sql .= ' VALUES '; |
534
|
|
|
|
535
|
5 |
|
foreach ($values as $row) { |
536
|
5 |
|
$sql .= ' (' . FormatLine::Insert($row) . '), '; |
537
|
|
|
} |
538
|
5 |
|
$sql = trim($sql, ', '); |
|
|
|
|
539
|
5 |
|
return $this->transport()->write($sql); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Prepares the values to insert from the associative array. |
544
|
|
|
* There may be one or more lines inserted, but then the keys inside the array list must match (including in the sequence) |
545
|
|
|
* |
546
|
|
|
* @param array $values - array column_name => value (if we insert one row) or array list column_name => value if we insert many lines |
547
|
|
|
* @return array - list of arrays - 0 => fields, 1 => list of value arrays for insertion |
548
|
|
|
*/ |
549
|
3 |
|
public function prepareInsertAssocBulk(array $values) |
|
|
|
|
550
|
|
|
{ |
551
|
3 |
|
if (isset($values[0]) && is_array($values[0])) { //случай, когда много строк вставляется |
|
|
|
|
552
|
2 |
|
$preparedFields = array_keys($values[0]); |
|
|
|
|
553
|
2 |
|
$preparedValues = []; |
554
|
2 |
|
foreach ($values as $idx => $row) { |
555
|
2 |
|
$_fields = array_keys($row); |
|
|
|
|
556
|
2 |
|
if ($_fields !== $preparedFields) { |
557
|
1 |
|
throw new QueryException("Fields not match: " . implode(',', $_fields) . " and " . implode(',', $preparedFields) . " on element $idx"); |
|
|
|
|
558
|
|
|
} |
559
|
2 |
|
$preparedValues[] = array_values($row); |
|
|
|
|
560
|
|
|
} |
561
|
|
|
} else { //одна строка |
562
|
1 |
|
$preparedFields = array_keys($values); |
|
|
|
|
563
|
1 |
|
$preparedValues = [array_values($values)]; |
|
|
|
|
564
|
|
|
} |
565
|
2 |
|
return [$preparedFields, $preparedValues]; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Inserts one or more rows from an associative array. |
570
|
|
|
* If there is a discrepancy between the keys of the value arrays (or their order) - throws an exception. |
571
|
|
|
* |
572
|
|
|
* @param string $table - table name |
|
|
|
|
573
|
|
|
* @param array $values - array column_name => value (if we insert one row) or array list column_name => value if we insert many lines |
|
|
|
|
574
|
|
|
* @return Statement |
575
|
|
|
* @throws QueryException |
576
|
|
|
* @throws Exception\TransportException |
577
|
|
|
*/ |
578
|
|
|
public function insertAssocBulk($table, array $values) |
|
|
|
|
579
|
|
|
{ |
580
|
|
|
list($columns, $vals) = $this->prepareInsertAssocBulk($values); |
581
|
|
|
return $this->insert($table, $vals, $columns); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* insert TabSeparated files |
586
|
|
|
* |
587
|
|
|
* @param string $table_name |
|
|
|
|
588
|
|
|
* @param string|array $file_names |
|
|
|
|
589
|
|
|
* @param array $columns_array |
|
|
|
|
590
|
|
|
* @return mixed |
591
|
|
|
* @throws Exception\TransportException |
592
|
|
|
*/ |
593
|
1 |
|
public function insertBatchTSVFiles($table_name, $file_names, $columns_array=[]) |
|
|
|
|
594
|
|
|
{ |
595
|
1 |
|
return $this->insertBatchFiles($table_name, $file_names, $columns_array, 'TabSeparated'); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* insert Batch Files |
600
|
|
|
* |
601
|
|
|
* @param string $table_name |
|
|
|
|
602
|
|
|
* @param string|array $file_names |
|
|
|
|
603
|
|
|
* @param array $columns_array |
|
|
|
|
604
|
|
|
* @param string $format ['TabSeparated','TabSeparatedWithNames','CSV','CSVWithNames'] |
|
|
|
|
605
|
|
|
* @return array |
|
|
|
|
606
|
|
|
* @throws Exception\TransportException |
607
|
|
|
*/ |
608
|
8 |
|
public function insertBatchFiles($table_name, $file_names, $columns_array=[], $format = "CSV") |
|
|
|
|
609
|
|
|
{ |
610
|
8 |
|
if (is_string($file_names)) |
|
|
|
|
611
|
|
|
{ |
612
|
|
|
$file_names = [$file_names]; |
613
|
|
|
} |
614
|
8 |
|
if ($this->getCountPendingQueue() > 0) { |
615
|
|
|
throw new QueryException('Queue must be empty, before insertBatch, need executeAsync'); |
616
|
|
|
} |
617
|
|
|
|
618
|
8 |
|
if (!in_array($format, $this->_support_format)) |
|
|
|
|
619
|
|
|
{ |
620
|
|
|
throw new QueryException('Format not support in insertBatchFiles'); |
621
|
|
|
} |
622
|
|
|
|
623
|
8 |
|
$result = []; |
624
|
|
|
|
625
|
8 |
|
foreach ($file_names as $fileName) { |
626
|
8 |
|
if (!is_file($fileName) || !is_readable($fileName)) { |
|
|
|
|
627
|
|
|
throw new QueryException('Cant read file: ' . $fileName . ' ' . (is_file($fileName) ? '' : ' is not file')); |
|
|
|
|
628
|
|
|
} |
629
|
|
|
|
630
|
8 |
|
if (empty($columns_array)) |
631
|
|
|
{ |
632
|
|
|
$sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format; |
633
|
|
|
|
|
|
|
|
634
|
|
|
} else |
|
|
|
|
635
|
|
|
{ |
636
|
8 |
|
$sql = 'INSERT INTO ' . $table_name . ' ( ' . implode(',', $columns_array) . ' ) FORMAT ' . $format; |
|
|
|
|
637
|
|
|
|
|
|
|
|
638
|
|
|
} |
639
|
8 |
|
$result[$fileName] = $this->transport()->writeAsyncCSV($sql, $fileName); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
// exec |
643
|
8 |
|
$this->executeAsync(); |
644
|
|
|
|
645
|
|
|
// fetch resutl |
646
|
8 |
|
foreach ($file_names as $fileName) { |
647
|
8 |
|
if ($result[$fileName]->isError()) { |
|
|
|
|
648
|
8 |
|
$result[$fileName]->error(); |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
|
652
|
6 |
|
return $result; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* insert Batch Stream |
657
|
|
|
* |
658
|
|
|
* @param string $table_name |
659
|
|
|
* @param array $columns_array |
|
|
|
|
660
|
|
|
* @param string $format ['TabSeparated','TabSeparatedWithNames','CSV','CSVWithNames'] |
|
|
|
|
661
|
|
|
* @return Transport\CurlerRequest |
662
|
|
|
*/ |
663
|
2 |
|
public function insertBatchStream($table_name, $columns_array=[], $format = "CSV") |
|
|
|
|
664
|
|
|
{ |
665
|
2 |
|
if ($this->getCountPendingQueue() > 0) { |
666
|
|
|
throw new QueryException('Queue must be empty, before insertBatch, need executeAsync'); |
667
|
|
|
} |
668
|
|
|
|
669
|
2 |
|
if (!in_array($format, $this->_support_format)) |
|
|
|
|
670
|
|
|
{ |
671
|
|
|
throw new QueryException('Format not support in insertBatchFiles'); |
672
|
|
|
} |
673
|
|
|
|
674
|
2 |
|
if (empty($columns_array)) |
675
|
|
|
{ |
676
|
|
|
$sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format; |
677
|
|
|
|
|
|
|
|
678
|
|
|
} else |
|
|
|
|
679
|
|
|
{ |
680
|
2 |
|
$sql = 'INSERT INTO ' . $table_name . ' ( ' . implode(',', $columns_array) . ' ) FORMAT ' . $format; |
|
|
|
|
681
|
|
|
|
|
|
|
|
682
|
|
|
} |
683
|
|
|
|
684
|
2 |
|
return $this->transport()->writeStreamData($sql); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* stream Write |
690
|
|
|
* |
691
|
|
|
* @param Stream $stream |
|
|
|
|
692
|
|
|
* @param string $sql |
693
|
|
|
* @param array $bind |
|
|
|
|
694
|
|
|
* @return Statement |
695
|
|
|
* @throws Exception\TransportException |
696
|
|
|
*/ |
697
|
1 |
|
public function streamWrite(Stream $stream,$sql,$bind=[]) |
|
|
|
|
698
|
|
|
{ |
699
|
1 |
|
if ($this->getCountPendingQueue() > 0) { |
700
|
|
|
throw new QueryException('Queue must be empty, before streamWrite'); |
701
|
|
|
} |
702
|
1 |
|
return $this->transport()->streamWrite($stream,$sql,$bind); |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
|
706
|
|
|
/** |
707
|
|
|
* stream Read |
708
|
|
|
* |
709
|
|
|
* @param Stream $streamRead |
|
|
|
|
710
|
|
|
* @param string $sql |
711
|
|
|
* @param array $bind |
|
|
|
|
712
|
|
|
* @return Statement |
713
|
|
|
* @throws Exception\TransportException |
714
|
|
|
*/ |
715
|
1 |
|
public function streamRead(Stream $streamRead,$sql,$bind=[]) |
|
|
|
|
716
|
|
|
{ |
717
|
1 |
|
if ($this->getCountPendingQueue() > 0) { |
718
|
|
|
throw new QueryException('Queue must be empty, before streamWrite'); |
719
|
|
|
} |
720
|
1 |
|
return $this->transport()->streamRead($streamRead,$sql,$bind); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
/** |
724
|
|
|
* Size of database |
725
|
|
|
* |
726
|
|
|
* @return mixed|null |
727
|
|
|
* @throws Exception\TransportException |
728
|
|
|
* @throws \Exception |
729
|
|
|
*/ |
730
|
|
|
public function databaseSize() |
731
|
|
|
{ |
732
|
|
|
$b = $this->settings()->getDatabase(); |
733
|
|
|
|
734
|
|
|
return $this->select(' |
735
|
|
|
SELECT database,formatReadableSize(sum(bytes)) as size |
736
|
|
|
FROM system.parts |
737
|
|
|
WHERE active AND database=:database |
738
|
|
|
GROUP BY database |
739
|
|
|
', ['database' => $b])->fetchOne(); |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
743
|
|
|
* Size of tables |
744
|
|
|
* |
745
|
|
|
* @param string $tableName |
746
|
|
|
* @return mixed |
747
|
|
|
* @throws Exception\TransportException |
748
|
|
|
* @throws \Exception |
749
|
|
|
*/ |
750
|
1 |
|
public function tableSize($tableName) |
|
|
|
|
751
|
|
|
{ |
752
|
1 |
|
$tables = $this->tablesSize(); |
753
|
|
|
|
754
|
1 |
|
if (isset($tables[$tableName])) { |
755
|
1 |
|
return $tables[$tableName]; |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
return null; |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* Ping server |
763
|
|
|
* |
764
|
|
|
* @return bool |
765
|
|
|
* @throws Exception\TransportException |
766
|
|
|
*/ |
767
|
36 |
|
public function ping() |
|
|
|
|
768
|
|
|
{ |
769
|
36 |
|
return $this->transport()->ping(); |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* Tables sizes |
774
|
|
|
* |
775
|
|
|
* @param bool $flatList |
776
|
|
|
* @return array |
|
|
|
|
777
|
|
|
* @throws Exception\TransportException |
778
|
|
|
* @throws \Exception |
779
|
|
|
*/ |
780
|
1 |
|
public function tablesSize($flatList = false) |
|
|
|
|
781
|
|
|
{ |
782
|
1 |
|
$z = $this->select(' |
783
|
|
|
SELECT name as table,database, |
784
|
|
|
max(sizebytes) as sizebytes, |
785
|
|
|
max(size) as size, |
786
|
|
|
min(min_date) as min_date, |
787
|
|
|
max(max_date) as max_date |
788
|
|
|
FROM system.tables |
789
|
|
|
ANY LEFT JOIN |
790
|
|
|
( |
791
|
|
|
SELECT table,database, |
792
|
|
|
formatReadableSize(sum(bytes)) as size, |
793
|
|
|
sum(bytes) as sizebytes, |
794
|
|
|
min(min_date) as min_date, |
795
|
|
|
max(max_date) as max_date |
796
|
|
|
FROM system.parts |
797
|
|
|
WHERE active AND database=:database |
798
|
|
|
GROUP BY table,database |
799
|
|
|
) USING ( table,database ) |
800
|
|
|
WHERE database=:database |
801
|
|
|
GROUP BY table,database |
802
|
1 |
|
', ['database'=>$this->settings()->getDatabase()]); |
|
|
|
|
803
|
|
|
|
804
|
1 |
|
if ($flatList) { |
805
|
|
|
return $z->rows(); |
806
|
|
|
} |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
809
|
1 |
|
return $z->rowsAsTree('table'); |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
812
|
|
|
} |
|
|
|
|
813
|
|
|
|
814
|
|
|
|
815
|
|
|
/** |
816
|
|
|
* isExists |
817
|
|
|
* |
818
|
|
|
* @param string $database |
819
|
|
|
* @param string $table |
820
|
|
|
* @return array |
|
|
|
|
821
|
|
|
* @throws Exception\TransportException |
822
|
|
|
* @throws \Exception |
823
|
|
|
*/ |
824
|
|
|
public function isExists($database, $table) |
|
|
|
|
825
|
|
|
{ |
826
|
|
|
return $this->select(' |
827
|
|
|
SELECT * |
828
|
|
|
FROM system.tables |
829
|
|
|
WHERE name=\''.$table . '\' AND database=\'' . $database . '\'' |
|
|
|
|
830
|
|
|
)->rowsAsTree('name'); |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
|
834
|
|
|
/** |
835
|
|
|
* List of partitions |
836
|
|
|
* |
837
|
|
|
* @return mixed[][] |
838
|
|
|
*/ |
839
|
|
|
public function partitions(string $table, int $limit = null, bool $active = null) |
|
|
|
|
840
|
|
|
{ |
841
|
|
|
$database = $this->settings()->getDatabase(); |
842
|
|
|
$whereActiveClause = $active === null ? '' : sprintf(' AND active = %s', (int) $active); |
843
|
|
|
$limitClause = $limit !== null ? ' LIMIT ' . $limit : ''; |
844
|
|
|
|
845
|
|
|
return $this->select(<<<CLICKHOUSE |
846
|
|
|
SELECT * |
847
|
|
|
FROM system.parts |
848
|
|
|
WHERE like(table,'%$table%') AND database='$database'$whereActiveClause |
849
|
|
|
ORDER BY max_date $limitClause |
850
|
|
|
CLICKHOUSE |
851
|
|
|
)->rowsAsTree('name'); |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
/** |
855
|
|
|
* dropPartition |
856
|
|
|
* @deprecated |
857
|
|
|
* @param string $dataBaseTableName database_name.table_name |
858
|
|
|
* @param string $partition_id |
859
|
|
|
* @return Statement |
860
|
|
|
* @throws Exception\TransportException |
861
|
|
|
*/ |
862
|
|
|
public function dropPartition($dataBaseTableName, $partition_id) |
|
|
|
|
863
|
|
|
{ |
|
|
|
|
864
|
|
|
|
865
|
|
|
$partition_id = trim($partition_id, '\''); |
|
|
|
|
866
|
|
|
$this->settings()->set('replication_alter_partitions_sync', 2); |
867
|
|
|
$state = $this->write('ALTER TABLE {dataBaseTableName} DROP PARTITION :partion_id', [ |
868
|
|
|
'dataBaseTableName' => $dataBaseTableName, |
869
|
|
|
'partion_id' => $partition_id |
|
|
|
|
870
|
|
|
]); |
871
|
|
|
return $state; |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
/** |
875
|
|
|
* Truncate ( drop all partitions ) |
876
|
|
|
* @deprecated |
877
|
|
|
* @param string $tableName |
878
|
|
|
* @return array |
|
|
|
|
879
|
|
|
* @throws Exception\TransportException |
880
|
|
|
* @throws \Exception |
881
|
|
|
*/ |
882
|
|
|
public function truncateTable($tableName) |
|
|
|
|
883
|
|
|
{ |
884
|
|
|
$partions = $this->partitions($tableName); |
885
|
|
|
$out = []; |
|
|
|
|
886
|
|
|
foreach ($partions as $part_key=>$part) |
|
|
|
|
887
|
|
|
{ |
888
|
|
|
$part_id = $part['partition']; |
|
|
|
|
889
|
|
|
$out[$part_id] = $this->dropPartition($tableName, $part_id); |
|
|
|
|
890
|
|
|
} |
891
|
|
|
return $out; |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
/** |
895
|
|
|
* Returns the server's uptime in seconds. |
896
|
|
|
* |
897
|
|
|
* @return array |
|
|
|
|
898
|
|
|
* @throws Exception\TransportException |
899
|
|
|
*/ |
900
|
1 |
|
public function getServerUptime() |
|
|
|
|
901
|
|
|
{ |
902
|
1 |
|
return $this->select('SELECT uptime() as uptime')->fetchOne('uptime'); |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
/** |
906
|
|
|
* Returns string with the server version. |
907
|
|
|
* |
908
|
|
|
* @throws Exception\TransportException |
909
|
|
|
*/ |
910
|
1 |
|
public function getServerVersion() : string |
911
|
|
|
{ |
912
|
1 |
|
return (string) $this->select('SELECT version() as version')->fetchOne('version'); |
913
|
|
|
} |
914
|
|
|
|
915
|
|
|
|
916
|
|
|
/** |
917
|
|
|
* Read system.settings table |
918
|
|
|
* |
919
|
|
|
* @param string $like |
920
|
|
|
* @return array |
|
|
|
|
921
|
|
|
* @throws Exception\TransportException |
922
|
|
|
*/ |
923
|
1 |
|
public function getServerSystemSettings($like='') |
|
|
|
|
924
|
|
|
{ |
925
|
1 |
|
$l=[]; |
|
|
|
|
926
|
1 |
|
$list=$this->select('SELECT * FROM system.settings'.($like ? ' WHERE name LIKE :like':'' ),['like'=>'%'.$like.'%'])->rows(); |
|
|
|
|
927
|
1 |
|
foreach ($list as $row) { |
928
|
1 |
|
if (isset($row['name'])) {$n=$row['name']; unset($row['name']) ; $l[$n]=$row;} |
|
|
|
|
929
|
|
|
} |
930
|
1 |
|
return $l; |
931
|
|
|
} |
932
|
|
|
|
933
|
|
|
|
934
|
|
|
|
935
|
|
|
/** |
936
|
|
|
* dropOldPartitions by day_ago |
937
|
|
|
* @deprecated |
938
|
|
|
* |
939
|
|
|
* @param string $table_name |
940
|
|
|
* @param int $days_ago |
|
|
|
|
941
|
|
|
* @param int $count_partitons_per_one |
|
|
|
|
942
|
|
|
* @return array |
|
|
|
|
943
|
|
|
* @throws Exception\TransportException |
944
|
|
|
* @throws \Exception |
945
|
|
|
*/ |
946
|
|
|
public function dropOldPartitions($table_name, $days_ago, $count_partitons_per_one = 100) |
|
|
|
|
947
|
|
|
{ |
948
|
|
|
$days_ago = strtotime(date('Y-m-d 00:00:00', strtotime('-' . $days_ago . ' day'))); |
|
|
|
|
949
|
|
|
|
950
|
|
|
$drop = []; |
|
|
|
|
951
|
|
|
$list_patitions = $this->partitions($table_name, $count_partitons_per_one); |
952
|
|
|
|
953
|
|
|
foreach ($list_patitions as $partion_id => $partition) { |
954
|
|
|
if (stripos($partition['engine'], 'mergetree') === false) { |
|
|
|
|
955
|
|
|
continue; |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
// $min_date = strtotime($partition['min_date']); |
959
|
|
|
$max_date = strtotime($partition['max_date']); |
|
|
|
|
960
|
|
|
|
961
|
|
|
if ($max_date < $days_ago) { |
|
|
|
|
962
|
|
|
$drop[] = $partition['partition']; |
963
|
|
|
} |
964
|
|
|
} |
965
|
|
|
|
966
|
|
|
$result = []; |
967
|
|
|
foreach ($drop as $partition_id) { |
968
|
|
|
$result[$partition_id] = $this->dropPartition($table_name, $partition_id); |
|
|
|
|
969
|
|
|
} |
970
|
|
|
|
971
|
|
|
return $result; |
972
|
|
|
} |
973
|
|
|
} |
974
|
|
|
|