Skip to content

Commit 413698c

Browse files
committed
ok
1 parent 039380d commit 413698c

File tree

6 files changed

+142
-82
lines changed

6 files changed

+142
-82
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
/start.php
22
/localenv.php
3+
/src/ClickHouseCRUD.php
4+
/src/ClickHouseTableType.php
5+
/src/known_functions.txt
6+
/tests/src/ClickHouseTableTypeTest.php

src/ClickHouseFunctions.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function changeIfIsAlias($type_src)
167167
* @param string $type_full (by ref)
168168
* @param string|null $name (by ref)
169169
* @param array|null $to_conv (by ref)
170-
* @return boolean|int
170+
* @return false|int Return false for unknown types, or length in bytes for known.
171171
*/
172172
public function parseType(&$type_full, &$name = null, &$to_conv = null)
173173
{
@@ -237,7 +237,7 @@ public function parseType(&$type_full, &$name = null, &$to_conv = null)
237237
* @param string $table table name
238238
* @param array $fields_arr keys=field names => field_type[ def]
239239
* @param integer $if_exists If table exists: 2=drop old table, 1-do nothing, 0-ret error)
240-
* @param string $ver if null, table will create with engine MergeTree
240+
* @param string $ver if null, table will create as MergeTree, otherwise ReplacingMergeTree
241241
* @return boolean|string
242242
*/
243243
public function createTableQuick($table, $fields_arr, $if_exists = 0, $ver = null)
@@ -253,7 +253,7 @@ public function createTableQuick($table, $fields_arr, $if_exists = 0, $ver = nul
253253
// If $ver defined, then change db-engine to ReplacingMergeTree
254254
if (!\is_null($ver)) {
255255
$sql_arr[6] = 'ReplacingMergeTree';
256-
$sql_arr[14] .= $ver;
256+
$sql_arr[14] .= (empty($ver) ? '': ", $ver");
257257
}
258258

259259
return $this->queryFalse(\implode($sql_arr));
@@ -297,7 +297,7 @@ public function sqlTableQuick($table_name, $fields_arr, $if_not_exist = 1)
297297
3 => implode(", ", \array_column($fields_arr, 'create')),
298298
4 => ' ) ',
299299
5 => 'ENGINE = ',
300-
6 => "MergeTree",
300+
6 => 'MergeTree',
301301
7 => '(',
302302
8 => $date_field,
303303
9 => ', (',
@@ -349,11 +349,15 @@ public function parseFieldsArr($fields_arr)
349349

350350
if (strlen($default)) {
351351
if (\substr($default, 0, 8) !== 'DEFAULT ') {
352-
$default = $this->quotePar($default);
353352
if ($to_conv) {
354-
$default = $to_conv[0] . $default . $to_conv[1];
353+
$lp = $to_conv[0];
354+
$rp = $to_conv[1];
355+
if (\substr($default,0, \strlen($lp)) != $lp) {
356+
$default = $lp . $this->quotePar($default) . $rp;
357+
}
355358
$create = 'DEFAULT ' . $default;
356359
} else {
360+
$default = $this->quotePar($default);
357361
$create = $type_full . ' DEFAULT ' . $default;
358362
}
359363
} else {

src/ClickHouseQuery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public function queryInsertArray($table_name, $fields_names, $fields_set_arr, $s
553553
// Resolve $fields_arr
554554
$use_json_each_row = false;
555555
if (isset($fields_set_arr[0])) {
556-
if (is_array($fields_set_arr[0])) {
556+
if (\is_array($fields_set_arr[0])) {
557557
if (!isset($fields_set_arr[0][0])) {
558558
$in_arr_fields_arr = \array_keys($fields_set_arr[0]);
559559
$use_json_each_row = true;
@@ -582,7 +582,7 @@ public function queryInsertArray($table_name, $fields_names, $fields_set_arr, $s
582582
$post_data = [\json_encode($fields_set_arr)];
583583
} else {
584584
$post_data = [];
585-
if (!$use_json_each_row && !is_array($fields_set_arr[0])) {
585+
if (!$use_json_each_row && !\is_array($fields_set_arr[0])) {
586586
$fields_set_arr = [$fields_set_arr];
587587
}
588588
foreach ($fields_set_arr as $row_arr) {

tests/src/ClickHouseAPITest.php

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,61 @@ 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+
2770
public function testConstructEmpty()
2871
{
2972
$r = new ClickHouseAPI();
3073
$this->assertEquals('127.0.0.1', $r->host);
3174
}
75+
3276
public function testConstructWithURL()
3377
{
3478
$r = new ClickHouseAPI('https://8.8.8.8:1234/');
3579
$this->assertEquals('8.8.8.8', $r->host);
3680
}
81+
3782
public function testConstructWithHostEtc()
3883
{
3984
$r = new ClickHouseAPI('1.2.3.4', 5678, 'default', '');
@@ -52,6 +97,7 @@ protected function resetServerUrl()
5297
}
5398
$ch->setServerUrl($clickhouse_url);
5499
}
100+
55101
/**
56102
* @covers Ierusalim\ClickHouse\ClickHouseAPI::setServerUrl
57103
*/
@@ -62,6 +108,7 @@ public function testSetServerUrl()
62108
$this->assertEquals($ch->host, '8.8.8.8');
63109
$this->resetServerUrl();
64110
}
111+
65112
public function testSetServerUrlException()
66113
{
67114
$ch = $this->object;
@@ -80,19 +127,23 @@ public function testGetVersion()
80127
$this->assertTrue(strpos($version, '.') > 0);
81128
}
82129
echo "Version of ClickHouse server: $version\n";
83-
$this->assertEquals($version, $ch->server_version);
130+
//$this->assertEquals($version, $ch->server_version);
84131
// test get cached version
85-
$fake_version = $ch->server_version = 'fake_version';
86-
$this->assertEquals($fake_version, $ch->getVersion());
132+
//$fake_version = $ch->server_version = 'fake_version';
133+
//$this->assertEquals($fake_version, $ch->getVersion());
87134

88135
$ch->session_autocreate = true;
89136
// set fake server for emulate session unsupported
90137
$ch->hook_before_api_call = function($s, $obj) {
91138
return 'http://google.com/notfound';
92139
};
93-
$version = $ch->getVersion(true);
140+
$ver_bad = $ch->getVersion(true);
94141
$this->assertFalse($ch->session_autocreate);
95-
$this->assertEquals("Unknown", $version);
142+
$this->assertEquals("Unknown", $ver_bad);
143+
144+
$ch->hook_before_api_call = false;
145+
$ver_good = $ch->getVersion(true);
146+
$this->assertEquals($version, $ver_good);
96147
}
97148

98149
/**
@@ -102,13 +153,20 @@ public function testIsSupported()
102153
{
103154
$ch = $this->object;
104155
$sess_sup = $ch->isSupported('session_id');
105-
echo "Sessions " .($sess_sup ? '':'is not ') . "supported\n";
156+
echo "Sessions " . ($sess_sup ? '' : 'is not ') . "supported\n";
106157

107158
if (!$ch->isSupported('query', true)) {
108159
echo "query is not supported; ClickHouse Server is not ready\n";
109160
echo "Server: {$ch->host}:{$ch->port}\n";
110161
}
111162

163+
$sess2_sup = (new ClickHouseAPI('https://google.com:443/'))->isSupported('session_id');
164+
$this->assertFalse($sess2_sup);
165+
if ($sess_sup) {
166+
echo ',';
167+
$this->assertTrue($ch->isSupported('session_id'));
168+
}
169+
112170
$this->assertFalse($ch->isSupported('unknown'));
113171
}
114172

@@ -197,7 +255,7 @@ public function testDoQuery()
197255
$ch->session_autocreate = false;
198256

199257
if ($ch->isSupported('query')) {
200-
// test default query SELECT 1
258+
// test default query SELECT 1
201259
$ans = $ch->doQuery();
202260
$this->assertEquals(\trim($ans['response']), 1);
203261

@@ -235,10 +293,11 @@ public function testDoQuery()
235293
}
236294
if ($ch->isSupported('query')) {
237295
// check query if not supported session
238-
$ch->support_fe['session_id'] = false;
296+
$ch->isSupported('session_id', false, false);
239297
$ch->setOption('session_id', 'test');
240298
$ans = $ch->doQuery("SELECT 321");
241299
$this->assertEquals(\trim($ans['response']), 321);
300+
$ch->isSupported('session_id', true);
242301
}
243302
}
244303

@@ -291,11 +350,11 @@ public function testGetOption()
291350
*/
292351
public function testSetSession()
293352
{
294-
$ch = $this->object;
295-
$prev_sess_id = $ch->setSession();
296-
$this->assertNull($prev_sess_id);
297-
$session_id = $ch->getSession();
298-
$this->assertEquals(strlen($session_id), 32);
353+
$ch = $this->object;
354+
$prev_sess_id = $ch->setSession();
355+
$this->assertNull($prev_sess_id);
356+
$session_id = $ch->getSession();
357+
$this->assertEquals(strlen($session_id), 32);
299358
}
300359

301360
/**
@@ -304,10 +363,10 @@ public function testSetSession()
304363
*/
305364
public function testGetSession()
306365
{
307-
$ch = $this->object;
308-
$ch->setSession();
309-
$session_id = $ch->getSession();
310-
$this->assertEquals($session_id, $ch->getOption('session_id'));
366+
$ch = $this->object;
367+
$ch->setSession();
368+
$session_id = $ch->getSession();
369+
$this->assertEquals($session_id, $ch->getOption('session_id'));
311370
}
312371

313372
/**
@@ -316,13 +375,13 @@ public function testGetSession()
316375
*/
317376
public function testDelOption()
318377
{
319-
$ch = $this->object;
320-
$ch->setSession();
321-
$session_id = $ch->getSession();
322-
$this->assertEquals(strlen($session_id), 32);
323-
$old = $ch->delOption("session_id");
324-
$this->assertEquals($session_id, $old);
325-
$new = $ch->getSession();
326-
$this->assertNull($new);
378+
$ch = $this->object;
379+
$ch->setSession();
380+
$session_id = $ch->getSession();
381+
$this->assertEquals(strlen($session_id), 32);
382+
$old = $ch->delOption("session_id");
383+
$this->assertEquals($session_id, $old);
384+
$new = $ch->getSession();
385+
$this->assertNull($new);
327386
}
328387
}

tests/src/ClickHouseFunctionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public function testCreateTableQuick()
625625
'id' => 'Int16',
626626
'dt' => ['Date', 'now()'],
627627
'ver' => 'UInt8'
628-
], 2, ', ver');
628+
], 2, 'ver');
629629
$this->assertFalse($ans);
630630
} else {
631631
echo '-';

0 commit comments

Comments
 (0)