Skip to content

Commit 733a3db

Browse files
committed
ok
1 parent 5fa9539 commit 733a3db

File tree

3 files changed

+130
-72
lines changed

3 files changed

+130
-72
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
/src/ClickHouseTableType.php
55
/tests/src/ClickHouseTableTypeTest.php
66
/doc/
7-
/tmp/
7+
/tmp/

src/ClickHouseAPI.php

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,41 @@ class ClickHouseAPI
8686
private $server_url;
8787

8888
/**
89-
* CURL option for do not verify hostname in SSL certificate
89+
* Options for http-request. Array [option => value]
9090
*
91-
* @var integer 0 by default. Set 2 if server have valid ssl-sertificate
91+
* @var array
9292
*/
93-
public $ssl_verify_host = 0;
93+
public $options = [];
9494

9595
/**
96-
* CURL option CURLOPT_CONNECTTIMEOUT
96+
* CURL options for each request
9797
*
98-
* @var integer 7 sec. by default
98+
* @var array
9999
*/
100-
public $curl_conn_timeout = 7;
100+
public $curl_options = [
101+
\CURLINFO_HEADER_OUT => true,
102+
\CURLOPT_RETURNTRANSFER => true,
103+
\CURLOPT_USERAGENT => "PHP-ClickHouse",
104+
105+
//Set 2 if server have valid ssl-sertificate
106+
\CURLOPT_SSL_VERIFYHOST => 0,
107+
\CURLOPT_SSL_VERIFYPEER => false,
108+
109+
// time limits in seconds
110+
\CURLOPT_CONNECTTIMEOUT => 7,
111+
\CURLOPT_TIMEOUT => 77,
112+
];
101113

102114
/**
103-
* CURL option CURLOPT_TIMEOUT
115+
* Parameters, interesting for get by function curl_getinfo after request.
104116
*
105-
* @var integer 77 sec. by default
117+
* @var array|false
106118
*/
107-
public $curl_timeout = 77;
119+
public $curl_getinfo = [
120+
\CURLINFO_EFFECTIVE_URL => 0,
121+
\CURLINFO_SIZE_DOWNLOAD => 0,
122+
\CURLINFO_SIZE_UPLOAD => 0,
123+
];
108124

109125
/**
110126
* Last error reported by CURL or empty string if no errors
@@ -120,12 +136,7 @@ class ClickHouseAPI
120136
*/
121137
public $last_code;
122138

123-
/**
124-
* Options for http-request. Array [option => value]
125-
*
126-
* @var array
127-
*/
128-
public $options = [];
139+
public $size_dowbload;
129140

130141
/**
131142
* Set true for show sending requests and server answers
@@ -446,29 +457,42 @@ public function doApiCall(
446457
\curl_setopt($ch, \CURLOPT_POST, true);
447458
\curl_setopt($ch, \CURLOPT_POSTFIELDS, $post_data);
448459
}
449-
450-
\curl_setopt($ch, \CURLOPT_SSL_VERIFYPEER, false);
451-
\curl_setopt($ch, \CURLOPT_SSL_VERIFYHOST, $this->ssl_verify_host);
452-
453-
\curl_setopt($ch, \CURLOPT_CONNECTTIMEOUT, $this->curl_conn_timeout);
454-
\curl_setopt($ch, \CURLOPT_TIMEOUT, $this->curl_timeout);
455-
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
456-
\curl_setopt($ch, \CURLOPT_USERAGENT, "PHP-ClickHouse");
460+
\curl_setopt_array($ch, $this->curl_options);
457461

458462
$response = \curl_exec($ch);
459463

460464
$this->last_curl_error_str = $curl_error = \curl_error($ch);
461-
462465
$this->last_code = $code = \curl_getinfo($ch, \CURLINFO_HTTP_CODE);
463466

464-
\curl_close($ch);
467+
if (\is_array($this->curl_getinfo)) {
468+
foreach (\array_keys($this->curl_getinfo) as $key) {
469+
$this->curl_getinfo[$key] = \curl_getinfo($ch, $key);
470+
}
471+
}
465472

466473
if ($this->debug) {
467-
echo "HTTP $code $curl_error \n{\n$response\n}\n";
474+
echo "HTTP $code $curl_error \n\n$response\n}\n";
468475
}
476+
477+
\curl_close($ch);
469478
return \compact('code', 'curl_error', 'response');
470479
}
471480

481+
/**
482+
* Set http-compression mode on/off
483+
*
484+
* @param boolean $true_or_false
485+
*/
486+
public function setCompression($true_or_false)
487+
{
488+
if ($true_or_false) {
489+
$this->setOption('enable_http_compression', 1);
490+
$this->curl_options[\CURLOPT_ENCODING] = 'gzip';
491+
} else {
492+
$this->setOption('enable_http_compression', null);
493+
unset($this->curl_options[\CURLOPT_ENCODING]);
494+
}
495+
}
472496
/**
473497
* Set addition http-request parameter.
474498
*
@@ -480,8 +504,8 @@ public function doApiCall(
480504
public function setOption($key, $value, $overwrite = true)
481505
{
482506
$old_value = isset($this->options[$key]) ? $this->options[$key] : null;
483-
if (is_null($old_value) || $overwrite) {
484-
if (is_null($value)) {
507+
if (\is_null($old_value) || $overwrite) {
508+
if (\is_null($value)) {
485509
unset($this->options[$key]);
486510
} else {
487511
$this->options[$key] = $value;

tests/src/ClickHouseAPITest.php

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,6 @@ protected function setUp()
2424
$ch->session_autocreate = false;
2525
}
2626

27-
/**
28-
* @covers Ierusalim\ClickHouse\ClickHouseAPI::query
29-
*/
30-
public function testQuery()
31-
{
32-
$ch = $this->object;
33-
$ans = $ch->query("SELECT 123")->results;
34-
$this->assertEquals("123\n", $ans);
35-
$table = "querytesttable";
36-
$this->assertEquals("111\n", $ch
37-
->query("CREATE TABLE IF NOT EXISTS $table (id UInt8, dt Date) ENGINE = MergeTree(dt, (id), 8192)")
38-
->query("INSERT INTO $table SELECT 111 as id, toDate(now()) as dt")
39-
->query("SELECT id FROM $table WHERE dt = toDate(now())")
40-
->query("DROP TABLE IF EXISTS $table")
41-
->results
42-
);
43-
try {
44-
$ans = $ch->query("BAD QUERY");
45-
} catch (\Exception $e) {
46-
$ans = $e->getMessage();
47-
}
48-
$this->assertTrue(\strpos($ans, 'Syntax error') !== false);
49-
50-
// curl error emulation
51-
$ch->hook_before_api_call = function($s, $obj) {
52-
return 'http://github.com:22/';
53-
};
54-
try {
55-
$ans = $ch->query("ANY QUERY");
56-
} catch (\Exception $e) {
57-
$ans = $e->getMessage();
58-
}
59-
print_r($ans);
60-
//$this->assertTrue(\strpos($ans, 'Syntax error') !== false);
61-
$ch->hook_before_api_call = false;
62-
63-
try {
64-
$ans =(new ClickHouseAPI("https://github.com:443/"))->query("");
65-
} catch (\Exception $e) {
66-
$ans = $e->getMessage();
67-
}
68-
}
69-
7027
public function testConstructEmpty()
7128
{
7229
$r = new ClickHouseAPI();
@@ -146,6 +103,30 @@ public function testGetVersion()
146103
$this->assertEquals($version, $ver_good);
147104
}
148105

