Passed
Pull Request — master (#90)
by Šimon
03:11
created

Client::enableExtremes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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
use function sprintf;
13
14
class Client
15
{
16
    /**
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...
17
     * @var Http
18
     */
19
    private $_transport = null;
20
21
    /**
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...
22
     * @var string
23
     */
24
    private $_connect_username = '';
25
26
    /**
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...
27
     * @var string
28
     */
29
    private $_connect_password = '';
30
31
    /**
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...
32
     * @var string
33
     */
34
    private $_connect_host = '';
35
36
    /**
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...
37
     * @var string
38
     */
39
    private $_connect_port = '';
40
41
    /**
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...
42
     * @var bool
43
     */
44
    private $_connect_user_readonly = false;
45
    /**
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...
46
     * @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...
47
     */
48
    private $_support_format = ['TabSeparated', 'TabSeparatedWithNames', 'CSV', 'CSVWithNames', 'JSONEachRow'];
49
50
    /**
51
     * Client constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Client constructor.".
Loading history...
52
     * @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...
53
     * @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...
54
     */
55 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...
56
    {
57 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...
58
            throw  new \InvalidArgumentException('not set username');
59
        }
60
61 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...
62
            throw  new \InvalidArgumentException('not set password');
63
        }
64
65 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...
66
            throw  new \InvalidArgumentException('not set port');
67
        }
68
69 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...
70
            throw  new \InvalidArgumentException('not set host');
71
        }
72
73 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...
74 2
            if (empty($settings)) {
75 2
                $settings = $connect_params['settings'];
76
            }
77
        }
78
79 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...
80 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...
81 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...
82 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...
83
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
84
85
        // init transport class
86 58
        $this->_transport = new Http(
87 58
            $this->_connect_host,
88 58
            $this->_connect_port,
89 58
            $this->_connect_username,
90 58
            $this->_connect_password
91
        );
92
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
93
94 58
        $this->_transport->addQueryDegeneration(new Bindings());
95
96
        // apply settings to transport class
97 58
        $this->settings()->database('default');
98 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...
99 2
            $this->settings()->apply($settings);
100
        }
101
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
102
103 58
        if (isset($connect_params['readonly']))
104
        {
105
            $this->setReadOnlyUser($connect_params['readonly']);
106
        }
107
108 58
        if (isset($connect_params['https']))
109
        {
110
            $this->https($connect_params['https']);
111
        }
112
113 58
        $this->enableHttpCompression();
114
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
115
116 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...
117
118
    /**
119
     * if the user has only read in the config file
120
     *
121
     * @param bool $flag
122
     */
123
    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...
124
    {
125
        $this->_connect_user_readonly = $flag;
126
        $this->settings()->setReadOnlyUser($this->_connect_user_readonly);
127
    }
128
    /**
129
     * Clear Degeneration processing request [template ]
130
     *
131
     * @return bool
132
     */
133 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...
134
    {
135 1
        return $this->_transport->cleanQueryDegeneration();
136
    }
137
138
    /**
139
     * add Degeneration processing
140
     *
141
     * @param Query\Degeneration $degeneration
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::addQueryDegeneration() has useless @param annotation for parameter $degeneration.
Loading history...
142
     * @return bool
143
     */
144
    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...
145
    {
146
        return $this->_transport->addQueryDegeneration($degeneration);
147
    }
148
149
    /**
150
     * add Conditions in query
151
     *
152
     * @return bool
153
     */
154 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...
155
    {
156 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...
157
    }
158
    /**
159
     * Set connection host
160
     *
161
     * @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...
162
     */
163
    public function setHost($host)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setHost() does not have void return type hint.
Loading history...
164
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
165
166
        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...
167
        {
168
            $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...
169
        }
170
171
        $this->_connect_host = $host;
172
        $this->transport()->setHost($host);
173
    }
174
175
    /**
176
     * Таймаут
177
     *
178
     * @param int $timeout
179
     * @return Settings
180
     */
181 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...
182
    {
183 2
        return $this->settings()->max_execution_time($timeout);
184
    }
185
186
    /**
187
     * Timeout
188
     *
189
     * @return mixed
190
     */
191 1
    public function getTimeout()
192
    {
193 1
        return $this->settings()->getTimeOut();
194
    }
195
196
    /**
197
     * ConnectTimeOut in seconds ( support 1.5 = 1500ms )
198
     *
199
     * @param int $connectTimeOut
200
     */
201 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...
202
    {
203 2
        $this->transport()->setConnectTimeOut($connectTimeOut);
204 2
    }
