Failed Conditions
Pull Request — master (#102)
by Šimon
04:10
created

Http::prepareQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 9
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
introduced by
An error occurred during processing; checking has been aborted. The error message was: "if" without curly braces is not supported.
Loading history...
2
3
namespace ClickHouseDB\Transport;
4
5
use ClickHouseDB\Exception\TransportException;
6
use ClickHouseDB\Query\Degeneration;
7
use ClickHouseDB\Query\Query;
8
use ClickHouseDB\Query\WhereInFile;
9
use ClickHouseDB\Query\WriteToFile;
10
use ClickHouseDB\Settings;
11
use ClickHouseDB\Statement;
12
use function array_merge;
13
use function http_build_query;
14
use const PHP_EOL;
15
16
class Http
17
{
18
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$username with single line content, use one-line comment instead.
Loading history...
19
     * @var string
20
     */
21
    private $username;
22
23
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$password with single line content, use one-line comment instead.
Loading history...
24
     * @var string
25
     */
26
    private $password;
27
28
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$host with single line content, use one-line comment instead.
Loading history...
29
     * @var string
30
     */
31
    private $host;
32
33
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$port with single line content, use one-line comment instead.
Loading history...
34
     * @var int
35
     */
36
    private $port;
37
38
    /** @var bool */
39
    private $https;
40
41
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$verbose with single line content, use one-line comment instead.
Loading history...
42
     * @var bool|int
43
     */
44
    private $verbose = false;
45
46
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_curler with single line content, use one-line comment instead.
Loading history...
47
     * @var CurlerRolling
48
     */
49
    private $_curler = null;
50
51
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$settings with single line content, use one-line comment instead.
Loading history...
52
     * @var Settings
53
     */
54
    private $settings;
55
56
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_query_degenerations with single line content, use one-line comment instead.
Loading history...
57
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Transport\Http::$_query_degenerations does not specify type hint for its items.
Loading history...
58
     */
59
    private $_query_degenerations = [];
60
61
    /**
62
     * Count seconds (float)
63
     *
64
     * @var float
65
     */
66
    private $timeout = 20;
67
68
    /**
69
     * Count seconds (float)
70
     *
71
     * @var float
72
     */
73
    private $connectTimeout = 5;
74
75
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$xClickHouseProgress with single line content, use one-line comment instead.
Loading history...
76
     * @var callable|null
77
     */
78
    private $xClickHouseProgress;
79
80
    /** @var string */
81
    private $database;
82
83 63
    public function __construct(
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function __construct()
Loading history...
84
        string $host,
85
        int $port,
86
        string $username,
87
        string $password,
88
        string $database,
89
        bool $https = false
90
    ) {
91 63
        $this->host     = $host;
92 63
        $this->port     = $port;
93 63
        $this->username = $username;
94 63
        $this->password = $password;
95 63
        $this->setDatabase($database);
96 63
        $this->setHttps($https);
97 63
        $this->settings = new Settings();
98
99 63
        $this->setCurler();
100 63
    }
101
102 63
    public function setHttps(bool $https) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setHttps()
Loading history...
103
    {
104 63
        $this->https = $https;
105 63
    }
106
107 63
    public function setCurler()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setCurler() does not have void return type hint.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function setCurler()
Loading history...
108
    {
109 63
        $this->_curler = new CurlerRolling();
110 63
    }
111
112
    /**
113
     * @return CurlerRolling
114
     */
115
    public function getCurler()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCurler() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRolling".
Loading history...
116
    {
117
        return $this->_curler;
118
    }
119
120 63
    public function setDatabase(string $database) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setDatabase()
Loading history...
121
    {
122 63
        $this->database = $database;
123 63
    }
124
125
    public function setHost(string $host) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setHost()
Loading history...
126
    {
127
        $this->host = $host;
128
    }
129
130
    /**
131
     * @return string
132
     */
133 63
    public function getUri()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUri() 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...
134
    {
135 63
        $proto = 'http';
136 63
        if ($this->https) {
137 1
            $proto = 'https';
138
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
139 63
        $uri = $proto . '://' . $this->host;
140 63
        if (stripos($this->host, '/') !== false || stripos($this->host, ':') !== 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...
141 1
            return $uri;
142
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
143 63
        if ($this->port > 0) {
144 63
            return $uri . ':' . $this->port;
145
        }
146
147 1
        return $uri;
148
    }
149
150 63
    public function getSettings() : Settings
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function getSettings()
Loading history...
151
    {
152 63
        return $this->settings;
153
    }
154
155
    public function setVerbose(bool $flag) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setVerbose()
Loading history...
156
    {
157
        $this->verbose = $flag;
158
    }
159
160
    /**
161
     * @param mixed[] $params
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
162
     * @return string
163
     */
164 63
    private function getUrl(array $params = [])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUrl() 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...
165
    {
166 63
        $settings = $this->getSettings()->getQueryableSettings($params);
167
168 63
        return $this->getUri() . '?' . http_build_query(array_merge(['database' => $this->database], $settings));
169
    }
170
171
    /**
172
     * @param array $extendinfo
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@param annotation of method \ClickHouseDB\Transport\Http::newRequest() does not specify type hint for items of its traversable parameter $extendinfo.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
173
     * @return CurlerRequest
174
     */
175 63
    private function newRequest($extendinfo)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::newRequest() does not have parameter type hint for its parameter $extendinfo but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::newRequest() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Type hint "array" missing for $extendinfo
Loading history...
176
    {
177 63
        $new = new CurlerRequest();
178 63
        $new->auth($this->username, $this->password)
179 63
            ->POST()
180 63
            ->setRequestExtendedInfo($extendinfo);
181
182 63
        if ($this->getSettings()->isHttpCompressionEnabled()) {
183 63
            $new->httpCompression(true);
184
        }
185
186 63
        if ($this->getSettings()->getSessionId()) {
187 1
            $new->persistent();
188
        }
189
190 63
        $new->setTimeout($this->timeout);
191 63
        $new->setConnectTimeout($this->connectTimeout); // one sec
192 63
        $new->keepAlive(); // one sec
193 63
        $new->verbose((bool) $this->verbose);
194
195 63
        return $new;
196
    }
197
198
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$query" missing
Loading history...
199
     * @param array $urlParams
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@param annotation of method \ClickHouseDB\Transport\Http::makeRequest() does not specify type hint for items of its traversable parameter $urlParams.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $urlParams does not match actual variable name $query
Loading history...
200
     * @param bool  $query_as_string
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
Coding Style introduced by
Doc comment for parameter $query_as_string does not match actual variable name $urlParams
Loading history...
201
     * @return CurlerRequest
202
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
203
     */
204 63
    private function makeRequest(Query $query, array $urlParams = [], $query_as_string = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() does not have parameter type hint for its parameter $query_as_string but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Expected type hint "array"; found "Query" for $urlParams
Loading history...
Coding Style introduced by
Expected type hint "bool"; found "array" for $query_as_string
Loading history...
205
    {
206 63
        $sql = $query->toSql();
207
208 63
        if ($query_as_string) {
209 1
            $urlParams['query'] = $sql;
210
        }
211
212 63
        $url = $this->getUrl($urlParams);
213
214
        $extendinfo = [
215 63
            'sql'    => $sql,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
216 63
            'query'  => $query,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
217 63
            'format' => $query->getFormat(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
218
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 8.
Loading history...
219
220 63
        $new = $this->newRequest($extendinfo);
221 63
        $new->url($url);
222
223 63
        if (! $query_as_string) {
224 63
            $new->parameters_json($sql);
225
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
226 63
        if ($this->getSettings()->isHttpCompressionEnabled()) {
227 63
            $new->httpCompression(true);
228
        }
229
230 63
        return $new;
231
    }
232
233
    /**
234
     * @param string|Query $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
235
     * @return CurlerRequest
236
     */
237 3
    public function writeStreamData($sql)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeStreamData() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
238
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
239
240 3
        if ($sql instanceof Query) {
241 1
            $query = $sql;
242
        } else {
243 2
            $query = new Query($sql);
244
        }
245
246 3
        $url        = $this->getUrl([
247 3
            'query' => $query->toSql(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 37 spaces, but found 12.
Loading history...
248
        ]);
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 36 space(s), but found 8.
Loading history...
249
        $extendinfo = [
250 3
            'sql'    => $sql,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
251 3
            'query'  => $query,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
252 3
            'format' => $query->getFormat(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
253
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 8.
Loading history...
254
255 3
        $request = $this->newRequest($extendinfo);
256 3
        $request->url($url);
257
258 3
        return $request;
259
    }
260
261
    /**
262
     * @param string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
263
     * @param string $file_name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
264
     * @return Statement
265
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
266
     */
267 8
    public function writeAsyncCSV($sql, $file_name)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeAsyncCSV() 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\Transport\Http::writeAsyncCSV() does not have parameter type hint for its parameter $file_name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::writeAsyncCSV() 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
Type hint "string" missing for $sql
Loading history...
Coding Style introduced by
Type hint "string" missing for $file_name
Loading history...
268
    {
269 8
        $query = new Query($sql);
270
271 8
        $url = $this->getUrl([
272 8
            'query' => $query->toSql(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 30 spaces, but found 12.
Loading history...
273
        ]);
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 29 space(s), but found 8.
Loading history...
274
275
        $extendinfo = [
276 8
            'sql'    => $sql,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
277 8
            'query'  => $query,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
278 8
            'format' => $query->getFormat(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
279
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 8.
Loading history...
280
281 8
        $request = $this->newRequest($extendinfo);
282 8
        $request->url($url);
283
284
        $request->setCallbackFunction(function (CurlerRequest $request) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
introduced by
Closure does not have void return type hint.
Loading history...
285 8
            $handle = $request->getInfileHandle();
286 8
            if (is_resource($handle)) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
introduced by
Function is_resource() should not be referenced via a fallback global name, but via a use statement.
Loading history...
287 8
                fclose($handle);
0 ignored issues
show
introduced by
Function fclose() should not be referenced via a fallback global name, but via a use statement.
Loading history...
288
            }
289 8
        });
290
291 8
        $request->setInfile($file_name);
292 8
        $this->_curler->addQueLoop($request);
293
294 8
        return new Statement($request);
295
    }
296
297
    /**
298
     * get Count Pending Query in Queue
299
     *
300
     * @return int
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
301
     */
302 12
    public function getCountPendingQueue()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::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...
303
    {
304 12
        return $this->_curler->countPending();
305
    }
306
307 1
    public function getTimeout() : float
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function getTimeout()
Loading history...
308
    {
309 1
        return $this->timeout;
310
    }
311
312 2
    public function setTimeout(float $timeout) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setTimeout()
Loading history...
313
    {
314 2
        $this->timeout = $timeout;
315 2
    }
316
317
    /**
318
     * Get ConnectTimeout in seconds
319
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
320 39
    public function getConnectTimeout() : float
321
    {
322 39
        return $this->connectTimeout;
323
    }
324
325
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$connectTimeOut" missing
Loading history...
326
     * Set Connect Timeout in seconds
327
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
328 2
    public function setConnectTimeout(float $connectTimeOut) : void
329
    {
330 2
        $this->connectTimeout = $connectTimeOut;
331 2
    }
332
333 1
    private function findXClickHouseProgress($handle)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::findXClickHouseProgress() does not have parameter type hint nor @param annotation for its parameter $handle.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::findXClickHouseProgress() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function findXClickHouseProgress()
Loading history...
334
    {
335 1
        $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
0 ignored issues
show
introduced by
Function curl_getinfo() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Constant CURLINFO_HTTP_CODE should not be referenced via a fallback global name, but via a use statement.
Loading history...
336
337
        // Search X-ClickHouse-Progress
338 1
        if ($code == 200) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
339 1
            $response    = curl_multi_getcontent($handle);
0 ignored issues
show
introduced by
Function curl_multi_getcontent() should not be referenced via a fallback global name, but via a use statement.
Loading history...
340 1
            $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
0 ignored issues
show
introduced by
Function curl_getinfo() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Constant CURLINFO_HEADER_SIZE should not be referenced via a fallback global name, but via a use statement.
Loading history...
341 1
            if (! $header_size) {
342
                return false;
343
            }
344
345 1
            $header = substr($response, 0, $header_size);
0 ignored issues
show
introduced by
Function substr() should not be referenced via a fallback global name, but via a use statement.
Loading history...
346 1
            if (! $header_size) {
347
                return false;
348
            }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
349 1
            $pos = strrpos($header, 'X-ClickHouse-Progress');
0 ignored issues
show
introduced by
Function strrpos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
350
351 1
            if (! $pos) {
352
                return false;
353
            }
354
355 1
            $last = substr($header, $pos);
0 ignored issues
show
introduced by
Function substr() should not be referenced via a fallback global name, but via a use statement.
Loading history...
356 1
            $data = @json_decode(str_ireplace('X-ClickHouse-Progress:', '', $last), true);
0 ignored issues
show
introduced by
Function json_decode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function str_ireplace() should not be referenced via a fallback global name, but via a use statement.
Loading history...
357
358 1
            if ($data && $this->xClickHouseProgress !== null) {
359 1
                ($this->xClickHouseProgress)($data);
360
            }
361
        }
362 1
    }
363
364
    /**
365
     * @param Query            $query
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() has useless @param annotation for parameter $query.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
366
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
367
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
368
     * @return CurlerRequest
369
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
370
     */
371 35
    public function getRequestRead(Query $query, $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() 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\Transport\Http::getRequestRead() 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\Transport\Http::getRequestRead() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
372
    {
373 35
        $query_as_string = false;
374 35
        $urlParams       = [];
375
        // ---------------------------------------------------------------------------------
376 35
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
377
            // $request = $this->prepareSelectWhereIn($request, $whereInFile);
378 1
            $structure       = $whereInFile->fetchUrlParams();
379 1
            $urlParams       = $structure;
380 1
            $query_as_string = true;
381
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
382
        // ---------------------------------------------------------------------------------
383
        // if result to file
384 35
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
385 1
            $query->setFormat($writeToFile->fetchFormat());
386 1
            unset($urlParams['extremes']);
387
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
388
        // ---------------------------------------------------------------------------------
389
        // makeRequest read
390 35
        $request = $this->makeRequest($query, $urlParams, $query_as_string);
391
        // ---------------------------------------------------------------------------------
392
        // attach files
393 35
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
394 1
            $request->attachFiles($whereInFile->fetchFiles());
395
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
396
        // ---------------------------------------------------------------------------------
397
        // result to file
398 35
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
399
400 1
            $fout = fopen($writeToFile->fetchFile(), 'w');
0 ignored issues
show
introduced by
Function fopen() should not be referenced via a fallback global name, but via a use statement.
Loading history...
401 1
            if (is_resource($fout)) {
0 ignored issues
show
introduced by
Function is_resource() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
402
403 1
                $isGz = $writeToFile->getGzip();
404
405 1
                if ($isGz) {
406
                    // write gzip header
407
                    // "\x1f\x8b\x08\x00\x00\x00\x00\x00"
408
                    // fwrite($fout, "\x1F\x8B\x08\x08".pack("V", time())."\0\xFF", 10);
409
                    // write the original file name
410
                    // $oname = str_replace("\0", "", basename($writeToFile->fetchFile()));
411
                    // fwrite($fout, $oname."\0", 1+strlen($oname));
412
413
                    fwrite($fout, "\x1f\x8b\x08\x00\x00\x00\x00\x00");
0 ignored issues
show
introduced by
Function fwrite() should not be referenced via a fallback global name, but via a use statement.
Loading history...
414
                }
415
416
                $request->setResultFileHandle($fout, $isGz)->setCallbackFunction(function (CurlerRequest $request) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
introduced by
Closure does not have void return type hint.
Loading history...
417
                    fclose($request->getResultFileHandle());
0 ignored issues
show
introduced by
Function fclose() should not be referenced via a fallback global name, but via a use statement.
Loading history...
418 1
                });
419
            }
420
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
421 35
        if ($this->xClickHouseProgress !== null) {
422
            $request->setFunctionProgress(function ($x) {
423 1
                return $this->findXClickHouseProgress($x);
424 1
            });
425
        }
426
427
        // ---------------------------------------------------------------------------------
428 35
        return $request;
429
    }
430
431 1
    public function cleanQueryDegeneration() : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function cleanQueryDegeneration()
Loading history...
432
    {
433 1
        $this->_query_degenerations = [];
434 1
    }
435
436 63
    public function addQueryDegeneration(Degeneration $degeneration) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function addQueryDegeneration()
Loading history...
437
    {
438 63
        $this->_query_degenerations[] = $degeneration;
439 63
    }
440
441
    /**
442
     * @param Query $query
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::getRequestWrite() has useless @param annotation for parameter $query.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
443
     * @return CurlerRequest
444
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
445
     */
446 63
    public function getRequestWrite(Query $query)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestWrite() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
447
    {
448 63
        return $this->makeRequest($query);
449
    }
450
451
    /**
452
     * @throws TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
453
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
454 39
    public function ping() : bool
455
    {
456 39
        $request = new CurlerRequest();
457 39
        $request->url($this->getUri())->verbose(false)->GET()->setConnectTimeout($this->getConnectTimeout());
458 39
        $this->_curler->execOne($request);
459
460 39
        return $request->response()->body() === 'Ok.' . PHP_EOL;
461
    }
462
463
    /**
464
     * @param string  $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
465
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
466
     * @return Query
467
     */
468 63
    private function prepareQuery($sql, $bindings)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareQuery() 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\Transport\Http::prepareQuery() does not have parameter type hint for its parameter $bindings but it should be possible to add it based on @param annotation "mixed[]".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::prepareQuery() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Query".
Loading history...
Coding Style introduced by
Type hint "string" missing for $sql
Loading history...
Coding Style introduced by
Type hint "array" missing for $bindings
Loading history...
469
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
470
471
        // add Degeneration query
472 63
        foreach ($this->_query_degenerations as $degeneration) {
473 63
            $degeneration->bindParams($bindings);
474
        }
475
476 63
        return new Query($sql, $this->_query_degenerations);
477
    }
478
479
    /**
480
     * @param Query|string     $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
481
     * @param mixed[]          $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
482
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
483
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
484
     * @return CurlerRequest
485
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
486
     */
487 34
    private function prepareSelect($sql, $bindings, $whereInFile, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareSelect() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Type hint "array" missing for $bindings
Loading history...
488
    {
489 34
        if ($sql instanceof Query) {
490
            return $this->getRequestWrite($sql);
491
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
492 34
        $query = $this->prepareQuery($sql, $bindings);
493 34
        $query->setFormat('JSON');
494
495 34
        return $this->getRequestRead($query, $whereInFile, $writeToFile);
496
    }
497
498
    /**
499
     * @param Query|string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
500
     * @param mixed[]      $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
501
     * @return CurlerRequest
502
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
503
     */
504 63
    private function prepareWrite($sql, $bindings = [])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareWrite() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Type hint "array" missing for $bindings
Loading history...
505
    {
506 63
        if ($sql instanceof Query) {
507
            return $this->getRequestWrite($sql);
508
        }
509
510 63
        $query = $this->prepareQuery($sql, $bindings);
511
512 63
        return $this->getRequestWrite($query);
513
    }
514
515
    /**
516
     * @return bool
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
517
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
518
     */
519 10
    public function executeAsync()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::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...
520
    {
521 10
        return $this->_curler->execLoopWait();
522
    }
523
524
    /**
525
     * @param Query|string     $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
526
     * @param mixed[]          $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
527
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
528
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
529
     * @return Statement
530
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
531
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
532
     */
533 29
    public function select($sql, array $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::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...
534
    {
535 29
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
536 29
        $this->_curler->execOne($request);
537
538 29
        return new Statement($request);
539
    }
540
541
    /**
542
     * @param Query|string     $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
543
     * @param mixed[]          $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
544
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
545
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
546
     * @return Statement
547
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
548
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
549
     */
550 5
    public function selectAsync($sql, array $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::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...
551
    {
552 5
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
553 5
        $this->_curler->addQueLoop($request);
554
555 5
        return new Statement($request);
556
    }
557
558 1
    public function setProgressFunction(callable $callback) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setProgressFunction()
Loading history...
559
    {
560 1
        $this->xClickHouseProgress = $callback;
561 1
    }
562
563
    /**
564
     * @param string  $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
565
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
566
     * @param bool    $exception
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
567
     * @return Statement
568
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
569
     */
570 63
    public function write($sql, array $bindings = [], $exception = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::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\Transport\Http::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\Transport\Http::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...
Coding Style introduced by
Type hint "string" missing for $sql
Loading history...
Coding Style introduced by
Type hint "bool" missing for $exception
Loading history...
571
    {
572 63
        $request = $this->prepareWrite($sql, $bindings);
573 63
        $this->_curler->execOne($request);
574 63
        $response = new Statement($request);
575 63
        if ($exception) {
576 63
            if ($response->isError()) {
577 3
                $response->error();
578
            }
579
        }
580
581 63
        return $response;
582
    }
583
584
    /**
585
     * @param Stream        $streamRW
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @param annotation for parameter $streamRW.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
586
     * @param CurlerRequest $request
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @param annotation for parameter $request.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
587
     * @return Statement
588
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
589
     */
590 2
    private function streaming(Stream $streamRW, CurlerRequest $request)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() 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...
591
    {
592 2
        $callable = $streamRW->getClosure();
593 2
        $stream   = $streamRW->getStream();
594
595
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
596
597 2
            if (! is_callable($callable)) {
0 ignored issues
show
introduced by
Function is_callable() should not be referenced via a fallback global name, but via a use statement.
Loading history...
598
                if ($streamRW->isWrite()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
599
600
                    $callable = function ($ch, $fd, $length) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
601
                        return ($line = fread($stream, $length)) ? $line : '';
0 ignored issues
show
introduced by
Useless parentheses.
Loading history...
introduced by
Function fread() should not be referenced via a fallback global name, but via a use statement.
Loading history...
602
                    };
603
                } else {
604
                    $callable = function ($ch, $fd) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
605
                        return fwrite($stream, $fd);
0 ignored issues
show
introduced by
Function fwrite() should not be referenced via a fallback global name, but via a use statement.
Loading history...
606
                    };
607
                }
608
            }
609
610 2
            if ($streamRW->isGzipHeader()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
611
612 1
                if ($streamRW->isWrite()) {
613 1
                    $request->header('Content-Encoding', 'gzip');
614 1
                    $request->header('Content-Type', 'application/x-www-form-urlencoded');
615
                } else {
616
                    $request->header('Accept-Encoding', 'gzip');
617
                }
618
            }
619
620 2
            $request->header('Transfer-Encoding', 'chunked');
621
622 2
            if ($streamRW->isWrite()) {
623 1
                $request->setReadFunction($callable);
624
            } else {
625 1
                $request->setWriteFunction($callable);
626
//                $request->setHeaderFunction($callableHead);
627
            }
628
629 2
            $this->_curler->execOne($request, true);
630 2
            $response = new Statement($request);
631 2
            if ($response->isError()) {
632
                $response->error();
633
            }
634
635 2
            return $response;
636
        } finally {
637 2
            if ($streamRW->isWrite())
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
638 2
                fclose($stream);
0 ignored issues
show
introduced by
Function fclose() should not be referenced via a fallback global name, but via a use statement.
Loading history...
639
        }
640
    }
641
642
    /**
643
     * @param Stream  $streamRead
644
     * @param string  $sql
645
     * @param mixed[] $bindings
646
     * @return Statement
647
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
648
     */
649 1
    public function streamRead(Stream $streamRead, $sql, $bindings = [])
650
    {
651 1
        $sql     = $this->prepareQuery($sql, $bindings);
652 1
        $request = $this->getRequestRead($sql);
653
654 1
        return $this->streaming($streamRead, $request);
655
    }
656
657
    /**
658
     * @param Stream  $streamWrite
659
     * @param string  $sql
660
     * @param mixed[] $bindings
661
     * @return Statement
662
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
663
     */
664 1
    public function streamWrite(Stream $streamWrite, $sql, $bindings = [])
665
    {
666 1
        $sql     = $this->prepareQuery($sql, $bindings);
667 1
        $request = $this->writeStreamData($sql);
668
669 1
        return $this->streaming($streamWrite, $request);
670
    }
671
}
672