1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\DatabaseException; |
6
|
|
|
use ClickHouseDB\Exception\QueryException; |
7
|
|
|
use ClickHouseDB\Query\Query; |
8
|
|
|
use ClickHouseDB\Transport\CurlerRequest; |
9
|
|
|
use ClickHouseDB\Transport\CurlerResponse; |
10
|
|
|
|
11
|
|
|
class Statement |
12
|
|
|
{ |
13
|
|
|
/** |
|
|
|
|
14
|
|
|
* @var string|mixed |
15
|
|
|
*/ |
16
|
|
|
private $_rawData; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $_http_code = -1; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @var CurlerRequest |
25
|
|
|
*/ |
26
|
|
|
private $_request = null; |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $_init = false; |
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* @var Query |
35
|
|
|
*/ |
36
|
|
|
private $query; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @var mixed |
40
|
|
|
*/ |
41
|
|
|
private $format; |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $sql = ''; |
47
|
|
|
|
48
|
|
|
/** |
|
|
|
|
49
|
|
|
* @var array |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
private $meta; |
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @var array |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
private $totals; |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @var array |
|
|
|
|
60
|
|
|
*/ |
61
|
|
|
private $extremes; |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
private $rows; |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @var bool|integer |
|
|
|
|
70
|
|
|
*/ |
71
|
|
|
private $rows_before_limit_at_least = false; |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @var array |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
private $array_data = []; |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @var array|null |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
private $statistics = null; |
82
|
|
|
|
83
|
|
|
|
84
|
40 |
|
public function __construct(CurlerRequest $request) |
85
|
|
|
{ |
86
|
40 |
|
$this->_request = $request; |
87
|
40 |
|
$this->format = $this->_request->getRequestExtendedInfo('format'); |
|
|
|
|
88
|
40 |
|
$this->query = $this->_request->getRequestExtendedInfo('query'); |
|
|
|
|
89
|
40 |
|
$this->sql = $this->_request->getRequestExtendedInfo('sql'); |
|
|
|
|
90
|
40 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return CurlerRequest |
94
|
|
|
*/ |
95
|
|
|
public function getRequest() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return $this->_request; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return CurlerResponse |
|
|
|
|
102
|
|
|
* @throws Exception\TransportException |
103
|
|
|
*/ |
104
|
36 |
|
private function response() |
|
|
|
|
105
|
|
|
{ |
106
|
36 |
|
return $this->_request->response(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return mixed |
|
|
|
|
111
|
|
|
* @throws Exception\TransportException |
112
|
|
|
*/ |
113
|
1 |
|
public function responseInfo() |
114
|
|
|
{ |
115
|
1 |
|
return $this->response()->info(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return mixed|string |
120
|
|
|
*/ |
121
|
9 |
|
public function sql() |
122
|
|
|
{ |
123
|
9 |
|
return $this->sql; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $body |
|
|
|
|
128
|
|
|
* @return array|bool |
|
|
|
|
129
|
|
|
*/ |
130
|
5 |
|
private function parseErrorClickHouse($body) |
|
|
|
|
131
|
|
|
{ |
132
|
5 |
|
$body = trim($body); |
|
|
|
|
133
|
5 |
|
$mathes = []; |
134
|
|
|
|
135
|
|
|
// Code: 115, e.displayText() = DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception |
136
|
|
|
// Code: 192, e.displayText() = DB::Exception: Unknown user x, e.what() = DB::Exception |
137
|
|
|
// Code: 60, e.displayText() = DB::Exception: Table default.ZZZZZ doesn't exist., e.what() = DB::Exception |
138
|
|
|
|
139
|
5 |
|
if (preg_match("%Code: (\d+),\se\.displayText\(\) \=\s*DB\:\:Exception\s*:\s*(.*)(?:\,\s*e\.what|\(version).*%ius", $body, $mathes)) { |
|
|
|
|
140
|
5 |
|
return ['code' => $mathes[1], 'message' => $mathes[2]]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
|
|
|
|
148
|
|
|
* @throws Exception\TransportException |
149
|
|
|
*/ |
150
|
8 |
|
public function error() |
|
|
|
|
151
|
|
|
{ |
152
|
8 |
|
if (!$this->isError()) { |
|
|
|
|
153
|
2 |
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
6 |
|
$body = $this->response()->body(); |
|
|
|
|
157
|
6 |
|
$error_no = $this->response()->error_no(); |
158
|
6 |
|
$error = $this->response()->error(); |
|
|
|
|
159
|
|
|
|
160
|
6 |
|
if (!$error_no && !$error) { |
|
|
|
|
161
|
5 |
|
$parse = $this->parseErrorClickHouse($body); |
162
|
|
|
|
163
|
5 |
|
if ($parse) { |
|
|
|
|
164
|
5 |
|
throw new DatabaseException($parse['message'] . "\nIN:" . $this->sql(), $parse['code']); |
165
|
|
|
} else { |
166
|
|
|
$code = $this->response()->http_code(); |
|
|
|
|
167
|
|
|
$message = "HttpCode:" . $this->response()->http_code() . " ; " . $this->response()->error() . " ;" . $body; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} else { |
170
|
1 |
|
$code = $error_no; |
|
|
|
|
171
|
1 |
|
$message = $this->response()->error(); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
throw new QueryException($message, $code); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return bool |
|
|
|
|
179
|
|
|
* @throws Exception\TransportException |
180
|
|
|
*/ |
181
|
36 |
|
public function isError() |
|
|
|
|
182
|
|
|
{ |
183
|
36 |
|
return ($this->response()->http_code() !== 200 || $this->response()->error_no()); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
29 |
|
private function check() : bool |
187
|
|
|
{ |
188
|
29 |
|
if (!$this->_request->isResponseExists()) { |
|
|
|
|
189
|
|
|
throw QueryException::noResponse(); |
190
|
|
|
} |
191
|
|
|
|
192
|
29 |
|
if ($this->isError()) { |
193
|
1 |
|
$this->error(); |
194
|
|
|
} |
195
|
|
|
|
196
|
28 |
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return bool |
|
|
|
|
201
|
|
|
* @throws Exception\TransportException |
202
|
|
|
*/ |
203
|
27 |
|
private function init() |
|
|
|
|
204
|
|
|
{ |
205
|
27 |
|
if ($this->_init) { |
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
|
209
|
27 |
|
$this->check(); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
212
|
26 |
|
$this->_rawData = $this->response()->rawDataOrJson($this->format); |
213
|
|
|
|
214
|
26 |
|
if (!$this->_rawData) { |
|
|
|
|
215
|
|
|
$this->_init = true; |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
26 |
|
$data=[]; |
|
|
|
|
219
|
26 |
|
foreach (['meta', 'data', 'totals', 'extremes', 'rows', 'rows_before_limit_at_least', 'statistics'] as $key) { |
|
|
|
|
220
|
|
|
|
221
|
26 |
|
if (isset($this->_rawData[$key])) { |
|
|
|
|
222
|
26 |
|
if ($key=='data') |
|
|
|
|
223
|
|
|
{ |
224
|
26 |
|
$data=$this->_rawData[$key]; |
|
|
|
|
225
|
|
|
} |
226
|
|
|
else{ |
|
|
|
|
227
|
26 |
|
$this->{$key} = $this->_rawData[$key]; |
228
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
26 |
|
if (empty($this->meta)) { |
234
|
|
|
throw new QueryException('Can`t find meta'); |
235
|
|
|
} |
236
|
|
|
|
237
|
26 |
|
$isJSONCompact=(stripos($this->format,'JSONCompact')!==false?true:false); |
|
|
|
|
238
|
26 |
|
$this->array_data = []; |
239
|
26 |
|
foreach ($data as $rows) { |
240
|
26 |
|
$r = []; |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
243
|
26 |
|
if ($isJSONCompact) |
244
|
|
|
{ |
245
|
1 |
|
$r[]=$rows; |
|
|
|
|
246
|
|
|
} |
247
|
|
|
else { |
248
|
25 |
|
foreach ($this->meta as $meta) { |
249
|
25 |
|
$r[$meta['name']] = $rows[$meta['name']]; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
26 |
|
$this->array_data[] = $r; |
254
|
|
|
} |
255
|
|
|
|
256
|
26 |
|
return true; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return array |
|
|
|
|
261
|
|
|
* @throws \Exception |
|
|
|
|
262
|
|
|
*/ |
263
|
|
|
public function extremes() |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
$this->init(); |
266
|
|
|
return $this->extremes; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return mixed |
|
|
|
|
271
|
|
|
* @throws Exception\TransportException |
272
|
|
|
*/ |
273
|
1 |
|
public function totalTimeRequest() |
274
|
|
|
{ |
275
|
1 |
|
$this->check(); |
276
|
1 |
|
return $this->response()->total_time(); |
277
|
|
|
|
278
|
|
|
} |
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return array |
|
|
|
|
282
|
|
|
* @throws \Exception |
|
|
|
|
283
|
|
|
*/ |
284
|
1 |
|
public function extremesMin() |
|
|
|
|
285
|
|
|
{ |
286
|
1 |
|
$this->init(); |
287
|
|
|
|
288
|
1 |
|
if (empty($this->extremes['min'])) { |
289
|
|
|
return []; |
290
|
|
|
} |
291
|
|
|
|
292
|
1 |
|
return $this->extremes['min']; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @return array |
|
|
|
|
297
|
|
|
* @throws \Exception |
|
|
|
|
298
|
|
|
*/ |
299
|
|
|
public function extremesMax() |
|
|
|
|
300
|
|
|
{ |
301
|
|
|
$this->init(); |
302
|
|
|
|
303
|
|
|
if (empty($this->extremes['max'])) { |
304
|
|
|
return []; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $this->extremes['max']; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return array |
|
|
|
|
312
|
|
|
* @throws Exception\TransportException |
313
|
|
|
*/ |
314
|
1 |
|
public function totals() |
|
|
|
|
315
|
|
|
{ |
316
|
1 |
|
$this->init(); |
317
|
1 |
|
return $this->totals; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
|
|
|
|
321
|
|
|
* |
322
|
|
|
*/ |
323
|
|
|
public function dumpRaw() |
|
|
|
|
324
|
|
|
{ |
325
|
|
|
print_r($this->_rawData); |
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
|
|
|
|
329
|
|
|
* |
330
|
|
|
*/ |
331
|
|
|
public function dump() |
|
|
|
|
332
|
|
|
{ |
333
|
|
|
$this->_request->dump(); |
334
|
|
|
$this->response()->dump(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @return bool|int |
|
|
|
|
339
|
|
|
* @throws Exception\TransportException |
340
|
|
|
*/ |
341
|
2 |
|
public function countAll() |
342
|
|
|
{ |
343
|
2 |
|
$this->init(); |
344
|
2 |
|
return $this->rows_before_limit_at_least; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param bool $key |
|
|
|
|
349
|
|
|
* @return array|mixed|null |
|
|
|
|
350
|
|
|
* @throws Exception\TransportException |
351
|
|
|
*/ |
352
|
|
|
public function statistics($key = false) |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
$this->init(); |
355
|
|
|
if ($key) |
356
|
|
|
{ |
357
|
|
|
if (!is_array($this->statistics)) { |
|
|
|
|
358
|
|
|
return null; |
359
|
|
|
} |
360
|
|
|
if (!isset($this->statistics[$key])) { |
|
|
|
|
361
|
|
|
return null; |
362
|
|
|
} |
363
|
|
|
return $this->statistics[$key]; |
364
|
|
|
} |
365
|
|
|
return $this->statistics; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return int |
|
|
|
|
370
|
|
|
* @throws Exception\TransportException |
371
|
|
|
*/ |
372
|
8 |
|
public function count() |
|
|
|
|
373
|
|
|
{ |
374
|
8 |
|
$this->init(); |
375
|
8 |
|
return $this->rows; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return mixed|string |
|
|
|
|
380
|
|
|
* @throws Exception\TransportException |
381
|
|
|
*/ |
382
|
3 |
|
public function rawData() |
383
|
|
|
{ |
384
|
3 |
|
if ($this->_init) { |
385
|
|
|
return $this->_rawData; |
386
|
|
|
} |
387
|
|
|
|
388
|
3 |
|
$this->check(); |
389
|
|
|
|
390
|
3 |
|
return $this->response()->rawDataOrJson($this->format); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param string $key |
|
|
|
|
395
|
|
|
* @return mixed|null |
396
|
|
|
* @throws Exception\TransportException |
397
|
|
|
*/ |
398
|
15 |
|
public function fetchOne($key = '') |
|
|
|
|
399
|
|
|
{ |
400
|
15 |
|
$this->init(); |
401
|
|
|
|
402
|
15 |
|
if (isset($this->array_data[0])) { |
403
|
15 |
|
if ($key) { |
404
|
15 |
|
if (isset($this->array_data[0][$key])) { |
405
|
14 |
|
return $this->array_data[0][$key]; |
406
|
|
|
} else { |
|
|
|
|
407
|
1 |
|
return null; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
2 |
|
return $this->array_data[0]; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
return null; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @param string|null $path |
|
|
|
|
419
|
|
|
* @return array |
|
|
|
|
420
|
|
|
* @throws Exception\TransportException |
421
|
|
|
*/ |
422
|
4 |
|
public function rowsAsTree($path) |
|
|
|
|
423
|
|
|
{ |
424
|
4 |
|
$this->init(); |
425
|
|
|
|
426
|
4 |
|
$out = []; |
427
|
4 |
|
foreach ($this->array_data as $row) { |
428
|
4 |
|
$d = $this->array_to_tree($row, $path); |
|
|
|
|
429
|
4 |
|
$out = array_replace_recursive($d, $out); |
|
|
|
|
430
|
|
|
} |
431
|
|
|
|
432
|
4 |
|
return $out; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Return size_upload,upload_content,speed_upload,time_request |
437
|
|
|
* |
438
|
|
|
* @return array |
|
|
|
|
439
|
|
|
* @throws Exception\TransportException |
440
|
|
|
*/ |
441
|
|
|
public function info_upload() |
|
|
|
|
442
|
|
|
{ |
443
|
|
|
$this->check(); |
444
|
|
|
return [ |
445
|
|
|
'size_upload' => $this->response()->size_upload(), |
446
|
|
|
'upload_content' => $this->response()->upload_content_length(), |
447
|
|
|
'speed_upload' => $this->response()->speed_upload(), |
448
|
|
|
'time_request' => $this->response()->total_time() |
|
|
|
|
449
|
|
|
]; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Return size_upload,upload_content,speed_upload,time_request,starttransfer_time,size_download,speed_download |
454
|
|
|
* |
455
|
|
|
* @return array |
|
|
|
|
456
|
|
|
* @throws Exception\TransportException |
457
|
|
|
*/ |
458
|
1 |
|
public function info() |
|
|
|
|
459
|
|
|
{ |
460
|
1 |
|
$this->check(); |
461
|
|
|
return [ |
462
|
1 |
|
'starttransfer_time' => $this->response()->starttransfer_time(), |
463
|
1 |
|
'size_download' => $this->response()->size_download(), |
464
|
1 |
|
'speed_download' => $this->response()->speed_download(), |
465
|
1 |
|
'size_upload' => $this->response()->size_upload(), |
466
|
1 |
|
'upload_content' => $this->response()->upload_content_length(), |
467
|
1 |
|
'speed_upload' => $this->response()->speed_upload(), |
468
|
1 |
|
'time_request' => $this->response()->total_time() |
|
|
|
|
469
|
|
|
]; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* get format in sql |
474
|
|
|
* @return mixed |
|
|
|
|
475
|
|
|
*/ |
476
|
1 |
|
public function getFormat() |
477
|
|
|
{ |
478
|
1 |
|
return $this->format; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @return array |
|
|
|
|
483
|
|
|
* @throws Exception\TransportException |
484
|
|
|
*/ |
485
|
9 |
|
public function rows() |
|
|
|
|
486
|
|
|
{ |
487
|
9 |
|
$this->init(); |
488
|
8 |
|
return $this->array_data; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @param array|string $arr |
|
|
|
|
493
|
|
|
* @param null|string|array $path |
|
|
|
|
494
|
|
|
* @return array |
|
|
|
|
495
|
|
|
*/ |
496
|
4 |
|
private function array_to_tree($arr, $path = null) |
|
|
|
|
497
|
|
|
{ |
498
|
4 |
|
if (is_array($path)) { |
|
|
|
|
499
|
|
|
$keys = $path; |
500
|
|
|
} else { |
501
|
4 |
|
$args = func_get_args(); |
|
|
|
|
502
|
4 |
|
array_shift($args); |
|
|
|
|
503
|
|
|
|
504
|
4 |
|
if (sizeof($args) < 2) { |
|
|
|
|
505
|
4 |
|
$separator = '.'; |
506
|
4 |
|
$keys = explode($separator, $path); |
|
|
|
|
507
|
|
|
} else { |
508
|
|
|
$keys = $args; |
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
// |
|
|
|
|
513
|
4 |
|
$tree = $arr; |
514
|
4 |
|
while (count($keys)) { |
|
|
|
|
515
|
4 |
|
$key = array_pop($keys); |
|
|
|
|
516
|
|
|
|
517
|
4 |
|
if (isset($arr[$key])) { |
518
|
4 |
|
$val = $arr[$key]; |
519
|
|
|
} else { |
520
|
|
|
$val = $key; |
521
|
|
|
} |
522
|
|
|
|
523
|
4 |
|
$tree = array($val => $tree); |
|
|
|
|
524
|
|
|
} |
525
|
4 |
|
if (!is_array($tree)) { |
|
|
|
|
526
|
|
|
return []; |
527
|
|
|
} |
528
|
4 |
|
return $tree; |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
|