205
206
    /**
207
     * get ConnectTimeOut
208
     *
209
     * @return int
210
     */
211 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...
212
    {
213 1
        return $this->transport()->getConnectTimeOut();
214
    }
215
216
217
    /**
218
     * transport
219
     *
220
     * @return Http
221
     */
222 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...
223
    {
224 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...
225
            throw  new \InvalidArgumentException('Empty transport class');
226
        }
227 58
        return $this->_transport;
228
    }
229
230
    /**
231
     * @return string
232
     */
233
    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...
234
    {
235
        return $this->_connect_host;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    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...
242
    {
243
        return $this->_connect_password;
244
    }
245
246
    /**
247
     * @return string
248
     */
249
    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...
250
    {
251
        return $this->_connect_port;
252
    }
253
254
    /**
255
     * @return string
256
     */
257
    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...
258
    {
259
        return $this->_connect_username;
260
    }
261
262
    /**
263
     * transport
264
     *
265
     * @return Http
266
     */
267
    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...
268
    {
269
        return $this->_transport;
270
    }
271
272
273
    /**
274
     * Режим отладки CURL
275
     *
276
     * @return mixed
277
     */
278
    public function verbose()
279
    {
280
        return $this->transport()->verbose(true);
281
    }
282
283
    /**
284
     * @return Settings
285
     */
286 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...
287
    {
288 58
        return $this->transport()->settings();
289
    }
290
291
    /**
292
     * @return $this
293
     */
294 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...
295
    {
296 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...
297
        {
298 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...
299
            {
300 2
                $this->settings()->makeSessionId();
301
            } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
302
            {
303
                $this->settings()->session_id($useSessionId);
304
            }
305
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
306
        }
307 2
        return $this;
308
    }
309
    /**
310
     * @return mixed
311
     */
312 2
    public function getSession()
313
    {
314 2
        return $this->settings()->getSessionId();
315
    }
316
317
    /**
318
     * Query CREATE/DROP
319
     *
320
     * @param string $sql
321
     * @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...
322
     * @param bool $exception
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
323
     * @return Statement
324
     * @throws Exception\TransportException
325
     */
326 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...
327
    {
328 22
        return $this->transport()->write($sql, $bindings, $exception);
329
    }
330
331
    /**
332
     * set db name
333
     * @param string $db
334
     * @return $this
335
     */
336 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...
337
    {
338 58
        $this->settings()->database($db);
339 58
        return $this;
340
    }
341
342
    /**
343
     * Write to system.query_log
344
     *
345
     * @param bool $flag
346
     * @return $this
347
     */
348
    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...
349
    {
350
        $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...
351
        return $this;
352
    }
353
354
    /**
355
     * Compress the result if the HTTP client said that it understands data compressed with gzip or deflate
356
     *
357
     * @param bool $flag
358
     * @return $this
359
     */
360 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...
361
    {
362 58
        $this->settings()->enableHttpCompression($flag);
363 58
        return $this;
364
    }
365
366
    /**
367
     * Enable / Disable HTTPS
368
     *
369
     * @param bool $flag
370
     * @return $this
371
     */
372 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...
373
    {
374 1
        $this->settings()->https($flag);
375 1
        return $this;
376
    }
377
378
    /**
379
     * Read extremes of the result columns. They can be output in JSON-formats.
380
     *
381
     * @param bool $flag
382
     * @return $this
383
     */
384 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...
385
    {
386 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...
387 2
        return $this;
388
    }
389
390
    /**
391
     * SELECT
392
     *
393
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
394
     * @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...
395
     * @param null|WhereInFile $whereInFile
396
     * @param null|WriteToFile $writeToFile
397
     * @return Statement
398
     * @throws Exception\TransportException
399
     * @throws \Exception
400
     */
401 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...
402
    {
403 25
        return $this->transport()->select($sql, $bindings, $whereInFile, $writeToFile);
404
    }
405
406
    /**
407
     * execute run
408
     *
409
     * @return bool
410
     * @throws Exception\TransportException
411
     */
412 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...
413
    {
414 10
        return $this->transport()->executeAsync();
415
    }
416
417
    /**
418
     * set progressFunction
419
     *
420
     * @param callable $callback
421
     */
422 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...
423
    {
424 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...
425
            throw new \InvalidArgumentException('Not is_callable progressFunction');
426
        }
427
428 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...
429
        {
430 1
            $this->settings()->set('send_progress_in_http_headers', 1);
431
        }
432 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...
433
        {
434 1
            $this->settings()->set('http_headers_progress_interval_ms', 100);
435
        }
436
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
437
438 1
        $this->transport()->setProgressFunction($callback);
439 1
    }
