Passed
Pull Request — master (#90)
by Šimon
04:05
created

Client::showProcesslist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
2
3
namespace ClickHouseDB;
4
5
use ClickHouseDB\Exception\QueryException;
6
use ClickHouseDB\Query\Degeneration\Bindings;
7
use ClickHouseDB\Query\WhereInFile;
8
use ClickHouseDB\Query\WriteToFile;
9
use ClickHouseDB\Quote\FormatLine;
10
use ClickHouseDB\Transport\Http;
11
use ClickHouseDB\Transport\Stream;
12
13
class Client
14
{
15
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_transport with single line content, use one-line comment instead.
Loading history...
16
     * @var Http
17
     */
18
    private $_transport = null;
19
20
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_username with single line content, use one-line comment instead.
Loading history...
21
     * @var string
22
     */
23
    private $_connect_username = '';
24
25
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_password with single line content, use one-line comment instead.
Loading history...
26
     * @var string
27
     */
28
    private $_connect_password = '';
29
30
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_host with single line content, use one-line comment instead.
Loading history...
31
     * @var string
32
     */
33
    private $_connect_host = '';
34
35
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_port with single line content, use one-line comment instead.
Loading history...
36
     * @var string
37
     */
38
    private $_connect_port = '';
39
40
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_user_readonly with single line content, use one-line comment instead.
Loading history...
41
     * @var bool
42
     */
43
    private $_connect_user_readonly = false;
44
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_support_format with single line content, use one-line comment instead.
Loading history...
45
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Client::$_support_format does not specify type hint for its items.
Loading history...
46
     */
47
    private $_support_format = ['TabSeparated', 'TabSeparatedWithNames', 'CSV', 'CSVWithNames', 'JSONEachRow'];
48
49
    /**
50
     * Client constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Client constructor.".
Loading history...
51
     * @param array $connect_params
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::__construct() does not specify type hint for items of its traversable parameter $connect_params.
Loading history...
52
     * @param array $settings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::__construct() does not specify type hint for items of its traversable parameter $settings.
Loading history...
53
     */
54 58
    public function __construct($connect_params, $settings = [])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::__construct() does not have parameter type hint for its parameter $connect_params but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::__construct() does not have parameter type hint for its parameter $settings but it should be possible to add it based on @param annotation "array".
Loading history...
55
    {
56 58
        if (!isset($connect_params['username'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
57
            throw  new \InvalidArgumentException('not set username');
58
        }
59
60 58
        if (!isset($connect_params['password'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
61
            throw  new \InvalidArgumentException('not set password');
62
        }
63
64 58
        if (!isset($connect_params['port'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
65
            throw  new \InvalidArgumentException('not set port');
66
        }
67
68 58
        if (!isset($connect_params['host'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
69
            throw  new \InvalidArgumentException('not set host');
70
        }
71
72 58
        if (isset($connect_params['settings']) && is_array($connect_params['settings'])) {
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
73 2
            if (empty($settings)) {
74 2
                $settings = $connect_params['settings'];
75
            }
76
        }
77
78 58
        $this->_connect_username    = $connect_params['username'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
79 58
        $this->_connect_password    = $connect_params['password'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
80 58
        $this->_connect_port        = $connect_params['port'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 8 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
81 58
        $this->_connect_host        = $connect_params['host'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 8 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
82
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
83
84
        // init transport class
85 58
        $this->_transport = new Http(
86 58
            $this->_connect_host,
87 58
            $this->_connect_port,
88 58
            $this->_connect_username,
89 58
            $this->_connect_password
90
        );
91
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
92
93 58
        $this->_transport->addQueryDegeneration(new Bindings());
94
95
        // apply settings to transport class
96 58
        $this->settings()->database('default');
97 58
        if (sizeof($settings)) {
0 ignored issues
show
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
98 2
            $this->settings()->apply($settings);
99
        }
100
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
101
102 58
        if (isset($connect_params['readonly']))
103
        {
104
            $this->setReadOnlyUser($connect_params['readonly']);
105
        }
106
107 58
        if (isset($connect_params['https']))
108
        {
109
            $this->https($connect_params['https']);
110
        }
111
112 58
        $this->enableHttpCompression();
113
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
114
115 58
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 2 blank lines before brace
Loading history...
116
117
    /**
118
     * if the user has only read in the config file
119
     *
120
     * @param bool $flag
121
     */
122
    public function setReadOnlyUser($flag)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setReadOnlyUser() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Client::setReadOnlyUser() does not have void return type hint.
Loading history...
123
    {
124
        $this->_connect_user_readonly = $flag;
125
        $this->settings()->setReadOnlyUser($this->_connect_user_readonly);
126
    }
127
    /**
128
     * Clear Degeneration processing request [template ]
129
     *
130
     * @return bool
131
     */
132 1
    public function cleanQueryDegeneration()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::cleanQueryDegeneration() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
133
    {
134 1
        return $this->_transport->cleanQueryDegeneration();
135
    }
136
137
    /**
138
     * add Degeneration processing
139
     *
140
     * @param Query\Degeneration $degeneration
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::addQueryDegeneration() has useless @param annotation for parameter $degeneration.
Loading history...
141
     * @return bool
142
     */
143
    public function addQueryDegeneration(Query\Degeneration $degeneration)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::addQueryDegeneration() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
144
    {
145
        return $this->_transport->addQueryDegeneration($degeneration);
146
    }
147
148
    /**
149
     * add Conditions in query
150
     *
151
     * @return bool
152
     */
153 1
    public function enableQueryConditions()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableQueryConditions() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
154
    {
155 1
        return $this->_transport->addQueryDegeneration(new \ClickHouseDB\Query\Degeneration\Conditions());
0 ignored issues
show
introduced by
Class \ClickHouseDB\Query\Degeneration\Conditions should not be referenced via a fully qualified name, but via a use statement.
Loading history...
156
    }
157
    /**
158
     * Set connection host
159
     *
160
     * @param string|array $host
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::setHost() does not specify type hint for items of its traversable parameter $host.
Loading history...
161
     */
162
    public function setHost($host)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setHost() does not have void return type hint.
Loading history...
163
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
164
165
        if (is_array($host))
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
166
        {
167
            $host = array_rand(array_flip($host));
0 ignored issues
show
introduced by
Function array_rand() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function array_flip() should not be referenced via a fallback global name, but via a use statement.
Loading history...
168
        }
169
170
        $this->_connect_host = $host;
171
        $this->transport()->setHost($host);
172
    }
173
174
    /**
175
     * Таймаут
176
     *
177
     * @param int $timeout
178
     * @return Settings
179
     */
180 2
    public function setTimeout($timeout)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setTimeout() does not have parameter type hint for its parameter $timeout but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::setTimeout() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Settings".
Loading history...
181
    {
182 2
        return $this->settings()->max_execution_time($timeout);
183
    }
184
185
    /**
186
     * Timeout
187
     *
188
     * @return mixed
189
     */
190 1
    public function getTimeout()
191
    {
192 1
        return $this->settings()->getTimeOut();
193
    }
194
195
    /**
196
     * ConnectTimeOut in seconds ( support 1.5 = 1500ms )
197
     *
198
     * @param int $connectTimeOut
199
     */
200 2
    public function setConnectTimeOut($connectTimeOut)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setConnectTimeOut() does not have parameter type hint for its parameter $connectTimeOut but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::setConnectTimeOut() does not have void return type hint.
Loading history...
201
    {
202 2
        $this->transport()->setConnectTimeOut($connectTimeOut);
203 2
    }
204
205
    /**
206
     * get ConnectTimeOut
207
     *
208
     * @return int
209
     */
210 1
    public function getConnectTimeOut()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectTimeOut() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
211
    {
212 1
        return $this->transport()->getConnectTimeOut();
213
    }
214
215
216
    /**
217
     * transport
218
     *
219
     * @return Http
220
     */
221 58
    public function transport()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::transport() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Http".
Loading history...
222
    {
223 58
        if (!$this->_transport) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
224
            throw  new \InvalidArgumentException('Empty transport class');
225
        }
226 58
        return $this->_transport;
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function getConnectHost()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectHost() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
233
    {
234
        return $this->_connect_host;
235
    }
236
237
    /**
238
     * @return string
239
     */
240
    public function getConnectPassword()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectPassword() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
241
    {
242
        return $this->_connect_password;
243
    }
244
245
    /**
246
     * @return string
247
     */
248
    public function getConnectPort()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectPort() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
249
    {
250
        return $this->_connect_port;
251
    }
252
253
    /**
254
     * @return string
255
     */
256
    public function getConnectUsername()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectUsername() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
257
    {
258
        return $this->_connect_username;
259
    }
260
261
    /**
262
     * transport
263
     *
264
     * @return Http
265
     */
266
    public function getTransport()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getTransport() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Http".
Loading history...
267
    {
268
        return $this->_transport;
269
    }
270
271
272
    /**
273
     * Режим отладки CURL
274
     *
275
     * @return mixed
276
     */
277
    public function verbose()
278
    {
279
        return $this->transport()->verbose(true);
280
    }
281
282
    /**
283
     * @return Settings
284
     */
285 58
    public function settings()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::settings() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Settings".
Loading history...
286
    {
287 58
        return $this->transport()->settings();
288
    }
289
290
    /**
291
     * @return $this
292
     */
293 2
    public function useSession($useSessionId = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::useSession() does not have parameter type hint nor @param annotation for its parameter $useSessionId.
Loading history...
294
    {
295 2
        if (!$this->settings()->getSessionId())
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
296
        {
297 2
            if (!$useSessionId)
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
298
            {
299 2
                $this->settings()->makeSessionId();
300
            } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
301
            {
302
                $this->settings()->session_id($useSessionId);
303
            }
304
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
305
        }
306 2
        return $this;
307
    }
308
    /**
309
     * @return mixed
310
     */
311 2
    public function getSession()
312
    {
313 2
        return $this->settings()->getSessionId();
314
    }
315
316
    /**
317
     * Query CREATE/DROP
318
     *
319
     * @param string $sql
320
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::write() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
321
     * @param bool $exception
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
322
     * @return Statement
323
     * @throws Exception\TransportException
324
     */
325 22
    public function write($sql, $bindings = [], $exception = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::write() does not have parameter type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::write() does not have parameter type hint for its parameter $bindings but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::write() does not have parameter type hint for its parameter $exception but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Client::write() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
326
    {
327 22
        return $this->transport()->write($sql, $bindings, $exception);
328
    }
329
330
    /**
331
     * set db name
332
     * @param string $db
333
     * @return $this
334
     */
335 58
    public function database($db)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::database() does not have parameter type hint for its parameter $db but it should be possible to add it based on @param annotation "string".
Loading history...
336
    {
337 58
        $this->settings()->database($db);
338 58
        return $this;
339
    }
340
341
    /**
342
     * Write to system.query_log
343
     *
344
     * @param bool $flag
345
     * @return $this
346
     */
347
    public function enableLogQueries($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableLogQueries() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
348
    {
349
        $this->settings()->set('log_queries', intval($flag));
0 ignored issues
show
introduced by
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
350
        return $this;
351
    }
352
353
    /**
354
     * Compress the result if the HTTP client said that it understands data compressed with gzip or deflate
355
     *
356
     * @param bool $flag
357
     * @return $this
358
     */
359 58
    public function enableHttpCompression($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableHttpCompression() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
360
    {
361 58
        $this->settings()->enableHttpCompression($flag);
362 58
        return $this;
363
    }
364
365
    /**
366
     * Enable / Disable HTTPS
367
     *
368
     * @param bool $flag
369
     * @return $this
370
     */
371 1
    public function https($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::https() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
372
    {
373 1
        $this->settings()->https($flag);
374 1
        return $this;
375
    }
376
377
    /**
378
     * Read extremes of the result columns. They can be output in JSON-formats.
379
     *
380
     * @param bool $flag
381
     * @return $this
382
     */
383 2
    public function enableExtremes($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableExtremes() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
384
    {
385 2
        $this->settings()->set('extremes', intval($flag));
0 ignored issues
show
introduced by
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
386 2
        return $this;
387
    }
388
389
    /**
390
     * SELECT
391
     *
392
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
393
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::select() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
394
     * @param null|WhereInFile $whereInFile
395
     * @param null|WriteToFile $writeToFile
396
     * @return Statement
397
     * @throws Exception\TransportException
398
     * @throws \Exception
399
     */
400 25
    public function select($sql, $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::select() does not have parameter type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::select() does not have parameter type hint for its parameter $bindings but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::select() does not have parameter type hint for its parameter $whereInFile but it should be possible to add it based on @param annotation "null|WhereInFile".
Loading history...
introduced by
Method \ClickHouseDB\Client::select() does not have parameter type hint for its parameter $writeToFile but it should be possible to add it based on @param annotation "null|WriteToFile".
Loading history...
introduced by
Method \ClickHouseDB\Client::select() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
401
    {
402 25
        return $this->transport()->select($sql, $bindings, $whereInFile, $writeToFile);
403
    }
404
405
    /**
406
     * execute run
407
     *
408
     * @return bool
409
     * @throws Exception\TransportException
410
     */
411 10
    public function executeAsync()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::executeAsync() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
412
    {
413 10
        return $this->transport()->executeAsync();
414
    }
415
416
    /**
417
     * set progressFunction
418
     *
419
     * @param callable $callback
420
     */
421 1
    public function progressFunction($callback)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::progressFunction() does not have parameter type hint for its parameter $callback but it should be possible to add it based on @param annotation "callable".
Loading history...
introduced by
Method \ClickHouseDB\Client::progressFunction() does not have void return type hint.
Loading history...
422
    {
423 1
        if (!is_callable($callback)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function is_callable() should not be referenced via a fallback global name, but via a use statement.
Loading history...
424
            throw new \InvalidArgumentException('Not is_callable progressFunction');
425
        }
426
427 1
        if (!$this->settings()->is('send_progress_in_http_headers'))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
428
        {
429 1
            $this->settings()->set('send_progress_in_http_headers', 1);
430
        }
431 1
        if (!$this->settings()->is('http_headers_progress_interval_ms'))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
432
        {
433 1
            $this->settings()->set('http_headers_progress_interval_ms', 100);
434
        }
435
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
436
437 1
        $this->transport()->setProgressFunction($callback);
438 1
    }
439
440
    /**
441
     * prepare select
442
     *
443
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
444
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::selectAsync() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
445
     * @param null|WhereInFile $whereInFile
446
     * @param null|WriteToFile $writeToFile
447
     * @return Statement
448
     * @throws Exception\TransportException
449
     * @throws \Exception
450
     */
451 5
    public function selectAsync($sql, $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::selectAsync() does not have parameter type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::selectAsync() does not have parameter type hint for its parameter $bindings but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::selectAsync() does not have parameter type hint for its parameter $whereInFile but it should be possible to add it based on @param annotation "null|WhereInFile".
Loading history...
introduced by
Method \ClickHouseDB\Client::selectAsync() does not have parameter type hint for its parameter $writeToFile but it should be possible to add it based on @param annotation "null|WriteToFile".
Loading history...
introduced by
Method \ClickHouseDB\Client::selectAsync() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
452
    {
453 5
        return $this->transport()->selectAsync($sql, $bindings, $whereInFile, $writeToFile);
454
    }
455
456
    /**
457
     * SHOW PROCESSLIST
458
     *
459
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::showProcesslist() does not specify type hint for items of its traversable return value.
Loading history...
460
     * @throws Exception\TransportException
461
     * @throws \Exception
462
     */
463
    public function showProcesslist()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showProcesslist() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
464
    {
465
        return $this->select('SHOW PROCESSLIST')->rows();
466
    }
467
468
    /**
469
     * show databases
470
     *
471
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::showDatabases() does not specify type hint for items of its traversable return value.
Loading history...
472
     * @throws Exception\TransportException
473
     * @throws \Exception
474
     */
475
    public function showDatabases()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showDatabases() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
476
    {
477
        return $this->select('show databases')->rows();
478
    }
479
480
    /**
481
     * statement = SHOW CREATE TABLE
482
     *
483
     * @param string $table
484
     * @return mixed
485
     * @throws Exception\TransportException
486
     * @throws \Exception
487
     */
488
    public function showCreateTable($table)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showCreateTable() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
489
    {
490
        return ($this->select('SHOW CREATE TABLE ' . $table)->fetchOne('statement'));
0 ignored issues
show
introduced by
Usage of language construct "return" with parentheses is disallowed.
Loading history...
491
    }
492
493
    /**
494
     * SHOW TABLES
495
     *
496
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::showTables() does not specify type hint for items of its traversable return value.
Loading history...
497
     * @throws Exception\TransportException
498
     * @throws \Exception
499
     */
500 1
    public function showTables()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showTables() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
501
    {
502 1
        return $this->select('SHOW TABLES')->rowsAsTree('name');
503
    }
504
505
    /**
506
     * Get the number of simultaneous/Pending requests
507
     *
508
     * @return int
509
     */
510 12
    public function getCountPendingQueue()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getCountPendingQueue() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
511
    {
512 12
        return $this->transport()->getCountPendingQueue();
513
    }
514
515
    /**
516
     * Insert Array
517
     *
518
     * @param string $table
519
     * @param array $values
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insert() does not specify type hint for items of its traversable parameter $values.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
520
     * @param array $columns
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insert() does not specify type hint for items of its traversable parameter $columns.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
521
     * @return Statement
522
     * @throws Exception\TransportException
523
     */
524 5
    public function insert($table, $values, $columns = [])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insert() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insert() does not have parameter type hint for its parameter $values but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::insert() does not have parameter type hint for its parameter $columns but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::insert() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
525
    {
526 5
        $sql = 'INSERT INTO `' . $table . "`";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ` does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
527
528 5
        if (0 !== count($columns)) {
0 ignored issues
show
introduced by
Yoda comparisons are disallowed.
Loading history...
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
529 5
            $sql .= ' (`' . implode('`,`', $columns) . '`) ';
0 ignored issues
show
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
530
        }
531
532 5
        $sql .= ' VALUES ';
533
534 5
        foreach ($values as $row) {
535 5
            $sql .= ' (' . FormatLine::Insert($row) . '), ';
536
        }
537 5
        $sql = trim($sql, ', ');
0 ignored issues
show
introduced by
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
538 5
        return $this->transport()->write($sql);
539
    }
540
541
    /**
542
      * Prepares the values to insert from the associative array.
543
      * There may be one or more lines inserted, but then the keys inside the array list must match (including in the sequence)
544
      *
545
      * @param array $values - array column_name => value (if we insert one row) or array list column_name => value if we insert many lines
546
      * @return array - list of arrays - 0 => fields, 1 => list of value arrays for insertion
547
      */
548 3
    public function prepareInsertAssocBulk(array $values)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::prepareInsertAssocBulk() does not have @param annotation for its traversable parameter $values.
Loading history...
introduced by
Method \ClickHouseDB\Client::prepareInsertAssocBulk() does not have return type hint nor @return annotation for its return value.
Loading history...
549
    {
550 3
        if (isset($values[0]) && is_array($values[0])) { //случай, когда много строк вставляется
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
551 2
            $preparedFields = array_keys($values[0]);
0 ignored issues
show
introduced by
Function array_keys() should not be referenced via a fallback global name, but via a use statement.
Loading history...
552 2
            $preparedValues = [];
553 2
            foreach ($values as $idx => $row) {
554 2
                $_fields = array_keys($row);
0 ignored issues
show
introduced by
Function array_keys() should not be referenced via a fallback global name, but via a use statement.
Loading history...
555 2
                if ($_fields !== $preparedFields) {
556 1
                    throw new QueryException("Fields not match: " . implode(',', $_fields) . " and " . implode(',', $preparedFields) . " on element $idx");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Fields not match: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style Comprehensibility introduced by
The string literal and does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Best Practice introduced by
Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
557
                }
558 2
                $preparedValues[] = array_values($row);
0 ignored issues
show
introduced by
Function array_values() should not be referenced via a fallback global name, but via a use statement.
Loading history...
559
            }
560
        } else { //одна строка
561 1
            $preparedFields = array_keys($values);
0 ignored issues
show
introduced by
Function array_keys() should not be referenced via a fallback global name, but via a use statement.
Loading history...
562 1
            $preparedValues = [array_values($values)];
0 ignored issues
show
introduced by
Function array_values() should not be referenced via a fallback global name, but via a use statement.
Loading history...
563
        }
564 2
        return [$preparedFields, $preparedValues];
565
    }
566
567
    /**
568
     * Inserts one or more rows from an associative array.
569
     * If there is a discrepancy between the keys of the value arrays (or their order) - throws an exception.
570
     *
571
     * @param string $table - table name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
572
     * @param array $values - array column_name => value (if we insert one row) or array list column_name => value if we insert many lines
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertAssocBulk() does not specify type hint for items of its traversable parameter $values.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
573
     * @return Statement
574
     * @throws QueryException
575
     * @throws Exception\TransportException
576
     */
577
    public function insertAssocBulk($table, array $values)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertAssocBulk() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertAssocBulk() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
578
    {
579
        list($columns, $vals) = $this->prepareInsertAssocBulk($values);
580
        return $this->insert($table, $vals, $columns);
581
    }
582
583
    /**
584
     * insert TabSeparated files
585
     *
586
     * @param string $table_name
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
587
     * @param string|array $file_names
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchTSVFiles() does not specify type hint for items of its traversable parameter $file_names.
Loading history...
588
     * @param array $columns_array
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchTSVFiles() does not specify type hint for items of its traversable parameter $columns_array.
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
589
     * @return mixed
590
     * @throws Exception\TransportException
591
     */
592 1
    public function insertBatchTSVFiles($table_name, $file_names, $columns_array=[])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertBatchTSVFiles() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$columns_array" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$columns_array"; expected 1 but found 0
Loading history...
593
    {
594 1
        return $this->insertBatchFiles($table_name, $file_names, $columns_array, 'TabSeparated');
595
    }
596
597
    /**
598
     * insert Batch Files
599
     *
600
     * @param string $table_name
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
601
     * @param string|array $file_names
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchFiles() does not specify type hint for items of its traversable parameter $file_names.
Loading history...
602
     * @param array $columns_array
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchFiles() does not specify type hint for items of its traversable parameter $columns_array.
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
603
     * @param string $format ['TabSeparated','TabSeparatedWithNames','CSV','CSVWithNames']
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
604
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::insertBatchFiles() does not specify type hint for items of its traversable return value.
Loading history...
605
     * @throws Exception\TransportException
606
     */
607 8
    public function insertBatchFiles($table_name, $file_names, $columns_array=[], $format = "CSV")
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertBatchFiles() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchFiles() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$columns_array" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$columns_array"; expected 1 but found 0
Loading history...
Coding Style Comprehensibility introduced by
The string literal CSV does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
608
    {
609 8
        if (is_string($file_names))
0 ignored issues
show
introduced by
Function is_string() should not be referenced via a fallback global name, but via a use statement.
Loading history...
610
        {
611
            $file_names = [$file_names];
612
        }
613 8
        if ($this->getCountPendingQueue() > 0) {
614
            throw new QueryException('Queue must be empty, before insertBatch, need executeAsync');
615
        }
616
617 8
        if (!in_array($format, $this->_support_format))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function in_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
618
        {
619
            throw new QueryException('Format not support in insertBatchFiles');
620
        }
621
622 8
        $result = [];
623
624 8
        foreach ($file_names as $fileName) {
625 8
            if (!is_file($fileName) || !is_readable($fileName)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function is_file() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function is_readable() should not be referenced via a fallback global name, but via a use statement.
Loading history...
626
                throw  new QueryException('Cant read file: ' . $fileName . ' ' . (is_file($fileName) ? '' : ' is not file'));
0 ignored issues
show
introduced by
Function is_file() should not be referenced via a fallback global name, but via a use statement.
Loading history...
627
            }
628
629 8
            if (empty($columns_array))
630
            {
631
                $sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format;
632
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
633
            } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
634
            {
635 8
                $sql = 'INSERT INTO ' . $table_name . ' ( ' . implode(',', $columns_array) . ' ) FORMAT ' . $format;
0 ignored issues
show
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
636
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
637
            }
638 8
            $result[$fileName] = $this->transport()->writeAsyncCSV($sql, $fileName);
639
        }
640
641
        // exec
642 8
        $this->executeAsync();
643
644
        // fetch resutl
645 8
        foreach ($file_names as $fileName) {
646 8
            if ($result[$fileName]->isError()) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
647 8
                $result[$fileName]->error();
648
            }
649
        }
650
651 6
        return $result;
652
    }
653
654
    /**
655
     * insert Batch Stream
656
     *
657
     * @param string $table_name
658
     * @param array $columns_array
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchStream() does not specify type hint for items of its traversable parameter $columns_array.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
659
     * @param string $format ['TabSeparated','TabSeparatedWithNames','CSV','CSVWithNames']
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
660
     * @return Transport\CurlerRequest
661
     */
662 2
    public function insertBatchStream($table_name, $columns_array=[], $format = "CSV")
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have parameter type hint for its parameter $columns_array but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have parameter type hint for its parameter $format but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Transport\CurlerRequest".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$columns_array" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$columns_array"; expected 1 but found 0
Loading history...
Coding Style Comprehensibility introduced by
The string literal CSV does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
663
    {
664 2
        if ($this->getCountPendingQueue() > 0) {
665
            throw new QueryException('Queue must be empty, before insertBatch, need executeAsync');
666
        }
667
668 2
        if (!in_array($format, $this->_support_format))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function in_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
669
        {
670
            throw new QueryException('Format not support in insertBatchFiles');
671
        }
672
673 2
        if (empty($columns_array))
674
        {
675
            $sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format;
676
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
677
        } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
678
        {
679 2
            $sql = 'INSERT INTO ' . $table_name . ' ( ' . implode(',', $columns_array) . ' ) FORMAT ' . $format;
0 ignored issues
show
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
680
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
681
        }
682
683 2
        return $this->transport()->writeStreamData($sql);
684
    }
685
686
687
    /**
688
     * stream Write
689
     *
690
     * @param Stream $stream
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamWrite() has useless @param annotation for parameter $stream.
Loading history...
691
     * @param string $sql
692
     * @param array $bind
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::streamWrite() does not specify type hint for items of its traversable parameter $bind.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
693
     * @return Statement
694
     * @throws Exception\TransportException
695
     */
696 1
    public function streamWrite(Stream $stream,$sql,$bind=[])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamWrite() does not have parameter type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::streamWrite() does not have parameter type hint for its parameter $bind but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::streamWrite() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$sql"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$bind"; 0 found
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$bind" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$bind"; expected 1 but found 0
Loading history...
697
    {
698 1
        if ($this->getCountPendingQueue() > 0) {
699
            throw new QueryException('Queue must be empty, before streamWrite');
700
        }
701 1
        return $this->transport()->streamWrite($stream,$sql,$bind);
702
    }
703
704
705
    /**
706
     * stream Read
707
     *
708
     * @param Stream $streamRead
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamRead() has useless @param annotation for parameter $streamRead.
Loading history...
709
     * @param string $sql
710
     * @param array $bind
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::streamRead() does not specify type hint for items of its traversable parameter $bind.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
711
     * @return Statement
712
     * @throws Exception\TransportException
713
     */
714 1
    public function streamRead(Stream $streamRead,$sql,$bind=[])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamRead() does not have parameter type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::streamRead() does not have parameter type hint for its parameter $bind but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::streamRead() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$sql"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$bind"; 0 found
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$bind" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$bind"; expected 1 but found 0
Loading history...
715
    {
716 1
        if ($this->getCountPendingQueue() > 0) {
717
            throw new QueryException('Queue must be empty, before streamWrite');
718
        }
719 1
        return $this->transport()->streamRead($streamRead,$sql,$bind);
720
    }
721
722
    /**
723
     * Size of database
724
     *
725
     * @return mixed|null
726
     * @throws Exception\TransportException
727
     * @throws \Exception
728
     */
729
    public function databaseSize()
730
    {
731
        $b = $this->settings()->getDatabase();
732
733
        return $this->select('
734
            SELECT database,formatReadableSize(sum(bytes)) as size
735
            FROM system.parts
736
            WHERE active AND database=:database
737
            GROUP BY database
738
        ', ['database' => $b])->fetchOne();
739
    }
740
741
    /**
742
     * Size of tables
743
     *
744
     * @param string $tableName
745
     * @return mixed
746
     * @throws Exception\TransportException
747
     * @throws \Exception
748
     */
749 1
    public function tableSize($tableName)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::tableSize() does not have parameter type hint for its parameter $tableName but it should be possible to add it based on @param annotation "string".
Loading history...
750
    {
751 1
        $tables = $this->tablesSize();
752
753 1
        if (isset($tables[$tableName])) {
754 1
            return $tables[$tableName];
755
        }
756
757
        return null;
758
    }
759
760
    /**
761
     * Ping server
762
     *
763
     * @return bool
764
     * @throws Exception\TransportException
765
     */
766 36
    public function ping()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::ping() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
767
    {
768 36
        return $this->transport()->ping();
769
    }
770
771
    /**
772
     * Tables sizes
773
     *
774
     * @param bool $flatList
775
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::tablesSize() does not specify type hint for items of its traversable return value.
Loading history...
776
     * @throws Exception\TransportException
777
     * @throws \Exception
778
     */
779 1
    public function tablesSize($flatList = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::tablesSize() does not have parameter type hint for its parameter $flatList but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Client::tablesSize() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
780
    {
781 1
        $z = $this->select('
782
        SELECT name as table,database,
783
            max(sizebytes) as sizebytes,
784
            max(size) as size,
785
            min(min_date) as min_date,
786
            max(max_date) as max_date
787
            FROM system.tables
788
            ANY LEFT JOIN 
789
            (
790
            SELECT table,database,
791
                        formatReadableSize(sum(bytes)) as size,
792
                        sum(bytes) as sizebytes,
793
                        min(min_date) as min_date,
794
                        max(max_date) as max_date
795
                        FROM system.parts 
796
                        WHERE active AND database=:database
797
                        GROUP BY table,database
798
            ) USING ( table,database )
799
            WHERE database=:database
800
            GROUP BY table,database
801 1
        ', ['database'=>$this->settings()->getDatabase()]);
0 ignored issues
show
Coding Style introduced by
Expected 1 space between "'database'" and double arrow; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between double arrow and "$this"; 0 found
Loading history...
802
803 1
        if ($flatList) {
804
            return $z->rows();
805
        }
806
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
807
808 1
        return $z->rowsAsTree('table');
809
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
810
811
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 2 blank lines before brace
Loading history...
812
813
814
    /**
815
     * isExists
816
     *
817
     * @param string $database
818
     * @param string $table
819
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::isExists() does not specify type hint for items of its traversable return value.
Loading history...
820
     * @throws Exception\TransportException
821
     * @throws \Exception
822
     */
823
    public function isExists($database, $table)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::isExists() does not have parameter type hint for its parameter $database but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::isExists() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::isExists() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
824
    {
825
        return $this->select('
826
            SELECT *
827
            FROM system.tables 
828
            WHERE name=\''.$table . '\' AND database=\'' . $database . '\''
0 ignored issues
show
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
829
        )->rowsAsTree('name');
830
    }
831
832
833
    /**
834
     * List of partitions
835
     *
836
     * @return mixed[][]
837
     */
838
    public function partitions(string $table, int $limit = null, bool $active = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::partitions() does not have return type hint for its return value but it should be possible to add it based on @return annotation "mixed[][]".
Loading history...
introduced by
Parameter $limit has null default value, but is not marked as nullable.
Loading history...
introduced by
Parameter $active has null default value, but is not marked as nullable.
Loading history...
839
    {
840
        $database = $this->settings()->getDatabase();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
841
        $whereActiveClause = $active===null ? '' : sprintf(' AND active = %s', (int) $active);
0 ignored issues
show
introduced by
Function sprintf() should not be referenced via a fallback global name, but via a use statement.
Loading history...
842
        $limitClause=$limit !== null ? ' LIMIT ' . $limit : '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
843
844
        return $this->select(<<<CLICKHOUSE
845
SELECT *
846
FROM system.parts 
847
WHERE like(table,'%$table%') AND database='$database'$whereActiveClause
848
ORDER BY max_date $limitClause
849
CLICKHOUSE
850
        )->rowsAsTree('name');
851
    }
852
853
    /**
854
     * dropPartition
855
     * @deprecated
856
     * @param string $dataBaseTableName database_name.table_name
857
     * @param string $partition_id
858
     * @return Statement
859
     * @throws Exception\TransportException
860
     */
861
    public function dropPartition($dataBaseTableName, $partition_id)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::dropPartition() does not have parameter type hint for its parameter $dataBaseTableName but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropPartition() does not have parameter type hint for its parameter $partition_id but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropPartition() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
862
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
863
864
        $partition_id = trim($partition_id, '\'');
0 ignored issues
show
introduced by
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
865
        $this->settings()->set('replication_alter_partitions_sync', 2);
866
        $state = $this->write('ALTER TABLE {dataBaseTableName} DROP PARTITION :partion_id', [
867
            'dataBaseTableName'  => $dataBaseTableName,
868
            'partion_id' => $partition_id
0 ignored issues
show
introduced by
Multiline arrays must have a trailing comma after the last element.
Loading history...
869
        ]);
870
        return $state;
871
    }
872
873
    /**
874
     * Truncate ( drop all partitions )
875
     * @deprecated
876
     * @param string $tableName
877
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::truncateTable() does not specify type hint for items of its traversable return value.
Loading history...
878
     * @throws Exception\TransportException
879
     * @throws \Exception
880
     */
881
    public function truncateTable($tableName)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::truncateTable() does not have parameter type hint for its parameter $tableName but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::truncateTable() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
882
    {
883
        $partions = $this->partitions($tableName);
884
        $out = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
885
        foreach ($partions as $part_key=>$part)
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
886
        {
887
            $part_id = $part['partition'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
888
            $out[$part_id] = $this->dropPartition($tableName, $part_id);
0 ignored issues
show
Deprecated Code introduced by
The function ClickHouseDB\Client::dropPartition() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

888
            $out[$part_id] = /** @scrutinizer ignore-deprecated */ $this->dropPartition($tableName, $part_id);
Loading history...
889
        }
890
        return $out;
891
    }
892
893
    /**
894
     * Returns the server's uptime in seconds.
895
     *
896
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::getServerUptime() does not specify type hint for items of its traversable return value.
Loading history...
897
     * @throws Exception\TransportException
898
     */
899 1
    public function getServerUptime()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getServerUptime() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
900
    {
901 1
        return $this->select('SELECT uptime() as uptime')->fetchOne('uptime');
902
    }
903
904
    /**
905
     * Returns string with the server version.
906
     *
907
     * @throws Exception\TransportException
908
     */
909 1
    public function getServerVersion() : string
910
    {
911 1
        return (string) $this->select('SELECT version() as version')->fetchOne('version');
912
    }
913
914
915
    /**
916
     * Read system.settings table
917
     *
918
     * @param string $like
919
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::getServerSystemSettings() does not specify type hint for items of its traversable return value.
Loading history...
920
     * @throws Exception\TransportException
921
     */
922 1
    public function getServerSystemSettings($like='')
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getServerSystemSettings() does not have parameter type hint for its parameter $like but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::getServerSystemSettings() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$like" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$like"; expected 1 but found 0
Loading history...
923
    {
924 1
        $l=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
925 1
        $list=$this->select('SELECT * FROM system.settings'.($like ? ' WHERE name LIKE :like':'' ),['like'=>'%'.$like.'%'])->rows();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
Coding Style introduced by
Expected 1 space between "'like'" and double arrow; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between double arrow and "'%'"; 0 found
Loading history...
926 1
        foreach ($list as $row) {
927 1
            if (isset($row['name'])) {$n=$row['name']; unset($row['name']) ; $l[$n]=$row;}
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
928
        }
929 1
        return $l;
930
    }
931
932
933
934
    /**
935
     * dropOldPartitions by day_ago
936
     * @deprecated
937
     *
938
     * @param string $table_name
939
     * @param int $days_ago
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
940
     * @param int $count_partitons_per_one
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
941
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::dropOldPartitions() does not specify type hint for items of its traversable return value.
Loading history...
942
     * @throws Exception\TransportException
943
     * @throws \Exception
944
     */
945
    public function dropOldPartitions($table_name, $days_ago, $count_partitons_per_one = 100)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have parameter type hint for its parameter $days_ago but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have parameter type hint for its parameter $count_partitons_per_one but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
946
    {
947
        $days_ago = strtotime(date('Y-m-d 00:00:00', strtotime('-' . $days_ago . ' day')));
0 ignored issues
show
introduced by
Function strtotime() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function date() should not be referenced via a fallback global name, but via a use statement.
Loading history...
948
949
        $drop = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
950
        $list_patitions = $this->partitions($table_name, $count_partitons_per_one);
951
952
        foreach ($list_patitions as $partion_id => $partition) {
953
            if (stripos($partition['engine'], 'mergetree') === false) {
0 ignored issues
show
introduced by
Function stripos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
954
                continue;
955
            }
956
957
            // $min_date = strtotime($partition['min_date']);
958
            $max_date = strtotime($partition['max_date']);
0 ignored issues
show
introduced by
Function strtotime() should not be referenced via a fallback global name, but via a use statement.
Loading history...
959
960
            if ($max_date < $days_ago) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
961
                $drop[] = $partition['partition'];
962
            }
963
        }
964
965
        $result = [];
966
        foreach ($drop as $partition_id) {
967
            $result[$partition_id] = $this->dropPartition($table_name, $partition_id);
0 ignored issues
show
Deprecated Code introduced by
The function ClickHouseDB\Client::dropPartition() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

967
            $result[$partition_id] = /** @scrutinizer ignore-deprecated */ $this->dropPartition($table_name, $partition_id);
Loading history...
968
        }
969
970
        return $result;
971
    }
972
}
973