Completed
Push — master ( 9ffdb9...8d9fcf )
by Dmitry
04:00
created

Clickhouse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 56%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 52
ccs 14
cts 25
cp 0.56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B select() 0 29 5
A insert() 0 15 3
1
<?php
2
3
namespace Basis;
4
5
use ClickHouseDB\Client;
6
7
class Clickhouse
8
{
9
    use Toolkit;
10
11
    public $bucketSize = 1000;
12
13 1
    public function select($fields, string $table, array $params = [])
14
    {
15 1
        if (is_array($fields)) {
16
            $fields = implode(', ', $fields);
17
        }
18
19 1
        $query = "SELECT $fields FROM $table";
20 1
        $binds = [];
21
22 1
        if (count($params)) {
23
            $where = [];
24
            foreach ($params as $k => $v) {
25
                $v = (array) $v;
26
                if (count($v) == 1) {
27
                    $binds[$k] = $v[0];
28
                    $where[] = $k.' = :'.$k;
29
                } else {
30
                    $binds[$k] = $v;
31
                    $where[] = $k.' in (:'.$k.')';
32
                }
33
            }
34
35
            $where = implode(' and ', $where);
36
37
            $query .= " where $where";
38
        }
39
40 1
        return $this->get(Client::class)->select($query, $binds);
41
    }
42
43 1
    public function insert(string $table, array $data, array $headers)
44
    {
45 1
        if (count($data) < $this->bucketSize) {
46 1
            $buckets = [$data];
47
        } else {
48 1
            $buckets = array_chunk($data, $this->bucketSize);
49
        }
50
51 1
        $client = $this->get(Client::class);
52 1
        foreach ($buckets as $bucket) {
53 1
            $client->insert($table, $bucket, $headers);
54
        }
55
56 1
        return count($buckets);
57
    }
58
}
59