Completed
Push — master ( 4484cf...67cb35 )
by Dmitry
02:58
created

Clickhouse::insert()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 3
crap 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 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
                $binds[$k] = (array) $v;
26 1
                $where[] = $k.' in (:'.$k.')';
27
            }
28
29 1
            $where = implode(' and ', $where);
30
31 1
            $query .= " where $where";
32
        }
33
34 2
        return $this->get(Client::class)->select($query, $binds);
35
    }
36
37 2
    public function insert(string $table, array $data, array $headers)
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39 2
        $client = $this->get(Client::class);
40 2
        if (count($data) < $this->bucketSize) {
41 2
            $buckets = [$data];
42
        } else {
43 1
            $buckets = array_chunk($data, $this->bucketSize);
44
        }
45
46 2
        foreach ($buckets as $bucket) {
47 2
            $client->insert('tester', $bucket, $headers);
48
        }
49
50 2
        return count($buckets);
51
    }
52
}
53