440
441
    /**
442
     * prepare select
443
     *
444
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
445
     * @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...
446
     * @param null|WhereInFile $whereInFile
447
     * @param null|WriteToFile $writeToFile
448
     * @return Statement
449
     * @throws Exception\TransportException
450
     * @throws \Exception
451
     */
452 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...
453
    {
454 5
        return $this->transport()->selectAsync($sql, $bindings, $whereInFile, $writeToFile);
455
    }
456
457
    /**
458
     * SHOW PROCESSLIST
459
     *
460
     * @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...
461
     * @throws Exception\TransportException
462
     * @throws \Exception
463
     */
464
    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...
465
    {
466
        return $this->select('SHOW PROCESSLIST')->rows();
467
    }
468
469
    /**
470
     * show databases
471
     *
472
     * @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...
473
     * @throws Exception\TransportException
474
     * @throws \Exception
475
     */
476
    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...
477
    {
478
        return $this->select('show databases')->rows();
479
    }
480
481
    /**
482
     * statement = SHOW CREATE TABLE
483
     *
484
     * @param string $table
485
     * @return mixed
486
     * @throws Exception\TransportException
487
     * @throws \Exception
488
     */
489
    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...
490
    {
491
        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...
492
    }
493
494
    /**
495
     * SHOW TABLES
496
     *
497
     * @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...
498
     * @throws Exception\TransportException
499
     * @throws \Exception
500
     */
501 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...
502
    {
503 1
        return $this->select('SHOW TABLES')->rowsAsTree('name');
504
    }
505
506
    /**
507
     * Get the number of simultaneous/Pending requests
508
     *
509
     * @return int
510
     */
511 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...
512
    {
513 12
        return $this->transport()->getCountPendingQueue();
514
    }
515
516
    /**
517
     * Insert Array
518
     *
519
     * @param string $table
520
     * @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...
521
     * @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...
522
     * @return Statement
523
     * @throws Exception\TransportException
524
     */
525 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...
526
    {
527 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...
528
529 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...
530 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...
531
        }
532
533 5
        $sql .= ' VALUES ';
534
535 5
        foreach ($values as $row) {
536 5
            $sql .= ' (' . FormatLine::Insert($row) . '), ';
537
        }
538 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...
539 5
        return $this->transport()->write($sql);
540
    }
541
542
    /**
543
      * Prepares the values to insert from the associative array.
544
      * There may be one or more lines inserted, but then the keys inside the array list must match (including in the sequence)
545
      *
546
      * @param array $values - array column_name => value (if we insert one row) or array list column_name => value if we insert many lines
547
      * @return array - list of arrays - 0 => fields, 1 => list of value arrays for insertion
548
      */
549 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...
550
    {
551 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...
552 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...
553 2
            $preparedValues = [];
554 2
            foreach ($values as $idx => $row) {
555 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...
556 2
                if ($_fields !== $preparedFields) {
557 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...
558
                }
559 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...
560
            }
561
        } else { //одна строка
562 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...
563 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...
564
        }
565 2
        return [$preparedFields, $preparedValues];
566
    }
567
568
    /**
569
     * Inserts one or more rows from an associative array.
570
     * If there is a discrepancy between the keys of the value arrays (or their order) - throws an exception.
571
     *
572
     * @param string $table - table name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
573
     * @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...
574
     * @return Statement
575
     * @throws QueryException
576
     * @throws Exception\TransportException
577
     */
578
    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...
579
    {
580
        list($columns, $vals) = $this->prepareInsertAssocBulk($values);
581
        return $this->insert($table, $vals, $columns);
582
    }
583
584
    /**
585
     * insert TabSeparated files
586
     *
587
     * @param string $table_name
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
588
     * @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...
589
     * @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...
590
     * @return mixed
591
     * @throws Exception\TransportException
592
     */
593 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...
594
    {
595 1
        return $this->insertBatchFiles($table_name, $file_names, $columns_array, 'TabSeparated');
596
    }
597
598
    /**
599
     * insert Batch Files
600
     *
601
     * @param string $table_name
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
602
     * @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...
603
     * @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...
604
     * @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...
605
     * @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...
606
     * @throws Exception\TransportException
607
     */
