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