Skip to content

Commit 14606e4

Browse files
authored
Merge pull request #189 from antanasja/feat/post-type-bindings
Post type bindings support
2 parents 026953b + 87b6cc8 commit 14606e4

File tree

8 files changed

+126
-3
lines changed

8 files changed

+126
-3
lines changed

src/Client.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,14 @@ public function partitions(string $table, int $limit = null, bool $active = null
817817
return $this->select(<<<CLICKHOUSE
818818
SELECT *
819819
FROM system.parts
820-
WHERE like(table,'%$table%') AND database='$database'$whereActiveClause
820+
WHERE table={tbl:String} AND database = {db:String}
821+
$whereActiveClause
821822
ORDER BY max_date $limitClause
822-
CLICKHOUSE
823+
CLICKHOUSE,
824+
[
825+
'db'=>$database,
826+
'tbl'=>$table
827+
]
823828
)->rowsAsTree('name');
824829
}
825830

src/Query/Degeneration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ interface Degeneration
55
{
66
public function process($sql);
77
public function bindParams(array $bindings);
8+
public function getBind():array;
89
}

src/Query/Degeneration/Bindings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function bindParams(array $bindings)
2828
}
2929
}
3030

31+
public function getBind(): array
32+
{
33+
return $this->bindings;
34+
}
35+
3136
/**
3237
* @param string $column
3338
* @param mixed $value

src/Query/Degeneration/Conditions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function bindParams(array $bindings)
2222
}
2323
}
2424

25+
public function getBind(): array
26+
{
27+
return $this->bindings;
28+
}
2529

2630
static function __ifsets($matches, $markers)
2731
{

src/Query/Query.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,36 @@ private function applyFormatQuery()
104104
*/
105105
public function getFormat()
106106
{
107-
108107
return $this->format;
109108
}
110109

110+
public function isUseInUrlBindingsParams():bool
111+
{
112+
// 'query=select {p1:UInt8} + {p2:UInt8}' -F "param_p1=3" -F "param_p2=4"
113+
return preg_match('#{[\w+]+:[\w+()]+}#',$this->sql);
114+
115+
}
116+
public function getUrlBindingsParams():array
117+
{
118+
$out=[];
119+
if (sizeof($this->degenerations)) {
120+
foreach ($this->degenerations as $degeneration) {
121+
if ($degeneration instanceof Degeneration) {
122+
$params=$degeneration->getBind();
123+
break;
124+
// need first response
125+
}
126+
}
127+
}
128+
if (sizeof($params)) {
129+
foreach ($params as $key=>$value)
130+
{
131+
$out['param_'.$key]=$value;
132+
}
133+
}
134+
return $out;
135+
}
136+
111137
public function toSql()
112138
{
113139
if ($this->format !== null) {

src/Transport/Http.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ private function makeRequest(Query $query, array $urlParams = [], bool $query_as
299299
* Build URL after request making, since URL may contain auth data. This will not matter after the
300300
* implantation of the todo in the `HTTP:newRequest()` method.
301301
*/
302+
303+
if ($query->isUseInUrlBindingsParams()) {
304+
$urlParams=array_replace_recursive($query->getUrlBindingsParams());
305+
}
302306
$url = $this->getUrl($urlParams);
303307
$new->url($url);
304308

tests/BindingsPostTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace ClickHouseDB\Tests;
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* @group BindingsPost
8+
*/
9+
final class BindingsPostTest extends TestCase
10+
{
11+
use WithClient;
12+
13+
14+
public function testSelectPostParams()
15+
{
16+
$xpx1=time();
17+
$result = $this->client->select(
18+
'SELECT number+{num_num:UInt8} as numbe_r, {xpx1:UInt32} as xpx1,{zoza:String} as zoza FROM system.numbers LIMIT 6',
19+
[
20+
'num_num'=>123,
21+
'xpx1'=>$xpx1,
22+
'zoza'=>'ziza'
23+
]
24+
);
25+
$this->assertEquals(null,$result->fetchRow('x')); //0
26+
$this->assertEquals(null,$result->fetchRow('y')); //1
27+
$this->assertEquals($xpx1,$result->fetchRow('xpx1')); //2
28+
$this->assertEquals('ziza',$result->fetchRow('zoza'));//3
29+
$this->assertEquals(127,$result->fetchRow('numbe_r')); // 123+4
30+
$this->assertEquals(128,$result->fetchRow('numbe_r')); // 123+5 item
31+
}
32+
33+
public function testSelectAsKeys()
34+
{
35+
// chr(0....255);
36+
$this->client->settings()->set('max_block_size', 100);
37+
38+
$bind['k1']=1;
39+
$bind['k2']=2;
40+
41+
$select=[];
42+
for($z=0;$z<4;$z++)
43+
{
44+
$bind['k'.$z]=$z;
45+
$select[]="{k{$z}:UInt16} as k{$z}";
46+
}
47+
$rows=$this->client->select("SELECT ".implode(",\n",$select),$bind)->rows();
48+
49+
$this->assertNotEmpty($rows);
50+
51+
$row=$rows[0];
52+
53+
for($z=10;$z<4;$z++) {
54+
$this->assertArrayHasKey('k'.$z,$row);
55+
$this->assertEquals($z,$row['k'.$z]);
56+
57+
}
58+
}
59+
60+
public function testArrayAsPostParam()
61+
{
62+
$arr = [1,3,6];
63+
$result = $this->client->select(
64+
'SELECT {arr:Array(UInt8)} as arr',
65+
[
66+
'arr'=>json_encode($arr)
67+
]
68+
);
69+
$this->assertEquals($arr, $result->fetchRow('arr'));
70+
}
71+
72+
}

tests/ClientTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,12 @@ public function testSelectAsync()
715715
$this->assertEquals(2, $state2->fetchOne('ping'));
716716
}
717717

718+
public function testPartsInfo()
719+
{
720+
$this->create_table_summing_url_views();
721+
$this->insert_data_table_summing_url_views();
722+
$this->assertIsArray($this->client->partitions('summing_url_views'));
723+
}
718724
/**
719725
*
720726
*/

0 commit comments

Comments
 (0)