106+
/**
107+
* @covers Ierusalim\ClickHouse\ClickHouseAPI::setCompression
108+
*/
109+
public function testSetCompression()
110+
{
111+
$ch = $this->object;
112+
$ch->setCompression(false);
113+
114+
$ans = $ch->query("SELECT number FROM system.numbers LIMIT 100")->results;
115+
116+
$size_d = $ch->curl_getinfo[\CURLINFO_SIZE_DOWNLOAD];
117+
$this->assertEquals(strlen($ans), $size_d);
118+
119+
$ch->setCompression(true);
120+
121+
$ans = $ch->query("SELECT number FROM system.numbers LIMIT 100")->results;
122+
123+
$size_d = $ch->curl_getinfo[\CURLINFO_SIZE_DOWNLOAD];
124+
if ($size_d < strlen($ans)) {
125+
echo "http-compression supported\n";
126+
}
127+
$this->assertGreaterThan($size_d, strlen($ans));
128+
}
129+
149130
/**
150131
* @covers ierusalim\ClickHouse\ClickHouseAPI::isSupported
151132
*/
@@ -167,9 +148,62 @@ public function testIsSupported()
167148
$this->assertTrue($ch->isSupported('session_id'));
168149
}
169150

151+
$this->assertEquals($ch->isSupported('version', true), $ch->getVersion());
152+
170153
$this->assertFalse($ch->isSupported('unknown'));
171154
}
172155

156+
157+
/**
158+
* @covers Ierusalim\ClickHouse\ClickHouseAPI::query
159+
*/
160+
public function testQuery()
161+
{
162+
$ch = $this->object;
163+
164+
if ($ch->isSupported('query')) {
165+
$ans = $ch->query("SELECT 123")->results;
166+
$this->assertEquals("123\n", $ans);
167+
$table = "querytesttable";
168+
$this->assertEquals("111\n", $ch
169+
->query("CREATE TABLE IF NOT EXISTS $table (id UInt8, dt Date) ENGINE = MergeTree(dt, (id), 8192)")
170+
->query("INSERT INTO $table SELECT 111 as id, toDate(now()) as dt")
171+
->query("SELECT id FROM $table WHERE dt = toDate(now())")
172+
->query("DROP TABLE IF EXISTS $table")
173+
->results
174+
);
175+
}
176+
177+
try {
178+
$ans = $ch->query("BAD QUERY");
179+
} catch (\Exception $e) {
180+
$ans = $e->getMessage();
181+
}
182+
$this->assertTrue(\strpos($ans, 'Syntax error') !== false);
183+
184+
$ch->curl_options[\CURLOPT_CONNECTTIMEOUT] = 2;
185+
186+
// curl error emulation
187+
$ch->hook_before_api_call = function($s, $obj) {
188+
return 'http://github.com:22/';
189+
};
190+
try {
191+
$ans = $ch->query("ANY QUERY");
192+
} catch (\Exception $e) {
193+
$ans = $e->getMessage();
194+
}
195+
//$this->assertTrue(\strpos($ans, 'Syntax error') !== false);
196+
$ch->hook_before_api_call = false;
197+
198+
try {
199+
$ch = new ClickHouseAPI("https://github.com:443/");
200+
$ch->curl_options[\CURLOPT_CONNECTTIMEOUT] = 2;
201+
$ans =$ch->query("");
202+
} catch (\Exception $e) {
203+
$ans = $e->getMessage();
204+
}
205+
}
206+
173207
/**
174208
* @covers Ierusalim\ClickHouse\ClickHouseAPI::anyQuery
175209
* @todo Implement testAnyQuery().

0 commit comments

Comments
 (0)