forked from sanchezzzhak/kak-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.php
660 lines (582 loc) · 17.7 KB
/
Command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<?php
namespace kak\clickhouse;
use kak\clickhouse\httpclient\Request;
use Yii;
use yii\base\Exception;
use yii\db\Command as BaseCommand;
use yii\db\Exception as DbException;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* Class Command
* @package kak\clickhouse
* @property $db \kak\clickhouse\Connection
*/
class Command extends BaseCommand
{
const FETCH = 'fetch';
const FETCH_ALL = 'fetchAll';
const FETCH_COLUMN = 'fetchColumn';
const FETCH_SCALAR = 'fetchScalar';
const FETCH_MODE_TOTAL = 7;
const FETCH_MODE_ALL = 8;
/** @var int fetch type result */
public $fetchMode = 0;
private $_format = null;
private $_pendingParams = [];
private $_is_result;
private $_options = [];
/**
* @var
*/
private $_meta;
/**
* @var
*/
private $_data;
/**
* @var
*/
private $_totals;
/**
* @var array
*/
private $_extremes;
/**
* @var int
*/
private $_rows;
/**
* @var array
*/
private $_statistics;
/**
* @var
*/
private $_rows_before_limit_at_least;
/**
* @return null
*/
public function getFormat()
{
return $this->_format;
}
/**
* @param null $format
* @return $this
*/
public function setFormat($format)
{
$this->_format = $format;
return $this;
}
/**
* @return array
*/
public function getOptions()
{
return $this->_options;
}
/**
* @param array $options
* @return $this
*/
public function setOptions($options)
{
$this->_options = $options;
return $this;
}
/**
* Adds more options to already defined ones.
* Please refer to [[setOptions()]] on how to specify options.
* @param array $options additional options
* @return $this self reference.
*/
public function addOptions(array $options)
{
foreach ($options as $key => $value) {
if (is_array($value) && isset($this->_options[$key])) {
$value = ArrayHelper::merge($this->_options[$key], $value);
}
$this->_options[$key] = $value;
}
return $this;
}
public function bindValues($values)
{
if (empty($values)) {
return $this;
}
//$schema = $this->db->getSchema();
foreach ($values as $name => $value) {
if (is_array($value)) {
$this->_pendingParams[$name] = $value;
$this->params[$name] = $value[0];
} else {
$this->params[$name] = $value;
}
}
return $this;
}
public function execute($prepare = false)
{
$rawSql = $this->getRawSql();
$response = $this->db->transport
->createRequest()
->setUrl($this->getBaseUrl())
->setMethod('POST')
->setContent($rawSql)
->send();
$this->checkResponseStatus($response);
if ($prepare) {
return $this->parseResponse($response);
}
return $response;
}
/**
* @return array|mixed
*/
public function queryColumn()
{
return $this->queryInternal(self::FETCH_COLUMN);
}
/**
* Executes the SQL statement and returns the value of the first column in the first row of data.
* This method is best used when only a single value is needed for a query.
* @return string|null|false the value of the first column in the first row of the query result.
* False is returned if there is no value.
* @throws Exception execution failed
*/
public function queryScalar()
{
$result = $this->queryInternal(self::FETCH_SCALAR, 0);
return (is_numeric($result)) ? ($result + 0) : $result;
}
public function getRawSql()
{
if (empty($this->params)) {
return $this->getSql();
}
$params = [];
foreach ($this->params as $name => $value) {
if (is_string($name) && strncmp(':', $name, 1)) {
$name = ':' . $name;
}
if (is_string($value)) {
$params[$name] = $this->db->quoteValue($value);
} elseif (is_bool($value)) {
$params[$name] = ($value ? 'TRUE' : 'FALSE');
} elseif ($value === null) {
$params[$name] = 'NULL';
} elseif (!is_object($value) && !is_resource($value)) {
$params[$name] = $value;
}
}
if (!isset($params[1])) {
return strtr($this->getSql(), $params);
}
$sql = '';
foreach (explode('?', $this->getSql()) as $i => $part) {
$sql .= (isset($params[$i]) ? $params[$i] : '') . $part;
}
return $sql;
}
/**
* @param string $method
* @param null $fetchMode
* @return array|DataReader|mixed
* @throws Exception
*/
protected function queryInternal($method, $fetchMode = null)
{
$rawSql = $this->getRawSql();
if ($method == self::FETCH) {
if (preg_match('#^SELECT#is', $rawSql) && !preg_match('#LIMIT#is', $rawSql)) {
$rawSql .= ' LIMIT 1';
}
}
if ($this->getFormat() === null && strpos($rawSql, 'FORMAT ') === false) {
$rawSql .= ' FORMAT JSON';
}
\Yii::info($rawSql, 'kak\clickhouse\Command::query');
if ($method !== '') {
$info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency);
if (is_array($info)) {
/* @var $cache \yii\caching\Cache */
$cache = $info[0];
$cacheKey = [
__CLASS__,
$method,
$fetchMode,
$this->db->dsn,
$this->db->username,
$rawSql,
];
$result = $cache->get($cacheKey);
if (is_array($result) && isset($result[0])) {
Yii::trace('Query result served from cache', 'kak\clickhouse\Command::query');
return $this->prepareResult($result[0], $method, $fetchMode);
}
}
}
$token = $rawSql;
try {
Yii::beginProfile($token, 'kak\clickhouse\Command::query');
$response = $this->db->transport
->createRequest()
->setUrl($this->getBaseUrl())
->setMethod('POST')
->setContent($rawSql)
->send();
$this->checkResponseStatus($response);
$data = $this->parseResponse($response);
$result = $this->prepareResult($data, $method, $fetchMode);
Yii::endProfile($token, 'kak\clickhouse\Command::query');
} catch (\Exception $e) {
Yii::endProfile($token, 'kak\clickhouse\Command::query');
throw new Exception("Query error: " . $e->getMessage());
}
if (isset($cache, $cacheKey, $info)) {
$cache->set($cacheKey, [$data], $info[1], $info[2]);
Yii::trace('Saved query result in cache', 'kak\clickhouse\Command::query');
}
return $result;
}
/**
* @param $result
* @return array
*/
protected function getStatementData($result)
{
return [
'meta' => $this->getMeta(),
'data' => $result,
'rows' => $this->getRows(),
'countAll' => $this->getCountAll(),
'totals' => $this->getTotals(),
'statistics' => $this->getStatistics(),
'extremes' => $this->getExtremes(),
];
}
protected function getBaseUrl()
{
$urlBase = $this->db->transport->baseUrl;
return $this->db->buildUrl($urlBase, array_merge([
'database' => $this->db->database
], $this->getOptions()));
}
/**
* Raise exception when get 500s error
* @param $response \yii\httpclient\Response
* @throws Exception
*/
public function checkResponseStatus($response)
{
if ($response->getStatusCode() != 200) {
throw new DbException($response->getContent());
}
}
private function prepareResult($result, $method = null, $fetchMode = null)
{
$this->prepareResponseData($result);
$result = ArrayHelper::getValue($result, 'data', []);
switch ($method) {
case self::FETCH_COLUMN:
return array_map(function ($a) {
return array_values($a)[0];
}, $result);
break;
case self::FETCH_SCALAR:
if (array_key_exists(0, $result)) {
return current($result[0]);
}
break;
case self::FETCH:
return is_array($result) ? array_shift($result) : $result;
break;
}
if ($fetchMode == self::FETCH_MODE_ALL) {
return $this->getStatementData($result);
}
if ($fetchMode == self::FETCH_MODE_TOTAL) {
return $this->getTotals();
}
return $result;
}
/**
* Parse response with data
* @param \yii\httpclient\Response $response
* @param null|string $method
* @param bool $prepareResponse
* @return mixed|array
*/
private function parseResponse(\yii\httpclient\Response $response)
{
$contentType = $response
->getHeaders()
->get('Content-Type');
list($type) = explode(';', $contentType);
$type = strtolower($type);
$hash = [
'application/json' => 'parseJson'
];
$result = (isset($hash[$type])) ? $this->{$hash[$type]}($response->content) : $response->content;
return $result;
}
private function prepareResponseData($result)
{
if (!is_array($result)) {
return;
}
$this->_is_result = true;
foreach (['meta', 'data', 'totals', 'extremes', 'rows', 'rows_before_limit_at_least', 'statistics'] as $key) {
if (isset($result[$key])) {
$attr = "_" . $key;
$this->{$attr} = $result[$key];
}
}
}
private function parseJson($content)
{
return Json::decode($content);
}
private function ensureQueryExecuted()
{
if (true !== $this->_is_result) {
throw new DbException('Query was not executed yet');
}
}
/**
* get meta columns information
* @return mixed
*/
public function getMeta()
{
$this->ensureQueryExecuted();
return $this->_meta;
}
/**
* get all data result
* @return mixed|array
*/
public function getData()
{
if ($this->_is_result === null && !empty($this->getSql())) {
$this->queryInternal(null);
}
$this->ensureQueryExecuted();
return $this->_data;
}
/**
* Generation sql `create table` for meta (only select query)
*
* ```php
* $sql = 'SELECT sum(click) as sum_click, event_date FROM table_name GROUP BY event_date LIMIT 10';
* $command = $connection->createCommand($sql);
* $data = $command->queryAll();
* $schemaSql = $command->getSchemaQuery();
* ```
*
* @return string
* @throws DbException
*/
public function getSchemaQuery()
{
$sql = $this->getSql();
$meta = $this->getMeta();
if (!preg_match('#^SELECT#is', $sql)) {
throw new DbException('Query was not SELECT type');
}
$table = "CREATE TABLE x (\n ";
$columns = [];
foreach ($meta as $item) {
$columns[] = '`' . $item['name'] . '` ' . $item['type'];
}
$table .= implode(",\n ", $columns);
$table .= "\n)";
return $table;
}
/**
* @return mixed
*/
public function getTotals()
{
$this->ensureQueryExecuted();
return $this->_totals;
}
/**
* @return mixed
*/
public function getExtremes()
{
$this->ensureQueryExecuted();
return $this->_extremes;
}
/**
* get count result items
* @return mixed
*/
public function getRows()
{
$this->ensureQueryExecuted();
return $this->_rows;
}
/**
* max count result items
* @return mixed
*/
public function getCountAll()
{
$this->ensureQueryExecuted();
return $this->_rows_before_limit_at_least;
}
/**
* @return mixed
*/
public function getStatistics()
{
$this->ensureQueryExecuted();
return $this->_statistics;
}
/**
* Creates an INSERT command.
* For example,
*
* ```php
* $connection->createCommand()->insert('user', [
* 'name' => 'Sam',
* 'age' => 30,
* ])->execute();
* ```
*
* The method will properly escape the column names, and bind the values to be inserted.
*
* Note that the created command is not executed until [[execute()]] is called.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column data (name => value) to be inserted into the table.
* @return $this the command object itself
*/
public function insert($table, $columns)
{
$params = [];
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
return $this->setSql($sql)->bindValues($params);
}
/**
* @param $table
* @param null $columns columns default columns get schema table
* @param array $files list files
* @param string $format file format
* @return \yii\httpclient\Response[]
*/
public function batchInsertFiles($table, $columns = null, $files = [], $format = 'CSV')
{
$categoryLog = 'kak\clickhouse\Command::batchInsertFiles';
if ($columns === null) {
$columns = $this->db->getSchema()->getTableSchema($table)->columnNames;
}
$sql = 'INSERT INTO ' . $this->db->getSchema()->quoteTableName($table) . ' (' . implode(', ', $columns) . ')' . ' FORMAT ' . $format;
Yii::info($sql, $categoryLog);
Yii::beginProfile($sql, $categoryLog);
$urlBase = $this->db->transport->baseUrl;
$requests = [];
$url = $this->db->buildUrl($urlBase, [
'database' => $this->db->database,
'query' => $sql,
]);
foreach ($files as $key => $file) {
/** @var Request $request */
$request = $this->makeBatchInsert($url, file_get_contents($file));
$requests[$key] = $request;
}
$responses = $this->db->transport->batchSend($requests);
/*foreach ($responses as $response){
var_dump($response->getContent());
var_dump($response->getHeaders());
var_dump($response->getFormat());
}*/
Yii::beginProfile($sql);
return $responses;
}
/**
* @param $table
* @param null $columns
* @param array $files
* @param string $format
* @param int $size
* @return \yii\httpclient\Response[]
*/
public function batchInsertFilesDataSize($table, $columns = null, $files = [], $format = 'CSV', $size = 10000)
{
$categoryLog = 'kak\clickhouse\Command::batchInsertFilesDataSize';
if ($columns === null) {
$columns = $this->db->getSchema()->getTableSchema($table)->columnNames;
}
$sql = 'INSERT INTO ' . $this->db->getSchema()->quoteTableName($table) . ' (' . implode(', ', $columns) . ')' . ' FORMAT ' . $format;
Yii::info($sql, $categoryLog);
Yii::beginProfile($sql, $categoryLog);
$urlBase = $this->db->transport->baseUrl;
$responses = [];
$url = $this->db->buildUrl($urlBase, [
'database' => $this->db->database,
'query' => $sql,
]);
foreach ($files as $key => $file) {
if (($handle = fopen($file, 'r')) !== false) {
$buffer = '';
$count = $part = 0;
while (($line = fgets($handle)) !== false) {
$buffer .= $line;
$count++;
if ($count >= $size) {
$responses[$key]['part_' . ($part++)] = ($this->makeBatchInsert($url, $buffer)->send());
$buffer = '';
$count = 0;
}
}
if (!empty($buffer)) {
$responses[$key]['part_' . ($part++)] = ($this->makeBatchInsert($url, $buffer)->send());
}
fclose($handle);
}
}
Yii::beginProfile($sql);
return $responses;
}
/**
* @param $url
* @param $data
* @return Request
*/
private function makeBatchInsert($url, $data)
{
/** @var Request $request */
$request = $this->db->transport->createRequest();
$request->setFullUrl($url);
$request->setMethod('POST');
$request->setContent($data);
return $request;
}
/**
* Creates a batch INSERT command.
* For example,
*
* ```php
* $connection->createCommand()->batchInsert('user', ['name', 'age'], [
* ['Tom', 30],
* ['Jane', 20],
* ['Linda', 25],
* ])->execute();
* ```
*/
public function batchInsert($table, $columns, $rows)
{
$sql = $this->db->getQueryBuilder()->batchInsert($table, $columns, $rows);
return $this->setSql($sql);
}
public function query()
{
throw new \yii\db\Exception('Clichouse unsupport cursor');
}
}