608 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...
609
    {
610 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...
611
        {
612
            $file_names = [$file_names];
613
        }
614 8
        if ($this->getCountPendingQueue() > 0) {
615
            throw new QueryException('Queue must be empty, before insertBatch, need executeAsync');
616
        }
617
618 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...
619
        {
620
            throw new QueryException('Format not support in insertBatchFiles');
621
        }
622
623 8
        $result = [];
624
625 8
        foreach ($file_names as $fileName) {
626 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...
627
                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...
628
            }
629
630 8
            if (empty($columns_array))
631
            {
632
                $sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format;
633
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
634
            } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
635
            {
636 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...
637
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
638
            }
639 8
            $result[$fileName] = $this->transport()->writeAsyncCSV($sql, $fileName);
640
        }
641
642
        // exec
643 8
        $this->executeAsync();
644
645
        // fetch resutl
646 8
        foreach ($file_names as $fileName) {
647 8
            if ($result[$fileName]->isError()) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
648 8
                $result[$fileName]->error();
649
            }
650
        }
651
652 6
        return $result;
653
    }
654
655
    /**
656
     * insert Batch Stream
657
     *
658
     * @param string $table_name
659
     * @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...
660
     * @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...
661
     * @return Transport\CurlerRequest
662
     */
663 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...
664
    {
665 2
        if ($this->getCountPendingQueue() > 0) {
666
            throw new QueryException('Queue must be empty, before insertBatch, need executeAsync');
667
        }
668
669 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...
670
        {
671
            throw new QueryException('Format not support in insertBatchFiles');
672
        }
673
674 2
        if (empty($columns_array))
675
        {
676
            $sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format;
677
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
678
        } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
679
        {
680 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...
681
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
682
        }
683
684 2
        return $this->transport()->writeStreamData($sql);
685
    }
686
687
688
    /**
689
     * stream Write
690
     *
691
     * @param Stream $stream
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamWrite() has useless @param annotation for parameter $stream.
Loading history...
692
     * @param string $sql
693
     * @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...
694
     * @return Statement
695
     * @throws Exception\TransportException
696
     */
697 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...
698
    {
699 1
        if ($this->getCountPendingQueue() > 0) {
700
            throw new QueryException('Queue must be empty, before streamWrite');
701
        }
702 1
        return $this->transport()->streamWrite($stream,$sql,$bind);
703
    }
704
705
706
    /**
707
     * stream Read
708
     *
709
     * @param Stream $streamRead
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamRead() has useless @param annotation for parameter $streamRead.
Loading history...
710
     * @param string $sql
711
     * @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...
712
     * @return Statement
713
     * @throws Exception\TransportException
714
     */
715 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...
716
    {
717 1
        if ($this->getCountPendingQueue() > 0) {
718
            throw new QueryException('Queue must be empty, before streamWrite');
719
        }
720 1
        return $this->transport()->streamRead($streamRead,$sql,$bind);
721
    }
722
723
    /**
724
     * Size of database
725
     *
726
     * @return mixed|null
727
     * @throws Exception\TransportException
728
     * @throws \Exception
729
     */
730
    public function databaseSize()
