Clickhouse   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88%

Importance

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

2 Methods

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