731
    {
732
        $b = $this->settings()->getDatabase();
733
734
        return $this->select('
735
            SELECT database,formatReadableSize(sum(bytes)) as size
736
            FROM system.parts
737
            WHERE active AND database=:database
738
            GROUP BY database
739
        ', ['database' => $b])->fetchOne();
740
    }
741
742
    /**
743
     * Size of tables
744
     *
745
     * @param string $tableName
746
     * @return mixed
747
     * @throws Exception\TransportException
748
     * @throws \Exception
749
     */
750 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...
751
    {
752 1
        $tables = $this->tablesSize();
753
754 1
        if (isset($tables[$tableName])) {
755 1
            return $tables[$tableName];
756
        }
757
758
        return null;
759
    }
760
761
    /**
762
     * Ping server
763
     *
764
     * @return bool
765
     * @throws Exception\TransportException
766
     */
767 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...
768
    {
769 36
        return $this->transport()->ping();
770
    }
771
772
    /**
773
     * Tables sizes
774
     *
775
     * @param bool $flatList
776
     * @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...
777
     * @throws Exception\TransportException
778
     * @throws \Exception
779
     */
780 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...
781
    {
782 1
        $z = $this->select('
783
        SELECT name as table,database,
784
            max(sizebytes) as sizebytes,
785
            max(size) as size,
786
            min(min_date) as min_date,
787
            max(max_date) as max_date
788
            FROM system.tables
789
            ANY LEFT JOIN 
790
            (
791
            SELECT table,database,
792
                        formatReadableSize(sum(bytes)) as size,
793
                        sum(bytes) as sizebytes,
794
                        min(min_date) as min_date,
795
                        max(max_date) as max_date
796
                        FROM system.parts 
797
                        WHERE active AND database=:database
798
                        GROUP BY table,database
799
            ) USING ( table,database )
800
            WHERE database=:database
801
            GROUP BY table,database
802 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...
803
804 1
        if ($flatList) {
805
            return $z->rows();
806
        }
807
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
808
809 1
        return $z->rowsAsTree('table');
810
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
811
812
    }
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...
813
814
815
    /**
816
     * isExists
817
     *
818
     * @param string $database
819
     * @param string $table
820
     * @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...
821
     * @throws Exception\TransportException
822
     * @throws \Exception
823
     */
824
    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...
825
    {
826
        return $this->select('
827
            SELECT *
828
            FROM system.tables 
829
            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...
830
        )->rowsAsTree('name');
831
    }
832
833
834
    /**
835
     * List of partitions
836
     *
837
     * @return mixed[][]
838
     */
839
    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...
840
    {
841
        $database          = $this->settings()->getDatabase();
842
        $whereActiveClause = $active === null ? '' : sprintf(' AND active = %s', (int) $active);
843
        $limitClause       = $limit !== null ? ' LIMIT ' . $limit : '';
844
845
        return $this->select(<<<CLICKHOUSE
846
SELECT *
847
FROM system.parts 
848
WHERE like(table,'%$table%') AND database='$database'$whereActiveClause
849
ORDER BY max_date $limitClause
850
CLICKHOUSE
851
        )->rowsAsTree('name');
852
    }
853
854
    /**
855
     * dropPartition
856
     * @deprecated
857
     * @param string $dataBaseTableName database_name.table_name
858
     * @param string $partition_id
859
     * @return Statement
860
     * @throws Exception\TransportException
861
     */
862
    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...
863
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
864
865
        $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...
866
        $this->settings()->set('replication_alter_partitions_sync', 2);
867
        $state = $this->write('ALTER TABLE {dataBaseTableName} DROP PARTITION :partion_id', [
868
            'dataBaseTableName'  => $dataBaseTableName,
869
            'partion_id' => $partition_id
0 ignored issues
show
introduced by
Multiline arrays must have a trailing comma after the last element.
Loading history...
870
        ]);
871
        return $state;
872
    }
873
874
    /**
875
     * Truncate ( drop all partitions )
876
     * @deprecated
877
     * @param string $tableName
878
     * @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...
879
     * @throws Exception\TransportException
880
     * @throws \Exception
881
     */
882
    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...
883
    {
884
        $partions = $this->partitions($tableName);
885
        $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...
886
        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...
887
        {
888
            $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...
889
            $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

889
            $out[$part_id] = /** @scrutinizer ignore-deprecated */ $this->dropPartition($tableName, $part_id);
Loading history...
890
        }
891
        return $out;
892
    }
893
894
    /**
895
     * Returns the server's uptime in seconds.
896
     *
897
     * @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...
898
     * @throws Exception\TransportException
899
     */
900 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...
901
    {
902 1
        return $this->select('SELECT uptime() as uptime')->fetchOne('uptime');
903
    }
904
905
    /**
906
     * Returns string with the server version.
907
     *
908
     * @throws Exception\TransportException
909
     */
910 1
    public function getServerVersion() : string
911
    {
912 1
        return (string) $this->select('SELECT version() as version')->fetchOne('version');
913
    }
914
915
916
    /**
917
     * Read system.settings table
918
     *
919
     * @param string $like
920
     * @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...
921
     * @throws Exception\TransportException
922
     */
923 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...
924
    {
925 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...
926 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...
927 1
        foreach ($list as $row) {
928 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...
929
        }
930 1
        return $l;
931
    }
932
933
934
935
    /**
936
     * dropOldPartitions by day_ago
937
     * @deprecated
938
     *
939
     * @param string $table_name
940
     * @param int $days_ago
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
941
     * @param int $count_partitons_per_one
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
942
     * @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...
943
     * @throws Exception\TransportException
944
     * @throws \Exception
945
     */
946
    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...
947
    {
948
        $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...
949
950
        $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...
951
        $list_patitions = $this->partitions($table_name, $count_partitons_per_one);
952
953
        foreach ($list_patitions as $partion_id => $partition) {
954
            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...
955
                continue;
956
            }
957
958
            // $min_date = strtotime($partition['min_date']);
959
            $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...
960
961
            if ($max_date < $days_ago) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
962
                $drop[] = $partition['partition'];
963
            }
964
        }
965
966
        $result = [];
967
        foreach ($drop as $partition_id) {
968
            $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

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